Beruflich Dokumente
Kultur Dokumente
DB2(DATABASE 2)
Database Group
T1 T2 T3 IND1 IND2 V1 V2
FILE -AID
DATA TYPES :
Integer -- 4 bytes
Small int -- 2 bytes
Char(n) – N bytes
Varchar(n) – N+2 bytes
Graph(n) – 2n bytes
Vargraph(n)– 2N+2 bytes
Date – 10 bytes
Time – 8 bytes
Timestamp – 26 bytes
NORMALIZATION :
Foreign key : -
Another identifier which can be used to build relationship between the tables
Must be the primary key of parent table with same data type & length
Can consists of single or multiple columns
Can contain Null or duplicate rows
Multiple foreign keys can be defined in one table
Foreign key should be defined at the time of defining child table in the create
command by “WITH REFERENCES” option.
REFERENCE INTEGRITY:
The relationship between two tables which can be achieved by defining foreign
key.
SUBQUERY:
Select max(sal) from emp where sal <(select max(sal) from emp)
CO-RELATED SUBQUERY:
For every row of outer query, inner query must executes at least once
First outer query executes & then inner query executes
Practical examples : to fine top 2,3 or n salaries
0 -- max
1 – 2nd max
2 – 3rd max
3 – 4th max
DCLGEN :
DCLGEN
5
Host variables:
Can be used to pass the data from cobol program to DB2 table or DB2 table to
COBOL program.
When host variables are coded with sql statements it must be prefixed with :
like :hv-cname.
Table name must be supplied as input to DCLGEN & partition dataset should be
as output.
After creating DCLGEN variables which must be copied to application program
in WORKING-STORAGE SECTION by using include command i.e.
Exec sql
Inlcude custDCL
End-exec.
Include & copy have the same functionality
SQLCODE :
Predefined numeric number which can be used to check SQL statements for
successful , unsuccessful execution.
SQLCODE can be stored in SQLCA(SQL Communication Area)
Copy SQLCA in WORKING-STORAGE SECTION
System defined variable
Evaluate or if statement must be coded to check the SQLCODE immediately after
SQL statement.
SQLCODE =00 ---- successful
= +100 --- end of table or record not found.
Sample program:
WORKING-STORAGE SECTION.
EXEC SQL
INCLUDE SQLCA
END-EXEC
EXEC SQL
INCLUDE CUSTDCL
END-EXEC.
01 WS-SQL-CODE PIC S9(4)
88 88-SUCCESS VALUE 00
88 88-NOTFOUND VALUE 100
6
PROCEDURE DIVISION.
UPDATE CUST
SET CNAME = :HV-CNAME
WHERE CNO=:HV-CNO
MOVE SQLCODE TO WS-SQLCODE.
EVALUE WS-SQL-CODE
WHEN 88-SUCCESS
DISPLAY “SUCCESSFULLY UPDATED”
WHEN 88-NOTFOUND
DISPLAY “ RECORD NOT FOUND”
WHEN 88-FOREIGNKEYVOILATION
DISPLAY “ FOREIGN KEY VOILATION”
WHEN OTHER
DISPLAY “ ERROR OCCURRED IN UPDATE”
STOP RUN
END-EVALUATE.
STOP RUN.
CURSOR:
Exec sql
Select cno,cname,cloc
into :hv-cno,:hv-cname,:hv-cloc
from cust where cloc =:hv-cloc
end-exec.
If the condition satisfy for one row it executes successfully. If the condition satisfy for
multiple rows it wont work. It returns –811 as SALCODE. For this we use cursors.
Practical examples : Can be used to retrieve the data based on loc, date, products.
EXEC SQL
DECLARE C1 CURSOR FOR
SELECT CNO,CNAME FROM CUST
WHERE CNAME=:HV-CNAME
END-EXEC.
EXEC SQL
OPEN C1.
END-EXEC.
PERFORM UNTIL SQLCODE= 100
EXEC SQL
FETCH C1 INTO :HV-CNO,:HV-CNAME
END-EXEC
END-PERFORM.
EXEC SQL
CLOSE C1
END-EXEC
Which can be used to update row by row when multiple rows are satisfied.
Before update cursor has to be declared with for update of column option.
Where current of cursor name option must be used with update command
Withhold: this option can be used to remain cursors open even after commit
statement.
Must be coded with cursor statement
EXEC SQL
DECLARE C2 CURSOR WITH HOLD FOR
SELECT CNO,CNAME FROM CUST
WHERE CNAME=:HV-CNAME
FOR UPDATE OF CNAME
END-EXEC.
8
EXEC SQL
OPEN C1.
END-EXEC.
EXEC SQL
FETCH C2 INTO :HV-CNO,:HV-CNAME
END-EXEC
EXEC SQL
UPDATE CUST SET CNAME=”ABC” WHERE CURRENT OF C2.
EMD=EXEC.
EXEC SQL
CLOSE C1
END-EXEC
INDEX:
VIEWS:
Advantages of Views:
9
Data security
Data correctness
Logical data independence
Part of the information can be visible to the sers
Accessing can be faster.
DELETE RULES:
UNION:
UNION is used to concatenate rows into one table from single or multiple tables.
Rules : no. of columns & data type of both the queries must be same. column
may be different
UNION can be used to eliminate duplicate rows
UNION ALL retrieved duplicate rows also.
JOINS:
JOINS can be used to concatenate columns from one table or multiple tables.
JOIN types are :
1. left outer join : which can be used to retrieve matching, non
matching rows from leftside table
2. right outer join: which can be used to retrieve matching, non
matching rows from right side table.
3. full outer join: which can be used to retrieve matching, non
matching rows from both the tables.
4. self join or inner join : can be achieved by defining alias for the
table.
EXPLAIN :
Plan table
Query table no.of index no.of indexs owner join type groupby cputime
block no name cols
DB2 CATALOG:
Consists of Table pace, Index space, Index, unique index, Views, Alias,
synonyms, keys.
When we create table, the details of table are entered in Systable automatically.
SysIBM.SYSTABLE
Table Name No.of cols Owner name Created by Created date Created time
Cust 10 Abc Xyz 02-apr-2004 0850
Item 15 Mno Rst 06-apr-2004 1020
SysIBM.SYSINDEX
SysIBM.SYSCOLS
Col name Table name Index name Primary key Foreign key
Cno Cust In1 Cno -----
Cname Cust In1 Cno
Cloc Cust In2 cno
Ino Item Ino Cno
Iname Item Ino Cno
Ides Item Ino cno
11
CATALOG:
PRECOMPILATION PROCESS:
Pre compiler takes COBOL+DB2 program as input & generates DBRM which will be
stored in userdefined PDS as separate member.
Precompiler functions:
BIND:
BIND takes DBRM as input & generate package & application plan. The package will
be loaded to the directory. Plan will be loaded to sysplans.
Bind functions:
BIND SUBCOMPONANTS/PARAMETERS:
1. OPTIMIZER:
It generates optimized access path by analyzing the statistics of SQL
statements which will be stored.
12
RUNSTATS utility is one of the ISPF panel option which is stored in DB2
defaults option.
Optimized path is stored in package which is not executable module.
2. ISOLATION LEVEL:
Which can be used to lock the database at the time of executing SQL statements.
Repeatable Read(RR): which acquires the address of a row & acquire lock
against the page(1 page -4024 bytes) & then released the lock after the commit
statements.
Default is RR.
3. RUNTIME SUPERVISOR:
Which is to oversee execution of SQL statements.
Statistics like no of tables, columns, indexes, keys
4. PLAN/APPLICATION PLAN:
It consists of executable module which is actual output of SQL statements which
must be specified in the RUNJCL to execute SQL queries if the program is batch
program. If the program is online which must be specified in RCT. Application
plan will be loaded to load module with time stamp tokens.
COBOL COMPILATION:
The compiler takes COBOL statement as input to generate object program, &
loaded to the load module by line/edit with time stamp tokens.
13
COBOL + DB2
PRECOMPILER
COBOL DBRM
PACKAGE
PLAN
OBJ PROG
APPL PLAN
Run time
supervisor
UTILITIES USED: