You are on page 1of 150

DB2 Objects

There are two types of elements or objects

Data Objects
Accessed under the users direction and by which the users data is organized.

System Objects
Controlled and used by DB2

DB2 Objects
Data Objects System Objects

Tables Indexes Views Tablespaces Databases Storage Groups

DB2 Catalog DB2 Directory Active & Archive Logs Boot Strap Data Set Buffer Pools Locks

What is a Database ? Is a repository for stored data - C.J.Date What is Data ? A representation of facts or instruction in a form suitable for communication - IBM Dictionary What is a database system ? An integrated and shared repository for stored data or collection of stored operational data used by application systems of some particular enterprise. Or Nothing more than a computer-based record keeping system.

System Objects
DB2 Catalog

Consists of tables of data about every object defined in the DB2 system. When an object is created, altered or dropped, a row is inserted, updated or deleted in the Catalog.
DB2 Directory Contains information required to start DB2 & for its normal operation Cannot be accessed using SQL Directory objects are not described in DB2 catalog

Bootstrap Dataset Contains inventory of all active and archive log datasets. During installation of DB2, two BSDS datasets are created, kept in different volumes.

Active & Archive Logs DB2 records all data changes and significant events in active logs as they occur. In case of failure, it uses this data to recover the lost information. When the active log is full, DB2 copies the contents of the active log to a DASD or magnetic tape data set called archive logs. Buffer Pools Are areas of virtual storage which DB2 uses during execution of an application program or an interface SQL, to temporarily store pages of tablespace There are four different bufferpools named BPO, BP1, BP2, and BP32.

Locks
DB2 guarantees data integrity by using several locking mechanisms. These strategies permits multiple users from multiple environments to access and modify data concurrently without lose of data integrity. DB2 supports locking at three levels. Table Space Level, Table Level and Page Level. DB2 uses locks to control concurrency & prevent inconsistent data.

Data Objects
Tables All data is stored in tables. A table is a collection of rows, all having same columns Rows are records and Columns are fields. Intersection of a column & row is a Value SQL is used to access data in DB2 tables.

Indexes
An ordered set of pointers to the data in DB2 table Stored separately from the table Each index occupies its own space Used to Improve performance; Ensure Uniqueness Multiple indexes can be defined on a table A table having a primary key will necessarily have a primary unique index.

Views
Is a virtual table derived from one or more base tables The view definition is stored in a catalog. Alternate way of representing data that exists in one or more tables Can include all or some of the columns from one or more base tables Indexes on Base table can improve the performance operations on the views

Tablespaces Is the physical space used to hold one or more tables Consists of 1 to 64 VSAM LDS can contain upto 64 GB of data Three types of tablespaces are

Database A Set of DB2 objects (tables, indexes, tablespaces & views) One database for one application recommended Default Database is DSNDB04 Storage Group (STOGROUP) A set of Volumes on DASD. Volumes hold the data in which tables and indexes are actually stored. Up to 133 DASD volumes can be assigned to a single STOGROUP. DB2 allocates storage for tablespaces and indexes. When a tablespace is created, DB2 defines the necessary VSAM datasets using IDCAMS.

STOGROUP
It is a collection of direct access volumes , all of the same device type. The option is defined as a part of table space definition. When a given space needs to be extended, storage is acquired from the appropriate STOGROUP. Max. 133 volumes in a single database. Syntax: CREATE STOGROUP MAPSG VOLUMES(SMS001) VCAT DSNCAT;

DATABASE
A collection of logically related objects like Tablespaces - Indexspaces - Tables etc Not a physical kind of object may occupy more than one disk space A STOGROUP & BUFFERPOOL ( is buffer area used to maintain recently accessed table & index pages) must be defined for each database. STOGROUP and user-defined VSAM are the two storage allocation for a DB2 database definition.

In a given database, all the spaces need not have the Same STOGROUP. More than one volume can be defined in a STOGROUP. DB2 keep track of which volume was defined first & uses that volume.

Syntax:
Create Database DBMAP STOGROUP MAPSG;

TABLESPACES Logical address space on secondary storage to hold one or more tables. A SPACE is basically an extendable collection of pages with each page of size 4k or 32k bytes. It is the storage unit for recovery & reorganizing purpose.

simple Tables paces A simple tablespace can contain one or more tables. A single page of a simple tablespace can contain rows from all the tables defined to the tablespace. In general only one table should be defined per simple tablespace. One table space can be up 64 billion bytes Page size 4kb to 32 kb Each row contains 127 tuples rows Lock is at page level Here data integrity will be there but data concurrency will not be there

simple tablespace can contain more than one stored table, though one is usually the best option. It normally consists of only one VSAM ESDS and may contain one or more table. Reading the data of one table requires a sequential read of the Tablespace(tablespace scan) thus increasing I/O and degrading performance. Concurrent access to tables in the same tablespace can cause lengthy response delays

LOCKSIZE - indicates the type of locking DB2 performs for the given TS. (PAGE/TABLE/TABLESPACE) USING PCTFREE method of storage allocations Stogroup or VCAT - % of space available for future inserts.

FREEPAGE - no. of pages after which an empty page is available. BUFFERPOOL BP1,BP2, BP32K CLOSE YES/NO- whether the underlying VSAM datasets be closed each time the table is used. ERASE YES/NO - whether physical DASD where the TS reside to be written with binary zeros when the TS is dropped. NUMPARTS - For partitioned tables paces SEGSIZE For segmented tables paces

Create simple tablespace

SYNTAX CREATE TABLESPACE SSSVIJTS IN SSSVIJDB USING STOGROUP ST0R1 PRIQTY 30720 SECQTY 10240 LOCKSIZE TABLE BUFFERPOOL BP0 CLOSE NO; character: A Cobol word must be formed from following character

Simple table space

Page1

Page 2

Page 3

Page 4

Table Row 1

Table 1 Row 3

Table 1 Row 4

Table 3 Row 2

Table 1 Row 2

Table 3 Row 1

Table 2 Row 3

Table 3 Row 3

Table 2 Row 1

Table 2 Row 2

Table 2 Row 4

Segmented Tablespaces The pages within a tablespace are organized into segments and each segment holds rows from only one table. Can hold more than one table. A segmented tablespace is the most efficient type of tablespace for most of the DB2 development efforts.

The space is divided in to equal-sized groups of pages called segments, and each segment can contain rows from only one table. Each segment is a group of 4 to 64 pages. Provides improved performance, concurrency and space management for multiple tables in a single table space. Related tables can be efficiently placed together in one Table space.

Segment table space


Segmented table space

Segment 1

Segment 2

Page 1

Page 2

Page 3

Page 4

Table 1 Row 1

Table 1 Row 4

Table 2 Row 1

Table 2 Row 4

Table 1 Row 2

Table 1 Row 5

Table 2 Row 2

Table 1 Row 3

Table 1 Row 6

Table 2 Row 3

Create segment
SYNTAX CREATE TABLESPACE KSATS

IN SSSVJDB

USING STOR1

PRIQTY 52 SECQTY 20 ERASE NO SEGSIZE 32 LOCKSIZE PAGE BUFFERPOOL BP1 close YES;

Partitioned Tablespaces
The tablespace is divided into partitions, and each partition is stored in one VSAM data set. A partitioned tablespace is divided into components called partitions. Each partition resides in a separate physical dataset. DB2 permits 1 to 64 partitions per tablespace. Only one table can be defined in a partitioned tablespace

Partitioned Tablespaces

SYNTAX CREATE TABLESPACE SALESHX IN SSSVJDB USING STOR1 PRIQTY 4000 SECQTY 130 ERASE NO NUMPARTS 82 LOCKSIZE PAGE BUFFERPOOL BP1

Partitioned table space Partition 1


Page 1 Page 2 Partition 2

Page 3

Page 4

Table 1 Row 1

Table 1 Row 4

Table 1 Row

Table 1 Row 10

Table 1 Row 2

Table 1 Row 5

Table 1 Row 8

Table 1 Row 11

Table 1 Row 3

Table 1 Row 6

Table 1 Row 9

DB2 Threads
Controls Structures used by DB2 to communicate with an application program. Used to send requests to DB2, to send data from DB2 to the program, and to communicate the result of each SQL statement after it is executed.

DB2 Operating Environment


All DB2 application types can execute concurrently by sharing the same DB2 databases. A DB2 application executing under CICS is an on-line application and uses CICS facilitates to communicate messages. A CICS/DB2 application can access DB2 data, IMS data, and VSAM data. The combination of DB2 with CICS performs as a full functions DB/DC system & CICS serves as the transaction manager.

Structured Query Language (SQL)


SQL is a high level language that provides a greater degree of abstraction than procedural languages. It is fashioned so that the programmer can specify what data is needed but need not specify how to retrieve it. Used to access data in DB2 tables. Ways of executing SQL's
Foreground

 Using SQL Processor Using File Input (SPUFI)  Using Query Management Facility (QMF)
Background

 By embedding them in application programs written in C, Cobol, Fortran, Assembler, PL/1, etc.

Data Definition Language (DDL)


DDL - Data Definition Language statements to define or create the objects of a database. CREATE, ALTER, DROP

Data Manipulation Language (DML)


DML - Data Manipulation Language statements to manipulate data in tables.
SELECT, INSERT, DELETE, UPDATE

Data Control Language (DCL)


DCL - Data Control Language statements to GRANT and REVOKE authorization to user resources.

Relational concepts

Relation : A table or File Tuple : Row contains an entry for each attribute Attributes : Columns or the characteristics that define the entity Domain:. A range of values (or Pool) Entity : Some object about which we wish to store information Null : Represents an unknown/empty value Atomic Value: Smallest unit of data; the individual data value

DB2 Database Design DB2 Datatypes

Null: All datatypes include the null value. The null value is a special value that is distinct from all other values and thereby denotes the absence of a value. It is NOT the same as 0 or blanks.

Date format

Time format

DB2 Datatypes & Lengths

NOT NULL: Value must be given NULL: If Value not given it is marked unknown NOT NULL: If value not given DB2 fill default value WITH DEFAULT: Numeric Becomes Zero, Character Becomes Blank, Date becomes Current

Date, Time becomes Current Time, Timestamp becomes Current Timestamp. If Nulls are allowed, internal storage length is increased by 1 byte

Primary & Foreign Key Concepts


Key identifies a row of data Keys can be composed of more than one column (composite keys) Two Types of keys Primary Key Identifies its own row Must be unique Foreign Key Identifies a row of related data Need not be unique Tables are related only by primary & foreign keys Table with the primary key is called parent table Table with the foreign key is called a child table or a dependent table.

Referential Integrity Rules


Referential Integrity or Relation Integrity is another elementary data integrity rule of the relational model. Referential Integrity defines the relationships among different columns and tables in a relational database. It is called referential integrity because the values in one column or set of columns refer to or must match the values in a related column or set of columns.

A mechanism that ensures data integrity between tables related by Primary & Foreign Keys

Insert Rules

When inserting a row with the foreign key, DB2 checks the values of the foreign key columns against the value of the primary key columns in the parent table. A new Primary Key can be inserted as long as it is unique.
Update Rules When updating foreign key values DB2 performs same checks as when it is inserting a row with a foreign key. Delete Rules

Cascade: When a row of the parent table is deleted, any related rows in the dependent table are also deleted. Restrict: Rows of parent table that have dependent rows cannot be deleted Set Null: When a row of a parent is deleted, the corresponding values of the foreign key in any dependent rows are set to null.

DDL, DML, DCL Commands DDL Statements


Creating DB2 Objects CREATE Statement is used to create all DB2 data objects. General format

CREATE Object, Object Name Option - List Object STOGROUP | DATABASE | TABLESPACE | TABLE | VIEW | INDEX

Object Name: name of the DB2 object

Option - list: Options for the object to be defined

CREATE TABLE This statement is used to create objects SYNTAX : For Creating a Table CREATE TABLE <tabname> (Col Definitions) PRIMARY KEY(Columns) / FOREIGN KEY UNIQUE (Colname) (referential constraint) CHECK (colname relational operator value) (check constraint) [IN Database.Tablespace Name ]

Create Tables
CREATE TABLE EMPTAB (EMPNO INTEGER(5) NOT NULL NOT NULL NOT NULL CHAR(25) DECIMAL(7,2) DATE NOT NULL

EMPNAME CHAR(25) DESIG SALARY DOB

IN GSLDB.GSLTS

Create Index
CREATE UNIQUE INDEX index name ON table name (FLD 1 ASC/DESC, FLD 2 ASC/DESC) Example: CREATE UNIQUE INDEX EMPIDX ON EMPTAB (EMPNO ASC);

Create View
CREATE VIEW view name AS SELECT FLD 1, FLD 2 .. From table-name WHERE condition (WITH CHECK OPTION)

Example: CREATE VIEW EMPV AS SELECT EMPNO, EMPNAME FROM EMPTAB WHERE dept name = TRG

Alter Table

to Add new column to Add Primary Key to DROP Primary Key & Foreign Key to Add Referential Constraints

Example:

ALTER TABLE EMPTAB ADD ADDRESS CHAR(30);

Notes:

Adding a primary key in a table puts the table in an incomplete state until a unique index is created on that particular column. Dropping a primary key on a parent table drops all references to dependent tables.

DB2 does not allow deletion of a column from a table. DB2 does not allow the characteristics of the column to be changed.

DCL Statements
Provides effective control over the resources at required levels, either at the row level or at the column level. Allows centralization / decentralization of control at required levels. DB2 users may be allowed to control the use of resources, without permission/assistance of Database Administrators. Control over DB2 objects and data is achieved through GRANT and REVOKE statements.

Revoke Revokes privileges from authids General format REVOKE authorization ON object object name FROM authid / PUBLIC

Example: REVOKE SELECT ON TABLE EMPTAB FROM TRGE010;

Grant
Grants privileges to Authorization Ids. General format GRANT authorization ON object object name to authid / PUBLIC [WITH GRANT OPTION ] Can be executed from an application program or can be executed interactively using SPUFI or QMF Authorization for table can be SELECT, INSERT, UPDATE, DELETE, ALTER, INDEX or ALL For update, authorization can be given on specific columns also. WITH GRANT OPTION allows the grantee to grant privileges to others. Example: GRANT SELECT ON TABLE EMPTAB TO TRG010;

DML Statements Insert


To insert values into the fields of a table There are 2 types of INSERT statements. INSERT statement with VALUES clause is used to insert a single row. INSERT with SELECT statement is used to insert one or more rows. General Format: INSERT INTO tablename (col1) VALUES (val1); INSERT INTO tablename (col1) sub-select; Example: INSERT INTO TABLE1 VALUES (005, RAM, MKR, SALES); EMPNO 005 Name RAM MKR GRPID SALES DESIG

Example: INSERT INTO TABLE1 SELECT* FROM TABLE2;

Update
Used to change any value in a table Example: UPDATE TABLE1 SET GRPID=MKR WHERE EMPNO=005;

So the record changes to:

EMPNO 005

NAME RAM FIN

GRPID PRG

DESIG

Changed from MKR to FIN

If WHERE clause is not used, all records are changed to FIN GRPID

Select

SELECT statement is used to retrieve all or part of the records in a table.

WHERE clause is used to specify the search criteria. If no criteria is specified, all the records are retrieved. Example: Table1

EMPNO 001 002 003

NAME JOSEPH SAM RAJA MKR FIN

GRPID SMG GM MGR

DESIG

SALES

Selecting all Columns

SELECT* FROM Table1 * to specify all columns EMPNO NAME 001 002 003 JOSEPH SAM RAJA GRPID MKR FIN SALES SMG GM MGR DESIG

The same answer is got even if we give SELECT EMPNO, NAME, GRPID, DESIG from TABLE1.

Selecting few Columns only

Conditional Select
WHERE clause is used to select data from only the rows that meet certain conditions. A WHERE clause specifies a search condition that, when evaluated against a row, yields TRUE or FALSE or UNKNOWN.

Selecting unique rows

In: To select from a list of values

Between: To select a range of values

Like: To select based on a subset of characters

Not: Negate a condition

Any
Is same as IN when given as =ANY

Joins
Joins are used to select data from multiple tables Example: Table2

We are joining table 1 and table 2 to get the information of manager(s) only.

Cartesian Product
Cartesian product is the result of a join that does not specify matching columns.

Subquery
Used to nest SELECT statements When one or more select statements are nested, it is referred to as sub query

To get the information for all the managers

To get the information about the 'FIN' group's manager

Union
UNION combines two sets of rows into a single set composed of all the rows in either or both of the two original sets. The two original sets must be union-compatible (i.e.)

The two sets must contain the same number of columns. Each column of the first set must be either the same data type as the corresponding column of the second set or convertible to the data type of the corresponding column of the second set.

Outer Join
When we want both matching & non - matching rows returned for one or more of the tables being joined, we use Outer Join.

Sorting & Grouping


The ORDER BY clause sorts result of a query in the specified order (ascending or descending) for each column. The GROUP BY clause sorts the resultant rows to apply functions that consolidate the data. By grouping data, users can use statistical functions on a column [Example: AVG, SUM, MIN, MAX] It does not do any sorting. For sorting, ORDER BY clause should be used. After having grouped using the GROUP BY clause to eliminate non - pertinent data we use Having clause. The HAVING clause is to specify conditions for the GROUP BY clause.

Note: Default Order is Ascending.

Note: If there are 50 MKR, 50 FIN & 50 SALES records, they will be grouped together and displayed. Table 3:

Aggregation& Column Functions


Column functions compute from a group of rows and return a single value for a designated column or expression. Example: AVG, COUNT, MAX, MIN, SUM

AVG
To find the Average of a column specified or expression given as the argument

Count
To count the number of rows, use distinct keyword to count only the number of distinct values for a given column.

Max
Max function returns the largest value in the specified column or expression.

Min
Min function returns the smallest value in the specified column or expression.

Sum This returns the accumulated total of the specified column or expression.

Delete To delete a record or set of records or all records General Format: DELETE FROM table name WHERE search-condition; If WHERE clause is not used, all records are deleted.

If WHERE clause is not used, all records are deleted.

DB2 & TSO


TSO or Timesharing Option, is one of the environments from which DB2 data can be accessed. TSO enables the users to interact with MVS using an on-line interface that is either screen or panel driven. The Interactive System Productivity Facility (ISPF) provides the mechanism for communicating by panels, which is the common method for interaction between TSO and application users.

Jcl to submit embedded sql


000100 //MA24A JOB ,,NOTIFY=MAPLE24,MSGLEVEL=(1,1) 000200 // JCLLIB ORDER=MAPLE24.DB2.PDS 000300 //STEP01 EXEC PROC=DSNHCOB,WSPC=500,MEM=SIMPLE user=userid To identify the library 000400 //PC.SYSIN DD DISP=SHR,DSN=MAE24.DB2.PDS(&MEM) path for the source code location 000410 //PC.SYSLIB DD DISP=SHR,DSN=MAE24.DB2.PDS path for the DCLGEN 000420 //PC.DBRMLIB DD DISP=SHR,DSN=MAPLE24.DBRM.PDS(&MEM) path for the DBRMLIB 000500 //LKED.SYSLMOD DD DISP=SHR,DSN=MAPLE24.RUNLIB.LOAD(&MEM) Path for the load module(generation) 000600 /* 000700 //BIND EXEC PGM=IKJEFT01,DYNAMNBR=20,COND=(4,LT) DB2 utility 000710 //STEPLIB DD DISP=SHR,DSN=DSN710.SDSNLOAD 000900 //SYSTSPRT DD SYSOUT=(*) 001000 //SYSTSIN DD * 001100 DSN SYSTEM(DSN2) To connect to TSO 001200 BIND PLAN(MAPLE24P)plan to be created with in userid 001300 MEMBER(SIMPLE)Application member name 001400 ISOLATION(CS) cursor stability locks amount of page where Db2 resides 001500 RELEASE(C) Close the cursor 001600 EXPLAIN(NO)Find the optimum path 001700 OWNER(MAPLE24)userid 001800 LIB('MAPLE24.DBRM.PDS') 001900 RUN PROGRAM(SIMPLE) PLAN(MAPLE24P)002000 LIB('MAPLE24.RUNLIB.LOAD') 002100 /* 002110 // 002200 //SYSIN DD * 002301 /* 002310 // 002340 //

SPUFI
SPUFI (SQL Processor Using File Input) reads the SQL statements contained as a text in a sequential file, processes those statements and places the output in the output dataset specified in the browse mode. The screen is self explanatory and the only inputs the user has to give is the input file name where the SQL statements are placed and the output file name where the results have to be placed. SPUFI supports the online execution of SQL statements from a TSO terminal.

DCLGEN Command
The DCLGEN or Declaration Generator Command is used to produce a COBOL copybook, which contains a SQL DECLARE TABLE statement along with the WORKING-STORAGE hostvariable definitions for each column of the table. When the DCLGEN command is issued DB2 reads catalog to determine the structure of the table and builds the COBOL copybook.

Preparation Steps

SOURCE

NON SQL

DB2 PRECOMPILE

SQL

=TIMESTAMP DB2 BIND

COMPILE & LNKEDIT

CATALOG

LOAD MOD

PLAN

Program Execution

EXEC

LOAD MOD

CALL

PLAN

OTHER DATA

DL/I or VSAM

DB2

DB2 TABLES

OTHER DATA

DL/I or VSAM

DB2

DB2 TABLES

DB2 Precompile Comments


Separates SQL from Non-SQL (Database Request Module) Checks for errors Does not access DB2 catalog Can add code from Libraries Timestamps both outputs

BIND Comments
Checks for errors, using DB2 catalog Checks authorisation Develops Access strategy for each statement Stores strategies as plan (reusable without repeating bind)

Pre-compilation
Pre-compiler Functions:
Program used: DSNHPC INPUT EMBEDDED SQL PROGRAM OUTPUT Modified source DBRMLIB Database Request Module Other Sources needed: DCLGEN: This produces modules that can be included in a program. It builds a COBOL copybook. Expands the DB2 related INCLUDE statements. Checks the SQL syntax. Separates SQL from NON-SQL statements and places SQL statements in Database Request Module (DBRM) Places TIMESTAMP in the Modified source in the DBRM member and load module.00

Compile
Program Used: IGYCRCTL INPUT Modified Source Program; OUTPUT OBJ Module Other Sources needed: COPYLIBS Process: The COPYLIB members will be expanded. The COBOL syntax will be checked. Places time stamp written in precompiler step in OBJ module.

Link Edit
Program Used: IEWL INPUT OBJ Module; OUTPUT Load Module Process: The executable load module is produced Subprogram exists will be merged along with system libraries Places the timestamp written from the precompiler step in the load module.

Bind
Program Used: IKJEFT01 INPUT DBRM LIB produced in precompiler step OUTPUT PLAN Function: Reads the SQL statements from DBRMs and produces a mechanism to access data from tables. Parameters given while binding: 1. 2. 3. 4. 5. 6. 7. 8. Owner, Qualifier Action (REPLACE/ADD) Isolation (CS/RR) Acquire (USE/DEALLOCATE) Release (COMMIT/DEALLOCATE) Validate (BIND/RN) Explain Flag.

Owner: Name of the owner who created the PLAN Qualifier: Name where the table reside Action: Add or Replace the existing plan (BINDADD & BINDREPLACE) Isolation: This determines the mode of page locking implemented by the programs as it runs.

CS Cursor Stability: Releases the page locks as soon as another page is accessed or an COMMIT is issued. Improves concurrency. RR Repeatable Read (DEFAULT): Does not releases the page locks until a COMMIT is issued.

Acquire: This determines the mode of table space locking implemented by the program as it runs.

Use: Table space locks are acquired when the table space is accessed.

Allocate: Table space locks are acquired when the plan is allocated.

Release: Commit: Table space locks are released at COMMIT or ROLLBACK time. Deallocate: Table space locks are released only when the plan finishes and is deallocated.

Validate: Method of checking for the existence and validity for DB2 access tables and authorization Bind Validates at Bind time; Run Validates at run time.

Flag: Returns informational, warning, error and completion messages.

DB2 Application Programming Concepts

Data Base Request Module (DBRM)


DBRM

is a module containing SQL statements from the source program, which

have been extracted by the DB2 Pre-compiler.

The module is stored as a member of a partitioned data set.

The DBRM is used as input to the BIND process

Bind
Bind reads SQL statements from DBRM s and produces a method to access data as directed by the SQL statements being bound. It stores this access method as Plan Two types of Binds - BIND PLAN, BIND PACKAGE BIND PACKAGE accepts a DBRM as input and produces a SINGLE package. A package is not executable. Packages should be bound into a plan before executing. Output of BIND PLAN is the application plan containing the executable logic giving the access path to DB2 data. BIND also performs Syntax checking of SQL statements, Checking of the DB2 table and column catalog Information, Authorization Validation.

Compile & Link Edit


Modified source program produced by the DB2 pre-compiler is the input for COBOL compiler. Compiled source code is given as input to Linkage editor. Linkage editor produces the executable load module. Appropriate DB2 host language interface module must be included in the link-edit step.

Running a DB2 Program


Program preparation steps will produce two separate physical components- DB2 Plan, Link-Edited Load Module. To run an Application program containing SQL statements, the name of the Plan should be specified. At execution time, DB2 checks for the compatibility of the Plan and the load module by comparing the TIMESTAMP. DB2 programs can be executed in Batch / TSO, Using Call Attach Facility, CICS

Embedded SQL
SQL statement are embedded anywhere in the application program. These are analyzed by DB2 precompiler & replaced by equivalent statements in host language In addition to data definition/manipulation statements, SQL statements unique to application program coding are provided. These include support for Declaration tables, Include statements, SQL Communications Area, usage of Host Variables within SQL statements & Cursor tables. DB2 application programs may be coded in COBOL, FORTRAN, PL/I and ASSEMBLY language. Each statement is delimited i.e. each SQL is enclosed in an EXEC SQL, END-EXEC block Data Values can be gathered from host program variables Multiple rows of table can be accessed through a CURSOR DB2 describes results of execution in SQLCA End Delimiter varies by Host Language. For COBOL, it is END-EXEC.

SQL/COBOL Program Components


Working Storage:

Host program variables are defined here to hold data. These variables are referred in the SQL statements in the Procedure Division. Procedure Division:

SQL statements to access and manipulate data are coded here. statements reference DB2 tables and host program variables. SQL statements begin with EXEC SQL and end with END-EXEC.

These

SQL Communication Area:


SQLCODE Field tells result of last executed SQL request Examples: 000 Successful +100 No more rows found - 530 New Foreign key value is invalid SQLCODE field should checked after each SQL statement If SQLCODE is less than 0, an SQL error was encountered.

Including TABLE DECLARATION


SQL provides a facility to copy source code from a library into application program, by coding INCLUDE statement. The INCLUDE statement directs DB2 pre compiler to copy source code into the program. This function is very similar to COPY statement in COBOL. INCLUDE statement is normally coded to copy DCLGEN outputs into the programs working-storage section. It can also be used to copy SQL or COBOL statements in the Procedure Division. However, the include code must not contain Include statements within it. I.e. nested INCLUDE. The general statement format is EXEC SQL INCLUDE source code name END-EXEC

Host Variables, Null Indicator Variable


Host variable is an area of storage allocated by the host language and referenced in an SQL statement. Host variables are defined and named using Host Language Syntax For COBOL, host variables are defined in Data Division or Linkage Section A : should precede the host variables used in embedded SQL statements Host variables can be used as: A host variable cannot be used for table names, column names, SQL verbs etc. A host structure is a COBOL Group level data area consisting host variables for all columns returned by SELECT statement. OCCURS clause cannot be used while declaring such structures.

Notes: For host variables not within a host structure, use COBOL level numbers 01 or 77. Variable character fields are defined as a 02 group level specifier, and two 49 level specifiers. One is the length specifier, and other is the actual data field. While inserting data into a variable character field, the actual number of characters instead is indicated by the length specifier. While retrieving data from a variable character field, the actual number of characters returned in the data field is indicated by the length specifier.

In the above example DCLDEPT is a Host Structure containing corresponding Host variables for DEPTNO, DEPTNAME & LOCATION

Host Structure
A series of variables referenced as a unit is called a structure. In a COBOL 01 (group) level data structure, representing all column selected in SQL statement. The 01 (group) level data name is prefixed by a colon Is coded in a SQL statement in place of elementary level fields. Can have a maximum of two levels. When elementary fields have duplicate data names, they must be qualified with the relevant structure name. The general coding format is: Structure Name.Field Name

Note: For data variable within a host structure use COBOL level numbers 02 through 48. Coding FILLER within a host structure makes the structure invalid.

Null Indicator Variables


Indicator variable is a 2 Byte integer S9 (4) Comp. Declared similar to Host variables. Negative value for Indicator variable means the associated column value has a NULL value. If a column is truncated, length of the field is returned in the Indicator variable. Can be declared as an array of Indicator variables. OCCURS clause can be used. To associate Null Indicator variable with a particular host variable for a column, code the indicator variable immediately after the Host variable, preceded by a colon. Can be used along with Host variables in Set Clause of UPDATE Statement VALUES Clause of INSERT Statement INTO Clause of SELECT or FETCH Statement

SQLCODE
Regardless of whether the application program provides an SQLCA or a stand-alone variable, SQLCODE is set by DB2 after each SQL statement is executed. DB2 conforms to the ISO/ANSI SQL standard as follows: If SQLCODE = 0, execution was successful. If SQLCODE > 0, execution was successful with a warning. If SQLCODE < 0, execution was not successful. SQLCODE = 100, "no data" was found. For example, a FETCH statement returned no data because the cursor was positioned after the last row of the result table.

Using Select - Into Clause


Only one row from the table is returned Example:

MODIFIED SOURCE MODULEb

SOURCE MODULE

DBRM

COMPILER COMPILER

PRECOMPILER PRECOMPILER

BIND BIND

OBJECT MODULE EDITOR EDITOR OTHER OTHER OBJECT OBJECT LIST OF LIST OF PACKAGE PACKAGE

PACKAGE BIND BIND

APPLICATION PLAN LOAD MODULE DB

Cursor Processing
In the Embedded SQL application program development, if select returns more than one row of information then we have to use cursors, since the program cannot process all the rows at once. It can process only one row at a time. Following operations can be performed on a CURSOR DECLARE OPEN FETCH CLOSE Cursor can return zero, one or many rows depending on the Fetch condition.

Static Embedded SQL


used when you know all the possible SQL statements that might be written

Dynamic Embedded SQL used when the SQL statements will be defined at run-time (ie. they are not known in advance)

STATIC

DYNAMIC

query plan and optimization occur during development (therefore, query execution will be faster) need to know all SQL statements in advance

query plan and optimization occur when the query is issued (query execution could be slow) allows for any SQL statement to be issued

database objects do not have database objects must exist to exist during app at BIND time development

might use old database statistics

queries use current database statistics

select name, ?, ? from ? where name = jones

Requires dynamic SQL

select name, age from employee where name = ?

Can use static SQL

The syntax of the Declare cursor statement


Exec Sql Declare cursor-name Cursor [with hold] for Select [Distinct] col-spe[As result-col],[col-spe[As res-col] from table-name [ Where selection-cond] {[for update of update-col[,update-col]..] [Order by sort-col [Asc/Des]]..} End-Exec.

__________________________________________ _

With hold Prevents the cursor from being closed as a result of a commit operation Update-col The name of the column for which you want to enable updates

DECLARE Cursor syntax:


EXEC SQL DECLARE cursor FOR union-expression [ ORDER BY columns) ] FOR [ FETCH ONLY | UPDATE OF columns) ] ] END-EXEC.

Declare Cursor
To define and identify a set of rows to be accessed. For updating / deleting a row, FOR UPDATE OF option should be specified. Cursor name cannot be a Hyphenated word Example of Cursor Declaration

WITH HOLD clause can be used with DECLARE, to retain the cursor position beyond the commit point.

Sql Fetch statement Exec Sql Fetch cursor-name INTO {:host-var [, :host-var]| :host-structure} End-Exec.

________________________________________________________________ For eg: Exec Sql Fetch custcur INTO :fname, :lname End-Exec.

Open Cursor
Activates the cursor Rows are retrieved and stored in the result table Pointer is positioned before the first record Example

Fetch Cursor
Moves the contents of the selected row into Program Host Variables Points to the next row Cursor must be opened explicitly before it can be fetched. Example

Rows are sorted by invoice in descending sequence Declare Invcur cursor for select invcust, invno ,invtotal form invtable where invcust = :custno order by invtotal des ____________________________________________ __ Declare custcur cursor for select fname, lname from custtable where state = ca order by 2 ____________________________________________ __ Declare invcur cursor for select invno, invdate, invtot+invtax As Total from invtable order by total

SYNTAX FOR UPDATE statement _________________________________ Exec Sql


Update {table-name | view-name} Set col-name = {:host-var| literal|expression|null} [,col-name = {:host-var| literal|expression|null}] [where {search-cond [with isolation-level]| current of cursor-name}] End-Exec.

