DB2-Notes

77
Data Base 2(DB2) Sridhar Babu Purama(32953_FS)

description

DB2-Notes

Transcript of DB2-Notes

Page 1: DB2-Notes

Data Base 2(DB2)Sridhar Babu Purama(32953_FS)

Page 2: DB2-Notes

Db2 Database

A Database management system (DBMS) is a software package that manages data

stored in databases.

IBM’S Database 2 , commonly referred to as DB2, was made available in 1983 as

RDBMS concept.

Prior to DB2, IBM developed a HDBMS product IMS DB.

Page 3: DB2-Notes

Db2 Database

Data on Mainframe is stored in two ways :1. Files2. Database

DB2 is the RDBMS developed by IBM for storing and accessing heavy data.

RDBMS – Relational Data Base Management System.RDBMS allows data access to entities by establishing the following

relationships :ONE TO MANYMANY TO ONEMANY TO MANY

Page 4: DB2-Notes

Db2 Database

Advantages of database over files :

1. Database supports high volume of data storage whereas files support low volume of data storage.

2. Database provides data security at various levels whereas files do not.3. Accessing database data is easy as compared to files where

programming is required.4. Database supports data concurrency & locking.5. Database avoids data redundancy.

Page 5: DB2-Notes

Db2 Database

When DB2 S/W is installed on the Mainframe, a system space and user space are

created.

In the SYSTEM SPACE, default tables called DB2 catalog tables & DB2 directory

tables are stored.

DB2 Catalog tables store the information of the user created database objects.

SYSIBM.SYSTABLESPACES store tablespaces’ information.SYSIBM.SYSTABLES store tables’ information.SYSIBM.SYSCOLUMNS store columns’ information.SYSIBM.SYSVIEWS store views’ information.

DB2 Directory tables store the information about the physical memory locations of

database objects.

In the USER SPACE, user created database objects like tablespaces, tables, views,

synonyms, stored procedures, etc.

Page 6: DB2-Notes

Db2 Database

System Space User Space

DB2 Catalog Tables

DB2 Directory Tables

System Space User SpaceSystem Space

Page 7: DB2-Notes

Db2 Database StructureDatabase

Tablespace1 Tablespace2TS2 Indexspace

Table1

Table2

View1

Rows

Columns

INDEX

Page 8: DB2-Notes

Db2 Database

Table: Table is the entity where we can store the data in the form of rows and columns.

Storage grp: It is a set of memory volumes allocated to the table space. Data in table spaces are stored in the form of the tables.

Page: Page is an amount of space in the table space which is used to transmit the data from system memory to buffer.Page size can be 4k, 8k….32k.Page can have maximum of 127 rows.

Page 9: DB2-Notes

Db2 Database

Table spaces are again 3 types.SimpleSegmentedPartitioned Simple table space: In this type of TS, a page can hold more than one table’s data.

Segmented table space: In this type of TS, a page can hold ONLY one table’s data.

Partitioned table space: In this type of TS, a set of pages are grouped into a partition and only specific data is stored in each partition.

Using partitioned table space, amount of memory to be scanned is reduced, thereby improves the DB2 performance. 

Page 10: DB2-Notes

Data types in DB2 and COBOL

DB2 COBOLSMALL INT 2 bytes S9(4) COMPINT 4 Bytes S9(9) COMPCHAR (N) N Bytes X(n)VARCHAR (N) (N+2) Bytes 01 VAR-FIELD

49 LEN PIC S9(4) COMP. 49 TEXT PIC X(N).

DECIMAL S9(M-N)V9(N) COMP-3DATE 10 Bytes X(10) [DD-MM-YYYY]TIME 8 Bytes X(08) [HH.MN.SS]TIMESTAMP 26 Bytes X(26) [DD-MM-YYYY- HH.MN.SS-NNNNN]

Page 11: DB2-Notes

DB2 data is accessed using SQL queries.

DDLCREATEALTERDROP

DMLINSERTUPDATEDELETESELECT

DCLGRANTREVOKE

Page 12: DB2-Notes

DDL: Data Definition language:

CREATE:It is used to create database objects. Creating a database:

CREATE DATABASE DB123. Creating a table space:

CREATE TABLE SPACE TS123STORGROUP3PAGESIZE 16KSEGMENTED IN DB123.

Creating a table: CREATE TABLE EMP_TBL

(EMP_ID INT NOT NULL, EMP_NAME CHR(30), EMP_DEPT SMALL INT, EMP_SAL DECIMAL (11,2), EMP_ADDR VARCHAR(50), EMP_JOIN_DT DATE NOT NULL WITH DEFAULT, EMP_JOIN_TM TIME, TRANS_TS TIMESTAMP. PRIMARY KEY (EMP_ID), ON DELETE CASCADE)

IN DB123.TS123

Page 13: DB2-Notes

Constraint is a mechanism to control the data in the tables.

1. Primary Key.2. NULL Constraints3. Referential Integrity

CONSTRAINTS

Page 14: DB2-Notes

Constraints

Primary Key:It is used to uniquely identify a row in the table.When primary key constraint set on a column it does not allow duplicate or null values.

Null value : It is an unknown value assigned to a column when no value is specified. Null Constraint:By default, any column is nullable (allows null values). Not Null Constraint:It does not allow null values to be stored and so a value must be specified. Not Null with Default:If no value is specified then instead of storing null value it stores a default value based on the data type.

Page 15: DB2-Notes

Default values

Note:

For SMALL INTINT andDECIMAL zeroes are the default values.

For CHAR Spaces are the default values.

For DATE Current date is the default value.

For TIME Current time is the default value.

For TIMESTAMP Current time stamp is the default value.

Page 16: DB2-Notes

Referential Integrity:It controls the data in the parent and child table

Foreign Key: It is used to build the relationship between the tables. It must be a primary key of the table and when we use this in the other table then it becomes as a foreign Key.

Table in which it is primary key then it is called Parent table and the table in which it is foreign key then it is called child table.

RI rules :1. Insert Rule2. Update Rule3. Delete Rules

- ON DELETE CASCADE - ON DELETE RESTRICT

- ON DELETE SET NULL

CONSTRAINTS

Page 17: DB2-Notes

INSERT RULE : It says before inserting a row with new foreign key value, insert it first into parent table.

Referential Integrity

UPDATE RULE : It says before updating a row with new foreign key value, update it first in the parent table.

DELETE Rules :

1. ON DELETE CASCADE : It says when a parent table row is deleted, the corresponding rows in all child tables also get deleted.

2. ON DELTE RESTRICT : It says when a parent table row with corresponding rows in child tables is to be deleted, then a restriction is applied. So, first, delete the child table rows and then parent table row.

3. ON DELETE SET NULL : It says when a parent table row is deleted, the foreign key values for the corresponding rows in all child tables are set to NULL.

Page 18: DB2-Notes

DDL: Data Definition language:

ALTER:

It is used to modify the database objects.

Syntax: ALTER EMP_TBL ADD EMP_DOB DATE

This column will add at the last in the table EMP_TBL.

Note: A column can be added only as the last column in the table. If we need to add the column in between the columns then we need to drop and create new table. We can change the data types and data lengths.

ALTER EMP_TBL SET EMP_SAL DECIMAL (11,2) DECIMAL(13,2)

We can increase/decrease the data length if a table is empty. But if the table is non empty we can only increase the data length. DROP:

Drop is used to permanently delete a data object in a database.

Syntax: DROP TABLE EMP_TBL DROP VIEW V_EMP_TBL  

Page 19: DB2-Notes

DML: Data Manipulation Language

INSERT:

It is used to insert a new row into the table. Only one row at a time.INSERT INTO EMP_TBL

VALUES (0314,’SRIDHAR’…)

If all the values are specified, then column names need not be specified.INSERT INTO EMP_TBL

(EMP_ID, EMP_NAME)VALUES (0314,’ANUUSHA’)

  UPDATE:

It is used to modify the existing data in the table.UPDATE EMP-TBL SET EMP-SAL=EMP-SAL+1000

The above code will update all EMP-SAL column values in the table.UPDATE EMP-TBL

SET EMP-SAL=EMP-SAL+1000 WHERE DEPT=’D1;

WHERE clause is used to specify a condition based on which rows selective.

Page 20: DB2-Notes

DML: Data Manipulation Language

DELETE:It is used to delete the rows from the table.

DELETE FROM EMP-TBL, it will delete all the rows.

DELETE FROM EMP-TBL WHERE DEPT = ‘D1’

 SELECT:It is used to retrieve the rows from the table.

SELECT * FROM EMP-TBLThe above query retrieves all the rows and columns.

SELECT EMP-ID,EMP-SAL FROM EMP-TBLThis query retrieves all the rows with column EMP-ID & EMP-SAL.

SELECT * FROM EMP-TBL WHERE DEPT = ‘D1’

This query will retrieve all the rows with DEPT value ‘D1’.

SELECT EMP-ID FROM EMP-TBL WHERE EMP-SAL BETWEEN 10000 AND 300000

Page 21: DB2-Notes

DCL: Data control language

It issues a permissions on DML commands GRANT:

To provide access or permissions on data base objects to the users.

Syntax: GRANT INSERT,SELECT ON EMP_TBL TO FSS141, GRP1.

GRANT ALL ON EMP_TBL TO FSS142, FSS156

 REVOKE:

To remove the authorizations or permissions.

REVOKE INSERT ON EMP_TBL FROM FSS141, GRP1.

REVOKE ALL ON EMP_TBLFROM FSS142, FSS156

   

Page 22: DB2-Notes

SQLCA

SQLCA is SQL Communication Area.

It is the communication are between COBOL and DB2.

SQLCA is used in the program as follows :EXEC SQL INCLUDE SQLCAEND-EXEC.

The above statement is resolved during DB2 pre-compilation as follows :01 SQLCA. 03 SQLAID PIC 03 SQLCODE PIC 03 SQLWARN PIC 03 SQLERR PIC 03 SQLSTATE PIC

SQLCODE is used to know the status of a Db2 statement.If SQLCODE is 0 or +ve, DB2 execution is successfulIf SQLCODE is -ve, DB2 execution is unsuccessful

SQLCODE = 0 Successful and row foundSQLCODE = 1000 Successful but row not found

Page 23: DB2-Notes

Write a program taking OT-FILE as input and increment employee salaries for the OT Employees with the OT-AMT.

