Sie sind auf Seite 1von 14

DB2(DATABASE 2) Relational Database Management System(RDBMS) DB2 will be stored as separate sub system in mainframe.

Any number of Sub Systems can be created in Mainframes as per the requirements. Sub systems can be used for deferent regions like development, Quality and Production regions. DB2 sub system consists database objects and they are organized as below Hierarchy of DB2 Sub system:
Database Group

Storage group1

Storage group2

Storage group3

TS1

TS2

TS3

TS4

INS1

TS5

INS21

TS6

TS7

T1

T2

T3

IND1

IND2

V1

V2

Rows

Cols

Index1

Index2

Rows

Cols

TS Table Space INS Index Space T Table IND Index V view SQL is used to access db2 objects. SQL queries can be executed by the following techniques 1. Application programming 2. Tools like QMF (Query Management Facility) SPUFI (SQL Processor User File Input) SQL (Structured Query Language) is mainly divided into four types DDL (Data Definition Language) Create, alter, drop DML (Data Manipulation Language) Insert, update, select & delete
Regd. Off: - #403 & 404, Pavani Prestige, Above R.S.Brothers, Ameerpet, Hyderabad. Cont No: 040-66462382, Fax No: 66465775.

Email ID: info@mat-soft.com, Web site: www.mat-soft.com

DCL (Data Control Language)

Grant, Revoke TCL (Transaction Control Language) :Commit, Rollback 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: Arranging data in the Database in organized manner. Types of normal forms 1NF: Avoiding multiple values or set of values in one column. 2NF: Avoiding repeated rows by defining primary key. 3NF: Separating functionally dependent and non-functionally dependent columns Primary key and building relationship: Uniquely identified row Which can be formed with single or multiple columns Does not allow duplicate records Cannot contain Null 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. CREATE TABLE ITEM ( INO INTEGER, INAME CHAR(15), CNO INTEGER,) PRIMARY KEY IS INO, FOREIGN KEY IS CNO WITH REFERENCES CUST)

Regd. Off: - #403 & 404, Pavani Prestige, Above R.S.Brothers, Ameerpet, Hyderabad. Cont No: 040-66462382, Fax No: 66465775.

Email ID: info@mat-soft.com, Web site: www.mat-soft.com

REFERENCE INTEGRITY: Relationship between two tables which can be achieved by defining foreign key. PRIMARY KEY 1. Cannot contain NULL values or Duplicate rows rows 2.Cannot be updated 3.Can be defined as a foreign key in another table 4.only one primary key can be defined for one table FOREIGN KEY 1.Can contain NULL values or duplicate rows 2. Can be updated 3. which must be primary key of another table. 4. multiple foreign keys can be defined for one table. 1

Static insertion: Insert into cust(cno, cname, cloc) values (10, xyz, hyd) Dynamic insertion: Insert into cust(cno, cname, cloc) values (v1, v2, v3) v1,v2, v3 are host variables to be defined in working storage section. Delete from cust Delete from cust where cno = 20 Update cust set cname = xyz where cno = 20 Select cno,cname from cust Select * from cust Select * from, cust where cno = v1 Select * from cust wehre cno=v1 and cname =v2 Select * from cust where cno between 20 and 60 Select * from cust where cname like %y% Column functions: Select max(sal) from emp Select min(sal) from emp Select avg(sal) from emp Select sum(sal) from emp To avoid duplicate rows : select distinct cno,cname from cust To get total no. of rows : select count(*) from cust

Regd. Off: - #403 & 404, Pavani Prestige, Above R.S.Brothers, Ameerpet, Hyderabad. Cont No: 040-66462382, Fax No: 66465775.

Email ID: info@mat-soft.com, Web site: www.mat-soft.com