EXAMPLE EXEC SQL UPDATE table SET column = scalar-expression [, column = scalar-expression ] .. WHERE CURRENT OF cursor END-EXEC.

The keyword NULL puts a null value in a column, but this cant be used for a column thats defined as NOT NULL. If a Check Constraint has been defined for a column, the value that the column is set to must satisfy the constraint. If the value in the sqlcode field isnt zero it means that the update failed so on changes were made to the table. Sqlerrd(3). The CURRENT OF phrase is used to update the4 current row in a cursor controlled table.

Syntax for DELETE Statement


Exec Sql Delete from {Table-name | view-name} [ Where {search-con [with isolation-level ] | Current of cursor-name}] End-Exec. _____________________________________________ For eg: Exec Sql Delete from invtable where custno = 30000 End-Exec.

Exec Sql Declare custcur cursor with hold for select custno, addr, city, state from invtable where invcust < 3330 for update of custno, addr, city, state End-Exec. __________________________________________________ Changes the row at the current cursor position. Exec Sql update invtable set custno = :custno, addr = :addr, city = :city, state = :state Where current of custcur End-Exec.

syntax for Insert with Values Exec sql Insert into (table-name | view-name) [ col-name [, col-name])] values({ :host-var | literal | null}..) End-Exec. ______________________________________________ Insert into customer values( :cust-no, :custname) If a check constraint has been defined a column the value thats supplied for the column must satisfy the constraint. Sqlcode value of -803 means that the statement failed the row to be inserted contained a duplicate value in a column that is defined unique