IDENTIFICATION DIVISION. PROGRAM-ID. DBPROG1. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT OTFILE ASSIGN TO DISK1.DATA DIVISION. FILE SECTION. FD OTFILE. 01 OTFILE-REC. 02 EMP-ID PIC X(5). 02 OT-AMT PIC 9(5). 02 FILLER PIC X(70). WORKING-STORAGE SECTION. EXEC SQL INCLUDE SQLCA END-EXEC. EXEC SQL INCLUDE DCLEMP END-EXEC. 01 WS-EOF PIC X(1) VALUE 'N'. PROCEDURE DIVISION. 0000-MAIN-PARA. PERFORM 1000-INITIALIZE-PARA. PERFORM 2000-READ-PARA. PERFORM 3000-PROCESS-PARA UNTIL WS-EOF = 'Y'. PERFORM 9000-CLOSE-PARA. STOP RUN. 1000-INITIALIZE-PARA. OPEN INPUT OTFILE. 2000-READ-PARA. READ OTFILE AT END MOVE ‘Y‘ TO WS-EOF.

3000-PROCESS-PARA. MOVE EMP-ID TO HV-EMP-ID. EXEC SQL SELECT EMP_SAL INTO :HV-EMP-SAL FROM EMP_TBL WHERE EMP_ID = :HV-EMP-ID END-EXEC. EVALUATE SQLCODE WHEN 0 ADD OT-AMT TO HV-EMP-SAL EXEC SQL UPDATE EMP_TBL

SET EMP_SAL = :HV-EMP-SAL WHERE EMP_ID = :HV-EMP-ID END-EXEC WHEN 100 MOVE EMP-ID TO HV-EMP-ID MOVE OT-AMT TO HV-EMP-SAL EXEC SQL INSERT INTO EMP_TBL (EMP_ID,EMP_SAL) VALUES(:HV-EMP-ID,:HV-EMP-SAL) END-EXEC END-EVALUATE. PERFORM 2000-READ-PARA. 9000-CLOSE-PARA. CLOSE OTFILE.

Page 24: DB2-Notes

DCLGEN

DCLGEN is a declaration generator used to generate DB2 equivalent COBOL variables called Host variables.

DCLGEN will avoid the conversion errors i.e., avoids any mismatch in data types and data lengths.

Page 25: DB2-Notes

DB2 Program Preparation

DB2 Program preparation involves the following steps :

1. Coding the program.2. Pre-compilation3. BIND4. Compilation5. LINK-EDIT6. Execution

Page 26: DB2-Notes

RUNSTATUtility

Modified Source Code(DB2 statements replaced by

COBOL CALL statements)

Object Module

Load Module

DBRM (Database Request Module)(Error free DB2 statements)

PACKAGE(DBRM + Best Access Path)

PLAN(Collection of PACKAGES)

Execution

Runtime Supervisor

OPTIMIZER

DB2 Catalog Tables

COBOL DB2 Program

TS1 TS1

TS1 TS1

TS1TS1

Pre-Compilation

DSNHPC

CompilationIGYCRCTL

LINK-EDITIEWL/HEWL

BINDIKJEFT01

BINDIKJEFT01

Page 27: DB2-Notes

Pre-compilation

Pre-compilation is the process of separating COBOL and DB2 statements to generate a Modified

Source Code and DBRM.

1. The pre-compiler utility DSNHPC checks the syntax errors of DB2 statements before placing them into DBRM (Database Request Module).

2. It replaces all the DB2 statements by COBOL CALL statements in the Modified Source Code.

3. It places a TIMESTAMP token in both Modified Source Code and DBRM.

*EXEC SQL* SELECT EMP_SAL FROM EMP_TBL *END-EXEC. CALL ‘DSNHLI’ USING PTR1

*EXEC SQL• UPDATE EMP_TBL• SET EMP_SAL : HV-EMP-SAL*END-EXEC. CALL ‘DSNHLI’ USING PTR2

PTR1 SELECT EMP_SAL FROM EMP_TBL

PTR2 UPDATE EMP_TBL SET EMP_SAL = :HV-EMP-SAL

Modified Source Code DBRM

Page 28: DB2-Notes

BIND

BIND is the process of binding DBRM and Best Access path into a PACKAGE/PLAN.

1. BIND utility IKJEFT01 checks the syntax errors of DB2 statements in DBRM. It not only checks the errors but also checks whether the columns and tables specified exist or not by comparing with DB2 catalog tables.

2. BIND checks the authorization of the user for BINDING and EXECUTING the queries.

3. BIND component, OPTIMIZER using the statistics of RUNSTAT utility generates the available access paths. Among the available access paths, it chooses the best one and place it into the PACKAGE.

Access Path determines how DB2 data can be accessed.

Access Path is derived based on factors like CPU consumption time and CPU resource utilization.

Page 29: DB2-Notes

BIND

BIND is the process of binding DBRM and Best Access path into a PACKAGE/PLAN.

1. BIND utility IKJEFT01 checks the syntax errors of DB2 statements in DBRM. It not only checks the errors but also checks whether the columns and tables specified exist or not by comparing with DB2 catalog tables.

2. BIND checks the authorization of the user for BINDING and EXECUTING the queries.

3. BIND component, OPTIMIZER using the statistics of RUNSTAT utility generates the available access paths. Among the available access paths, it chooses the best one and place it into the PACKAGE.

4. One or more PACKAGES are bound again into an EXECUTABLE PLAN.