SUBQUERY: Query within Query First inner query executes & out query executes based on the result of inner query Max of 15 sub queries can be coded To simplify sub queries, logic can be built with combination of COBOL + SQL statements To retrieve second maximum salary from emp table: Select max(sal) from emp where sal <(select max(sal) from emp) To retrieve third maximum salary from emp table: Select max(sal) from emp where sal < (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

Select a. sal from emp a where 0 = (select count(*) from emp b Where a.sal < b.sal)

-- max 2nd max 3rd max 4th max DCLGEN : Declaration Generator is a tool to generates the equivalent COBOL variables. Which can be used to generate host variables with equivalent data types of DB2 columns.
1 2 3

DB2 Table

DCLGEN

COBOL

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 WORKINGSTORAGE SECTION by using include command i.e. Exec sql
Regd. Off: - #403 & 404, Pavani Prestige, Above R.S.Brothers, Ameerpet, Hyderabad. Cont No: 040-66462382, Fax No: 66465775.

Email ID: info@mat-soft.com, Web site: www.mat-soft.com

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 88 88-FORIENG KEY VOILATION VALUE 532 88 88- MULITPLE ROW VALUE 811 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.
Regd. Off: - #403 & 404, Pavani Prestige, Above R.S.Brothers, Ameerpet, Hyderabad. Cont No: 040-66462382, Fax No: 66465775.

Email ID: info@mat-soft.com, Web site: www.mat-soft.com

CURSOR: To retrieve multiple rows for a given condition. Let us take the following example: 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. Cursors can be used to retrieve multiple rows for a given condition Cursor cycle is declare open fetch close Declare: declares or define name for cursor against a table Can be coded in working-storage section or procedure division For better readability code in working-storage section. Open: Can be used to open a cursor with rows for a given conditions in buffer. Must be coded in the procedure division only. Where condition variable value must supply before opening cursor. Fetch: can be used to retrieve rows one by one from buffer into application prog. Which must be coded in procedure divison after open. Must be coded with host variables No. of host variables in fetch & no of columns in the declare must be same Can be executed multiple times by using perform. i.e. till EOT or record not found which can be identified by SQLCODE = 100 Close : used to close the cursor Must be coded in procedure division only Must be executed after open statement. 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
Regd. Off: - #403 & 404, Pavani Prestige, Above R.S.Brothers, Ameerpet, Hyderabad. Cont No: 040-66462382, Fax No: 66465775.

Email ID: info@mat-soft.com, Web site: www.mat-soft.com

END-PERFORM. EXEC SQL CLOSE C1 END-EXEC

For Update of where current of: 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. 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: Index allows duplicate values unique index doesnt allow duplicate rows cross reference between index table & table is called clustered index. Create index in1 on cust(cno) PRIMARY KEY 1. Uniquely identified row. 2. No duplicated rows, no null values 3. Can consist of single or multiple columns 4. Which will be stored in SYSKEYS. INDEX UNIQUE INDEX

Record identified based on Records identified based on the index the index Duplicate rows, null values No duplicate rows are allowed Dan consist of single or Can consist of single or multiple columns columns Which is stored in SYSINDEX Which is stored in sysindex

Regd. Off: - #403 & 404, Pavani Prestige, Above R.S.Brothers, Ameerpet, Hyderabad. Cont No: 040-66462382, Fax No: 66465775.

Email ID: info@mat-soft.com, Web site: www.mat-soft.com

VIEWS: CREATE VIEW CVIEW(VCNO,VCNAME,VCLOC) AS (SELECT CNO,CNAME,CLOC FROM CUST WHERE CNAME LIKE %X%) Logical representation of the table Stored in virtual memory Can be derived from single table or multiple tables Views are updateable if they are derived from single table without any column functions , group by Multiple views can be generated from single table. Views are stored in sysviews Advantages of Views: Data security Data correctness Logical data independence Part of the information can be visible to the sers Accessing can be faster. DELETE RULES: Delete rules can be applied for delete command against Database. Delete rules are 3 types 1. on delete cascade all matching child rows will be deleted automatically when we delete parent row. 2. on delete restrict all matching rows will be restricted when we delete parent row which is default. 3. on delete set null all matching child row will be set to null when we delete parent row. 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. SELECT CNO,CNAME FROM CUST WHERE CNO=10 UNION/UNIONALL SELECT CNO,CNAME FROM ITEM WHERE INO=20
Regd. Off: - #403 & 404, Pavani Prestige, Above R.S.Brothers, Ameerpet, Hyderabad. Cont No: 040-66462382, Fax No: 66465775.

Email ID: info@mat-soft.com, Web site: www.mat-soft.com

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 left side 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 : It can be used to evaluate the performance of SQL queries. It can be used to tune SQL queries. Input is SQL queries and output is plan-table. For every SQL query one plan-table will generate. All plan-tables are stored in physical seq file. Plan table Query table block no name 1 cust no.of cols 10 index in1 no.of indexs owner join type groupby 1 custc self y cputime 10 min

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 Index name Table Name No.of cols Owner name Created by Created date Created time In1 Cust 10 Abc Xyz 02-apr-2004 0850
Regd. Off: - #403 & 404, Pavani Prestige, Above R.S.Brothers, Ameerpet, Hyderabad. Cont No: 040-66462382, Fax No: 66465775.

Email ID: info@mat-soft.com, Web site: www.mat-soft.com

In2

Item

15

Mno

Rst

06-apr-2004

1020

SysIBM.SYSCOLS Col name Cno Cname Cloc Ino Table name Cust Cust Cust Item Index name In1 In1 In2 Primary key Cno Cno cno Ino Foreign key ----Cno

Iname Ides

Item Item

Ino Ino

Cno cno

SysIBM.SYSKEYS: All primary & foreign keys. Grant table syscols to all Grant table syscols(select/delete/update) to user1,user2 Revoke table syscols from all. CATALOG: SYSTABLE, SYSCOL, SYSKEYS, SYSINDEX, SYSPKS, SYSFKS, SYSALIAS, SYSSYNONYMS, SYSINDEX, SYSVIEWS,SYSTABLESPACE, SYSINDEXSPACE. PRECOMPILATION PROCESS: Pre compiler takes COBOL+DB2 program as input & generates DBRM which will be stored in user defined PDS as separate member. DSNHPC --- IBM supplied utility used for pre compilation. Precompiler functions: BIND:
Regd. Off: - #403 & 404, Pavani Prestige, Above R.S.Brothers, Ameerpet, Hyderabad. Cont No: 040-66462382, Fax No: 66465775.

Separates SQL & COBOL statements Check SQL syntaxs Replace all SQL statements with host language call statements in the compiled program. Which generates timestamp tokens

Email ID: info@mat-soft.com, Web site: www.mat-soft.com

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: Checks authorization for SQL statement Checks the syntax errors of SQL statements like 1. Missing co name in the select list & used in order by & group by 2. Mismatch columns host variables 3. Data type mismatch of columns & host variables 4. Indicator variables not declared 5. Data truncation. BIND SUBCOMPONANTS/PARAMETERS: 1. OPTIMIZER: It generates optimized access path by analyzing the statistics of SQL statements which will be stored. 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. Cusrsor stability(CS): It acquires the address of a row. Sets the pointer to a specified row based on SQL query & acquires the lock against that row. Then release the lock after the transaction before commit. 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: It is to oversee the 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
Regd. Off: - #403 & 404, Pavani Prestige, Above R.S.Brothers, Ameerpet, Hyderabad. Cont No: 040-66462382, Fax No: 66465775.

Email ID: info@mat-soft.com, Web site: www.mat-soft.com

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.

COBOL + DB2

PRECOMPILER

COBOL OBJ PROG

DBRM BIND PACKAGE PLAN OBJ PROG APPL PLAN Run time supervisor

UTILITIES USED: DSNHPC : system utility pre compiler. IKJEFT01 or IKJEFT01B --- BIND IGYCRCTL or IKFCBLOO --- COBOL compilation
Regd. Off: - #403 & 404, Pavani Prestige, Above R.S.Brothers, Ameerpet, Hyderabad. Cont No: 040-66462382, Fax No: 66465775.

Email ID: info@mat-soft.com, Web site: www.mat-soft.com

IEWL or HEWL --- link/edit INTERVIEW QUESTIONS: 1. What is RI ? where did u use RI in your project? Explain with example? 2. What is the difference between primary key, foreign key, index & unique index? 3. Can we create index when table has duplicate rows? 4. Can we create unique index when table has duplicate rows? 5. Can we create index or unique index on empty table? 6. What happens to the package when index is dropped? Whats the process or steps to be taken?

7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24.
25.

26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40.

How to delete package? Where package is stored? How to retrieve package? Difference between plan & package? What are the steps to be taken when SQL statements are changed without changing any COBOL statements? Do we need to pre compile when index is dropped? Can we bind when table is dropped? Can optimized access path consist of multiple DBRMS. What is the significance of timestamp tokens? What is the significance of normalization? Where do we specify run program, plan name, library & DB2 subsystem IBM supplied utility to run COBOL + DB2 What is difference between DB2 & COBOL files & give some example for COBOL files & DB2 tables related to your project? Can we load data from sequential file to table or table to sequential file? What are the steps to be followed to develop DB2+ COBOL program? Can we prepare a program/compile when DB2 system is down? How to identify DB2 test or production system by seeing run JCL? What is the output of explain? What is the difference between correlated sub query & sub query? How to find 4th max sal? How to find nth max sal? How to evaluate SQLcodes & where it is stored? How to count total no of unique rows from the table? How to sum repeated rows? How to write a cobol program for above query? Retrieve row by row & use cobol logic to sum repeated rows? What is the significance of DCLGEN? Where DCLGEN is stored? Difference between join & union? difference between UNION & UNIONALL? Can be have different col names in union? How do u evaluate/tune/analyze SQL queries? What are the JCL utilities for compile, pre compile, bind & link edit? Wha5t is the significance of isolated levels? Can we alter foreign key? Can we alter primary key? Email ID: info@mat-soft.com, Web site: www.mat-soft.com

Regd. Off: - #403 & 404, Pavani Prestige, Above R.S.Brothers, Ameerpet, Hyderabad. Cont No: 040-66462382, Fax No: 66465775.

41. Can we alter data type & length? 42. What are the equivalent cobol variables for varchar? 43. What is the time stamp & its format?

Regd. Off: - #403 & 404, Pavani Prestige, Above R.S.Brothers, Ameerpet, Hyderabad. Cont No: 040-66462382, Fax No: 66465775.

Email ID: info@mat-soft.com, Web site: www.mat-soft.com

Das könnte Ihnen auch gefallen