Insert with a sub query Exec sql Insert into (table-name | view-name) [ col-name [, col-name])] select-statement [with isolation-level] End-Exec. _________________________________________ Exec sql Insert into oldcust-table select * from customer where custno < 30000 End-Exec.

Updating/Deleting of Row Fetched


All columns to be updated should be included in the FOR UPDATE OF Clause in DECLARE CURSOR Statement. WHERE CURRENT OF Clause is used in UPDATE and DELETE statements to modify the current row. UPDATE and DELETE statement must be preceded by a FETCH statement. Examples:

Close Cursor
CLOSES the Cursor Releases the locks held by the cursor DB2 automatically closes cursor when program terminates COMMIT statement also closes all the cursors

-117

The number of insert values is not the same as the number of object columns -304 The value inserted is out of range -305 The null value cannot be assigned to output host variable number because no indicator variable is specified -805 Program not found in the PLAN -503 A column cannot be updated because it is not identified in the update clause of the select statement of the cursor

-803

Inserted or updated value invalid because index index-name constrains columns of table such that no 2 rows can contain duplicate values in those columns. Rid of existing row is Xrid Result of an Embedded Select Statement Is a Table of More Than One Row, or the Result of the Sub query of a Basic Predicate Is More Than One Value The Insert, Update, Delete, DDL or Authorization Statement Cannot Be Executed Because the IMS/VS Transaction Is Inquiry Only The precompiler-generated timestamp x in the load module is different from the bind timestamp y built from the DBRM

-811

-817

-818

Error Handling
WHENEVER statement used to enable automatic testing of return codes for subsequent SQL statements Affects all subsequent SQL statements until the next WHENEVER statement General Format

Advanced SQL Topics Dynamic SQL


DB2 supports two types of SQL statements in an application program - Static SQL - Dynamic SQL

Static SQL are SQL statements embedded in a host language and bound before the program runs.

Dynamic SQL are SQL statements that are prepared and executed, while the program is running.

Statements that are allowed as dynamic sql statement _____________________________________________ Statements u may be likely to use DELETE,INSERT,SELECT,UPDATE. Statements are not likely to use ALTER,COMMERNT, COMMIT, CREATE, DROP, EXPLAIN, GRANT, LABEL, LOCK TABLE, REVOKE, ROLLBACK. Cursor Processing Open, fetch, close. Precompiler directives Declare Include Whenever. Dynamic sql Execute Immediate Prepare . Execute Declare.