Page 30: DB2-Notes

COMPILATION

Compilation is the process of checking syntax errors and converts Source code into object module.

Compiler IGYCRCTL takes Modified Source Code as input and generates Object Module.

Page 31: DB2-Notes

LINK-EDIT

LINK-EDIT is the process of linking object modules into an single executable load module.

Page 32: DB2-Notes

Execution

Runtime Supervisor allows execution only when TIMESTAMP tokens match between Load module

and PLAN.If they do not match, it abends the program with SQLCODE -818.

SQLCODE -818 TIMESTAMP Token mismatch

Solution : Run the entire process again.

Page 33: DB2-Notes

BIND PACKAGE JCL//JOB1 JOB ‘A123,’RAMESH’,CLASS=A,MSGCLASS=X//STEP1 EXEC PGM=IKJEFT01//SYSTSIN DD *

BIND PACKAGE(PKG1) -MEMBER(DBRM1) -OWNER(ALLST) -QUALIFIER(ALLST2) -ACTION(ADD/REPLACE) -VALIDATE(BIND/RUN) -EXPLAIN(YES/NO) -ACQUIRE(ALLOCATE/USE) -RELEASE(COMMIT/ROLLBACK) -ISOLATION(CS/RR/UR/RS)

/*

Page 34: DB2-Notes

BIND PACKAGE JCL//JOB1 JOB ‘A123,’RAMESH’,CLASS=A,MSGCLASS=X//STEP1 EXEC PGM=IKJEFT01//SYSTSIN DD *

BIND PACKAGE(PKG1) -MEMBER(DBRM1) -OWNER(ALLST) -QUALIFIER(ALLST2) -ACTION(ADD/REPLACE) -VALIDATE(BIND/RUN) -EXPLAIN(YES/NO) -ACQUIRE(ALLOCATE/USE) -RELEASE(COMMIT/ROLLBACK) -ISOLATION(CS/RR/UR/RS)

/*//JOB2 JOB ‘A123,’RAMESH’,CLASS=A,MSGCLASS=X//STEP1 EXEC PGM=IKJEFT01//SYSTSIN DD *

BIND PACKAGE(PKG2) -MEMBER(DBRM2) -OWNER(ALLST) -QUALIFIER(ALLST2) -ACTION(ADD/REPLACE) -VALIDATE(BIND/RUN) -EXPLAIN(YES/NO) -ACQUIRE(ALLOCATE/USE) -RELEASE(COMMIT/ROLLBACK) -ISOLATION(CS/RR/UR/RS)

/*

Page 35: DB2-Notes

BIND PLAN & RUN JCL//JOB3 JOB ‘A123,’RAMESH’,CLASS=A,MSGCLASS=X//STEP1 EXEC PGM=IKJEFT01//SYSTSIN DD *

BIND PLAN(PLN1) -PKLIST(PKG1,PKG2 - - - - -) -

/*//STEP2 EXEC PGM=IKJEFT01//SYSTSIN DD *

RUN PROGRAM(PROG1) -PLAN(PLN1) -LIB(‘FSS141.TALENT.LOADLIB’)

