DB2
DB2
DB2
DB2
DB2 is an abbreviation of IBM DATABASE II; it is Relational Data Base Management System (RDBMS) for
MVS operating system
In DB2, data is stored in the form of tables (relations) comprised of rows (records) and columns (fields). All
access to and manipulation of data in DB2 is accomplished via Structured Query Language (SQL).
DB2 objects
In MVS environment, the relational database manager itself (DB2) is considered a subsystem (an MVS
started task). Typically an installation consists of two or more subsystems. i.e, there is one for test and one
for production. For each DB2 subsystem, there are some system objects e.g. a catalog, a log, and a
directory.
DB2 Database
It is a logical grouping of tables, tablespaces and their indexes for management purposes. Normally, data for
a specific application is contained within one database
DB2 Tablespace
DB2 Guide Page 3 of 28
Tablespace is physical collection of tables and their associated indexes. Tablespaces uses one or more
VSAM linear datasets (LDS) and the maximum size of Tablespace is 64GB. A Tablespace is divided into
equal sized units (of 4k bytes), called pages which are written to or read from DASD in one operation.
In order to service a request to see one byte of data, DB2 brings in the 4K page containing that byte.
DB2 tables
DB2 tables holds data in the form of rows and columns.
DB2 Index
An Index is an ordered set of pointers to data in a DB2 table. An index space is automatically defined in the
same database as the table.
DB2 Views
View is a virtual table defined on another table or view. The data only exist in the base table only the
definition of view is stored.
DB2 Catalog
The DB2 catalog consists of a group of tables containing information about other objects with in the DB2
subsystem. When you create, alter, or drop any object, DB2 inserts, updates, or deletes rows of the catalog
tables that describe the object and also have the information how this object relates to other objects.
SYSTABLES Contains one row for each table, view and alias
SYSINDEXES Contains one row for each index
SYSCOLUMNS Contains one row for each table and view
SYSVIEWS Contains one row fro each view
SYSRELS Contains one row for every referential constraint
Structured Query language is a standardized language for defining and manipulating data. All SQL
statements should be prepared (Bind) before they can be executed. The result of preparation is executable
form of statement (plan).
Static SQL
Dynamic SQL
In dynamic SQL the statements are constructed, prepared and executed at runtime.
Statements Available
CREATE
ALTER
DROP
CREATE
CREATE statement is used to create various database objects for example DB2 table, Index, Tablespace,
View etc.
e.g.
The data type of a column determines what you can and cannot do with the column. When you perform
operations on columns, the data must be compatible with the data type of the referenced column. For
example, you cannot insert character data, like a last name, into a column whose data type is numeric.
Similarly, you cannot compare columns containing incompatible data types.
To better understand the concepts that are presented in this section, you must understand the data types of
the columns to which an example refers. As shown in built-in data types have three general categories:
datetime, string, and numeric
DB2 Guide Page 5 of 28
Signed Numeric
String
GRAPHIC(n) Double byte character strings(DBCS), I char is stored in two bytes., so this data type
will take 2n bytes.
VARGRAPHIC(n) Variable Double byte character strings(DBCS), Maximum it uses 2n + 2 bytes
depending upon n
DB2 Guide Page 6 of 28
Datetime
DATE YYYYMMDD, stored in packed decimal format and it will take 4 bytes.
DATETIME HHMMSS, stored in packed decimal format and it will take 3 bytes.
TIMESTAMP Timestamp is nothing but date and time accurate up to nearest micro second.
YYYYMMDDHHMMSSnnnnnn. Timestamp will take 10 bytes.
Use the CREATE TABLE statement to create a table. The following SQL statement creates a table named
PRODUCT:
Identifying defaults
If you want to constrain the input or identify the default of a column, you can use the following values:
Check Constraint
We can restrict user to certain set of inputs, This constraint will be useful when you know the exclusive list
of values that can be contained in that column.
PRIMARY KEY
Primary key value uniquely identifies the row
FOREIGN KEY
Foreign Key value identifies a row of related data. (Usually the row being related is in another table)
The table containing primary key is called as parent table and the one containing the foreign key is called as
dependent table. A dependent of dependent is a descendent table.
A table can be parent of many dependents and it can also be dependent of many parents.
REFERENTIAL INTEGRITY
Referential Integrity maintains data validity by enforcing rules during processing so that
Delete Rules
When a row with primary key is deleted what should be done about the rows with matching keys.
Cascade – if parent table row is deleted then all the matching rows in dependent table will also be
deleted
Set Null – if parent table row is deleted then all the matching columns (foreign key column) in
dependent table will be set to null.
Restrict – delete request will be cancelled if there is at least one row in dependent table.
INDEX
It is set of pointers to DB2 tables. If an index is unique, DB2 will ensure that uniqueness is maintained (it will
not allow another row with the same key to be added to the table).
CLUSTERING INDEX
DB2 Guide Page 9 of 28
Many users will want to see the group of rows for a single dept value. Performance will improved if all of the
rows for a single DEPT value were physically grouped together. Then we ask for all of C01 employees since
all three of C01 rows are in a single page, and are accessed by a single read.
When the index is defined (via CREATE INDEX statement) add the CLUSTER at the end of the statement.
When REORG utility is executed, the utility will rearrange the data rows into the same sequence as the
entries in clustering index.
ALTER
The most common alteration on a table is to add another column to it. In this case we alter table EMP_TAB
by adding new column DOJ.
Only one column can be added through an ALTER TABLE statement. (Two columns can be added by using
two ALTER statements)
DROP
The drop statement is simple in format, as the user does not need to supply any descriptive informatiob to
DB2 about the object being dropped.
The DROP process has a powerful cascade effect, which makes it very easy to clear out large amounts of
data. All the indexes and views defined on the table will be dropped with the table. Because an index or view
is meaningless without a table.
DB2 Guide Page 10 of 28
Statements insert, update, delete, or select data from the database objects
Using DML we can add or modify data in an existing table using the statements INSERT, UPDATE, and
DELETE:
INSERT
Insert statement is used to insert rows into db2 table.
E.g. INSERT INTO EMP_TAB (EMPID, DEPT, NAME, DESIG) VALUES (030,’C01’,’NAVEEN’,’SSE’)
UPDATE
Update statement is used to update existing row in a DB2 table.
For e.g.
SELECT clause
The select clause specifies the columns of the final result. The SELECT statement is used to select data
from a table. The tabular result is stored in a result table (called the result-set).
The SELECT statement is used to select data from a table. The tabular result is stored in a result table
(called the result-set).
Note: SQL statements are not case sensitive. SELECT is the same as select.
E.g.
To select the content of columns named "LastName" and "FirstName", from the database table called
"Persons", use a SELECT statement like this:
The result
LastName FirstName
Rao Vivek
Pathi Narender
Satya Kalyan
To select all columns from the "Persons" table, use a * symbol instead of column names, like this:
THE RESULT SET: The result from a SQL query is stored in a result-set.
With SQL, all we need to do is to add a DISTINCT keyword to the SELECT statement:
Syntax
SELECT DISTINCT column_name(s)
FROM table_name
To select ALL values from the column named "Company" we use a SELECT statement like this:
SELECT Company FROM Orders
"Orders" table
Company OrderNumber
Sega 3412
Lemuria 2312
Pepsi 4678
Lemuria 6798
Result
Company
Sega
Lemuria
Pepsi
Lemuria
DB2 Guide Page 12 of 28
To select only DIFFERENT values from the column named "Company" we use a SELECT DISTINCT
statement like this:
Result:
Company
Sega
Lemuria
Pepsi
AND & OR
The AND operator displays a row if ALL conditions listed are true. The OR operator displays a row if ANY of
the conditions listed are true.
To display the persons with LastName equal to "Hansen" or "Pettersen", use the following SQL:
Result:
GROUP BY
GROUP BY... was added to SQL because aggregate functions (like SUM) return the aggregate of all column
values every time they are called, and without the GROUP BY function it was impossible to find the sum for
each individual group of column values.
Syntax
DB2 Guide Page 13 of 28
Company Amount
W3Schools 5500
IBM 4500
W3Schools 7100
Company SUM(Amount)
W3Schools 17100
IBM 17100
W3Schools 17100
The above code is invalid because the column returned is not part of an aggregate. A GROUP BY clause will
solve this problem:
Company SUM(Amount)
W3Schools 12600
IBM 4500
HAVING
HAVING... was added to SQL because the WHERE keyword could not be used against aggregate functions
(like SUM), and without HAVING... it would be impossible to test for result conditions.
Syntax
Company Amount
W3Schools 5500
IBM 4500
W3Schools 7100
DB2 Guide Page 14 of 28
This SQL:
Company SUM(Amount)
W3Schools
UNION
UNION combine two sets of rows into single set, but the two relations (tables) must be union compatible.
e.g.
NORMALIZATION
Both are alternate names for a table. Following are the differences between them.
Dropping a table or view has no effect on its aliases. But dropping a table or view does drop its
synonyms.
An alias is a qualified name that can be used by any authorization ID. A synonym is an unqualified
name that can only be used by the authorization ID that created it.
JOIN
Sometimes the information that you want to see is not in a single table. To form a row of the result table, you
might want to retrieve some column values from one table and some column values from another table. You
can use a SELECT statement to retrieve and join column values from two or more tables into a single row.
Inner join
Left outer join
Right outer join
Full outer join.
DB2 Guide Page 15 of 28
Inner Join:
Extracts matching as well as unmatching rows from left and right table
e.g.
VIEW
View is a virtual table (i.e., it is not present physically) which is defined from other tables.
If a column of the view is derived from an expression involing a scalar operator or a scalar function.
Column of the view is derived from aggregate function.
Group by or Having at outermost level
Distinct
View involving subquery on same table
DB2 Guide Page 17 of 28
EMBEDDED SQL
Static SQL
Dynamic SQL
In dynamic SQL the statements are constructed, prepared and executed at runtime
Static SQL statements are generally preferable to dynamic SQL statements (prepared by DB2 at execution
time) because they typically are more efficient (since in dynamic sql binding is done at execution time).
The first step in DB2 program preparation is writing a program that contains embedded SQL statements.
These programs are called as embedded SQL programs. Before passing this program to COBOL compiler
we need to take out SQL statements because COBOL compilers cannot recognize SQL, this is done by DB2
precompiler.
DB2 Guide Page 18 of 28
1. A modified program source module. The precompiler comments out each of the program's embedded
SQL statements, and inserts a call to DB2 for each statement.
2. A database request module (DBRM). A DBRM contains the SQL statements found in the program
source.
The precompiler places a unique identifier, called a consistency token(timestamp), into each of these
outputs. These consistency tokens are better understood when we get to program execution.
Following the precompile process, you compile and link-edit the modified source program into an executable
load module and bind the associated DBRM. In the DB2 for OS/390 bind process,
Functions of Bind
Syntax Checking
Qualifying object names
Access path selection (optimization),
Access authorization
Database object validation
The output of the bind process is a control structure that DB2 will use to execute the SQL statements when
the application program is run. The control structure will either be part of a plan (if the DBRM is bound
directly into a plan) or contained within a package that will be executed via a plan.
DB2 Guide Page 19 of 28
The DB2 package bind process has been around for quite some time (since DB2 version 2 release 3), and it
offers some important advantages over plan-direct binds.
• Improved availability. If you change a SQL statement in a program, you only have to rebind one
package. You can rebind one package quickly, and it's important to do so: A package cannot be
executed while it's being rebound. If, on the other hand, programs are bound directly into plans, a
change of one SQL statement requires that the plan be rebound. If you've bound a large number
of DBRMs into the plan, the rebind could take a fair amount of time, during which the plan cannot
be executed.
If you use the package bind process, you have to bind the package into what is called a collection. How do
you create a collection? Pretty simple: You bind a package into it.
MEMBER(dbrm name)
e.g.,
BIND PACKAGE(COL1) –
MEMBER(PGM1)
Package contains machine code of best access path. Package is not executable independently; package
can be executed via plan.
Collection is physical collection of packages, both packages and collections are stored in DB2 directory.
DB2 Guide Page 20 of 28
List of packages will be input to BIND PLAN process the main function of BIND PLAN process is to create
application plan from packages.
APPLICATION PLAN or PLAN: it is set of pointers to packages (like index pointing to data) or we can say
plan is logical collection of packages. Plan is executable.
PKLIST(collection_name.package_name,….)
e.g.,
BIND PLAN(PLAN1) -
PKLIST(COL1.PGM1,COL2.*)
Now we have two executable components LOAD MODULE from modified source and APPLICATION PLAN
from DBRM.
A Runtime Supervisor oversees SQL application programs during execution. When such a program requests
some database operation, control goes first to the Runtime Supervisor according to the CALLs inserted by
the pre-compiler. The Runtime Supervisor then routes control to the application plan and the application plan
in turn, invokes a Data Manager to perform the required function. A Data Manager manages the actual
database, storing and retrieving records as requested by application plans
When controls go to Application plan first it will search for corresponding program name when it finds that
(package) it will search for corresponding access path using consistency token (nothing but timestamp
inserted by Pre-compiler. When it finds that it performs the required action using Data manager and buffer
manager.
Say that if the corresponding package is not found in the plan, DB2 throws Sqlcode -805.
-818 -> Timestamp mismatch in load module and application plan. Means corresponding program is found in
plan but in that package there is no access path matching with the timestamp of load module.
Check the PKLIST in BIND PLAN; it may not be pointing to required package.
How to code an embedded SQL program ‘or’ Basics of coding SQL in an application program
Following are the steps which should be followed while coding an embedded SQL program.
• Choose a method for communicating with DB2. You can use one of the following methods:
o Static SQL
o Embedded dynamic SQL
Use EXEC SQL and END-EXEC. to delimit an SQL statement in a COBOL program:
EXEC SQL
an SQL statement
END-EXEC.
Before your program issues SQL statements that select, insert, update, or delete data, you should declare
the tables and views that your program accesses. To do this, include an SQL DECLARE statement in your
program.
You do not need to declare tables or views, but doing so offers advantages. One advantage is
documentation. For example, the DECLARE statement specifies the structure of the table or view you are
working 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. Another advantage is that the DB2 precompiler uses your
declarations to make sure that you have used correct column names and data types in your SQL statements.
The DB2 precompiler issues a warning message when the column names and data types do not correspond
to the SQL DECLARE statements in your program.
One way to declare a table or view is to code a DECLARE statement in the WORKING-STORAGE SECTION
or LINKAGE SECTION within the DATA DIVISION of your COBOL program. Specify the name of the table
DB2 Guide Page 22 of 28
and list each column and its data type. When you declare a table or view, you specify DECLARE table-name
TABLE regardless of whether the table-name refers to a table or a view.
For example, the DECLARE TABLE statement for the SSS033.DEPT table looks like the following
DECLARE statement in COBOL:
As an alternative to coding the DECLARE statement yourself, you can use DCLGEN, the declarations
generator that is supplied with DB2. (DCLGEN can be created by option 2 in DB2 Interactive menu).
In addition to these basic requirements, you should also consider the following special topics:
DB2 Guide Page 23 of 28
• Cursors — to use a cursor in your application program to select a set of rows and then process the
set either one row at a time or one row set at a time.
• DCLGEN —how to use DB2's declarations generator, DCLGEN, to obtain accurate SQL DECLARE
statements for tables and views.
An SQLCA is a collection of variables that is updated at the end of the execution of every SQL statement. A
program that contains executable SQL statements (except for DECLARE, INCLUDE, and WHENEVER)
The SQL INCLUDE statement can be used to provide the declaration of the SQLCA
EXEC SQL
INCLUDE SQLCA
END-EXEC
Simple COBOL DB2 program which reads from a file and inserts into a table
IDENTIFICATION DIVISION.
PROGRAM-ID. INSPGM.
******************************************************************
* *
* TITLE INVALID GROUP ACCOUNT REPORT *
* *
* DESCRIPTION THIS PROGRAM WILL READ INFORMATION FROM EMPLOYEE *
* FILE AND INSERTS THE SAME DATA IN EDCL AND PDCL *
* TABLES
* *
* INPUT EMP – EMPLOYEE FILE THAT HAVE EMPLOYEE INFORMATION*
* *
* OUTPUT N/A *
* *
* DB2 OBJECTS SSS035.EDCL – GENERAL EMPLOYEE INFO *
* SSS035.PDCL – PAY INFORMATION OF AN EMPLOYEE *
* *
* CREATED NARENDER KUMAR PATHI AUG 2006 *
* *
******************************************************************
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT EMP-FILE ASSIGN TO EMP.
DATA DIVISION.
FILE SECTION.
FD EMP-FILE.
01 EMP-REC.
05 EMP-ID PIC 9(10).
05 EMP-NAME PIC X(20).
05 EMP-SSN PIC 9(10).
05 EMP-DOJ PIC X(10).
05 EMP-DOB PIC X(10).
05 EMP-ADDRESS PIC X(100).
05 EMP-BASIC PIC 9(8)V9(2).
DB2 Guide Page 24 of 28
******************************************************************
* DECLARATION OF TABLE SSS035.PDCL *
******************************************************************
EXEC SQL DECLARE SSS035.PDCL TABLE
( EMPID INTEGER NOT NULL,
BASIC DECIMAL(10, 2),
HRA DECIMAL(10, 2),
ITAX DECIMAL(10, 2),
GROSS DECIMAL(10, 2),
NETSAL DECIMAL(10, 2))
END-EXEC.
******************************************************************
* HOST VARIABLE DECLARATION OF TABLE SSS035.PDCL *
******************************************************************
01 DCLPDCL.
10 HV-EMPID PIC S9(9) USAGE COMP.
10 HV-BASIC PIC S9(8)V9(2) USAGE COMP-3.
10 HV-HRA PIC S9(8)V9(2) USAGE COMP-3.
10 HV-ITAX PIC S9(8)V9(2) USAGE COMP-3.
10 HV-GROSS PIC S9(8)V9(2) USAGE COMP-3.
10 HV-NETSAL PIC S9(8)V9(2) USAGE COMP-3.
01 WORK-AREAS.
05 WS-SQLCODE PIC -9(4).
05 WS-EMP-EOF PIC X VALUE 'N'.
88 EMP-EOF VALUE 'Y'.
PROCEDURE DIVISION.
0000-MAIN-PARA.
OPEN INPUT EMP-FILE.
READ EMP-FILE AT END SET EMP-EOF TO TRUE
END-READ
IF EMP-EOF
DB2 Guide Page 25 of 28
:HV-GROSS,
:HV-NETSAL)
END-EXEC
IF SQLCODE = 0
DISPLAY " RECORDS INSERTED.............."
ELSE
MOVE SQLCODE TO WS-SQLCODE
DISPLAY "ERROR OCCURRED IN PAY TABLE " WS-SQLCODE
END-IF
ELSE
MOVE SQLCODE TO WS-SQLCODE
DISPLAY "ERROR OCCURRED IN EMP TABLE " WS-SQLCODE
END-IF.
Use null indicators. Syntax ... INTO: HOSTVAR [INDICATOR] : NULLIND otherwise DB2 throws error,
SQLCODE = -305.
CURSOR
COBOL program cannot handle more than one row at a time, Cursor is used to extract more than one row
from DB2 table or View.
DECLARE
OPEN
FETCH
CLOSE
DECLARE
Options
WITH HOLD – if you don’t specify WITH HOLD cursor will be closed when you issue COMMIT DB2 closes
the cursor and you will lose the current position of the cursor. If you want to retain the position of the
CURSOR we need to specify this option.
FOR FETCH ONLY/ FOR UPDATE OF – FOR FETCH ONLY is used when you want to just read so that
DB2 will apply locks accordingly.
FOR UPDATE OF is used when you want to update any column so that DB2 can apply appropriate lock.
This option is just for better locking its nothing to do with the result produced by the cursor.
OPTIMIZE FOR n ROWS – If you inform BIND about approximate number of rows that may be retrieved for
your select expression then BIND OPTIMIZER can choose more appropriate access method which will
improve performance of the SQL statement however if you give wrong estimate it can degrade the
performance as well so you should be careful while using this option
OPEN
Before fetching from Cursor first we should open it. Query in cursor will be executed when we open it (not at
the time of declare)
EXEC SQL
OPEN cursor_name
END-EXEC
FETCH
Fetch reads a row sequentially from result set. Fetch is like a sequential read from a file.
EXEC SQL
FETCH cursor_name INTO host variables
END-EXEC
CLOSE
EXEC SQL
CLOSE cursor_name
END-EXEC
Simple COBOL DB2 program which reads from a table based on department and stores in corresponding file
BIND OPTIONS
ACTION
Indicates whether the package can be added or replaced.
ADD
Indicates that the named package does not exist, and that a new package is to be created. If the package
already exists, execution stops, and a diagnostic error message is returned.
REPLACE
DB2 Guide Page 28 of 28
Indicates that the existing package is to be replaced by a new one with the same package name and
creator. This is the default value for the ACTION option.
EXPLAIN
Stores information in the Explain tables about the access plans chosen for each SQL statement in the
package
NO
Explain information will not be captured.
YES
Explain tables will be populated with information about the chosen access plan at prep/bind time for
static statements and at run time for incremental bind statements.
ISOLATION
Determines how far a program bound to this package can be isolated from the effect of other executing
programs.
CS
Specifies Cursor Stability as the isolation level When controls mo
RR
Specifies Repeatable Read as the isolation level
UR
Specifies Uncommitted Read as the isolation level
RELEASE
Indicates whether resources are released at each COMMIT point, or when the application terminates. This
DRDA precompile/bind option is not supported by DB2 for Windows and UNIX.
COMMIT
Release resources at each COMMIT point. Used for dynamic SQL statements.
DEALLOCATE
Release resources only when the application terminates