Static Vs Dynamic
For static SQL, the structure of the statement should be known beforehand, while dynamic SQL allows complete flexibility in the structure of the statement. Static SQL can contain host variables; Dynamic SQL cannot host variables. Static SQL are bound beforehand, while dynamic SQL are bound at runtime and hence runtime overhead is greater. Static SQL select statements can have INTO clause whereas dynamic SQL select statements cannot have INTO clause. Examples of dynamic SQL: SPUFI, QMF These two accept almost any SQL statement, and process and execute the SQL statements dynamically.

Syntax for Sql EXECUTE IMMEDIATE _____________________________________________ Exec Sql Execute Immediate :Host-var End-Exec. Host-var Cobol host variables that contains the statement string to be translated, bound and executed. Must contain data compatible with the DB2 varchar data type.

Sql prepare statement


Exec Sql Prepare parpared-statement-name [Into Sqlda-name] From :host-var End-Exec. Prepared statement Symbolic name 18 characters long u create to identify the prepared statement . Sqlda-name The name of an sqlda where information about the prepared statement will be stored Host-var Cobol host variables that contains the statement string to be translated, bound and associated with prepared statement.

Where a host var would not used to allowed in a static statement. [as in from ?] As column specifications [ as in select ?,?,?] As both elements in a comparison [as in where ? = ?] As both elements of an arithmetic operation [as in where invtotal =?+?]

Categories of Dynamic SQL


Non - Select SQL Statements Fixed - List Select Statement Varying -List Select Statement

Non-Select SQL Statements


The SQL statement is stored as a string. Can be used for insert, update or delete. Example

Processing for a Dynamic non- Processing for a select statement executed Dynamic non-select once statement executed more than once

Processing for a Dynamic non-select statement that produces a known results table

Processing for a Dynamic non-select statement that produces a unknown results table

Format non-select statement in a variable-length string host variables

Format non-select statement in a variablelength string host variables

Format non-select statement in a variablelength string host variables

Format non-select statement in a variable-length string host variables

Execute Immediate

Prepare Execute

Declare Cursor Prepare Open Fetch close

Declare Cursor Prepare Describe Allocate storage for host variables Open Fetch close

Example

Methods for using Non Select SQL


Prepare And Execute Execute Immediate May Or May Not Have Parameter Markers.

Prepare
The SQL statement is prepared and assigned a unique label. A prepared statement can be executed several times in a program General format