/*

Page 36: DB2-Notes

BIND ParametersPACKAGE specifies the package name which is the output of BIND process.

MEMBER specifies the DBRM name which is the input for BIND process.

ACTION ACTION(ADD) - It includes the new PACKAGE information in SYSIBM.SYSPACKAGESACTION(REPLACE) - It overrides the existing PACKAGE information in

SYSIBM.SYSPACKAGES with the new one.

Page 37: DB2-Notes

BIND ParametersOWNER specifies the owner id of the tables used in the program.It is usually the Project ID.OWNER ID is same in both PRODUCTION and DEVELOPMENT regions.

QUALIFIER is used to uniquely identify a table when it has duplicates.Multiple programs can tested simultaneously on same table using Qualifiers.

There can be more than one Qualifiers in DEVELOPMENT region.Qualifier is same as OWNER ID in production region.

OWNER(ALLST) QUALIFIER(ALLST1)QUALIFIER(ALLST2)QUALIFIER(ALLST3)

Page 38: DB2-Notes

BIND ParametersEXPLAIN is a DB2 tool used to generate the access paths’ information into EXPLAIN Tables.These tables will help the DBA to analyze the Query performance.

Access paths’ information – CPU consumption time, CPU resource utilization, CPU cost, etc.

Page 39: DB2-Notes

BIND ParametersVALIDATE will check the authorization of the USER ID for BINDING and EXECUTING the queries.

Page 40: DB2-Notes

BIND ParametersACQUIRE will allocate the locks on tablespaces i.e., allocates the tabvlespaces.

ACQUIRE(ALLOCATE) will allocate the tablespaces at BIND time.ACQUIRE(USE) will allocate the tablespaces at RUN time.

Page 41: DB2-Notes

BIND ParametersRELEASE will release the locks on tablespaces upon COMMIT or ROLLBACK is issued.

COMMIT : It saves the data permanently into the database.

ROLLBACK : It does not the save the changes permanently till the last commit point.

Note : When the program executes successfully, an Auto-commit is issued.When the program executes unsuccessfully, an Auto-rollback is issued.

Page 42: DB2-Notes

BIND ParametersLocking : Locking is a security mechanism to control the data.

Locking modes : 1. SHARE Lock (S-lock) : It allows to just read the data in tablespaces. More than once user can issue S-lock on same data.

2. UPDATE Lock (U-lock) : It allows to modify the data in tablespaces. Only one user can issue U-lock on same data while other users

can issue S-lock.

3. EXCLUSIVE Lock (X-lock) : It allows to modify the data in tablespaces. Only one user can issue X-lock on same data while other

users cannot issue any other lock.

Page 43: DB2-Notes

IntroductionState the purpose of the discussionIdentify yourself

Page 44: DB2-Notes

BIND ParametersISOLATION specifies the level of locking.

1. CS (Cursor Stability) : It is Row-level locking.When a row is to be modified, then an X-lock is acquired on the row and once it is modified & committed, it is released and lock is acquired on the next updateable row.

2. RR (Repeatable Read) : It is Page level locking.When a row is to be modified, then an X-lock is acquired on the entire page on which the row is present. Once all the rows on the page are updated & committed, lock is released and acquired on the next updateable page.

3. UR (Uncommitted Read) : When a row is to be modified, an U-lock is acquired. While it is modified, other users can read that uncommitted data.

Page 45: DB2-Notes

Cursor

What is the need of cursor?When a SELECT query retrieves more than one row, the program will abend with SQLCODE -811 since it cannot hanlde multiple rows at a time.

Eg : SELECT EMP_SAL FROM EMP_TBL WHERE DEPT_ID = ‘D6’

Then how to handle multiple rows in the program?Using Cursor.

What is a Cursor ?Cursor is a pointer to a row in the resultant table.

Cursor Life Cycle :

1. Declaring cursor.2. Opening the cursor.3. Fetching the cursor.4. Closing the cursor.

Page 46: DB2-Notes

Declaring the cursorEXEC SQL DECLARE EMPCUR CURSOR FOR SELECT EMP_SAL FROM EMP_TBL WHERE DEPT_ID = ‘D6’END-EXEC.

DECLARE CURSOR statement just creates the cursor for the associated query but the query is not executed.

CURSOR WITH HOLD : It is used to keep the cursor open even after COMMIT is issued

EXEC SQL DECLARE EMPCUR CURSOR WITH HOLD FOR SELECT EMP_SAL FROM EMP_TBL WHERE DEPT_ID = ‘D6’END-EXEC.

To Update the table using cursor, use FOR UPDATE OF clause

EXEC SQL DECLARE EMPCUR CURSOR WITH HOLD FOR SELECT EMP_SAL FROM EMP_TBL WHERE DEPT_ID = ‘D6’ FOR UPDATE OF EMP_SALEND-EXEC.

Page 47: DB2-Notes

Opening the cursorEXEC SQL OPEN EMPCUREND-EXEC.

When OPEN CURSOR statement is processed, then the query associated with the cursor is executed, the resultant table is created and the cursor is positioned at the FIRST ROW in the resultant table.

Page 48: DB2-Notes

Fetching the cursorEXEC SQL FETCH EMPCUR INTO :HV-EMP-SALEND-EXEC.

When FETCH CURSOR statement is processed, then the row to which cursor is currently pointing to is retrieved into the program.

Each time FETCH cursor statement executes, one row is retrieved and so SQLCODE = 0.To retrieve all rows, FETCH till SQLCODE = 100.

Page 49: DB2-Notes

Closing the cursorEXEC SQL CLOSE EMPCUREND-EXEC.

When CLOSE CURSOR statement is processed, then both the CURSOR and the RESULTANT TABLE are destroyed.

Page 50: DB2-Notes

Write a program to increment salaries of employees of desired DEPT?

ID DIVISION. PROGRAM-ID. PROG1. DATA DIVISION. WORKING-STORAGE SECTION. EXEC SQL INCLUDE SQLCA END-EXEC. EXEC SQL INCLUDE DCLEMP END-EXEC. 01 WS-UPD-CNT PIC 9(2) VALUE 0.01 WS-SAL-INC PIC 9(5). PROCEDURE DIVISION. 0000-MAIN-PARA. PERFORM 1000-INITIALIZE-PARA. PERFORM 2000-FETCH-PARA. PERFORM 3000-PROCESS-PARA UNTIL SQLCODE NOT = 0. PERFORM 9000-CLOSE-PARA. STOP RUN. 1000-INTIALIZE-PARA. EXEC SQL DECLARE EMPCUR CURSOR WITH HOLD FOR SELECT EMP_SAL FROM EMP_TBL WHERE DEPT_ID = :HV_DEPT_ID FOR UPDATE OF EMP_SAL END-EXEC. ACCEPT HV-DEPT-ID. ACCEPT WS-SAL-INC. EXEC SQL OPEN EMPCUR END-EXEC.

2000-FETCH-PARA. EXEC SQL FETCH EMPCLR INTO :HV-EMP_SAL END-EXEC 3000-PROCESS-PARA. EVALUATE SQLCODE WHEN 0 ADD WS-SAL-INC TO HV-EMP-SAL EXEC SQL UPDATE EMP_TBL SET EMP_SAL = :HV-EMP-SAL WHERE CURRENT OF EMPCUR END-EXEC ADD +1 TO WS-UPD-CNT IF WS-UPD-CNT=50 EXEC SQL COMMIT END-EXEC. MOVE 0 TO WS-UPD-CNT END-IF WHEN OTHER DISPLAY 'SQLCODE:'SQLCODE. END-EVALUATE. PERFORM 2000-FETCH-PARA. 9000-CLOSE-PARA EXEC SQL CLOSE EMPCUR END-EXEC.

Page 51: DB2-Notes

VIEWSVIEW is a database object used to provide data security.VIEW is just a virtual table which acts like a window to the base table data.It provides data security by restricting user from accessing secure columns of base table.

Creating a VIEW :CREATE VIEW V_CC_TBL AS SELECT CC_NUM, CC_EXP_DT FROM CC_TBL

When user is granted access to view V-CC-TBL, he can access only CC_NUM and CC_EXP_DT of base table CC_TBL

View holds no data as it is only a structure.Use can access Base table data through view as follows :

SELECT * FROM V_CC_TBL

Page 52: DB2-Notes

VIEWSViews are of two types.

1. Non-Updateable View / Read-Only View.2. Updateable View.

Page 53: DB2-Notes

Updateable ViewA View is said to be Updateable when it satisfies all of the below specified conditions :

1. View must be created on a single table.2. No DISTINCT, GROUP BY or HAVING must be used.3. No SUB-QUERIES must be used.4. No Arithmetic operations must be used.5. Base Table columns other than View columns must be nullable.

Page 54: DB2-Notes

Non-Updateable ViewA View is said to be Non-Updateable when it does not satisfy any one or all of the below specified conditions :

1. View must be created on a single table.2. No DISTINCT, GROUP BY or HAVING must be used.3. No SUB-QUERIES must be used.4. No Arithmetic operations must be used.5. Base Table columns other than View columns must be nullable.

1. View is created on more than one table.2. DISTINCT or GROUP BY or HAVING are used.3. SUB-QUERIES are used.4. Arithmetic operations are used.5. Base Table columns other than View columns are not nullable.

Page 55: DB2-Notes

Aggregate Functions

These aggregate functions are used to act on a set of column values.

1. MAX : It gives the maximum value from a set of column values.SELECT MAX(EMP_SAL) FROM EMP_TBL

2. MIN : It gives the minimum value from a set of column values.SELECT MIN(EMP_SAL) FROM EMP_TBL

3. SUM : It gives the sum of a set of column values.SELECT SUM(EMP_SAL) FROM EMP_TBL

4. AVG : It gives the average of a set of column values.SELECT AVG(EMP_SAL) FROM EMP_TBL

Note : AVG will give an incorrect value when the column has null values.

5. COUNT : It gives the count of column values.SELECT COUNT(EMP_SAL) FROM EMP_TBLIt gives the count of column values in the table excluding null values.

SELECT COUNT(*) FROM EMP_TBLIt gives the count of rows in the table.

Page 56: DB2-Notes

GROUP BY

It is used to group a set of similar column values.GROUP BY is mandatory when a combination of non-aggregate and aggregate columns are used.

To get the count of employees department wise :SELECT DEPT_ID, COUNT(*) FROM EMP_TBL GROUP BY DEPT_ID

To get the count of students class wise :SELECT CLASS, COUNT(*) FROM SCH_TBL GROUP BY CLASS

To get the count of students class & section wise :SELECT CLASS, SECTION, COUNT(*) FROM SCH_TBL GROUP BY CLASS, SECTION

Page 57: DB2-Notes

HAVING

It is used to specify a condition which acts on a set of column values.HAVING must always follow GROUP BY clause.

Example :SELECT DEPT_ID, COUNT(*) FROM EMP_TBL GROUP BY DEPT_ID HAVING COUNT(*) > 3

SELECT DEPT_ID, COUNT(*) FROM EMP_TBL GROUP BY DEPT_ID HAVING DEPT-ID = ‘D3’

Page 58: DB2-Notes

Sub-Queries

A query within a query is a sub-query.

Syntax : SELECT - - - - - - - - - WHERE - - - (SELECT - - - - - - - - - - - - - -)

Outer Query / Main Query

Inner Query / Sub Query

Note : We can write a maximum of 15 sub-queries.

Page 59: DB2-Notes

Sub-Queries

There are two types of sub-queries :

1. Non-correlated sub-queries.2. Correlated sub-queries.

Non-correlated sub-queries : First the Inner Query executes and then based

on its result, the Outer query executes.

Correlated sub-queries : First the Outer Query executes and for each row of outer

query, the inner query executes i.e., first the outer query executes then the inner

query.

Page 60: DB2-Notes

Non-correlated Sub-queries

First the Inner Query executes and then based on its result, the Outer query

executes.

Example : To find the 2nd maximum salary

SELECT MAX(SAL) FROM EMP_TBL WHERE SAL < (SELECT MAX(SAL) FROM EMP_TBL)

Example : To find the 3rd maximum salary

SELECT MAX(SAL) FROM EMP_TBL WHERE SAL < (SELECT MAX(SAL) FROM EMP_TBL) WHERE SAL < (SELECT MAX(SAL) FROM

EMP_TBL)

Page 61: DB2-Notes

Non-correlated Sub-queries

First the Inner Query executes and then based on its result, the Outer query

executes.

Example : To find the 2nd minimum salary

SELECT MIN(SAL) FROM EMP_TBL WHERE SAL > (SELECT MIN(SAL) FROM EMP_TBL)

Example : To find the 3rd minimum salary

SELECT MIN(SAL) FROM EMP_TBL WHERE SAL < (SELECT MIN(SAL) FROM EMP_TBL) WHERE SAL < (SELECT MIN(SAL) FROM

EMP_TBL)

Page 62: DB2-Notes

Correlated Sub-queries

First the Outer Query executes and for each row of outer query, the inner query

executes i.e., first the outer query executes then the inner query.

Example : To find the 2nd maximum salary

SELECT E1.SAL FROM EMP_TBL E1 WHERE 1 = (SELECT COUNT(DISTINCT E2.SAL) FROM

EMP_TBL E2 WHERE E2.SAL > E1.SAL)

Example : To find the 3rd maximum salary

SELECT E1.SAL FROM EMP_TBL E1 WHERE 2 = (SELECT COUNT(DISTINCT E2.SAL) FROM

EMP_TBL E2 WHERE E2.SAL > E1.SAL)

To find the nth maximum salary

SELECT E1.SAL FROM EMP_TBL E1 WHERE n-1 = (SELECT COUNT(DISTINCT E2.SAL) FROM

EMP_TBL E2 WHERE E2.SAL > E1.SAL)

Page 63: DB2-Notes

Correlated Sub-queries

First the Outer Query executes and for each row of outer query, the inner query

executes i.e., first the outer query executes then the inner query.

Example : To find the 2nd minimum salary

SELECT E1.SAL FROM EMP_TBL E1 WHERE 1 = (SELECT COUNT(DISTINCT E2.SAL) FROM

EMP_TBL E2 WHERE E2.SAL < E1.SAL)

Example : To find the 3rd minimum salary

SELECT E1.SAL FROM EMP_TBL E1 WHERE 2 = (SELECT COUNT(DISTINCT E2.SAL) FROM

EMP_TBL E2 WHERE E2.SAL < E1.SAL)

To find the nth minimum salary

SELECT E1.SAL FROM EMP_TBL E1 WHERE n-1 = (SELECT COUNT(DISTINCT E2.SAL) FROM

EMP_TBL E2 WHERE E2.SAL < E1.SAL)

Page 64: DB2-Notes

Correlated Sub-queries

First the Outer Query executes and for each row of outer query, the inner query

executes i.e., first the outer query executes then the inner query.

Example : To get the list of TOP 5 salaries

SELECT E1.SAL FROM EMP_TBL E1 WHERE 5 > (SELECT COUNT(DISTINCT E2.SAL) FROM

EMP_TBL E2 WHERE E2.SAL > E1.SAL)

To get the list of TOP N salaries

SELECT E1.SAL FROM EMP_TBL E1 WHERE n > (SELECT COUNT(DISTINCT E2.SAL) FROM

EMP_TBL E2 WHERE E2.SAL > E1.SAL)

Page 65: DB2-Notes

Correlated Sub-queries

First the Outer Query executes and for each row of outer query, the inner query

executes i.e., first the outer query executes then the inner query.

Example : To get the list of LEAST 5 salaries

SELECT E1.SAL FROM EMP_TBL E1 WHERE 5 > (SELECT COUNT(DISTINCT E2.SAL) FROM

EMP_TBL E2 WHERE E2.SAL < E1.SAL)

To get the list of LEAST N salaries

SELECT E1.SAL FROM EMP_TBL E1 WHERE n > (SELECT COUNT(DISTINCT E2.SAL) FROM

EMP_TBL E2 WHERE E2.SAL < E1.SAL)

Page 66: DB2-Notes

JOINS

JOIN is used to combine columns from more than one table i.e., data can be

retrieved from more than one table by joining them on some common columns.

Types of Joins :1. INNER JOIN2. OUTER JOIN

- LEFT OUTER JOIN- RIGHT OUTER JOIN

- FULL OUTER JOIN

Page 67: DB2-Notes

INNER JOIN

INNER JOIN is used to retrieve matched rows from the joining tables.

E01 RAMU D4 10000E02 RAMU D7 20000E03 RAMU D2 30000E04 RAMU D6 40000E05 RAMU D2 20000E06 RAMU D1 15000

D1 ITD2 HRD3 ADMND4 PAYROLLD5 MAINT

EMP_TBL DEPT_TBL

SELECT EMP_ID, DEPT_ID, DEPT_NUM, DEPT_NAME

FROM EMP_TBL(INNER) JOIN DEPT_TBL ON DEPT_ID = DEPT_NUM

E01 D4 D4 PAYROLLE03 D2 D2 HR E05 D2 D2 HR E06 D1 D1 IT

Page 68: DB2-Notes

OUTER JOINLEFT OUTER JOIN will retrieve all the matched rows and unmatched rows

from LEFT table with the RIGHT table column values set to NULL.

E01 RAMU D4 10000E02 RAMU D7 20000E03 RAMU D2 30000E04 RAMU D6 40000E05 RAMU D2 20000E06 RAMU D1 15000

D1 ITD2 HRD3 ADMND4 PAYROLLD5 MAINT

EMP_TBL DEPT_TBL

SELECT EMP_ID, DEPT_ID, DEPT_NUM, DEPT_NAME

FROM EMP_TBLLEFT (OUTER) JOIN DEPT_TBL ON DEPT_ID = DEPT_NUM

E01 D4 D4 PAYROLLE03 D2 D2 HR E05 D2 D2 HR E06 D1 D1 ITE02 D7 - - E04 D6 - -

Page 69: DB2-Notes

OUTER JOINRIGHT OUTER JOIN will retrieve all the matched rows and unmatched rows

from RIGHT table with the LEFT table column values set to NULL.

E01 RAMU D4 10000E02 RAMU D7 20000E03 RAMU D2 30000E04 RAMU D6 40000E05 RAMU D2 20000E06 RAMU D1 15000

D1 ITD2 HRD3 ADMND4 PAYROLLD5 MAINT

EMP_TBL DEPT_TBL

SELECT EMP_ID, DEPT_ID, DEPT_NUM, DEPT_NAME

FROM EMP_TBLRIGHT (OUTER) JOIN DEPT_TBL ON DEPT_ID = DEPT_NUM

E01 D4 D4 PAYROLLE03 D2 D2 HR E05 D2 D2 HR E06 D1 D1 IT - - D3 ADMN - - D5 MAINT

Page 70: DB2-Notes

OUTER JOINFULL OUTER JOIN will retrieve all the matched rows and unmatched rows

from LEFT and RIGHT tables with the corresponding RIGHT and LEFT table column values

set to NULL.

E01 RAMU D4 10000E02 RAMU D7 20000E03 RAMU D2 30000E04 RAMU D6 40000E05 RAMU D2 20000E06 RAMU D1 15000

D1 ITD2 HRD3 ADMND4 PAYROLLD5 MAINT

EMP_TBL DEPT_TBL

SELECT EMP_ID, DEPT_ID, DEPT_NUM, DEPT_NAME

FROM EMP_TBLFULL (OUTER) JOIN DEPT_TBL ON DEPT_ID = DEPT_NUM

E01 D4 D4 PAYROLLE03 D2 D2 HR E05 D2 D2 HR E06 D1 D1 ITE02 D7 - - E04 D6 - - - - D3 ADMN - - D5 MAINT

Page 71: DB2-Notes

UNIONS

UNION is used to combine rows from more than one table.For using UNION, the data types and number of columns in both the

queries must be same. Column name may be different.

SELECT EMP_ID, DEPT_ID FROM DEPT_TBL1 UNIONSELECT EMP_ID, DEPT_ID FROM DEPT_TBL2

This will retrieve all the rows from both the tables but only unique rows are retrieved.

SELECT EMP_ID, DEPT_ID FROM DEPT_TBL1 UNION ALL SELECT EMP_ID, DEPT_ID FROM DEPT_TBL2

This will retrieve all the rows from both the tables including duplicate rows.

Page 72: DB2-Notes

Performance Tuning

DB2 Performance Tuning Techniques :1. By using Indexes2. By using DB2 utilities – RUNSTAT and REORG3. By avoiding arithmetic operations in DB2 queries.4. By using Joins

Page 73: DB2-Notes

IndexIndex is a database object used to improve the DB2 performance.When an index is created on a column, it does not allow duplicate values

but allows a single NULL value.

CREATE UNIQUE INDEX IX1 ON EMP_TBL(EMP_ID)

E01 D3 10000

E100 D5 20000

EMP_TBL

EMP_ID

E01 – E35

E71 – E100

E36 – E70

E01 D3 10000

E35 D5 20000

E36 D3 10000

E70 D5 20000

E71 D3 10000

E100 D5 2000

ROOT

NODE1 NODE2 NODE3

LEAF1 LEAF2 LEAF3

Page 74: DB2-Notes

INDEX

Index reduces the amount of memory to be scanned, thereby reducing the

execution time and hence improves the DB2 performance.

Page 75: DB2-Notes

DB2 Utilities

RUNSTAT utility : When it is run, all the statistics about the database objects

are collected from DB2 catalog tables.

//JOB1 JOB - - - - - - //STEP1 EXEC PGM=IKJEFT01//SYSTSIN DD *

RUNSTAT DB123.TS123/*

When RUNSTAT holds the latest information, OPTIMIZER can generate the BEST

ACCESS PATH which will inturn improve the DB2 performance.

Page 76: DB2-Notes

DB2 Utilities

REORG utility : When it is run, the tablespace memory is reorganized i.e., all the

data pages are accumulated releasing the unused pages into the free memory.

//JOB1 JOB - - - - - - //STEP1 EXEC PGM=IKJEFT01//SYSTSIN DD *

REORG DB123.TS123/*

When REORG reorganizes the data in the tablespace, the amount of memory to be

scanned reduces, thereby improving the DB2 performance .

Page 77: DB2-Notes

Using JOINs

Normal Query : SELECT EMP_ID, DEPT_ID, DEPT_NUM, DEPT_NAME

FROM EMP_TBL, DEPT_TBL WHERE DEPT_ID = DEPT_NUM

AND EMP_SAL > 20000 This query retrieves the entire data from both the tables and then applies the

WHERE condition.

Query using JOIN SELECT EMP_ID, DEPT_ID, DEPT_NUM, DEPT_NAME FROM EMP_TBL, JOIN DEPT_TBL ON DEPT_ID = DEPT_NUM WHERE EMP_SAL > 20000 This query retrieves only the filtered data satisfying the ON condition and then

applies the WHERE condition.

Since the amount of data to be processed is less using JOINs as compared to normal query, DB2 performance will be good using JOINs.