Documente Academic
Documente Profesional
Documente Cultură
known as binding.
of the SQL statements, turns them into host language comments, and
IMMEDIATE. DB2 also uses dynamic SQL for system-directed access (1)
.
(SQL processor using file input). SPUFI prepares and executes these
statements dynamically.
1.2.6 Indexes
an object that is separate from the data in the table. When you
define an
index using the CREATE INDEX statement, DB2 builds this structure
and
maintains it automatically.
1.2.7 Keys
The same column can be part of more than one key. A key composed of
more than
table. The term value, when used with respect to a composite key,
denotes
a composite value. Thus, a rule such as, "the value of the foreign
key
must be equal to the value of the primary key" means that each
component
utility and the SQL INSERT and UPDATE statements. The mechanism
used to
attribute.
A table cannot have more than one primary key, and the columns of a
primary key cannot contain null values. Primary keys are optional
and can
index must already exist on the columns of that primary key. This
unique
specific primary key. A table can have zero or more foreign keys.
The
null.
table space A table space can hold one or more base tables. All
tables
statement.
1.2.10 Databases
spaces contain indexes on the tables in the table spaces of the same
1.2.11 Catalog
catalogs."
Tables in the catalog are like any other database tables with
respect to
at data in the catalog tables in the same way that you retrieve data
from
any other table in the system. Each DB2 ensures that the catalog
contains
view consists of the rows that would result if the subselect were
like a base table. However, columns added to the base tables after
the
view is defined do not appear in the view. For retrieval, all views
can
Views can be used to control access to a table and make data easier
to
table. The view can be defined to show only portions of data in the
table. A view can show summary data for a given table, combine two
or
more tables in meaningful ways, or show only the selected rows that
are
Example: The following SQL statement defines a view named XYZ. The
view
data in the table comes from the columns EMPNO and HIREDATE of the
sample
table DSN8310.EMP. The rows from which the data is taken are for
FROM DSN8310.EMP
on the view.
of SQL Reference.
An SQL DROP VIEW statement can drop a view, and the definition of
the view
from the catalog when any view or base table on which the view
depends is
dropped.
following elements:
_ The bound form of SQL statements taken from one or more DBRMs.
are created using the DB2 subcommands BIND PLAN and BIND PACKAGE,
respectively.
DB2 library, this table is called a result table. Like the tables
from
which the data is retrieved, a result table has rows and columns. A
FROM DSN8310.EMP
ORDER BY LASTNAME;
You do not need to know the column names to select DB2 data. Use an
SELECT *
FROM DSN8310.DEPT;
The dashes for MGRNO in the fourth row of the result table indicate
that
Select the column or columns you want by naming each column. All
columns
appear in the order you specify, not in their order in the table.
FROM DSN8310.DEPT;
====== ======
000010 A00
000020 B01
000030 C01
------ D01
000050 E01
000060 D11
000070 D21
000090 E11
000100 E21
------ F22
------ G22
------ H22
------ I22
------ J22
columns of each row in the DSN8310.DEPT table. You can retrieve one
+-------------------------------------------------------------------
-----+
+-------------------------------------------------------------------
-----¦
¦ Type of ¦ Specified ¦
Example ¦
¦ comparison ¦ with...
¦ ¦
+--------------------------------+--------------
+------------------------¦
+--------------------------------+--------------
+------------------------¦
¦ Equal to ¦ = ¦ DEPTNO =
'X01' ¦
+--------------------------------+--------------
+------------------------¦
+--------------------------------+--------------
+------------------------¦
+--------------------------------+--------------
+------------------------¦
+--------------------------------+--------------
+------------------------¦
+--------------------------------+--------------
+------------------------¦
+--------------------------------+--------------
+------------------------¦
+--------------------------------+--------------
+------------------------¦
+--------------------------------+--------------
+------------------------¦
¦ ¦ ¦ STATUS LIKE
'N_' ¦
+--------------------------------+--------------
+------------------------¦
¦ ¦ ¦ '1965-01-01' OR
SALARY ¦
¦ ¦ ¦ <
16000 ¦
+--------------------------------+--------------
+------------------------¦
¦ ¦ ¦ '1965-01-01'
AND ¦
¦ ¦ ¦ SALARY <
16000 ¦
+--------------------------------+--------------
+------------------------¦
¦ ¦ ¦ AND
40000 ¦
+--------------------------------+--------------
+------------------------¦
¦ ¦ ¦ 'C01',
'D01') ¦
+-------------------------------------------------------------------
-----+
You can also search for rows that do not satisfy one of the above
topic 2.1.5.4 for more information about using the NOT keyword.
A WHERE clause can specify a column that, for some rows, contains a
null
value. Normally, values from such a row are not retrieved, because
a null
value is neither less than, equal to, nor greater than the value
specified
specify:
Use the equal (=) comparison operator to select data only from the
rows
the value specified. To select only the rows where the department
number
FROM DSN8310.EMP
The statement retrieves the department number and the first and last
name
FROM DSN8310.EMP
The example retrieves the date hired and the name for each employee
hired
before 1960.
Use the NOT keyword or comparison operators (<>, >=, and <=) to
select all
rows except the rows identified with the search condition. (2) The
NOT
FROM DSN8310.EMP
WHERE NOT (SALARY + BONUS + COMM) > 30000 AND JOB = 'MANAGER'
ORDER BY WORKDEPT;
The NOT keyword cannot be used with the comparison operators. The
You also can precede other SQL keywords with NOT: NOT LIKE, NOT IN,
or
NOT BETWEEN are all acceptable. For example, the following two
clauses
are equivalent:
(2) Although the not sign (¬) can be used with comparison
operators, it is not recommended. See Chapter 3 of SQL
characters.
_ Use the LIKE predicate with character or graphic data only, not
with
The following SQL statement selects data from each row for employees
with
FROM DSN8310.EMP
The following SQL statement selects data from each row of the
department
FROM DSN8310.DEPT
because the blank that follows 1 is not taken into account. If the
DEPTNO
The following SQL statement selects data from each row of the
department
FROM DSN8310.DEPT
statement,
FROM DSN8310.DEPT
The SQL statement below selects data from each row whose four-digit
phone
FROM DSN8310.EMP
example the ESCAPE '+' indicates that the + is the escape character
in the
would allow you to search for a single plus sign (+) as part of the
string.
The following sections explain ways to use more than one predicate.
The
This example retrieves the employee number, date hired, and salary
for
each employee hired before 1965 and having a salary of less than
$16,000
per year.
satisfied:
FROM DSN8310.EMP
This example retrieves the employee number, date hired, and salary
for
each employee who either was hired before 1965, or has a salary less
than
If you use more than two conditions with AND or OR, you can use
selects the row of each employee that satisfies at least one of the
following conditions:
_ The employee was hired before 1965 AND is paid less than
$20,000.
Based on this WHERE clause, the selected rows are for employees
000290,
With the parentheses moved, however, the meaning of the WHERE clause
changes significantly:
WHERE HIREDATE < '1965-01-01' AND (SALARY < 20000 OR EDLEVEL < 13)
This clause selects the row of each employee that satisfies both of
the
following conditions:
SELECT EMPNO
FROM DSN8310.EMP
When using the NOT condition with AND and OR, the placement of the
parentheses is important.
In this SQL statement, only the first predicate (SALARY >= 50000) is
negated.
FROM DSN8310.EMP
FROM DSN8310.EMP
conditions:
Specify the lower boundary of the BETWEEN condition first, then the
upper
Example
FROM DSN8310.DEPT
Example
SELECT EMPNO, SALARY
FROM DSN8310.EMP
The example retrieves the employee numbers and the salaries for all
employees who either earn less than $40,000 or more than $50,000.
You can use the BETWEEN predicate when comparing floating point data
in
COL1.
113.01.
following query might not yield the expected result (that is, a host
variable floating point value for 113.01 might not be considered
Possibly wrong:
SELECT *
FROM MYTABLE
Better:
SELECT *
FROM MYTABLE
In the values list after IN, the order of the items is not important
and
does not affect the ordering of the result. Enclose the entire list
in
FROM DSN8310.DEPT
The example retrieves the department number and manager number for
Using the IN predicate gives the same results as a much longer set
of
FROM DSN8310.EMP
You can concatenate strings by using the CONCAT keyword. You can
use
FROM DSN8310.EMP;
concatenates the last name, comma, and first name of each result
row.
selected rows.
FROM DSN8310.EMP
example, although the name shown (for example, SALARY/12) does not
appear
FROM DSN8310.EMP
If you will be using dates, you should assign datetime data types to
all
columns containing dates. This not only allows you to do more with
your
table but it can save you from problems like the following:
to the BIRTHDATE column and then fill it with dates of the form
yyyymmdd.
older:
FROM YEMP
DB2 then recognizes the person as 26 years old, not 27. If,
however, the
DATE(SUBSTR(DIGITS(C2),1,4)CONCAT'-'
CONCAT SUBSTR(DIGITS(C2),5,2)
CONCAT'-'CONCAT
SUBSTR(DIGITS(C2),7,2))
Two types of built-in functions are available for use with a SELECT
Subtopics
Two types of built-in functions are available for use with a SELECT
of data which can then be retrieved. The calculated values are based
on
selected rows only (all rows that satisfy the WHERE clause).
The following SQL statement calculates for department D11, the sum
of
employee salaries, the minimum, average, and maximum salary, and the
count
DISTINCT can be used with the SUM, AVG, and COUNT functions.
DISTINCT
In this case SUM and AVG can only be applied to numbers. MIN, MAX,
and
the table.
SELECT COUNT(*)
FROM DSN8310.EMP;
a set of departments.
SELECT AVG(EDLEVEL)
FROM DSN8310.EMP
The SQL statement below counts the different jobs in the DSN8310.EMP
table.
FROM DSN8310.EMP;
SELECT YEAR(HIREDATE)
FROM DSN8310.EMP
YEAR(HIREDATE)
==============
1972
1965
1965
1958
1963
The scalar function YEAR produces a single scalar value for each row
of
are three rows that satisfy the search condition, so YEAR is applied
three
Table 3 shows the scalar functions that can be used. For complete
details
on using these functions see Chapter 4 of SQL Reference.
+-------------------------------------------------------------------
-----+
¦ Table 3. Scalar
Functions ¦
+-------------------------------------------------------------------
-----¦
¦ Scalar ¦ Returns... ¦
Example ¦
¦ Function ¦
¦ ¦
+-------------+-----------------------------
+----------------------------¦
+-------------+-----------------------------
+----------------------------¦
¦ ¦ argument.
¦ ¦
+-------------+-----------------------------
+----------------------------¦
¦ ¦ argument.
¦ ¦
+-------------+-----------------------------
+----------------------------¦
+-------------+-----------------------------
+----------------------------¦
¦ ¦ a numeric value. ¦
¦
+-------------+-----------------------------
+----------------------------¦
¦ ¦ representation of its
¦ ¦
¦ ¦ argument.
¦ ¦
+-------------+-----------------------------
+----------------------------¦
¦ FLOAT ¦ floating-point ¦
FLOAT(SALARY)/COMM ¦
¦ ¦ representation of its
¦ ¦
¦ ¦ argument.
¦ ¦
+-------------+-----------------------------
+----------------------------¦
¦ HEX ¦ a hexadecimal ¦
HEX(BCHARCOL) ¦
¦ ¦ representation of its
¦ ¦
¦ ¦ argument.
¦ ¦
+-------------+-----------------------------
+----------------------------¦
¦ ¦ argument.
¦ ¦
+-------------+-----------------------------
+----------------------------¦
¦ ¦ of its argument.
¦ ¦
+-------------+-----------------------------
+----------------------------¦
+-------------+-----------------------------
+----------------------------¦
¦ ¦ argument.
¦ ¦
+-------------+-----------------------------
+----------------------------¦
¦ ¦ argument.
¦ ¦
+-------------+-----------------------------
+----------------------------¦
¦ ¦ argument.
¦ ¦
+-------------+-----------------------------
+----------------------------¦
¦ ¦ argument.
¦ ¦
+-------------+-----------------------------
+----------------------------¦
+-------------+-----------------------------
+----------------------------¦
¦ ¦ argument. ¦
'13:00:00' ¦
+-------------+-----------------------------
+----------------------------¦
+-------------+-----------------------------
+----------------------------¦
+-------------+-----------------------------
+----------------------------¦
¦ ¦ argument.
¦ ¦
+-------------+-----------------------------
+----------------------------¦
¦ ¦ argument.
¦ ¦
+-------------------------------------------------------------------
-----+
ORDER BY lets you specify the order in which rows are retrieved.
FROM DSN8310.EMP
FROM DSN8310.EMP
Rows are sorted in the EBCDIC collating sequence. To order the rows
by
more than one column's value, use more than one column name in the
ORDER
BY clause.
When several rows have the same first ordering column value, those
rows
clause, and then on the third ordering column, and so on. For
example,
statements. The first one orders selected rows by job and next by
FROM DSN8310.EMP
FIELDREP 14 LEE
FIELDREP 14 WONG
FIELDREP 16 GOUNOT
FIELDREP 16 ALONZO
FIELDREP 16 MEHTA
MANAGER 14 SPENSER
FROM DSN8310.EMP
FIELDREP 14 LEE
FIELDREP 14 WONG
MANAGER 14 SPENSER
FIELDREP 16 MEHTA
FIELDREP 16 GOUNOT
FIELDREP 16 ALONZO
When a null value is encountered, its value is treated as if it were
higher than all other values. Therefore, a null value appears last
in an
FROM DSN8310.EMP
For example, ORDER BY 3 orders the rows by the third column of the
result
table.
FROM DSN8310.EMP
ORDER BY 3 DESC;
EMPNO LASTNAME
You can use the column number even if the ordering column has a
name.
The following SQL statement lists names and phone numbers of all
employees
FROM DSN8310.EMP
ORDER BY 1;
The DISTINCT keyword removes duplicate rows from your result. Each
row
administrating departments:
ADMRDEPT
========
A00
D01
E01
SELECT ADMRDEPT
FROM DSN8310.DEPT;
ADMRDEPT
========
A00
A00
A00
A00
A00
D01
D01
E01
E01
E01
E01
E01
E01
E01
duplicate rows.
of column-name values.
Except for the grouping columns (the columns named in the GROUP BY
The following SQL statement lists, for each department, the lowest
and
highest education level within that department.
FROM DSN8310.EMP
GROUP BY WORKDEPT;
You can use ORDER BY to specify the order in which rows are
retrieved.
The following SQL statement calculates the average salary for each
department:
FROM DSN8310.EMP
GROUP BY WORKDEPT
ORDER BY WORKDEPT;
WORKDEPT AVG(SALARY)
======== ===============
A00 40850.00000000
B01 41250.00000000
C01 29722.50000000
D11 25147.27272727
D21 25668.57142857
E01 40175.00000000
E11 21020.00000000
E21 24086.66666666
each group of column values (SALARY) and return one row for each
group of
function because its value determines the group (that is, every
member of
You can also specify that you want the rows grouped by more than one
column. For example, you can find the average salary for men and
women in
FROM DSN8310.EMP
A00 F 49625.00000000
A00 M 35000.00000000
C01 F 29722.50000000
The rows are grouped first by department number and next (within
each
department) by sex before DB2 derives the average SALARY value for
each
group.
If there are null values in the column you specify in the GROUP BY
clause,
When it is used, the GROUP BY clause follows the FROM clause and any
WHERE
The SQL statement below lists, for each job, the number of employees
with
FROM DSN8310.EMP
GROUP BY JOB
ORDER BY JOB;
satisfy. The HAVING clause acts like a WHERE clause for groups, and
can
FROM DSN8310.EMP
GROUP BY WORKDEPT
ORDER BY WORKDEPT;
WORKDEPT AVG(SALARY)
======== ===============
A00 40850.00000000
C01 29722.50000000
D11 25147.27272727
D21 25668.57142857
E11 21020.00000000
E21 24086.66666666
COUNT(*) > 1 clause ensures that only departments with more than one
member are displayed. (In this case, departments B01 and E01 are
not
displayed.)
equal to 16, you can use the HAVING clause. Assuming you are only
FROM DSN8310.EMP
GROUP BY WORKDEPT
A00 49625.00000000 18
D11 25817.50000000 17
When GROUP BY and HAVING are both specified, the HAVING clause must
follow
if you have not used DISTINCT anywhere else in the same SELECT
statement.
with AND and OR, and you can use HAVING NOT for any predicate of a
search
condition.
The search condition for the join is the link between the tables
that
identify two columns that must be equal (one from each of the tables
being
Each row of the result table contains data that has been joined from
both
tables (for rows that satisfy the search condition). Each column of
the
result table contains data from one, but not both, of the tables.
specified.
numbers, managers last and first names, and department names from
table
department number.
The WHERE clause makes the connection between the two tables.
Column
values are selected only from those rows where MGRNO (in
DSN8310.DEPT)
education level, first name, last name, and department name for each
number and name of each "major" project followed by the number and
name of
table. The join condition is such that the value in column PROJNO
in
UNION ALL
FROM DSN8310.DEPT A
UNION ALL
FROM DSN8310.EMP B
Using the UNION keyword, you can combine two or more SELECT
statements to
form a single result table. When DB2 encounters the UNION keyword,
it
combines the interim result table of each statement. You use UNION
to
merge lists of values from two or more tables. You can use any of
the
clauses and techniques you have learned so far when coding SELECT
SELECT EMPNO
FROM DSN8310.EMP
WHERE WORKDEPT = 'D11'
UNION
SELECT EMPNO
FROM DSN8310.EMPPROJACT
PROJNO = 'MA2113' OR
PROJNO = 'AD3111'
ORDER BY 1;
======
000060
000150
000160
000170
000180
000190
000200
000210
000220
000230
000240
200170
200220
Any ORDER BY clause must appear after the last SELECT statement that
is
(in a union, you cannot use column names for this). The number
refers to
union. When DB2 returns your results, the last column contains the
constant for the SELECT statement that was the source of that row.
For
SELECT EMPNO
FROM DSN8310.EMP
UNION ALL
SELECT EMPNO
FROM DSN8310.EMPPROJACT
PROJNO = 'MA2113' OR
PROJNO = 'AD3111'
ORDER BY 1;
000060
000150
000150
000150
000160
000160
000170
000170
000170
000170
000180
000180
000190
000190
000190
000200
000210
000210
000210
000220
000230
000230
000230
000230
000230
000240
000240
200170
200220
compose for remote objects differ from those you compose for local
objects
in the way they identify tables and views. For remote objects, you
must
periods:
_ The first identifier is the location name for the object.
name is EMP. The location name could be the name of your local
subsystem,
Suppose that you want the name, employee number, and department ID
of
following query:
FROM DALLAS.DSN8310.EMP
not need DB2 authority to use it. However, you must have authority
to use
of the alias.
SMITH.
by JONES.
Assume now that the alias SMITH.DALEMP has been defined at your
local
FROM SMITH.DALEMP
in which it appears need not be changed if the table or view for the
alias
valid, drop the original alias and create it again, and, for
imbedded SQL,
owned by others. For example, you could create the alias appearing
in the
Use the CREATE TABLE statement to create a table. The following SQL
MFGDEPT CHAR(3),
MARKUP SMALLINT,
SALESDEPT CHAR(3),
_ A list of the columns that make up the table. For each column,
specify:
topic 2.1.2.
- NOT NULL, when the column cannot contain null values and
does not
- NOT NULL WITH DEFAULT, when the column cannot contain null
values
You must separate each column description from the next with a
comma
two work tables and how to fill a work table with the contents of
another
table.
Each example shown in this chapter assumes you are logged on using
your
If you want to access your own table YDEPT, you need only to refer
to it
as "YDEPT".
for YDEPT:
LIKE DSN8310.DEPT;
CREATE UNIQUE INDEX YDEPTX
ON YDEPT (DEPTNO);
You must use two statements to create YDEPT and its index as shown
above.
PRIMARY KEY(DEPTNO);
SELECT *
FROM DSN8310.DEPT;
You can use the following statements to create and fill a new
employee
WORKDEPT CHAR(3) ,
PHONENO CHAR(4) ,
HIREDATE DATE ,
JOB CHAR(8) ,
EDLEVEL SMALLINT ,
SEX CHAR(1) ,
BIRTHDATE DATE ,
SALARY DECIMAL(9, 2) ,
BONUS DECIMAL(9, 2) ,
COMM DECIMAL(9, 2) ,
PRIMARY KEY(EMPNO),
key in YEMP (WORKDEPT) and the primary key in YDEPT (DEPTNO). Now,
create
SELECT *
FROM DSN8310.EMP;
The CREATE TABLE statement can create tables with primary keys or
foreign
unique index that matches the primary key and enforces the
uniqueness of
the primary key.
every SQL INSERT, DELETE, and UPDATE operation, and through the LOAD
utility.
topic 2.2.1.1.2.
and columns. A view can present any or all of the data in one or
more
Use the CREATE VIEW statement to define a view and give the view a
name,
This view adds each department manager's name to the department data
in
When a program accesses the data defined by a view, DB2 uses the
view
definition to return a set of rows the program can access with SQL
statements. Now that the view VDEPTM exists, you can manipulate
data by
FROM VDEPTM
the value used for USER or CURRENT SQLID is related to the person
than the person who created the view. In other words, a reference
to a
You can use views to limit access to certain kinds of data, such as
salary
_ Combine data from two or more tables and make the combined data
values in one table with those in another table, you can create
a view
_ The owner of the plan or package that contains the program must
be
_ When inserting a row into a table (via a view), the row must
have a
value for each column of the table that does not have a default
value.
value, you cannot insert rows into the table via the view.
INSERT statement.
data for the new row (or rows) is contained in another table or
view.
In either case, for every row you insert, you must provide a value
for any
Alternatively, you can omit the column name list; when the program
is
bound, DB2 inserts the name of each column of the table or view into
the
By naming the columns, you need not list the values based on their
position in the table. When you list the column names, supply their
It is a good idea to name all columns into which you are inserting
values
because:
_ You can verify that you are giving the values in order.
For example,
After inserting a new department row into your YDEPT table, you can
use a
SELECT statement to see what you have loaded into the table. This
SQL
statement:
SELECT *
FROM YDEPT
ORDER BY DEPTNO;
shows you all the new department rows that you have inserted:
_ You can use the DB2 LOAD utility to enter data from other
sources.
See Command and Utility Reference for more information about the
LOAD
utility.
_ Each non-null value you insert into a foreign key column must be
equal
table).
_ If any field in the foreign key is null, the entire foreign key
is
considered null.
_ If the index enforcing the primary key of the parent table has
been
fails.
For example, the sample application project table (PROJ) has foreign
keys
inserted into the project table must have a value of RESPEMP that is
either equal to some value of EMPNO in the employee table or is
null. The
row must also have a value of DEPTNO that is equal to some value of
DEPTNO
inserting data. Nothing is inserted into the table, and error codes
are
set in the SQLCODE and SQLSTATE fields of the SQLCA. If the row is
SQLERRD. INSERT with subselect can insert more than one row. The
number
more information.
the YEMP table. Since YEMP has a foreign key WORKDEPT referencing
the
primary key DEPTNO in YDEPT, the value being inserted for WORKDEPT
(E31)
500, 1900);
The following statement also inserts a row into the YEMP table.
However,
allow it, null values are inserted into columns not named: PHONENO,
YDEPT.
PHONE CHAR(4));
This statement copies data from DSN8310.EMP into the newly created
table:
FROM DSN8310.EMP
The two previous statements create and fill a table, TELE, that
looks like
this:
empty. The table has columns for last names, first names, and phone
The INSERT statement fills the newly created table with data
selected from
specifies the data you want selected from one table to be inserted
into
another table.)
statement within the INSERT statement fills the DLIST table with
data from
DNAME VARCHAR(36) ,
INIT CHAR ,
PHONE CHAR(4) );
To change the data in a table, use the UPDATE statement. You can
also use
items of the employee's data in the YEMP work table to reflect the
move,
PHONENO ='5678'
The SET clause names the columns you want updated and provides the
values
you want them changed to. The value you specify can be:
value when the table was created or when the column was added,
or an
error occurs.
register value:
_ CURRENT DATE
_ CURRENT DEGREE
_ CURRENT PACKAGESET
_ CURRENT SERVER
_ CURRENT SQLID
_ CURRENT TIME
_ CURRENT TIMESTAMP
_ CURRENT TIMEZONE
_ USER
_ To update a single row, use a WHERE clause that locates one, and
only
one, row
_ To update several rows, use a WHERE clause that locates only the
rows
If you omit the WHERE clause; DB2 updates every row in the table or
view
an update value that is too large for the column), it stops updating
and
rows in the table are changed (rows already changed, if any, are
restored
UPDATE YEMP
UPDATE YEMP
If you are updating a parent table, you cannot modify a primary key
for
that you enter must match the primary key for each relationship in
which
You can use the DELETE statement to remove entire rows from a
table. The
many rows satisfy the search condition you specified in the WHERE
clause.
If you omit a WHERE clause from a DELETE statement, DB2 removes all
the
rows from the table or view you have named. The DELETE statement
does not
This DELETE statement deletes each row in the YEMP table that has an
When this statement is executed, DB2 deletes any row from the YEMP
table
deleting data and returns error codes in the SQLCODE and SQLSTATE
fields
row must obey the delete rules specified for the table. All delete
rules
fails.
Delete Rules:
DELETE RESTRICT
For example, you can delete an employee from the employee table
even
DELETE CASCADE
First the named rows are deleted, then the dependent rows are
deleted,
department table; that also deletes the rows for all departments
that
table unless you specify a WHERE clause that limits the deletion.
(With
table continues to exist (that is, you can insert rows into it) but
it is
when using DELETE. If you use DROP, all views and authorizations
are
Use the DROP TABLE statement with care: When a table is dropped, it
loses
its data as well as its definition. When you drop a table, all
synonyms,
a view can be dropped, using the DROP VIEW statement. This does not
cause
With the proper authorization, you can also drop a database. When a
you drop a table you also drop all the relationships in which the
table
carefully.
Reference.
Subtopics
FROM DSN8310.EMP
WHERE EMPNO
But you cannot go further because the DSN8310.EMP table does not
include
project number data. You do not know which employees are working on
DSN8310.EMPPROJACT table.
You can nest one SELECT statement within another to solve this
problem.
FROM DSN8310.EMP
WHERE EMPNO IN
(SELECT EMPNO
FROM DSN8310.EMPPROJACT
(SELECT EMPNO
FROM DSN8310.EMPPROJACT
(from DSN8310.EMPPROJACT)
+------+
¦000200¦
+------¦
¦000220¦
+------+
FROM DSN8310.EMP
WHERE EMPNO IN
Fetch +------------------------------+
+---------+-----------+--------¦
+------------------------------+
2.3.1.1 Correlation
only once.
A case in point is the previous query. Its content is the same for
every
uncorrelated.
SELECT AVG(SALARY)
SELECT EMPNO
The result table produced by a subquery can have zero or more rows.
For
INSERT statement.
_ Correlated subqueries.
If you do, the subquery can return at most one value. DB2 compares
that
FROM DSN8310.EMP
(SELECT AVG(EDLEVEL)
FROM DSN8310.EMP);
2.3.2.2 Quantified Predicates: ALL, ANY, and SOME
ALL, ANY, or SOME. When used in this way, the subquery can return
zero,
one, or many values, including null values. You use ALL, ANY, and
SOME in
_ Use ALL to indicate that the value you have supplied must
compare in
the indicated way to all the values the subquery returns. For
ALL:
greater than all the values (that is, greater than the highest
value)
greater than at least one of the values (that is, greater than
the
The results when a subquery returns one or more null values could in
some
You can use IN to say that the value in the expression must be among
the
or "= SOME."
In the subqueries presented thus far, DB2 evaluates the subquery and
uses
when you use the keyword EXISTS, DB2 simply checks whether the
subquery
example:
SELECT EMPNO,LASTNAME
FROM DSN8310.EMP
WHERE EXISTS
(SELECT *
FROM DSN8310.PROJ
search condition. You can also write a subquery that DB2 has to
correlated subquery.
January 1986. This example does not show the full power of EXISTS,
because the result is always the same for every row examined for the
outer
also use the EXISTS keyword with the NOT keyword in order to select
rows
when the data or condition you specify does not exist; that is, you
can
code
search condition. You can also write a subquery that DB2 has to
correlated subquery.
Suppose that you want a list of all the employees whose education
levels
employee's department.
evaluated.
In the subquery, you tell DB2 to compute the average education level
for
the department number in the current row. A query that does this
follows:
FROM DSN8310.EMP X
(SELECT AVG(EDLEVEL)
FROM DSN8310.EMP
Consider what happens when the subquery is executed for a given row
of
replaced with the value of the WORKDEPT column for that row.
Suppose, for
example, that the row is for CHRISTINE HAAS. Her work department is
A00,
which is the value of WORKDEPT for this row. The subquery executed
for
(SELECT AVG(EDLEVEL)
FROM DSN8310.EMP
Thus, for the row considered, the subquery produces the average
education
in place of A00. For example, for the row for MICHAEL L THOMPSON,
this
value is B01, and the subquery for his row delivers the average
education
The result table produced by the query has the following values:
(from DSN8310.EMP)
Fetch +------------------------------------------+
+---------+-----------+----------+---------¦
+---------+-----------+----------+---------¦
+---------+-----------+----------+---------¦
+---------+-----------+----------+---------¦
_ _ _ _
_ _ _ _
_ _ _ _
represents.
You can define a correlation name for each table name appearing in a
FROM
clause. Simply append the correlation name after its table name.
Leave
one or more blanks between a table name and its correlation name,
and
table name. The following FROM clause, for example, defines the
correlation names TA and TB for the tables TABLEA and TABLEB, and no
in a containing subquery.
name represents the row you delete. DB2 evaluates the correlated
subquery
once for each row in the table named in the DELETE statement to
decide
time or less. The department then deletes the rows for that project
from
WHERE .5 >
(SELECT SUM(ACSTAFF)
FROM DSN8310.PROJACT
WHERE PROJNO = X.PROJNO);
that project is less than 0.5. If it is, DB2 deletes that row from
the
DSN8310.PROJ table.
(SELECT *
FROM DSN8310.PROJ
depend on whether the row for any department was accessed before or
after
is prohibited.
table:
DELETE FROM DSN8310.DEPT THIS
(SELECT WORKDEPT
FROM DSN8310.EMP
With the referential constraints defined for the sample tables, the
and the last delete rule in the path to EMP is SET NULL, not
RESTRICT. If
2.4 Chapter 2-4. Using SPUFI: Executing SQL from Your Terminal
terminal using the SPUFI (SQL processor using file input) facility.
You
and using the sample tables shown in Appendix A, "DB2 Sample Tables"
in
topic APPENDIX1.1.
other tasks:
To invoke SPUFI, select SPUFI from the DB2I Primary Option Menu as
shown
in Figure 6.
+-------------------------------------------------------------------
---------------+
¦
¦
¦
¦
¦
¦
¦
¦
¦
¦
¦
¦
¦
¦
¦
¦
+-------------------------------------------------------------------
---------------+
Data you enter on the SPUFI panel tells DB2 how to process your
input data
When the SPUFI panel is first displayed, enter the name of an input
data
set (where you will put SQL statements you want DB2 to execute) and
an
output data set (where DB2 will put the results of your queries).
You can
also enter new processing option defaults to specify how you want
the next
The next time (and subsequent times) the SPUFI panel is displayed,
the
data entry fields on the panel will contain the values that were set
on
the panel previously. You can specify data set names and processing
options each time the SPUFI panel displays, as needed. Values you
do not
+-------------------------------------------------------------------
---------------+
¦
¦
¦ DSNESP01 SPUFI
SSID: DSN ¦
¦ ===>
¦
¦
¦
¦
¦ Specify processing
options: ¦
¦ 10 CONNECT LOCATION
===> ¦
¦
¦
+-------------------------------------------------------------------
---------------+
_ Allocate this data set before you invoke SPUFI. The name
must
_ The data set can be empty before you begin the session.
You can
then add the SQL statements by editing the data set from
SPUFI.
for any data set that was not created by the EXPORT
command
of QMF
If you use this panel a second time, the name of the data set
you
partitioned data set, change only the member name. If you need
to
Enter the name of a data set to receive the output of the SQL
data.
Look at the processing options:
5 CHANGE DEFAULTS
you specify Y(YES) you can look at the SPUFI defaults panel.
See
information about the values you can specify and how they
affect
6 EDIT INPUT
To edit the input data set, leave Y(YES) on line 6. You can
use the
ISPF editor to create a new member of the input data set and
enter
7 EXECUTE
Y(YES) on line 7.
8 AUTOCOMMIT
9 BROWSE OUTPUT
results are saved in the output data set. You can look at them
at
any time, until you delete or write over the data set.
10 CONNECT LOCATION
When you finish with the SPUFI panel, press the ENTER key. Because
you
specified YES on line 5 of the SPUFI panel, the next panel you see
is the
+-------------------------------------------------------------------
---------------+
¦
¦
¦
===>
¦
¦ returned from a
SELECT) ¦
¦
¦
¦ Output format
characteristics: ¦
¦
¦
¦
¦
¦
¦
+-------------------------------------------------------------------
---------------+
Default values are provided for each user the first time SPUFI is
used.
Defaults are set for all options except the DB2 subsystem name. Any
changes you make to these values remain in effect until the values
are
topic 2.4.1.
panel:
1 ISOLATION LEVEL
3 RECORD LENGTH
4 BLOCKSIZE
an even multiple of LRECL. For VB and VBA only, the block size
must
5 RECORD FORMAT
6 DEVICE TYPE
storage device.
fields.
9 COLUMN HEADING
You can specify NAMES, LABELS, ANY or BOTH for column headings.
label.
_ BOTH creates two title lines, one with names and one with
labels.
When you have specified SPUFI options, press the ENTER key to
continue.
If you press the END key, you return to the SPUFI panel, but all
changes
made on the SPUFI Defaults panel are lost. If you press ENTER, your
Next, SPUFI lets you edit the input data set. Initially, editing
consists
of entering an SQL statement into the input data set. You can also
edit
an input data set that contains SQL statements and you can change,
delete,
On the panel, use the ISPF EDIT program to enter SQL statements that
you
Move the cursor to the first input line and enter the first part of
an SQL
You can put more than one SQL statement in the input data set. You
can
put an SQL statement on one line of the input data set or on more
than one
line. When the data set is processed, DB2 executes the statements
one
after the other. Do not put more than one SQL statement on a single
line.
The first one is executed, but other SQL statements on the same line
are
ignored.
When using SPUFI, end each SQL statement with a semicolon (;). This
tells
When you have entered the SQL statements that you want, press the
END PF
+-------------------------------------------------------------------
---------------+
¦
¦
¦ 000200 FROM
DSN8310.EMP ¦
¦ 000400 ORDER BY
LASTNAME; ¦
¦
¦
¦
¦
¦
¦
+-------------------------------------------------------------------
---------------+
Pressing the END PF key saves the data set. You can save the data
set and
Figure 9 shows what the panel looks like if you enter the sample SQL
The editing step is bypassed when you reset the EDIT INPUT
processing
option by specifying:
You can put comments about SQL statements either on separate lines
or on
the same line. In either case, two hyphens (--) are used to begin a
SPUFI passes the input data set to DB2 for processing. The SQL
statement
The DB2 processing step is bypassed when you specify the EXECUTE
processing option:
large a table DB2 has to search, or on how many rows DB2 has to
process.
To interrupt DB2's execution, press the PA1 key and respond to the
This cancels the executing SQL statement and returns you to the
ISPF-PDF
menu.
What happens to the output data set? This depends on how far
execution
DB2 might not have opened the output data set yet, or the output
data set
SPUFI formats and displays the output data set using the ISPF Browse
program. The output from the sample program is shown in Figure 10.
An
output data set contains these items for each SQL statement executed
by
DB2:
_ The SQL statement that was executed, copied from the input data
set
the statement.
_ At the end of the data set are summary statistics that describe
the
+-------------------------------------------------------------------
-----------------------+
¦
¦
¦
¦
¦
¦
¦ BROWSE-- userid.RESULT
COLUMNS 001 072 ¦
¦ --------+---------+---------+---------+---------+---------
+---------+---------+ ¦
¦ FROM
DSN8310.EMP
00020000 ¦
¦ WHERE WORKDEPT =
'D11' 00030000 ¦
¦ ORDER BY
LASTNAME;
00040000 ¦
¦ ---------+---------+---------+---------+---------+---------
+---------+---------+ ¦
¦ LASTNAME FIRSTNME
PHONENO ¦
¦ BROWN DAVID
4501 ¦
¦ JOHN REBA
0672 ¦
¦ JONES WILLIAM
0942 ¦
¦ LUTZ JENNIFER
0672 ¦
¦ PIANKA ELIZABETH
3782 ¦
¦ SCOUTTEN MARILYN
1682 ¦
¦ STERN IRVING
6423 ¦
¦ WALKER JAMES
2986 ¦
¦ YAMAMOTO KIYOSHI
2890 ¦
¦ YOSHIMURA MASATOSHI
2890 ¦
¦ ---------+---------+---------+---------+---------+---------
+---- ¦
¦ ---------+---------+---------+---------+---------+---------
+---- ¦
¦ ---------+---------+---------+---------+---------+---------
+---- ¦
DB2 sends back a return code; your program should test the return
code to
in topic 3.1.2.
_ Declare the data items used to pass data between DB2 and a host
topic 2.0 and in SQL Reference. Details about how to use SQL
that are indicated with return codes from DB2, in the SQLCA.
See
information.
Using a Cursor to Retrieve a Set of Rows" discusses how you can use
a
written in assembler, C, COBOL, FORTRAN, and PL/I. You can also use
SQL
Ada. See IBM Ada/370 SQL Module Processor for DB2 Database
Manager
are not the defaults). Character string literals within SQL and
host
_ The SQL statements access data in the sample tables that are
shipped
COBOL END-EXEC
For example, use EXEC SQL and END-EXEC to delimit an SQL statement
in a
EXEC SQL
an SQL statement
END-EXEC.
or insert data, you can declare the tables and views your program
accesses
with, and the data type of each column. You can refer to the
DECLARE
statement for the column names and data types in the table or view.
make sure you have used correct column names and data types in your
SQL
names and data types do not correspond to the SQL DECLARE statements
in
your program.
your COBOL program. Specify the name of the table and list each
column
and its data type. When you declare a table or view, you specify
DECLARE
a view.
For example, the DECLARE TABLE statement for the DSN8310.DEPT table
looks
like this:
EXEC SQL
MGRNO CHAR(6) ,
LOCATION CHAR(16) )
END-EXEC.
topic 3.3.
You can access data using host variables and host structures.
case, COBOL) for use within an SQL statement. Using host variables,
you
can:
_ Retrieve data and put it into the host variable for use by the
application program
clause.
Etc.
Any valid host variable name can be used in an SQL statement. The
name
the associated data in the data base to get the best performance.
For
information.)
that the variable is not a column name. (4) Host variables outside
of SQL
language section:
You can use a host variable to specify a program data area that is
to
For example, suppose you are retrieving the EMPNO, LASTNAME, and
WORKDEPT
column values from rows in the DSN8310.EMP table. You can define a
data
area in your program to hold each column, then name the data areas
with an
preceded by a colon):
EXEC SQL
FROM DSN8310.EMP
END-EXEC.
and CBLDEPT must be declared such that each is compatible with the
data
WORKDEPT.
If the SELECT statement returns more than one row, this is an error,
and
items in the SELECT clause, you are not restricted to the column
names of
EXEC SQL
FROM DSN8310.EMP
END-EXEC.
The results are shown below with column headings that represent the
names
rows that will be returned, or if you expect that more than one row
will
be returned, then you must use an alternative to the SELECT ... INTO
statement.
a set of rows and retrieve one row at a time from the result table.
This
variable. Use the host variable name in the SET clause of UPDATE or
the
EXEC SQL
UPDATE DSN8310.EMP
END-EXEC.
you can retrieve the name of the employee whose number is 000110
with:
EXEC SQL
SELECT LASTNAME
INTO :PGM-LASTNAME
FROM DSN8310.EMP
WHERE EMPNO = :EMPID
END-EXEC.
Retrieving Data into Host Variables: If the value for the column
you are
information.
When DB2 retrieves the value of a column, you can test the indicator
value is null. When the column value is null, DB2 puts nothing into
the
string.
the host variable. Optionally, you can use the word INDICATOR
between the
host variable and its indicator variable. Thus, the following two
+-------------------------------------------------------------------
-------------------------------+
¦ END-EXEC. ¦ END-
EXEC. ¦
+-------------------------------------------------------------------
-------------------------------+
of CBLPHONE.
When a column value is fetched using a cursor, you can use the same
Inserting Null Values into Columns Using Host Variables: You can
use an
greater than -1, the associated host variable contains a value for
the
column.
For example, you could put a value in a column (using INSERT or
UPDATE),
but you are not sure whether the value is always specified with the
input
can code:
EXEC SQL
UPDATE DSN8310.EMP
END-EXEC.
When NEWPHONE contains other than a null value, set PHONEIND to zero
by
MOVE 0 TO PHONEIND.
MOVE -1 TO PHONEIND.
3.1.4.1.5 Considerations
If you transfer data between a DB2 column and a host variable, and
the two
do not have the same data type or length attribute, you can expect
the
host structures.
EXEC SQL
FROM DSN8310.VEMP
END-EXEC.
EXEC SQL
INTO :PEMP
FROM DSN8310.VEMP
END-EXEC.
You can declare a host structure yourself, or you can use DCLGEN to
program retrieves into a host structure can be null, you can attach
an
01 PEMP-ROW.
10 FIRSTNME.
10 LASTNAME.
49 LASTNAME-LEN PIC S9(4) USAGE COMP.
01 INDICATOR-TABLE.
EXEC SQL
INTO :PEMP-ROW:EMP-IND
FROM DSN8310.EMP
END-EXEC.
EMP-IND array values are always zero. The first five columns of
each row
are defined NOT NULL. In the above example, DB2 selects the values
for a
any) selected column values are null. For information on using the
IS
in topic 2.1.
return codes in the SQLCODE (5) and SQLSTATE fields of the SQLCA.
The
failed.
SQLCA.
¦
¦
¦ The -911 SQLCODE indicates that a unit of work has been rolled
back. ¦
¦ compromised.
¦
¦
¦
+-------------------------------------------------------------------
-----+
The meaning of SQLCODEs other than 0 and 100 varies with the
particular
product implementing SQL.
the same way for different IBM database management systems. See
Appendix
The WHENEVER statement causes DB2 to check the SQLCA and continue
a general condition is true. You can specify more than one WHENEVER
EXEC SQL
END-EXEC
error.
NOT FOUND Indicates what should be done when DB2 cannot find a
row to
CONTINUE
GOTO or GO TO host-label
unqualified paragraph-name.
continue to execute and return good data for rows in which the error
does
For rows in which the error does occur, one or more selected items
have no
in the SQLCA.
DB2 can be used to retrieve and process a set of rows. Each row in
the
program cannot process all the rows at once. The program needs to
process
result table (6) to hold all the rows retrieved by executing the
SELECT
statement. DB2 uses a cursor to make rows from the result table
available
retrieve each row sequentially from the result table until end-of-
data
statement defines and names the cursor, identifying the set of rows
to be
The cursor must be opened (with an OPEN statement) before any rows
are
row. FETCH can be executed repeatedly until all rows have been
retrieved.
When the end-of-data condition occurs, you must close the cursor
with a
Your program can have several cursors. Each cursor requires its
own:
implementation.
data is kept in the DSN8310.EMP table. The following shows the SQL
+-------------------------------------------------------------------
-----+
¦
Program ¦
+-------------------------------------------------------------------
-----¦
+------------------------------------
+-----------------------------------¦
¦ WORKDEPT, JOB
¦ ¦
¦ FROM DSN8310.EMP
¦ ¦
¦ END-EXEC. ¦
¦
+------------------------------------
+-----------------------------------¦
¦ END-EXEC.
¦ ¦
+------------------------------------
+-----------------------------------¦
¦ GO TO CLOSE-THISEMP ¦ topic
3.2.2.3 ¦
¦ END-EXEC.
¦ ¦
+------------------------------------
+-----------------------------------¦
¦ :DEPT, :JOB-NAME ¦
¦
¦ END-EXEC.
¦ ¦
+------------------------------------
+-----------------------------------¦
¦
¦ ¦
¦ EXEC SQL
¦ ¦
¦ UPDATE DSN8310.EMP
¦ ¦
¦ END-EXEC.
¦ ¦
¦
¦ ¦
+------------------------------------
+-----------------------------------¦
¦ ¦
¦
¦ EXEC SQL
¦ ¦
¦ END-EXEC.
¦ ¦
+------------------------------------
+-----------------------------------¦
+------------------------------------
+-----------------------------------¦
¦ CLOSE THISEMP
¦ ¦
¦ END-EXEC.
¦ ¦
+-------------------------------------------------------------------
-----+
EXEC SQL
SELECT column-name-list
FROM table-name
WHERE search-condition
END-EXEC.
The SELECT statement shown here is rather simple. You can code
several
the rows of the identified table, include the FOR UPDATE OF clause,
which
names each column you intend to update. The effects of the FOR
UPDATE OF
names of columns you intend to update, and you do not specify the
of the result table. In this case, you do not need to name the
column in
the SELECT statement (but do not forget to name it in the FOR UPDATE
OF
column value you want to update, you can use UPDATE ... WHERE
CURRENT OF
to update the row.
For example, assume that each row of the result table includes the
EMPNO,
update the JOB column (one of the columns in the DSN8310.EMP table),
the
DECLARE CURSOR statement must include FOR UPDATE OF JOB even though
JOB is
To tell DB2 you are ready to process the first row of the result
table,
have your program issue the OPEN statement. When this happens, DB2
identify a set of rows using the current value of any host variables
EXEC SQL
OPEN cursor-name
END-EXEC.
When used with cursors, the CURRENT DATE, CURRENT TIME, and CURRENT
Test the SQLCODE field for a value of 100 or the SQLSTATE field for
a
These codes occur when a FETCH statement has retrieved the last row
in the
EXEC SQL
END-EXEC.
use the FETCH statement. The SELECT statement within the DECLARE
CURSOR
wants (that is, the result table is defined), but DB2 does not
retrieve
to the next row in the result table, making it the current row. DB2
then
moves the current row contents into your program host variables
(specified
with the INTO clause). This sequence is repeated each time FETCH is
issued, until you have processed all rows in the result table.
EXEC SQL
FETCH cursor-name
END-EXEC.
current row. Block fetch cannot be used when a cursor is used for
update
or delete.
EXEC SQL
UPDATE table-name
END-EXEC.
When used with a cursor, the UPDATE statement differs from the one
topic 2.2.
_ The WHERE clause identifies the cursor that points to the row to
be
updated.
After you have updated a row, the cursor points to the current row
until
Remember that you cannot update a row if your update violates any
the UPDATE statement repeatedly when you update all rows that meet a
fields of the SQLCA when you try to update the columns. This
When your program has retrieved the current row, you can delete the
row by
EXEC SQL
END-EXEC.
When used with a cursor, the DELETE statement differs from the one
you
topic 2.2.
_ The WHERE clause identifies the cursor that points to the row to
be
deleted.
DECLARE statement.
After you have deleted a row, you cannot update or delete another
row
using that cursor until you issue a FETCH statement to position the
cursor
"Deleting Rows: DELETE" in topic 2.2.2.4 showed you how to use the
DELETE
repeatedly when you want to obtain a copy of the row, examine it,
and then
delete it.
Remember that you cannot delete a row if doing so will result in the
table unless the employee has already been deleted from the project
table
and the project activity table. This is because of the way the
information.
EXEC SQL
CLOSE cursor-name
END-EXEC.
If you are finished processing the rows of the "result table" and
you do
not want to use the cursor, you can let DB2 automatically close the
cursor
rolls back the changes made so far), DB2 automatically closes all
open
cursors that were not declared with the WITH HOLD option. You can
reopen
the cursor after it has been closed, but you begin processing at the
To maintain a cursor and its position across commit points, use the
WITH
only locks that are not required to maintain cursor position. After
the
the last row retrieved and before the next logical row of the result
table
to be returned.
EXEC SQL
FROM DSN8310.EMP
ORDER BY EMPNO
END-EXEC.
DECLARE CURSOR WITH HOLD should not be used with the new user
signon
+--- IMS
------------------------------------------------------------+
¦
¦
¦ for a new user. You can use WITH HOLD for non-message-driven
BMP ¦
¦
¦
+-------------------------------------------------------------------
-+
+--- CICS
-----------------------------------------------------------+
¦
¦
¦ let DB2 close a CICS attachment cursor, the cursor might not
close ¦
¦
¦
+-------------------------------------------------------------
partitioned data set that you can include in your program. When you
use
COBOL record description. You can use DCLGEN for table declarations
only
the table or view name before you precompile your program. To use
the
DB2 must be active before you can use DCLGEN. You can invoke DCLGEN
in
_ From ISPF through DB2I. Select the DCLGEN option on the DB2I
Primary
Option Menu panel. Next, fill in the DCLGEN panel with the
then DCLGEN.
_ With JCL. Supply the required information, using JCL, and run
DCLGEN
in batch.
you can enter DBCS character using the hex mode of ISPF edit.
Options Menu. For more instructions on using DB2I, see "Using ISPF
and
+-------------------------------------------------------------------
---------------+
¦
¦
¦ DSNEDP01 DCLGEN
SSID: DSN ¦
¦
===>
¦
¦
¦
¦
¦
¦
¦
¦ Enter options as
desired: ¦
¦
¦
¦
¦
¦
¦
¦
¦
¦
¦
¦
¦
+-------------------------------------------------------------------
---------------+
generator.
'DON''S TABLE'
apostrophes.
TABLE OWNER
AT LOCATION
location_name.owner_id.table_name
for example,
PLAINS_GA.CARTER.CROP_YIELD_89
ACTION
set name.
already exists.
COLUMN LABEL
STRUCTURE NAME
this field blank, the field names are the same as the
column
DELIMIT DBCS
default.
_ The name is a DBCS string, and you have requested that DBCS
names be
delimited.
source program:
EXEC SQL
END-EXEC.
EXEC SQL
INCLUDE DECEMP
END-EXEC.
For various reasons, there are times when DCLGEN does not produce
the
results you expect. You might need to edit the results, tailoring
the
+-------------------------------------------------------------------
--------------------------------------------------+
+-------------------------------------------------------------------
--------------------------------------------------¦
+---------------------+------------------------
+-------------------------------------------------
+--------------------¦
¦ ¦ ¦
USAGE COMP ¦
¦
+---------------------+------------------------
+-------------------------------------------------
+--------------------¦
¦ ¦ ¦ USAGE
COMP ¦ ¦
+---------------------+------------------------
+-------------------------------------------------
+--------------------¦
¦ ¦ the declaration. ¦ is
generated. ¦ ¦
+---------------------+------------------------
+-------------------------------------------------
+--------------------¦
¦ FLOAT(n) ¦
¦
¦ ¦
¦ 1 <= n <= 21 ¦
¦
¦ ¦
+---------------------+------------------------
+-------------------------------------------------
+--------------------¦
¦ or FLOAT(n) ¦
¦ ¦
¦
+---------------------+------------------------
+-------------------------------------------------
+--------------------¦
+---------------------+------------------------
+-------------------------------------------------
+--------------------¦
+---------------------+------------------------
+-------------------------------------------------
+--------------------¦
¦ VARCHAR(n) ¦ struct ¦ 10
var. ¦ CHAR(n) VAR ¦
¦ ¦ } var; ¦
¦ ¦
+---------------------+------------------------
+-------------------------------------------------
+--------------------¦
¦ ¦ exact equivalent); ¦
or ¦ ¦
¦ ¦ declaration.
¦
¦ ¦
+---------------------+------------------------
+-------------------------------------------------
+--------------------¦
¦ ¦ declaration. ¦
or ¦ ¦
¦ ¦ ¦ 10
var. ¦ ¦
¦ ¦ ¦ 49 var_LEN PIC
9(4) USAGE COMP. ¦ ¦
¦ ¦ ¦ 49 var_TEXT PIC
N(n).(1) ¦ ¦
+---------------------+------------------------
+-------------------------------------------------
+--------------------¦
+---------------------+------------------------
+-------------------------------------------------
+--------------------¦
+---------------------+------------------------
+-------------------------------------------------
+--------------------¦
+-------------------------------------------------------------------
--------------------------------------------------¦
¦
Notes:
¦
¦
¦
¦
¦
¦
¦ 3. This declaration is used unless there is a time installation
exit for formatting times, in which case the ¦
+-------------------------------------------------------------------
--------------------------------------------------+
following scenario:
Figure 16.
new defaults, if any, and return to the DB2I Primary Option menu.
+-------------------------------------------------------------------
---------------+
¦
¦
¦
¦
¦
¦
¦ DSNEOP01 DB2I
DEFAULTS ¦
¦ COMMAND
===>_
¦
¦
¦
¦ Change defaults as
desired: ¦
¦
¦
¦
¦
¦
===> //
* ¦
¦ ===> //*
¦
¦
===> //
* ¦
¦
¦
¦
¦
¦
¦
¦
¦
+-------------------------------------------------------------------
---------------+
+-------------------------------------------------------------------
---------------+
¦
¦
¦
¦
¦
¦
¦ DSNEOP02 COBOL
DEFAULTS ¦
¦ COMMAND
===>_
¦
¦
¦
¦ Change defaults as
desired: ¦
¦
¦
¦
¦
¦
¦
Figure 16. The COBOL Defaults Panel. Shown only if the application
Select option 2 on the DB2I Primary Option menu, and press Enter to
Fill in the fields as shown in Figure 17, and then press Enter.
+-------------------------------------------------------------------
---------------+
¦ DSNEDP01 DCLGEN
SSID: DSN ¦
¦
===>
¦
¦
¦
¦ 2 TABLE OWNER
===> ¦
¦
¦
¦
¦
¦ Enter options as
desired: ¦
¦
¦
¦
¦
¦
¦
+-------------------------------------------------------------------
---------------+
+-------------------------------------------------------------------
---------------+
¦
***
¦
¦
¦
DB2 then displays the screen as shown in Figure 19. Press Enter to
return
+-------------------------------------------------------------------
---------------+
¦
¦
¦ DSNEDP01 DCLGEN
SSID: DSN ¦
¦
===>
¦
¦ 2 TABLE OWNER
===> ¦
¦
¦
¦
¦
¦ Enter options as
desired: ¦
¦
¦
¦
¦
+-------------------------------------------------------------------
---------------+
Figure 19. DCLGEN Panel--Displaying System and User Return Codes
command line of the DB2I Primary Option menu. The ISPF/PDF menu is
then
displayed, and you can select either the browse or the edit option
to view
the results.
***** DCLGEN
TABLE(DSN8310.VPHONE) ***
*****
LIBRARY(SYSADM.TEMP.COBOL(VPHONEC)) ***
***** QUOTE
***
) END-EXEC.
01 DCLVPHONE.
10 LASTNAME.
10 FIRSTNAME.
10 PHONENUMBER.
10 DEPTNAME.
As with any application, you must plan and design programs to meet
the
that you also plan for binding, locking, recovery, and perhaps for
using
possible.
program design.
requests to DB2 by some other means. The DB2 precompiler does the
following:
code.
After you have precompiled your source program, you create a load
module,
PICTURE 2
Figure 21. Program Preparation Overview. Two processes are
performed:
affect the way you design or code the program. They allow you to
tell the
you use or what value you depend on for the maximum precision of a
decimal
number. Or, they tell the precompiler what you want it to do--how
many
report. In many cases, you may want to accept the default value
provided.
A few options, however, can affect the way you code. For example,
you
need to know if you are using NOFOR or STDSQL(86) before you begin
coding.
Before you begin coding, please review the list of options in Table
24 in
topic 4.2.2.4.
4.1.1.2 Planning to Bind
path when the statement executes. For statements that are not
executed
system resources, the access paths used each time the statements
execute
Depending upon how you design your DB2 application, you might bind
all
you might bind some or all of your DBRMs into separate packages in
for the entire application, listing the packages that are included
and
binding any DBRMs that are not bound into packages. Regardless of
what
the plan contains, you must bind a plan before the application can
run.
4.1.1.2.1 Deciding How to Use Packages
the beginning. For example, you might decide to put certain SQL
the same DBRM and then bind them into the same package.
At its simplest, you can bind each DBRM into its own package. A
At the other extreme, you can bind all your DBRMs to a single plan.
This
operation includes all of the DBRMs, even though not all of them
have
in topic 4.1.1.3 .
You must decide how to use packages based on your application design
and
your operational objectives. Keep in mind the following:
_ When using packages, the entire plan need not be rebound when a
change
by using packages allows you to maintain only one plan and helps
to
_ Most options specified when binding the plan apply only to the
DBRMs
bound directly to the plan. You can use different options when
you
When you bind the plan, you can name a different qualifier for
+--- CICS
-----------------------------------------------------------+
¦
¦
¦
selection. ¦
¦
¦
+-------------------------------------------------------------------
-+
Input to binding the plan can include DBRMs only, a package list
only, or
Binding with a Package List Only: Binding a plan that includes only
a
when binding each package, you can control such things as qualifiers
and
isolation level.
Binding with Both DBRMs and a Package List: Binding DBRMs directly
to the
change is needed.
REBIND PACKAGE(*)
DB2 allows more than one application process to access the same data
at
must be controlled.
might both read the same row from the database, and both
calculate new
update is lost.
not later committed, but backed out, B's calculations are based
on
other SQL requests. Later, A reads the first row again and must
find
in a way that affects how other processes can access the same
resource.
lock. DB2 uses locks to ensure that no process accesses data that
has
basis of certain general parameters; you can make better use of your
parameters.
statements. Those locks are the ones over which you have the
most
Guide.
The object of a lock is the resource being locked. You have most
control
over locks on user data in tables. But there are also locks on DB2
internal objects. Most of those you are never aware of, but
sometimes you
user data also lock portions of the DB2 catalog. Also, operations
subject
example, if you try to delete a row from a parent table, DB2 might
have to
delete rows from the dependent table as well. If you try to update
a
row exists. In both those cases, DB2 must lock and read data in the
Simple Tablespace
Partitioned Tablespace
Segmented Tablespace
Simple Tablespaces
Partitioned Tablespaces
Segmented Tablespaces
Segment size can be defined while defining the table using SEGSIZE
parameter.
space lock (the largest size) controls the most data, a page lock
(the
+---------------------------+
+---------------------------+
¦ ¦
¦ ¦
+---------------------------+
+---------------------------+
¦ ¦
+---------------+ ¦
¦ ¦ ¦
¦ Table lock ¦ ¦
+---------------+ ¦
¦ ¦
+---------+ +---------+
¦ Page ¦ ¦ Page ¦
¦ lock ¦ ¦ lock ¦
+---------+ +---------+
Figure 22. Sizes of Objects Locked
pages.
Guide.
In a simple table space, a single page can contain rows from more
than one
table. A lock on the page or on the entire table space locks all
the data
in the page or table space, no matter what table the data belongs
to. A
lock needed to access data from one table can make data from other
tables
temporarily unavailable.
space does not lock data from more than one table.
Duration of Table and Table Space Locks: Table and table space
locks can
them until the resource they lock is first used. For the principal
means
Duration of Page Locks: Page locks are acquired when the page is
first
topic 4.1.2.7.2.
The mode (sometimes state) of a lock tells what access to the locked
processes.
Figure 23 shows the possible modes for page locks; Figure 24 shows
the
modes for table and table space locks. When a page is locked, the
table
table space lock has one of the intent modes: IS, IX, or SIX. (In
contrast, the modes S, U, and X of table and table space locks are
equally to all indexes on all tables in the table space. The lock
subpages are not locked separately. If the process has an IS, IX,
or SIX
separately.
without locking the table space, through the claim and drain
mechanisms.
+-------------------------------------------------------------------
-----+
¦
¦
¦ Page Lock
Modes ¦
¦
¦
¦ over
resources. ¦
¦
¦
¦ S
(SHARE)
¦
¦ The lock owner and any concurrent processes can read, but
not ¦
¦ lock.
¦
¦
¦
¦ U
(UPDATE)
¦
¦ The lock owner can read, but not change, the locked
page; ¦
¦ acquire a U
lock. ¦
¦
¦
¦
¦
¦ X
(EXCLUSIVE)
¦
¦
¦
¦
¦
¦
¦
¦
¦
¦
¦
¦
¦
+-------------------------------------------------------------------
-----+
+-------------------------------------------------------------------
-----+
¦
¦
¦
¦
¦ over
resources. ¦
¦
¦
¦
¦
¦
¦
¦ IS (INTENT
SHARE) ¦
¦ The lock owner can read data in the table or table space,
but ¦
¦ the data. The lock owner might acquire a page lock on any
data ¦
¦ it
reads. ¦
¦
¦
¦ IX (INTENT
EXCLUSIVE) ¦
¦ The lock owner and concurrent processes can read and change
data ¦
¦
¦
¦ S
(SHARE)
¦
¦ The lock owner and any concurrent processes can read, but
not ¦
¦
¦
¦ U (UPDATE)
¦
¦ The lock owner can read, but not change, the locked
data; ¦
¦
¦
¦
¦
¦ The lock owner can read and change data in the table or
table ¦
¦ space, but not change it. Only when the lock owner
changes ¦
¦
¦
¦ X
(EXCLUSIVE)
¦
¦
¦
¦
¦
¦
¦
¦
¦
¦
¦
¦
¦
+-------------------------------------------------------------------
-----+
Figure 24. Modes of Table and Table Space Locks
of some modes do not shut out all other users. Assume that
application
the mode of A's lock permits B's request, the two locks (or modes)
are
said to be compatible.
If the two locks are not compatible, B cannot proceed. It must wait
until
A releases its lock. (And, in fact, it must wait until all existing
+------------------------------+
+------------------------------¦
¦ Page ¦ ¦ ¦ ¦
¦ Lock Mode¦ S ¦ U ¦ X ¦
+------------+-----+-----+-----¦
¦ S ¦ Yes ¦ Yes ¦ No ¦
+------------+-----+-----+-----¦
¦ U ¦ Yes ¦ No ¦ No ¦
+------------+-----+-----+-----¦
¦ X ¦ No ¦ No ¦ No ¦
+------------------------------+