Exec Sql Execute prepared-statement-name [using { :host-var[: host-var] Descriptor Sqlda -name} End-Exec. ________________________________________________________ 1. Symbolic name specified on the prepare statement the processed the prepared statement u want to execute. 2. Cobol host variable whose name will be substituted for a parameter marker in the prepared sql statement. 3. Sqlda-name that contain descriptions of the host variables whose values are to be substituted for parameter markers in the prepared statement.

Execute
Executes the SQL statement

Execute Immediate
Prepares and executes the SQL statements in one shot. Syntax.

Fixed - List Select Statements


Fixed-list implies that the number 0f columns and the datatypes of those columns are known in advance. Number of rows that are returned need not be known

Steps

Declare a cursor for the statement name. Prepare the statement. Open the cursor Fetch rows from result table. Close the cursor. Example

Declare
Exec Sql

Open

Fetch

Close

Declare cursor-name for prepared-select-name End-Exec. __________________________________________________________ Exec Sql Open cursor-name using {:host-var Descriptor sqlda-name} End-Exec. _________________________________________________________ Exec Sql Fetch cursor-name Into {:host-var[:host-structure]} [using Descriptor sqlda-name] End-Exec.

Example

Varying List Select


Returns rows containing an unknown number of values of unknown type. Host variables to be declared are not known in advance. May or may not have parameter markers

Steps Involved:
Include an SQLCA Load the input SQL statement into a data area. Prepare and execute the statement Include an SQLDA Declare a cursor & prepare the variable statement Obtain the information about the type and size of each column of the result table Open the cursor; Fetch a row Eventually close cursor and free main storage Handle any errors that may arise. SQLDA must be explicitly coded and DB2 does not provide any include file.

TABLE CONTENTS SYSCOLUMNS Table contains one row for every column of each table and view SYSRELS Table contains one row for every referential constraint. SYSFOREIGNKEYS Table contains one row for every column or every foreign key. SYSVIEWS Table Contains One or More Row for Each View. SYSVIEWDEPT Table records the dependencies of views on tables and other views. ___________________________________________________________ SELECT NAME, COLTYPE, LENGTH FROM SYSIBM.SYSCOLUMNS WHERE TBNAME=DEPT AND TBCREATOR=IKSA01;

SQLDA Format

Performance Considerations for DB2 Programming


General Guidelines Avoid Numeric conversions Select only columns that are used For single row retrieval, SELECT can be used instead of CURSOR processing. Cursors cannot be used for Update or Delete if DECLARE CURSOR includes any of the following : UNION, GROUP BY, HAVING, SUB-QUERIES, DISTINCT, ORDER BY, JOINS DECLARE CURSOR must precede any other comma (OPEN, FETCH, CLOSE) relating to cursor. Initialize Host variables used by the cursor before opening the cursor. Otherwise data integrity problems may arise. Explicitly close all cursors, to increase performance.

WITH HOLD option is not available for CURSORS coded in a CICS-DB2 program.

For Update & Delete, use Cursors to improve performance. For non-cursor Update and Delete statements, always use primary key in the WHERE clause. Avoid SELECT * in Embedded SQL program. Filter data using WHERE clause Use joins instead of correlated sub select statements.

INDEX Considerations Using an INDEX can Save a tablespace scan Avoid a sort. DB2 decides on use of an INDEX. A table can have more than one Index on different Columns. Access paths :Tablespace Scan Clustered Index Scan, Non-clustered Index Scan.

DB2 & CICS


CICS or Customer Information Control System is a database/data communication control system. We can access DB2 data from CICS programs. When DB2 data is accessed from CICS, multiple threads can be active simultaneously, enabling concurrent access to a DB2 subsystem by multiple users of a CICS region. A mechanism called CICS Attachment Facility connects CICS with DB2. Using this facility, each CICS region can be connected to only one DB2 subsystem at a time. Each subsystem can, however, to be connected to more than one CICS region at a time.

CICS provides an interface to DB2, but DB2 requires its own region independent of the CICS region. Both CICS and DB2 require its own unique regions under the OS. CICS application programs can be issue a set of commands for SQL services in order to access the DB2 database.

Locks
DB2 allow more than one application to access the same data at essentially same time. This is known as concurrency. To control such undesirable results such as lost updates, access to uncommitted data and unrepeatable reads, concurrency must be controlled. DB2 uses lock to control concurrency. DB2 locks can be compared in four basic aspects: object, size, duration, and mode. Object of lock is the resource being locked. Size of lock can be page, table or tablespace Duration of lock is the length of time the lock is held. It varies according to when the lock is acquired and released. This is controlled by bind options ACQUIRE, RELEASE and ISOLATION. Mode of lock tells what access to the locked object is permitted to the lock owner and to any concurrent application processes.

Page Lock modes:


S (SHARE) The lock owner and any concurrent processes can read but not change the locked page. Concurrent processes can acquire S or U locks on the page. U (UPDATE) The lock owner can read but not change the locked page; however the owner can promote the lock to an X lock and then can change the page. Processes concurrent with the U lock can acquire S locks and read the page, but no concurrent processes can acquire a U lock. X (EXCLUSIVE) The lock owner can read or change the locked page. Concurrent processes cannot acquire any lock on the page nor can they access the locked page.

Effects of DB2 locks:


The IMS resource lock manager (IRLM) is responsible for managing all requests for locks and controlling access to DB2 databases. Suspension: The IRLM suspends an application process if it requests a lock on an object that is already owned by another application process and cannot be shared. The suspended process is said to be Waiting for the lock. The process resumes running when the lock is available. Timeout: Timeout occurs when the application process is suspended for longer than a preset interval. DB2 reports a timeout by returning a SQLCODE of 911 or 913 to the process.

Deadlock: A deadlock occurs when 2 or more application processes each hold lock on resources that the others need and without which they cannot proceed. After the end of the preset time interval, DB2 can rollback the current unit of work for one of the processes, or request the process to terminate. That frees the locks and allows the remaining processes to continue. ACQUIRE and RELEASE options: These options of BIND or REBIND are used to specify when DB2 is to lock the tables or tablespaces and when to release the locks. ACQUIRE (ALLOCATE)/RELEASE (DEALLOCATE) This option avoids deadlocks by locking your resource at the beginning. Use this option if processing efficiency is more important than concurrency. All tables or tablespaces are locked when the plan is allocated and released only when the plan is terminated. This option reduces concurrency.

ACQUIRE (USE)/RELEASE (DEALLOCATE): The table or tablespace is locked only when it is needed while running. All tables and tablespaces are unlocked only when the plan terminates.

ACQUIRE(USE)/RELEASE(COMMIT) : This is the default option & provides greater concurrency. A table or tablespace is locked only when needed. The lock is released when a SQL COMMIT or ROLLBACK statement is issued or when the application process terminates. This option can increase the frequency of deadlocks

ACQUIRE(ALLOCATE)/RELEASE(COMMIT) is not allowed.

ISOLATION option.
This option on BIND or REBIND helps DB2 release S or U page locks as soon as possible. The ISOLATION option does not apply to changed data. All changed data remains locked at least until the next commit point. ISOLATION option can be either CS or RR The

CURSOR STABILITY (CS)


For read only processing, the page lock is held only while the cursor is positioned on that page. When the cursor moves to another page, that lock is released. The application is protected from concurrent updates or deletion of the row it is currently processing.

REPEATABLE READ(RR)
Page locks are held at least until the next commit point. With this option, if the application process returns to the same page and reads the same row again, the data cannot be changed. The application process is assured that SQL SELECT statements it issues are repeatable. Use Repeatable Read when your application has to retrieve the same rows several times.

APPENDIX Common SQL CODES

Limits Imposed by DB2 Identifier Length Limits

Numeric Limits

String Length Limits

Date Time Limits

DB2 Limits

Exercises
Code and execute CREATE statements to create EMPLOYEE & DEPT Tables. Table Specifications Customer Details

Account Details

Using the Basic Select: Display all the rows and columns from Customer table. Display Acc_no,Acc_Type, Name, Balance from Customer table List all the information for any account type where Rate_o_int is unknown Display the Acc_no, Name , Acc_Type, DOB and Balance of all Customers who have less than 4000 Balance. Sort the results by name. List the Accounttypes & MinBal where Rate_o_int is unknown. Show Acc_no, name, address and phone no of customers whose accounttype is either CA or SB. Produce a list of customers whose name begins with V. Sort the list by name. Display name, Acc_type & balance. for customers with a balance greater than 10,000. Produce a list of customers whose Balance is < 3,000. List the output in descending order by of balance.

Using the Complete Select Statement Display the sum of all balances along with the company average balance balance, minimum and maximum balance Which customer name comes first alphabetically? How many different acctypes do customers have? Show the average balance for each acc_no. List the average balance for each acc_type 50,00.00 Display name, Acc_no and acc_type name in all account types whose Acc_type begins with C. Show the average balance for men and the average balance for women for each acc_type. Identify the acc_type by name. Order the results by descending balances within account details. Produce a report that gives the no. of customers and total balance for acc_types that begin with C, S or F . List the results in order by acc_type balance. where the average balance is greater than

Using Select Statement with Subqueries Show the name and acc_type of customers in all phone nos where phone no includes 472 Display name and acc_type of customers in all phone nos where phone no includes 472 List each acc_type that has an average balance less than banks average balance. Display list of all acc_no of female customers and male customers in all acc_type where acc_type includes "SB" Write a program to insert data into the customer and account details table after accepting the required data from the terminal Write a program to list all the customersin the bank and write the result to a sequential file Write a program to update the account details table by accepting the acc type from the terminal

Source Program

Modified Source Non-SQL

DB2 Precompiler or HostLanguage Compiler

SQL

Compilation DB2 Catalog Object Program

DBRM

Bind Package

Bind Plan

Plan Link Edit

Load Module

Source Program

Modified Source Non-SQL

DB2 Precompiler or HostLanguage Compiler

SQL

Compilation

DBRM

DB2 Catalog Object Program

Bind Package

Bind Plan

Plan Link Edit

Load Module