Académique Documents
Professionnel Documents
Culture Documents
<Basic>
Courseware Metadata
Created By: Credential Information: Version and Date: Shyamala P 102986 4 years with Mainframe 1.0
Courseware Information
Course Description Prerequisites Target Audience This course gives an Overview of DB2 It provides an insight into developing applications with COBOL DB2 TSO/ISPF, MVS and COBOL programming Trainees / DB2 Beginners
Course Outline
1. Introduction to DB2 2. Querying database and other DB2 objects 3. Application Programming
3.2 Pre-Compilation 3.3 Compiling & Linking 3.4 Binding 3.5 Execution 3.6 Application development guidelines 3.7 DB2 Utilities 3.8 DB2 Security, Catalog Tables and Optimizer
Copyright 2004, Cognizant Academy, All Rights Reserved 7
Objective
After completing this module, you will understand the DB2 architecture and be able to have a hands on with SQL
Session 1: Overview
Introduction to Database Concepts & types of Database Systems Network, Hierarchical, Relational A detailed discussion on the relational model Overview on DB2 - Its internal components, objects and data types
1.1 Database & RDBMS concepts: Data, Database & Database Systems
What is Data ?
A representation of facts or instruction in a form suitable for communication - IBM Dictionary
What is a Database ?
Is a repository for stored data - C.J.Date
1.1 Database & RDBMS concepts: Data, Database & Database Systems
Characteristics of Hierarchical Database
Can be visualized an upside down tree of data a single table acts as the "root" (Parent)of the database from which other tables "branch(Children) out Child may only have one parent but a parent can have multiple children This child/parent rule assures that data is systematically accessible Redundancy would occur because hierarchical databases handle oneto-many relationships well but do not handle many-to-many relationships well
Copyright 2004, Cognizant Academy, All Rights Reserved
Hierarchical Database
11
1.1 Database & RDBMS concepts: Data, Database & Database Systems
Characteristics of Network Database
The network model uses set theory to provide a architecture. Child tables were allowed to have more than one parent Network model supports many-to-many relationships The Network model solves the problem of data redundancy by representing relationships in terms of sets rather than hierarchy The model was difficult to implement and maintain
12
1.1 Database & RDBMS concepts: Data, Database & Database Systems
Characteristics of Relational Database
The concept of a table in which all data is stored The database is represented in terms of Entities, Relations and Attributes. Database is a collection of tables , constraints
C#
C Name
C Addr
Customers
I#
C#
Am t
13
1.1 Database & RDBMS concepts: Data, Database & Database Systems
Advantages of Database systems over File Management System are:
Data redundancy Multiple views Shared data Data independence (logical/physical) Data dictionary Search versatility Cost effective Security & Control Recovery restart & Backup Concurrency
Copyright 2004, Cognizant Academy, All Rights Reserved 14
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 value Atomic : Smallest unit of data; the individual data value
Copyright 2004, Cognizant Academy, All Rights Reserved 15
16
17
19
21
DB2 Optimizer
D D F
Buffer Pools
IRLM
DB2 Catalog
22
26
INDEX
27
30
31
USING - method of storage allocations - Stogroup or VCAT PCTFREE - % 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. Max no of datasets that can be open in DB2 at a time is 10,000 ERASE - Yes/No - whether physical DASD Where the TS reside to be written with binary zeros when the TS is dropped NUMPARTS - For Partitioned Tablespaces SEGSIZE - For Segmented Tablespaces
Copyright 2004, Cognizant Academy, All Rights Reserved 32
33
1.3 DB2 objects and Data types : Tables, Views & Index
Tables store the data in the form of Columns of values attached to a specific datatype with allowable value constraints A row for each record. Columns
EMPNO FI ST NME C I TI 20 MIC A EL 30 SA LLY MI I NI T ST NAME I AAS L T MPSON A KWA N WORKDEPT A 00 B01 C 01
Rows
A View is a subset of columns that are open for a specific purpose. It can be a join of more than one tables. Indexes are based on the values of data in one or more columns. An index is an object that is separate from the data in the table. Indexes are used to enhance performance. These are discussed in detail further in the course.
Copyright 2004, Cognizant Academy, All Rights Reserved 34
B L
2 4 INTEGER (p 2) + 1 8 (Sing e p e ision if n>21) LOAT(n) 4 (Doub e p e ision if n<21) DATE 4 TIME 3 10 TIMESTAMP
Copyright 2004, Cognizant Academy, All Rights Reserved
Data Type Numeric Fixed length String Date Time Time Stamp
Default Value Zero Blanks Current Date Current Time Current Timestamp
36
37
Session 1: Summary
- Database System An integrated and shared repository for stored data or collection of stored operational data used by application systems of some particular enterprise. - Types of Database System Hierarchical Network Relational - A relational database is perceived as a set of tables - Keys - Primary, composite, alternate, foreign - Integrity - Entity, Referential & Domain
38
The basic DB2 objects are: 1) STOGROUP 2) DATABASE 3) TABLESPACES- Simple, Segmented and Partitioned 4) TABLES, VIEWS & INDEXES
39
40
Session 2 : Overview
Introduction to what is SQL. The tools used to manipulate DB2 online QMF & SPUFI SQL DCL and DDL
41
Selectio n
The WHERE clause defines the Predicates for the SQL operation. The above WHERE clause can have multiple conditions using AND & OR Many other clauses can be used in conjunction with the WHERE clause to code the required predicate, some are:
Between / Not Between, In / Not In, Like / Not Like, IS NULL / IS NOT NULL
Copyright 2004, Cognizant Academy, All Rights Reserved 43
SQL
DDL
DML
DCL
GRANT REVOKE
45
Alter Table Format: ALTER : ALTER TABLE Tablename ADD Column Data-type [ not null with default] Alter allows primary & Foreign key specifications to be changed It does not support changes to width or data type of a column or dropping a column Drop Table Format: DROP : DROP TABLE Tablename Similar statements are there for other database objects
Copyright 2004, Cognizant Academy, All Rights Reserved 46
47
49
Session 2: Summary
SQL A non procedural language that fetches a resulset, performing the functions of DML,DDL and DCL. Selection and Projection - Criteria & subset of columns retreived Tools used for working on DB2 online - QMF & SPUFI Check connectivity with DB2
50
Session 3 : Overview
Introduction Introduces the listener to various DML statements Writing simple and complex queries using functions, sorting and grouping records, joins and subqueries. This also discusses in detail about additional DB2 objects like Indexes, Views, Alias & Synonyms Objective After completing this session, the listener will be able to generate queries.
Copyright 2004, Cognizant Academy, All Rights Reserved 51
Session 3 : Overview
Discussion on the DML statements INSERT statement Single row selects Multi row selects Where clause predicates Comparison Operators Logical connectors Aggregate function Scalar function Arithmetic function Sorting - Single & multi column Grouping records
Copyright 2004, Cognizant Academy, All Rights Reserved 52
If any column is omitted in an INSERT stmt and that column is NOT NULL, then INSERT fails; if nullable it is set to null If the column is defined as NOT NULL BY DEFAULT, it is set to that default value Omitting the list of columns is equivalent to specifying all values INSERT using SELECT INSERT INTO TEMP (A#, B) SELECT A#, SUM(B) FROM TEMP1 GROUP BY A# ;
Copyright 2004, Cognizant Academy, All Rights Reserved 53
54
AND OR NOT
56
000010
I I I I
S HA A S
R A 00
57
58
Try these
(Between/Not Between)
SELECT EMPNO,FIRSTNME,MIDINIT,LASTNAME,WORKDEPT FROM DSN8710.EMP WHERE EMPNO BETWEEN '000150' AND '000200'
(IN/Not IN)
SELECT EMPNO,FIRSTNME,MIDINIT,LASTNAME,WORKDEPT FROM DSN8710.EMP WHERE EMPNO IN ('000150' ,'000200')
Copyright 2004, Cognizant Academy, All Rights Reserved 59
N 0 1 0 0 0 0 0
I N E JO H N JA M E S J E N N IF E R JA M E S JO H N JA S O N
I B H K J R R
IN I
60
61
62
A00_AVGSAL
63
64
65
66
Default is ASC.
67
68
69
70
Session 3: Summary
INSERT Inserting values into tables. Insert fails if a omitted column is not nullable. SELECT The search conditions can use the following Comparison Operators Logical Connectors Other predicates that can be used are Between / Not Between In / Not In Like / Not Like IS NULL / IS NOT NULL
71
Session 3: Summary
Aggregate functions are AVG, COUNT, MAX, MIN, SUM which operate on groups of rows. Scalar functions operate on a single row like CHAR ,DATE,SUBSTR etc. Arithmetic functions operate between values of columns selected and provide a computed value as output. DISTINCT clause eliminates duplicates ORDER BY- Sorts record set GROUP BY - Groups the output based on the criteria
72
Session 4 : Overview
Joins - Outer (left, right and full), Inner Sub queries - Non Correlated and Correlated Sub queries Unions Other DML - Update and Delete statements Indexes - Creation and guidelines View, Alias & Synonyms
73
74
75
000060
T IRVING
A T A STERN
T D11
T A MANUFACTURING 000060
76
77
78
79
80
Locate the dept information of all employees with a specific Hire Date. Query returns SELECT DEPTNO , DEPTNAME multiple values FROM DSN8710.DEPT
WHERE DEPTNO IN ( SELECT WORKDEPT FROM DSN8710.EMP WHERE HIREDATE IN ('1 47-05-05', '1 63-12-05'));
Copyright 2004, Cognizant Academy, All Rights Reserved 81
SELECT PROJNO , PROJNAME FROM DSN8710.PROJ A, DSN8710.EMP B WHERE B.EMPNO= A.RESPEMP AND B.FIRSTNME = 'CHRISTINE'
82
83
85
86
87
88
89
90
91
92
93
94
96
97
Session 4 : Summary
Joins - Used for selecting data from multiples tables. Types - Inner and Outer Joins Sub queries - nested select statements Types - Correlated & Non correlated Sub Query. Union - combining the result sets fetched by two different queries Update- Updating values in a table singularly or bulk. Delete - Deleting values from table singularly or mass
98
Session 4 : Summary
Index - Pointers to data rows used for improving data access efficiency. View - Subset of table/tables. Alias - Are local names provided for remote tables. Synonym - Are local names private to the user.
99
Session 5 : Overview
Coding a DB2 application using Host variables using Embedded SQL using Cursors DCLGEN Pre compile the program Compile & Link edit the program Bind
101
TSO
CICS
BATCH
IMS
102
103
104
105
Pre-compile
DCLGEN
DBRM
106
Load Module
Application Plan
Execute
107
109
112
****************************************************************** 01 DCLEMP. 10 EMPNO 10 FIRSTNME. 49 FIRSTNME-LEN 49 FIRSTNME-TEXT 10 MIDINIT . . 10 SALARY 10 BONUS 10 COMM PIC S9(7)V9(2) USAGE COMP-3. PIC S9(7)V9(2) USAGE COMP-3. PIC S9(7)V9(2) USAGE COMP-3.
Host Structure
PIC X(6).
Host Variables
PIC S9(4) USAGE COMP. PIC X(12). PIC X(1).
114
The DCLGEN is added to the COBOL program EXEC SQL INCLUDE DCLEMP END-EXEC Include statement copies the DCLGEN into the program like a copybook
115
118
119
120
121
122
124
125
126
127
128
130
131
132
Pre-compile
DCLGEN
DBRM
133
3.2. Precompilation
All DB2 related INCLUDE statements must be placed between EXEC SQL & END EXEC keywords for the precompiler to recognize them. Searches all the SQL statements and DB2 related INCLUDE members and comments out every SQL statement in the program. The SQL statements are replaced by a CALL to the DB2 runtime interface module, along with parameters. All SQL statements are extracted and put in a Database Request Module (DBRM). Places a time stamp in the modified source and the DBRM so that these are tied. If there is a mismatch in this a runtime error of -818, timestamp mismatch, happens. DNSHPC is the precompiler program.
Copyright 2004, Cognizant Academy, All Rights Reserved 134
Compile
Object Module
Link
Load Module
Copyright 2004, Cognizant Academy, All Rights Reserved 135
136
DBRM
Bind
Application Plan
137
3.4. Binding
Bind Command is a type of compiler for SQL statements It reads the SQL statements from the DBRM and produces a mechanism to access data(access path) ,in an efficient manner, as directed by the SQL statements being bound Checks syntax, checks for correctness of table & column definitions against the catalog info & performs authorization validation The output of this stage is the application plan that is executable with a corresponding load module.
138
139
140
141
142
143
FLAG(I) BIND command returns all information, warning, error, and completion messages.
144
145
Session 5 : Summary
Host variables are defined in the host language to use the predicates of a DB2 table. These are referenced in the SQL stmt The SQL are embedded in the program between an EXEC SQL and END-EXEC DCLGEN prepares the copybook structure for the DB2 table. SQLCA is the DB2 communication area with essential variables like SQLCODE. Cursors are structures define to process the resulset one row at a time - DECLARE,OPEN,FETCH and CLOSE.
146
Session 5 : Summary
Pre-Compilation - Splits the db2 source into DBRM and Intermediate source. Compiling & Linking - Compiles the Intermediate source into object module & link edits it into Load Module. Binding -Binds the DBRM into the application plan - Plan & Package
147
Session 6 : Overview
Executing DB2 applications Efficient program coding guidelines Summary of DB2 utilities available - Check , Copy, Load etc. DB2 Locking services Catalogs and Optimizer. Performance Tuning
148
Load Module
Application Plan
Execute
149
150
151
152
154
155
156
157
158
159
160
161
163
164
Row
age
165
166
3.8. DB2 Security, Catalog tables and Optimizer : Explicit Locking Facilities
The SQL statement LOCK TABLE The ISOLATION parameter on the BIND PACKAGE command - the two possible values are RR(Repeatable Read) & CS(Cursor Stability) Page or Row level locks The tablespace LOCKSIZE parameter - physically DB2 locks data in terms of pages or tables or tablespaces. This parameter is specified in CREATE or ALTER Tablespace option LOCKSIZE. The options are Tablespace, Table, Page or Any the ACQUIRE/RELEASE parameters on the BIND PLAN command specifies when table locks(which are implicitly acquired by DB2) are to be acquired and released. Types : ACQUIRE USE & ACQUIRE ALLOCATE RELEASE USE & RELEASE ALLOCATE
167
168
3.8. DB2 Security, Catalog tables and Optimizer : Catalog tables and DB2 directory
DB2 catalog is integrated - DB2 catalog and DB2 DBMS are inherently bound together It is nonsubvertible - DB2 catalog cannot be updated behind DB2s back. i.e. if a table of 10 columns is created, it is not possible to go and change the number of columns directly on the catalog to 15. It has to be done using the standard SQL statements for dropping and recreating the table
169
171
Session 6 : Summary
Running a DB2 application program is by executing with the PLAN with the link edited module. Utilities Check - Checks the referential integrity between two tables and also checks DB2 indexes for consistency Copy -Used to create an imagecopy for the complete tablespace or a partition of the tablespace - full imagecopy or incremental imagecopy Recover -Restore DB2 tablespaces and indexes to a specific instance Load - To accomplish bulk inserts into DB2 table Reorg - To reorganize DB2 tables and indexes and thereby improving their efficiency of access Runstats - Collects statistical information for DB2 tables, tablespaces, partitions, indexes, and columns. Explain - This feature can be detail the access paths chosen by the DB2 optimizer for SQL statements
Copyright 2004, Cognizant Academy, All Rights Reserved 172
Session 6 : Summary
Locking services are provided by the IRLM - page or row level locks. Catalog tables -Repository for all DB2 tables.
173
DB2 Basic