Vous êtes sur la page 1sur 175

DB2

<Basic>

Courseware Metadata
Created By: Credential Information: Version and Date: Shyamala P 102986 4 years with Mainframe 1.0

Copyright 2004, Cognizant Academy, All Rights Reserved

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

Copyright 2004, Cognizant Academy, All Rights Reserved

Course Objective & Outline


Course Objective
After completion of this course, you will be able to 1. get a good understanding of DB2 2. develop applications using DB2 (using COBOL)

Course Outline
1. Introduction to DB2 2. Querying database and other DB2 objects 3. Application Programming

Copyright 2004, Cognizant Academy, All Rights Reserved

Course Objective & Outline


1. Introduction to DB2
1.1 Database & RDBMS concepts 1.2 DB2 Overview 1.3 DB2 objects and Data types 1.4 Introduction to SQL 1.5 Tools QMF & SPUFI 1.6 SQL DDL 1.7 SQL DCL

Copyright 2004, Cognizant Academy, All Rights Reserved

Course Objective & Outline


2. Querying database and other DB2 objects
2.1 SQL DML (Simple Select) 2.2 Functions 2.3 Sorting & Grouping records 2.4 Joins & Subqueries 2.5 Correlated Subquery & Union 2.6 SQL DML(Update & Delete) 2.7 Indexes 2.8 View , Alias & Synonyms

Copyright 2004, Cognizant Academy, All Rights Reserved

Course Objective & Outline


3. Application Programming
3.1 Coding the application
Host Variables Cursors

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

1.0 Introduction to DB2: Overview


Introduction
This module introduces the listener to basic database and RDBMS concepts. This provides an overview of DB2 , its associated objects and data types available. This module also introduces the TSO tools used to manipulate data in DB2. This module then covers SQL DDL and DCL

Objective
After completing this module, you will understand the DB2 architecture and be able to have a hands on with SQL

Copyright 2004, Cognizant Academy, All Rights Reserved

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

Copyright 2004, Cognizant Academy, All Rights Reserved

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

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. Types of Database System Hierarchical Network Relational
Copyright 2004, Cognizant Academy, All Rights Reserved 10

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

Copyright 2004, Cognizant Academy, All Rights Reserved

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

Sol dTo Invoices

I#

Dat e Relational Database

C#

Am t

Copyright 2004, Cognizant Academy, All Rights Reserved

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

1.1 Database & RDBMS concepts: Relational Properties & Terminology


Relation is a mathematical term for a table - Hence Relational database is perceived by the users as a set of tables.
All data values are atomic. Entries in columns are from the same domain Sequence of rows (T-B) is insignificant Each row is unique Sequence of columns (L-R) is insignificant

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

1.1 Database & RDBMS concepts: Relational Properties & Terminology


PRIMARY KEY - Column used to uniquely identify a Tuple COMPOSITE KEY A combination of columns used to identify a unique row. Combination of customer name and address can consider to be the composite key of the customer table. ALTERNATE KEY Column or combination of columns which has unique values but not selected as primary key and is not part of the primary key. FOREIGN KEY Column of a table used to establish relationship with other tables and present itself in all the relation tables.

Copyright 2004, Cognizant Academy, All Rights Reserved

16

1.1 Database & RDBMS concepts: E-R Model


Entity-Relationship model is a logical representation of data for a business area Represented as entities, relationship between entities and attributes of both relationships and entities E-R models are outputs of analysis phase i.e they are conceptual data models expressed in the form of an E-R diagram

Copyright 2004, Cognizant Academy, All Rights Reserved

17

1.1 Database & RDBMS concepts: Normalization (1NF 5NF)


Normalization is done to bring the design of database to a standardized mode 1NF : All entities must have a unique identifier, or key, that can be composed of one or more attributes. All attributes must be atomic and non repeating. 2NF : Partial functional dependencies removed - all attributes that are not a part of the key must depend on the entire key for that entity. 3NF : Transitive dependencies removed - attributes that are not a part of the key must not depend on any non-key attribute. 4NF : Multi valued dependencies removed 5NF : Remaining anomalies removed
Copyright 2004, Cognizant Academy, All Rights Reserved 18

1.1 Database & RDBMS concepts: Types of Integrity


Entity Integrity: Rule states that no column that is part of a primary key can have a null value Referential Integrity: Rule states that every foreign key in the first table must either match a primary key value in the second table or must be wholly null Domain Integrity: Integrity of information allowed in column

Copyright 2004, Cognizant Academy, All Rights Reserved

19

1.1 Database & RDBMS concepts: Relational Example


CUSTOMER Places ORDERS ORDERS Has PRODUCTS The above example can be interpreted as follows:
A Customer can place any number of orders (one-to-many) Each order relates to only one customer (one-to-one) One order can contain many products (one-to-many) One Product can be a part of many orders(one-to-many) In the above example Customer, Order & Product are called ENTITIES An Entity may transform into table(s) The unique identity for information stored in an ENTITY is called a PRIMARY KEY. Eg. Customer-No uniquely identifies each customer A table essentially consists of Attributes, which define the characteristics of the table Primary key, which uniquely identifies each row of data stored in a table Secondary & Foreign Keys/indexes
Copyright 2004, Cognizant Academy, All Rights Reserved 20

1.1 Database & RDBMS concepts: Relational Example cntd..


Hence the Table Definition: Table - Customer Attributes - Customer-No, Cust-name, Cust-location, Cust-Id, Order-no... Primary Key - Customer-No Secondary Key - Cust-Id Foreign-Key - Order-no The Relationships transform into Foreign Keys. For eg. Customer is related to Orders thru Order-No which is the Foreign-key in Customer and Primary key in Order. So basically the relationship Places is thru the Order-No. As per the relational integrity the Primary-Key ,Order-No, for the table Orders can never be Null, while it can be so in the table Customer

Copyright 2004, Cognizant Academy, All Rights Reserved

21

1.2 DB2 Overview : DB2 Internal structure


CICS TSO IMS System Services
Active Logs

Relational Data Systems


DB2 Utilities

DB2 Optimizer

Data Manager Buffer Manager

D D F

Buffer Pools

VSAM Media Manager


DB2 Table Data
DB2 Directory

IRLM

DB2 Catalog
22

Copyright 2004, Cognizant Academy, All Rights Reserved

1.2 DB2 Overview : DB2 Internal components


The Four major components of the complex DB2 internal structure are : 1) System services 2) Locking services 3) Database services 4) Distributed data facility SYSTEM SERVICES Controlling connections to other MVS subsystems like CICS, IMS/DC, TSO Handles system start-up, shutdown and operator communication Managing the system log, which records the information necessary for recovering user and system data in case of a system failure. When the active log dataset becomes full the system shifts to a new dataset and copies the old data to archive log. Information regarding the log datasets is recorded on a system dataset called the Bootstrap dataset
Copyright 2004, Cognizant Academy, All Rights Reserved 23

1.2 DB2 Overview : DB2 Internal components


LOCKING SERVICES Provides necessary controls for managing concurrent access to data using the IRLM. IRLM- IMS resource lock manager. It is an MVS subsystem and a general-purpose lock manager that aids in maintaining data integrity. DATABASE SERVICES Supports the definition, retrieval and update of DB2 data using a series of six sub components 1) Pre compiler 2) Bind 3) Runtime supervisor 4) Database manager 5) Buffer manager 6) Utilities
Copyright 2004, Cognizant Academy, All Rights Reserved 24

1.2 DB2 Overview : DB2 Internal components


Precompiler- which is a pre-processor for the host programming language. Produces a DBRM Bind -Bind a given DBRM to produce what is known as Plan. Bind has two main functions syntax checking and optimisation. Runtime supervisor- when the program executes first time the control goes to the runtime supervisor. It oversees execution and makes requests to the Data Manager Data manager- Does all the operations such as the search, retrieval, update index maintenance etc.It also manages the physical databases.It invokes the system components as necessary in order to perform detailed functions such as locking, logging, i/o operations etc. Buffer manager- Component responsible for the physical transfer of data between external storage and virtual memory. It uses techniques such as read-ahead buffering and look-aside buffering to get the best performance of the buffer pools and to minimize the amount of physical I/O required.
Copyright 2004, Cognizant Academy, All Rights Reserved 25

1.2 DB2 Overview : DB2 Internal components


The database services component also maintains certain control and descriptor information regarding the database tables and their columns, database backup operations. This information is divided into two groups, which is namely catalogue and the directory. The catalogue consists of regular tables and is accessible by means of sql statements. The directory cannot be accessed by SQL statements and it is for the internal use of the DB2. Both contain the same information. Distributed Data Facility Distributed DB2 requests are carried out through DDF. Enables database access by remote systems.

Copyright 2004, Cognizant Academy, All Rights Reserved

26

1.3 DB2 objects and Data types : STOGROUP


STOrage GROUP It is a collection of direct access volumes, all of the same device type The option is defined as a part of tablespace definition When a given space needs to be extended, storage is acquired from the appropriate stogroup Stogroup and user-defined VSAM are the two storage allocations for a DB2 dataset definition.

STORAGE GROUP DATA BASE(S) TABLESPACE(S) TABLE(S) VIEW(S)

INDEX

Copyright 2004, Cognizant Academy, All Rights Reserved

27

1.3 DB2 objects and Data types : Database


DATABASE It is a collection of logically related objects - like Tablespaces, Indexspaces, Tables etc. A STOGROUP & BUFFERPOOL (is buffer area used to maintain recently accessed table and index pages) must be defined for each database. A database may occupy more than one disk space In a given database, all the spaces need not have the same stogroup More than one volume can be defined in a stogroup. DB2 keeps track of which volume was defined first & uses that volume
Copyright 2004, Cognizant Academy, All Rights Reserved 28

1.3 DB2 objects and Data types : Tablespaces


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 and reorganizing purpose Three Type of Tablespaces - Simple, Partitioned & Segmented Simple Tablespace Can contain more than one stored table Depending on application, storing more than one Table might enable faster retrieval for joins using these tables Usually only one table is preferred. This is because a single page can contain rows from all tables defined in the database. LOAD with replace option deletes all data
Copyright 2004, Cognizant Academy, All Rights Reserved 29

1.3 DB2 objects and Data types : Tablespaces


Segmented Tablespace Can contain multiple tables.The tables are stored in different segments. A Segment consists of a logically contiguous set of n pages. Segsize parameter decides the allocation size for the tablespace No segment is allowed to contain records for more than one table Sequential access to a particular table is more efficient Mass Delete is much more efficient than in any other Tablespace Reorganizing the tablespace will restore every table to its clustered order Lock Table on table, locks only the table, not the entire tablespace If a table is dropped, the space for that table can be reclaimed with minimum reorg

Copyright 2004, Cognizant Academy, All Rights Reserved

30

1.3 DB2 objects and Data types : Tablespaces


Partitioned Tablespaces Primarily used for very large tables Only one table in a partitioned TS; 1 to 64 partitions/TS Numpart parameter specifies the no. of partitions It is partitioned in accordance with value ranges for single or a combination of columns. Hence these column(s) cannot be updated Individual partitions can be independently recovered and reorganized Different partitions can be stored on different storage groups for efficient access.

Copyright 2004, Cognizant Academy, All Rights Reserved

31

1.3 DB2 objects and Data types : Tablespace Parameters


LOCKSIZE - indicates the type of locking DB2 performs for the given TS
Page Table Tablespace ANY - DB2 decides the starting page

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

1.3 DB2 objects and Data types : Tablespace Parameters


VCAT option User Defined VSAM datasets have to be defined explicitly by the AMS utility IDCAMS Two types of VSAM datasets are used -ESDS & LDS. Linear Data set is more efficiently used by DB2 VSAM datasets defined here are different from the plain VSAM datasets can access them only through VSAM Media Manager

Copyright 2004, Cognizant Academy, All Rights Reserved

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

1.3 DB2 objects and Data types : Datatypes


Da a T e CHAR(n ARCHAR(n SMALLINT INTEGER ECIMAL(p ) te a Le th n a =n+2 orki torage C X(n PIC S9(4 COMP PIC X(n PIC S 9(4 COMP PIC S9(9) COMP PIS S9(p)V9(s) COMP-3 COMP-2 COMP-1 PIC X(10) PIC X(8) PIC X(26)
35

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

1.3 DB2 objects and Data types : Default Values

Data Type Numeric Fixed length String Date Time Time Stamp

Default Value Zero Blanks Current Date Current Time Current Timestamp

Varying length String String of length Zero

Copyright 2004, Cognizant Academy, All Rights Reserved

36

1.3 DB2 objects and Data types : Null Value


Null - records missing or unknown information. Either the Column does not apply for the row(inapplicable data) or the value does not exist currently(unknown data). Represented by special Indicator Variables. An Indicator variable is provided for each column that allows nulls. This is transparent to the user but must be provided during programming. Default being NULL, nulls can be prohibited by specifying NOT NULL or NOT NULL WITH DEFAULT during column definition.

Copyright 2004, Cognizant Academy, All Rights Reserved

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

Copyright 2004, Cognizant Academy, All Rights Reserved

38

Session 1: Summary cntd.


The Four major components of the complex DB2 internal structure are: 1) System services 2) Locking services 3) Database services 4) Distributed data facility

The basic DB2 objects are: 1) STOGROUP 2) DATABASE 3) TABLESPACES- Simple, Segmented and Partitioned 4) TABLES, VIEWS & INDEXES

Copyright 2004, Cognizant Academy, All Rights Reserved

39

Session 1 : Summary cntd.


The basic data types are: CHAR VARCHAR SMALLINT INTEGER DECIMAL(p,s) FLOAT DATE,TIME,TIMESTAMP Null - missing or unknown information

Copyright 2004, Cognizant Academy, All Rights Reserved

40

Session 2 : Overview
Introduction to what is SQL. The tools used to manipulate DB2 online QMF & SPUFI SQL DCL and DDL

Copyright 2004, Cognizant Academy, All Rights Reserved

41

1.4.1 Introduction to SQL : Features


SQL or Structured Query Language is A Powerful language that performs the functions of data manipulation(DML), data definition(DDL) and data control or data authorization(DAL/DCL). A Non procedural language - the capability to act on a set of data and the lack of need to know how to retrieve it. An SQL can perform the functions of more than a procedure. The De Facto Standard query language for RDBMS Very flexible SQL - Features Unlike COBOL or 4GLs, SQL is coded without data-navigational instructions. The optimal access paths are determined by the DBMS. This is advantageous because the database knows better how it has stored data than the user. What you want and not how to get it Set level processing & multiple row processing
Copyright 2004, Cognizant Academy, All Rights Reserved 42

1.4.1 Introduction to SQL : Operations


Selection & Projection Select retrieves a specific number of rows from a table Projection operation retrieves a specified subset of columns(but all rows) from the table Projection E.g.. : SELECT CUST_NO, CUST_NAME FROM CUSTOMER WHERE OFFICE_CD IN (02, 04)

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

1.4.2 Introduction to SQL : Tools


Query Management Facility (QMF) It is an MVS- and VM- based query tool It allows end users to enter SQL queries to produce a variety of reports and graphs as a result of this query QMF queries can be formulated in several ways : by direct SQL stmts, by means of relational prompted query interface or by query-by-example (QBE). QBE is similar to SQL in some ways but more user friendly SPUFI It supports the online execution of SQL statements from a TSO terminal Used for developers to check SQL statements or view table details Spufi menu contains the input file in which the SQL statements are coded, option for default settings and editing and the output file. Enter option 8.1 on the Cognizant m/f main menu. Provide the input/output datasets.
Copyright 2004, Cognizant Academy, All Rights Reserved 44

1.4.3 Introduction to SQL : DDL


Data Definition Language Create Table Syntax: CREATE TABLE Table_name ( Col Name Datatype,) PRIMARY KEY(Columns) FOREIGN KEY Name (Column) REFERENCES dbname.tablename ON DELETE (SET NULL/CASCADE) UNIQUE (Col name) (referential constraint) 1. LIKE Table name / View name 2. IN Database Tablespace Name Foreign Key references dbname.table on relation condition for delete.
Copyright 2004, Cognizant Academy, All Rights Reserved

SQL

DDL

DML

DCL

CREATE ALTER DROP

INSERT SELECT UPDATE DELETE

GRANT REVOKE

45

1.4.4 Introduction to SQL : DDL


Table1 references table2(target) - Table2s Primary key is the foreign key defined in Table1 The Conditions are CASCADE, RESTRICT & SET NULL (referential constraint for the foreign key definition) Inserting (or updating ) rows in the target is allowed only if there are no rows in the referencing table

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

1.4.5 Introduction to SQL : DDL Rules


Some general rules for RI & Table Parameters Avoid nulls in columns participating in Arithmetic logic or comparisons Primary key cols cannot be nulls Limit referential structures to no more than three levels in a direction Use DB2s inherent features rather than program coded RIs. Do not use RIs on tables built from another RI system Consider using Fieldprocs or Editprocs or Validprocs

Copyright 2004, Cognizant Academy, All Rights Reserved

47

1.4.6 Introduction to SQL : DCL


GRANT & REVOKE GRANT : grants the table privileges, plan & package privileges, collection privileges, database privileges, use privileges and system privileges user with a SYSADM privilege will be responsible for overall control of the system The table privileges allowed are SELECT, UPDATE, DELETE, INSERT, (both base tables & views), ALTER(Table) & (Create)INDEX(only to base tables) There are no specific DROP privilages;the table can be dropped by its owner or a SYSADM Format of GRANT : GRANT SELECT, UPDATE(NAME,NO) ON TABLE EMPL TO A, B, C(or PUBLIC); GRANT ALL ON EMPL TO PUBLIC; GRANT EXECUTE ON PLAN PLANA TO USER; A user having authority to grant privilege to another, also has the authority to grant the privilage with with the GRANT Option
Copyright 2004, Cognizant Academy, All Rights Reserved 48

1.4.6 Introduction to SQL : DCL


REVOKE : this stmt revokes the privileges given to a user. The user granting the privileges has the authority to REVOKE also. It is not possible to be column specific when revoking an UPDATE privilege REVOKE SELECT ON TABLE EMPL FROM USERA;

Copyright 2004, Cognizant Academy, All Rights Reserved

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

Copyright 2004, Cognizant Academy, All Rights Reserved

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

2.1 SQL DML


INSERT : Eg: INSERT INTO Tablename(column1, column2, column3 ,......) VALUES( value1, value2, value3 ,........)

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

2.1. SQL DML


SELECT: SELECT column_list FROM table_name {WHERE search_condition } - optional {GROUP BY grouping cols } - optional {HAVING group search } - optional {ORDER BY sort_order } - optional The search conditions can use the following Comparison Operators Logical Connectors

Copyright 2004, Cognizant Academy, All Rights Reserved

54

2.1. SQL DML


Comparison Operators The following operators are used for comparisons. The comparisons that can take place are a) Numeric data can only be compared with numeric data, data conversion from one form to other might be done to facilitate the operation b) Character data is compared character by character from left to right. The collating sequence used for comparison is EBCDIC. = EQUAL TO ~= NOT EQUAL TO < LESS THAN > GREATER THAN <= LESS THAN AND EQUAL TO >= GREATER THAN AND EQUAL TO
Copyright 2004, Cognizant Academy, All Rights Reserved 55

2.1. SQL DML


Logical Connectors
Used to obtain logical TRUE values. These must not be used with logical operators. These are used to specify more than one criteria in the search clause.

AND OR NOT

Copyright 2004, Cognizant Academy, All Rights Reserved

56

2.1. SQL DML


Simple Select : Single Row Select SELECT EMPNO,FIRSTNME,MIDINIT,LASTNAME,WORKDEPT FROM DSN8710.EMP WHERE EMPNO = '000010

000010

IRS CHRIS TINE

I I I I

S HA A S

R A 00

Copyright 2004, Cognizant Academy, All Rights Reserved

57

2.1. SQL DML


Multi Row select SELECT EMPNO,FIRSTNME,MIDINIT,LASTNAME,WORKDEPT FROM DSN8710.EMP WHERE MIDINIT = 'R'
M 1 0 28 0 290 0 20 0 28 0 0 20 0 R M E L I A E TH E TH E L OHN ASON E IL E E N RO M R R R R R R W R P IA N K A D11 S C H N E ID E R E 1 1 PARKER E 11 O NOT E 21 S CHW A RT E 11 A LO N O E 21 M

Copyright 2004, Cognizant Academy, All Rights Reserved

58

2.1. SQL DML


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

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

2.1. SQL DML


(LIKE/Not LIKE) SELECT EMPNO,FIRSTNME,MIDINIT,LASTNAME,WORKDEPT FROM DSN8710.EMP WHERE FIRSTNME LIKE 'J%' WITH UR; Note :- _ for a single char ; % for a string of chars Escape \ - escape char;if precedes _ or % overrides their meaning

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

N E E ER E 01 W A LK E R D11 LUT D11 JE F F E R S O N D 1 PARKER E 11 OUNOT E 1

Copyright 2004, Cognizant Academy, All Rights Reserved

60

2.1. SQL DML


(NULL/NOT NULL) : To check for null the syntax is IS NULL or IS NOT NULL. SELECT * FROM DSN8710.DEPT WHERE MGRNO IS NULL ; Remember NULL is not SPA CES
TNO D01 F G H I TN D E E L O P M E N T C E N TE R R AN C H O F F IC E F R AN C H O F F IC E G R AN C H O F F IC E H R AN C H O F F IC E I R AN C H O F F IC E NO A0 0 E 01 E 01 E 01 E 01 E 01 T

Copyright 2004, Cognizant Academy, All Rights Reserved

61

2.2. Functions : Type


Types of Functions are : Aggregate (Column) Function Scalar Function Arithmetic function

Copyright 2004, Cognizant Academy, All Rights Reserved

62

2.2. Functions : Aggregate(Column)


Compute from a group of rows aggregate value for a specified column(s) AVG, COUNT, MAX, MIN, SUM
SELECT AVG(SALARY) A00_AVGSAL FROM DSN8710.EMP WHERE WORKDEPT = 'A00'

Try the others

A00_AVGSAL

Copyright 2004, Cognizant Academy, All Rights Reserved

63

2.2. Functions : Scalar


Are applied to a column or expression and operate on a single value. CHAR, DATE, DAY(S), DECIMAL, DIGITS, FLOAT, HEX, HOUR, INTEGER, LENGTH, MICROSECOND, MINUTE, MONTH, SECOND, SUBSTR, TIME, TIMESTAMP, VALUE, VARGRAPHIC, YEAR
SELECT SUBSTR(DEPTNAME,1,7) FROM DSN8710.DEPT

Rules for Functions - Refer handout

Copyright 2004, Cognizant Academy, All Rights Reserved

64

2.2. Functions : Arithmetic


The Valid symbols are -*/()
SELECT BONUS+COMM VARIABLE_COMP FROM DSN8710.EMP WHERE WORKDEPT = 'A00' SELECT EMPNO, (SALARY/12) MONTHLY_GROSS FROM DSN8710.EMP WHERE WORKDEPT = 'A00'

V ARIABLE_CO M P 5220.00 4620.00 2940.00 5220.00 2940.00

EMPNO 000010 000110 000120 200010 200120

MONTHLY_GROSS 4395.83333333 3875.00000000 2437.50000000 3875.00000000 2437.50000000

Copyright 2004, Cognizant Academy, All Rights Reserved

65

2.2. Functions : Duplicates Elimination


Duplicates are eliminated using DISTINCT
SELECT DISTINCT WORKDEPT FROM DSN8710.EMP

W O R K D EP T A00 B01 C01 D11 D21 E01 E11 E21

Copyright 2004, Cognizant Academy, All Rights Reserved

66

2.3. Sorting and Grouping Records : Sorting


The result set from the query (with a where clause) is sorted based column(s) in a specific order using the ORDER BY clause SELECT EMPNO,FIRSTNME,MIDINIT,LASTNAME,WORKDEPT FROM DSN8710.EMP ORDER BY WORKDEPT( or 5) ASC

Default is ASC.

Copyright 2004, Cognizant Academy, All Rights Reserved

67

2.3. Sorting and Grouping Records : Grouping


Used when aggregate functions are used along with regular columns. Group by operator causes the table represented by the FROM clause to be rearranged into groups, such that within one group all rows have the same value for the Group by column (not physically in the database). The Select clause is applied to the grouped data and not to the original table. Here HAVING is used to eliminate groups, just like WHERE is used for rows.

Copyright 2004, Cognizant Academy, All Rights Reserved

68

2.3. Sorting and Grouping Records : Grouping


Examples :1) Find the Max salary in each department among employees hired after 1 65-01-01
SELECT WORKDEPT DEPARTMENT,MAX(SALARY) MAX_SALARY FROM DSN8710.EMP WHERE HIREDATE > '1965-01-01' GROUP BY WORKDEPT

Where is used on a column Having is used on aggregate data

Copyright 2004, Cognizant Academy, All Rights Reserved

69

2.3. Sorting and Grouping Records : Grouping


2) Find the Max salary among employees hired after 1 65-01-01 in each department that has Max Salary more than 30,000.
SELECT WORKDEPT DEPARTMENT,MAX(SALARY) MAX_SALARY FROM DSN8710.EMP WHERE HIREDATE > '1965-01-01' GROUP BY WORKDEPT HAVING MAX(SALARY) > 30000

Copyright 2004, Cognizant Academy, All Rights Reserved

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

Copyright 2004, Cognizant Academy, All Rights Reserved

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

Copyright 2004, Cognizant Academy, All Rights Reserved

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

Copyright 2004, Cognizant Academy, All Rights Reserved

73

2.4. Complex SQLS : Joins and Sub queries


One terms a SQL to be complex when data that is to be retrieved comes from more than one table A Complex SQL contains one or both of Joins Sub queries

Copyright 2004, Cognizant Academy, All Rights Reserved

74

2.4. Complex SQLS : Joins


Data can be selected from multiple tables by joining the tables on common columns Data is retrieved based on matching key values(Join Criteria) SELECT tableA.column3, tableA.column4, tableB.column1, tableB.column3 tableA, tableB tableA.primary = tableB.foreign optional search criteria

FROM WHERE AND

Copyright 2004, Cognizant Academy, All Rights Reserved

75

2.4. Complex SQLS : Joins examples


Find the Department details of the Employee 000060. SELECT A.EMPNO, A.FIRSTNME, A.LASTNAME, A.WORKDEPT, B.DEPTNAME, B.MGRNO FROM DSN8710.EMP A, Table Aliases DSN8710.DEPT B WHERE A.WORKDEPT=B.DEPTNO AND A.EMPNO = '000060'

000060

T IRVING

A T A STERN

T D11

T A MANUFACTURING 000060

Copyright 2004, Cognizant Academy, All Rights Reserved

76

2.4. Complex SQLS : Types of Joins


OUTER JOIN For one or more tables being joined, both matching and non matching rows are returned. Duplicate columns may be eliminated The non matching columns will have nulls in them.
SELECT A.EMPNO,A.FIRSTNME,A.LASTNAME,A.WORKDEPT,B.DEPTNAME,B.MGRNO FROM DSN8710.EMP A FULL OUTER JOIN DSN8710.DEPT B ON A.WORKDEPT=B.DEPTNO ORDER BY 1

Copyright 2004, Cognizant Academy, All Rights Reserved

77

2.4. Complex SQLS : Outer Joins


Useful when we want rows that have the matching values and also the ones that dont have matching rows in the other table. LEFT - All rows in first table plus matching rows in 2nd table. RIGHT - All rows in 2nd table plus matching rows in 1st FULL - All rows in 1st and 2nd table plus matching rows .

Copyright 2004, Cognizant Academy, All Rights Reserved

78

2.4. Complex SQLS : Inner Joins


INNER JOIN (Default) Here there is a possibility one or more of the rows from either or both tables being joined will not be included in the table that results from the join operation

Copyright 2004, Cognizant Academy, All Rights Reserved

79

2.4. Complex SQLS : Sub queries


Nested select statements specified using the IN(or NOT IN) predicate, equality or non-equality predicate(= or <>) and comparative operator(<, <=, >, >=) When using the equality, non-equality or comparative operators, the inner query should return only a single value Correlated & Non Correlated Sub queries Usage EXISTS clause

Copyright 2004, Cognizant Academy, All Rights Reserved

80

2.4. Complex SQLS : Sub queries


Locate the Project information of Employee whose first name is Christine
SELECT PROJNO , PROJNAME FROM DSN8710.PROJ WHERE RESPEMP =( SELECT EMPNO FROM DSN8710.EMP WHERE FIRSTNME = 'CHRISTINE')

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

2.4. Complex SQLS : Sub queries


The nested loop statements gives the user the flexibility for querying multiple tables Noncorrelated Sub query as above works in Bottom-to-Top fashion A join is always preferred over a Sub query . Eg. Query1 of above can be re written as:
Locate the Project information of Employee whose first name is Christine

SELECT PROJNO , PROJNAME FROM DSN8710.PROJ A, DSN8710.EMP B WHERE B.EMPNO= A.RESPEMP AND B.FIRSTNME = 'CHRISTINE'

Copyright 2004, Cognizant Academy, All Rights Reserved

82

2.5. Correlated Sub queries And Union : Correlated Sub queries


A specialized form is Correlated Sub query - the nested Select statement refers back to the columns in previous select statements It works on Top-Bottom-Top fashion Get the Employee details of all Managers SELECT FIRSTNME, SALARY, WORKDEPT FROM DSN8710.EMP A WHERE WORKDEPT IN (SELECT DEPTNO FROM DSN8710.DEPT B WHERE B.MGRNO = A.EMPNO)

Copyright 2004, Cognizant Academy, All Rights Reserved

83

2.5. Correlated Sub queries And Union : Correlated Sub queries


Correlated sub queries using EXIST clause : Checks for the existence of a row matching the criteria. The columns values do not matter List all employees who are responsible for at least one project. SELECT EMPNO FROM DSN8710.EMP A WHERE EXISTS ( SELECT 1 FROM DSN8710.PROJ P WHERE P.RESPEMP = A.EMPNO) Similarly a NOT EXISTS is used for checking the negative value and is preferred over a NOT IN when column values dont matter.
Copyright 2004, Cognizant Academy, All Rights Reserved 84

2.5. Correlated Sub queries And Union : Correlated Sub queries


Multiple levels of sub query
SELECT Cust-no, Cust-name, Cust-addr FROM CUSTOMER A WHERE Order-no IN (SELECT order-no FROM ORDER B WHERE Prod-id IN (SELECT Prod-id FROM PRODUCTS WHERE Prod-name = NUTS));

Copyright 2004, Cognizant Academy, All Rights Reserved

85

2.5. Correlated Sub queries And Union : Union


The UNION combines 2 sets of rows into a single set composed of all the rows in either or both of the original sets. The two original sets must be union compatible. For union compatibility: The two sets must contain the same no of columns Each column of the first set must be either the same data type as the corresponding column of the second or must be convertible. UNION retrieves distinct records.

Copyright 2004, Cognizant Academy, All Rights Reserved

86

2.5. Correlated Sub queries And Union : Correlated Union


Example for Union SELECT EMPNO FROM DNS8710.EMP WHERE LASTNME = HAAS UNION SELECT EMPNO FROM DSN8710.EMP WHERE JOB= PRES;

Copyright 2004, Cognizant Academy, All Rights Reserved

87

2.5. Correlated Sub queries And Union : Correlated Union


The union can be replaced by an OR SELECT EMPNO FROM DSN8710.EMP WHERE LASTNME = HAAS OR JOB = PRES But the union would suppress any duplicates If the two selects were from different tables , the OR would not be possible.

Copyright 2004, Cognizant Academy, All Rights Reserved

88

2.6 SQL DML : Update


UPDATE Eg: UPDATE tablename SET Columnname(s) = scalar expression (,) WHERE [ condition ]

Single or Multiple row updates Update with a Subquery

Copyright 2004, Cognizant Academy, All Rights Reserved

89

2.6. SQL DML : Delete


DELETE Eg: DELETE FROM Tablename WHERE [condition ];

Single or multiple row delete Deletion of all rows

Copyright 2004, Cognizant Academy, All Rights Reserved

90

2.7. Indexes : Parameters


Index parameters for creation CLUSTER USING STOGROUP/VCAT (the corresponding name) FREEPAGE PCTFREE PRIQTY / SECQTY BUFFERPOOL CLOSE - Yes/No ERASE Yes/No

Copyright 2004, Cognizant Academy, All Rights Reserved

91

2.7. Indexes : Index Guidelines What to do


Syntax CREATE INDEX <indexname> ON <tablename> (colname asc/desc) 1. Consider indexing on columns used in UNION, DISTINCT, GROUP BY, ORDER BY & WHERE clauses. 2. Limit the indexing of frequently updated columns 3. Create explicitly, a clustering index 4. Create a unique index on the primary key and indexes on foreign keys 5. Overloading of index when row length of a table to be accessed is short 6. Atleast one index must be defined for a table with more than 100 pages

Copyright 2004, Cognizant Academy, All Rights Reserved

92

2.7. Indexes : Index Guidelines - What to do


7. Use Multicolumn index rather than a multi-index (appln dependent); however the latter requires more DASD 8. Create indexes before loading the table . Clustering reduces I/O; DB2 optimizer usually tries to use an index on clustered column before using the other indexes 10.Specify Indexspace freespace the same as tablespace freespace 11.Use the DEFER option while creating the index. RECOVER INDEX utility can then be used to populate the index. Recover utility populates index entries faster 12.Use different STOGROUPs for Tablespaces & indexspaces 13.Create Critical indexes in a different bufferpool than the tablespaces

Copyright 2004, Cognizant Academy, All Rights Reserved

93

2.7. Index Guidelines What not to do


1. Avoid indexing on Variable columns 2. Limit the number of indexes on partitioned TS 3. Avoid indexes if the table is very small (< 10 pages) it has heavy inserts and deletes and is relatively small (< 20 pages) it is accessed with a scan. 4. Avoid defining redundant indexes

Copyright 2004, Cognizant Academy, All Rights Reserved

94

2.8. Views, Alias, Synonyms : Views


Views are a logical derivation of a table from other table/tables. A View does not exist in its own right. They provide a certain amount if logical independence They allow the same data to be seen by different users in different ways In DB2 a view that is to accept a update must be derived from a single base table Syntax: CREATE VIEW <Viewname> (<columns>) AS Subquery (Subquery - SELECT FROM other Table(s))
Copyright 2004, Cognizant Academy, All Rights Reserved 95

2.8. Views, Alias, Synonyms : Aliases


Mean another name for the table. Aliases are used basically for accessing remote tables (in distributed data processing), which add a location prefix to their names. Using aliases creates a shorter name. Syntax : CREATE ALIAS <Aliasname> FOR <Tablename>

Copyright 2004, Cognizant Academy, All Rights Reserved

96

2.8. Views, Alias, Synonyms : Synonym


Synonym Also means another name for the table, but is private to the user who created it. Syntax :
CREATE SYNONYM <Synonymname> FOR <Tablename>

Copyright 2004, Cognizant Academy, All Rights Reserved

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

Copyright 2004, Cognizant Academy, All Rights Reserved

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.

Copyright 2004, Cognizant Academy, All Rights Reserved

99

Session 5 Overview : Application programming


Introduction Introduces the listener to The ways of coding host applications with DB2 statements The process of compiling and executing these host language programs DB2 utilities and Security Objective At the end of this session , the listener should be able to code and execute DB2 application programs. Should have knowledge on the DB2 utilities available.
Copyright 2004, Cognizant Academy, All Rights Reserved 100

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

Copyright 2004, Cognizant Academy, All Rights Reserved

101

3.1. Coding the Application : Application programming using DB2


Ad hoc SPUFI QMF Platinum DB2 Relational Data Base Management System (RDBMS) DB2 Databases

TSO

CICS

BATCH

IMS

Copyright 2004, Cognizant Academy, All Rights Reserved

102

3.1. Coding the Application : Static SQL


Hard-coded into an application program Cannot be modified during the programs execution except for changes to the values assigned to the host variables Cursors are used to access set-level data The general form is EXEC SQL [SQL stmts] END-EXEC.

Copyright 2004, Cognizant Academy, All Rights Reserved

103

3.1. Coding the Application : Dynamic SQL


Statements can change throughout the programs execution When the SQL is bound, the application plan or package that is created does not contain the same info as that for a static SQL program The access paths cannot be determined before execution

Copyright 2004, Cognizant Academy, All Rights Reserved

104

3.1. Coding the Application : Steps involved in creating a DB2 application


Coding the application using Host variables using Embedded SQL using Cursors issue DCLGEN command Pre compile the program Compile & Link edit the program Bind

Copyright 2004, Cognizant Academy, All Rights Reserved

105

3.1. Coding the Application : DB2 Application development stages


Original Source Program

Pre-compile

DCLGEN

Intermediate Source Program

DBRM

Compile Bind Object Module

Copyright 2004, Cognizant Academy, All Rights Reserved

106

3.1. Coding the Application : DB2 Application development stages


A
Link

Load Module

Application Plan

Execute

Copyright 2004, Cognizant Academy, All Rights Reserved

107

3.1. Coding the Application : Host Variables


SELECT Cust_No, Cust_name, Cust_addr FROM CUSTOMER INTO :H-Cust-No, :H-Cust-name, :H-Cust-addr WHERE Cust_No = :H_Cust_No; These are variables(or rather area of storage) defined in the host language to use the predicates of a DB2 table. These are referenced in the SQL stmt. A means of moving data from and to DB2 tables DCLGEN produces host variables, the same as the columns of the table . This is included in the source program.
Copyright 2004, Cognizant Academy, All Rights Reserved 108

3.1. Coding the Application : Host Variables


Host variables can be used in INTO Clause of SELECT & FETCH statements As input of SET clause of UPDATE statements AS input for the VALUES clause of INSERT statement In WHERE clause of SELECT, INSERT, UPDATE & DELETE As literals in SELECT list of a SELECT statement

Copyright 2004, Cognizant Academy, All Rights Reserved

109

3.1. Coding the Application : Embedded SQL statements


To include SQL in a COBOL program, you must code the following statements in column 12 or greater: EXEC SQL SQL Statements here . . END-EXEC. It is like the file I/O Normally the embedded SQL statements contain the host variables coded in the INTO or SELECT .... as shown above SELECT, INSERT, UPDATE & DELETE statements can be coded inline
Copyright 2004, Cognizant Academy, All Rights Reserved 110

3.1. Coding the Application : Embedded SQL


EXEC SQL INSERT INTO EMP ( EMPNO, FIRSTNME, LASTNME, WORKDEPT) VALUES ( :EMPNO, :FIRSTNME, :LASTNME :WORKDEPT) END-EXEC
Copyright 2004, Cognizant Academy, All Rights Reserved 111

3.1. Coding the Application : DCLGEN


DCLGEN Issued for a single table Prepares the structure of the table in a COBOL copybook The copybook contains a SQL DECLARE TABLE statement along with a working storage host variable definition for the table Option 8.2 on CTS M/F specify the table and output dataset name.

Copyright 2004, Cognizant Academy, All Rights Reserved

112

3.1. Coding the Application : DCLGEN


****************************************************************** * DCLGEN TABLE(DSN8710.EMP) * * LIBRARY(U101823.DCLGEN.EMP) * * LANGUAGE(COBOL) * * QUOTE * * INDVAR(YES) * * ... IS THE DCLGEN COMMAND THAT MADE THE FOLLOWING STATEMENTS * ****************************************************************** EXEC SQL DECLARE DSN8710.EMP TABLE ( EMPNO CHAR(6) NOT NULL, FIRSTNME VARCHAR(12) NOT NULL, MIDINIT CHAR(1) NOT NULL, LASTNAME VARCHAR(15) NOT NULL, . . SALARY DECIMAL(9, 2), BONUS DECIMAL(9, 2), COMM DECIMAL(9, 2) ) END-EXEC.
Copyright 2004, Cognizant Academy, All Rights Reserved 113

3.1. Coding the Application : DCLGEN


****************************************************************** * COBOL DECLARATION FOR TABLE DSN8710.EMP *

****************************************************************** 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).

Copyright 2004, Cognizant Academy, All Rights Reserved

114

3.1. Coding the Application : DCLGEN

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

Copyright 2004, Cognizant Academy, All Rights Reserved

115

3.1. Coding the Application : SQLCA


DB2 Communication Area It is a structure or collection of variables that is updated after each SQL statement executes. An application program that contains executable SQL statements must provide exactly one SQLCA. Contains important pre defined variables like SQLCODE that stores the return code after execution of each SQL statement. IF SQLCODE = 0 , successful execution IF SQLCODE > 0 , successful execution with warning IF SQLCODE < 0 , execution was not successful IF SQLCDOE = 100 , data not found
Copyright 2004, Cognizant Academy, All Rights Reserved 116

3.1. Coding the Application : SQLCA


Structure of the SQLCA (for COBOL) 01 SQLCA. 05 SQLCAID PIC X(8). 05 SQLCABC PIC S9(9) COMP. 05 SQLCODE PIC S9(9) COMP. 05 SQLERRM. : 05 SQLWARN. 10 SQLWARN0 PIC X(1). : 10 SQLWARNA PIC X(1). 10 SQLSTATE PIC X(5).
Copyright 2004, Cognizant Academy, All Rights Reserved 117

3.1. Coding the Application : Null Indicator Variables


Every nullable column should have an indicator variable. Used for Select, Insert or Update Defined in the WORKING-STORAGE as COMP variables 01 EMP-INDICATORS. 10 WORKDEPT-IND 10 PHONENO-IND

PIC S9(4) COMP. PICS9(4) COMP.

Copyright 2004, Cognizant Academy, All Rights Reserved

118

3.1. Coding the Application : Null Indicator Variables


To associate Null indicators with the host variable column code as follows: EXEC SQL SELECT EMPNO,SALARY Host Variable INTO :EMPNO, :SALARY :SALARY-IND FROM EMP Indicator WHERE EMPNO = 000010 Variable END-EXEC

Copyright 2004, Cognizant Academy, All Rights Reserved

119

3.1. Coding the Application : Null Indicator Variables


The host variable contains the value of the column when it is not null. Indicator variable contains following values to indicate a columns null status A ve value indicates Null value. A ve or 0 value indicates Not Null value. Failure to code or check for Null values gives SQLCODE 305.

Copyright 2004, Cognizant Academy, All Rights Reserved

120

3.1. Coding the Application : Null Indicator Variables


Null indicator values with corresponding host variables can be used for the following situations: SET clause of the INSERT statement VALUES clause of the INSERT statement INTO clause of the SELECT or FETCH statement.

Copyright 2004, Cognizant Academy, All Rights Reserved

121

3.1. Coding the Application : Cursors


High level language structure Cursors are advised when processing a result set. Row level processing as against set level processing by SQL. Can be likened to a pointer navigates through a set of rows returned by the embedded SQL statement. Can be used for modifying data using FOR UPDATE OF clause

Copyright 2004, Cognizant Academy, All Rights Reserved

122

3.1. Coding the Application : Cursors


The four (4) Cursor control statements are Declare : Defines the cursor, gives a unique name in which it is embedded and assigns an SQL statement. Written in the Working Storage Section in COBOL . EXEC SQL DECLARE EMPCUR CURSOR FOR SELECT Empno, Empname,Dept, Job FROM EMP WHERE Dept = 'D11' FOR UPDATE OF Job END-EXEC.
Copyright 2004, Cognizant Academy, All Rights Reserved 123

3.1. Coding the Application : Cursors


Open : Readies the cursor for row retrieval; executable statement and sometimes builds the result table. However it does not assign values to the host variables Written in the Procedure Division. EXEC SQL OPEN EMPCUR END-EXEC.

Copyright 2004, Cognizant Academy, All Rights Reserved

124

3.1. Coding the Application : Cursors


Fetch : Returns data from the results table one row at a time and assigns the value to specified host variables. Executable statement and SQLCODE = 100 indicates end of records in the result set. EXEC SQL FETCH EMPCUR INTO :Empno, :Empname, :Dept, :Job END-EXEC.

Copyright 2004, Cognizant Academy, All Rights Reserved

125

3.1. Coding the Application : Cursors


Close : releases all resources used by the cursor EXEC SQL CLOSE EMPCUR END-EXEC.

Copyright 2004, Cognizant Academy, All Rights Reserved

126

3.1. Coding the Application : Cursors


WHENEVER E.g. - For the Whenever Clause EXEC SQL WHENEVER NOT FOUND Go To Close-EMPCUR END-EXEC.

Note :- Not recommended for use in application programs

Copyright 2004, Cognizant Academy, All Rights Reserved

127

3.1. Coding the Application : Cursors


UPDATE E.g. - For the Update statement using cursors EXEC SQL UPDATE EMP Set Job = :New-job WHERE current of EMPCUR END-EXEC. The cursor should be defined with FOR UPDATE OF clause.

Copyright 2004, Cognizant Academy, All Rights Reserved

128

3.1. Coding the Application : Cursors


FOR UPDATE OF clause must contain all the columns that are included in the SET clause. Must use the WHERE CURRENT OF <Cursor name> clause (Without this all rows in the table would be updated) Cannot be used if SELECT statement contains ORDER BY DISTINCT GROUP BY JOIN UNION Aggregate Function
Copyright 2004, Cognizant Academy, All Rights Reserved 129

3.1. Coding the Application : Cursors


DELETE E.g. - For the Delete statement using cursors EXEC SQL DELETE FROM EMP WHERE current of EMPCUR END-EXEC.

Copyright 2004, Cognizant Academy, All Rights Reserved

130

3.1. Coding the Application : Commit


COMMIT Registering the current unit of work EXEC SQL COMMIT END-EXEC EXEC SQL DECLARE EMPCUR CURSOR WITHHOLD FOR SELECT.. END-EXEC

Copyright 2004, Cognizant Academy, All Rights Reserved

131

3.1. Coding the Application : Commit


Reasons to Commit : When a programs fails all changes since the previous save point are backed out. A resubmit of a failing program cases reprocessing of all records. Repeatable Read page lock , locks the tablespace and page until a COMMIT is issued. Commit should always be used with caution as they affect performance.

Copyright 2004, Cognizant Academy, All Rights Reserved

132

3.2. Precompilation : Precompiling stage


Original Source Program

Pre-compile

DCLGEN

Intermediate Source Program

DBRM

Copyright 2004, Cognizant Academy, All Rights Reserved

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

3.3. Compiling and Linking : Compiling and Link Editing Stage


Intermediate Source Program

Compile

Object Module

Link

Load Module
Copyright 2004, Cognizant Academy, All Rights Reserved 135

3.3. Compiling And Linking


Modified Precompiler COBOL output is compiled Compiled source is Link edited to an executable load module Appropriate DB2 host language interface module should also be included in the link edit step(i.e DSNALI)

Copyright 2004, Cognizant Academy, All Rights Reserved

136

3.4. Binding : Binding Stage

DBRM

Bind

Application Plan

Copyright 2004, Cognizant Academy, All Rights Reserved

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.

Copyright 2004, Cognizant Academy, All Rights Reserved

138

3.4. Binding : Types


BIND PLAN : accepts as input one or more DBRMs and outputs an application plan containing executable logic representing optimized access paths to DB2 data. BIND PACKAGE : acceps as input a single DBRM and produces a single package containing the optimized access path. The PLAN in this case contains a reference to the physical location of the package(s).

Copyright 2004, Cognizant Academy, All Rights Reserved

139

3.4. Binding : Package


It is a single bound DBRM with optimized access paths It also contains a location identifier, a collection identifier and a package identifier A package can have multiple versions, each with its own version identifier It is not executable by itself and has to be bound into a PLAN before execution

Copyright 2004, Cognizant Academy, All Rights Reserved

140

3.4. Binding : Advantages of Package


Reduced bind time can specify bind options at the programmer level versioning provides for remote data access(in version DB2 V2.3 or higher)

Copyright 2004, Cognizant Academy, All Rights Reserved

141

3.4. Binding : Parameters


ISOLATION(CS) Specifies the mode of page locking implemented by the program as it runs(isolation level). DB2 implements Page and Row level locking. Cursor Stability (CS) : Releases Read-only page lock as soon as another page is accessed. Repeatable Read (RR) : All page locks are held until released by a COMMIT . Read Stability (RS): Retrieved row or page is locked until the end of the unit of work. No other program can modify the data but new rows can be inserted. Uncommitted Read (UR) : Read without acquiring locks. Data as it is being change is visible . Advised only for Read-only purposes.

Copyright 2004, Cognizant Academy, All Rights Reserved

142

3.4. Binding : Parameters


ACQUIRE(USE) & RELEASE (COMMIT) Specifies when to Acquire and Release tablespace locks. USE : Tablespace locks are taken when tablespace is accessed. ALLOCATE : Locks are taken when the plan is first allocated. COMMIT : Locks are released at commit or rollback time. DEALLOCATE : All locks are held until the plan finishes and is deallocated.

Copyright 2004, Cognizant Academy, All Rights Reserved

143

3.4. Binding : Parameters


VALIDATE(BIND) Specifies validation strategy A validation strategy refers to the method of checking for the existence and validity of DB2 tables and DB2 access authorization. BIND : Bind fails for invalid access authority. RUN : Validated during each execution. Degrades performance.

FLAG(I) BIND command returns all information, warning, error, and completion messages.

Copyright 2004, Cognizant Academy, All Rights Reserved

144

3.4. Binding : Parameters


DEGREE(ANY) DB2 attempts to execute queries in parallel engines. Parallelism considered during optimization are : I/O : multiple read engines CPU : mutiple processor and read engines Sysplex : multiple data sharing subsystems EXPLAIN (YES) Monitoring the production access path selection made by DB2. ACTION(REPLACE) ADD : Indicates it is a new plan REPLACE : Replaces an old plan

Copyright 2004, Cognizant Academy, All Rights Reserved

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.

Copyright 2004, Cognizant Academy, All Rights Reserved

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

Copyright 2004, Cognizant Academy, All Rights Reserved

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

Copyright 2004, Cognizant Academy, All Rights Reserved

148

3.5. Execution : Execution Stage

Load Module

Application Plan

Execute

Copyright 2004, Cognizant Academy, All Rights Reserved

149

3.5. Execution : Running a DB2 Program


The link edited module is executed with the PLAN The PLAN contains optimized access path for the SQLs in the program The link edited module contains the machine code for the COBOL statements. The l.e.m is tied forever with the DBRM by the timestamp. The two have to be produced by the same precompile. At execution time the timestamp is verified for compatibility and a SQLCODE = 818 is issued when they do not match.

Copyright 2004, Cognizant Academy, All Rights Reserved

150

3.5. Execution : Running a DB2 Program


DB2 programs can be executed in 4 ways
Batch TSO Call attach CICS IMS

Running a DB2 program in TSO Batch PGM=IKJEFT01 SYSTSIN DD *


DSN SYSTEM(DSN) RUN PROGRAM (program name) PLAN (plan name) LIB ( load library) END

Copyright 2004, Cognizant Academy, All Rights Reserved

151

3.5. Execution : Running a DB2 Program


Refer to handout for JCLs for Pre-Compilation Compiling & Linking Binding Executing

Copyright 2004, Cognizant Academy, All Rights Reserved

152

3.6. Application Development Guidelines


Code modular DB2 programs and make them as small as possible Use unqualified SQL stmts;this enables movement from one environment to another(test to prodn) Never use Select* in an embedded SQL program; Use joins rather than subqueries Use WHERE clause and filter out data Use cursors when fetching multiple rows, though they add overheads Use FOR UPDATE OF clause for UPDATE or DELETE with cursor - this ensures data integrity. Use INSERTs minimally ; use LOAD utility instead of INSERT, if the inserts are not application dependent
Copyright 2004, Cognizant Academy, All Rights Reserved 153

3.7. DB2 Utilities


DB2 UTILITIES CHECK COPY, MERGECOPY RECOVER LOAD REORG, RUNSTATS EXPLAIN

Copyright 2004, Cognizant Academy, All Rights Reserved

154

3.7. DB2 Utilities : Check


Checks the integrity of DB2 data structures Checks the referential integrity between two tables and also checks DB2 indexes for consistency Can delete invalid rows and copies them to a exception table Use CHECK DATA when loading a table without specifying the ENFORCE CONSTRAINTS option or after the partial recovery of tablespaces in a referential set

Copyright 2004, Cognizant Academy, All Rights Reserved

155

3.7. DB2 Utilities : Copy


Used to create an imagecopy for the complete tablespace or a partition of the tablespace - full imagecopy or incremental imagecopy Every successful execution of COPY utility places in the table SYSIBM.SYSCOPY, at least one row that indicates the status of the imagecopy

Copyright 2004, Cognizant Academy, All Rights Reserved

156

3.7. DB2 Utilities : Mergecopy


The MERGECOPY utility combines multiple incremental image copy data sets into a new full or incremental image copy data set

Copyright 2004, Cognizant Academy, All Rights Reserved

157

3.7. DB2 Utilities : Recover


Standard unit of recovery is a Tablespace Restore DB2 tablespaces and indexes to a specific instance Data can be recovered for single pages,pages that contain I/O errors, a single partition or an entire tablespace Indexes are always recovered from the actual table data, not from image copy and log data, as in the case of tablespace recovery

Copyright 2004, Cognizant Academy, All Rights Reserved

158

3.7. DB2 Utilities : Load


To accomplish bulk inserts into DB2 table Can replace the current data or append to it .i.e. LOAD DATA REPLACE or LOAD DATA RESUME(S) If a job terminates in any phase of LOAD REPLACE the utility has to be terminated and rerun If a job terminates in any phase other than UTILINIT(which sets up and initializes the LOAD utility), the tablespace must be first restored using the full RECOVER, if LOG NO option of the LOAD was mentioned.. After the tablespace is restored, the error is to be corrected, the utility terminated and the job rerun.

Copyright 2004, Cognizant Academy, All Rights Reserved

159

3.7. DB2 Utilities : Reorg


To reorganize DB2 tables and indexes and thereby improving their efficiency of access Reclusters data, resets free space to the amount specified in the create ddl statement and deletes and redefines underlying vsam datasets for stogroup defined objects

Copyright 2004, Cognizant Academy, All Rights Reserved

160

3.7. DB2 Utilities : Runstats


Collects statistical information for DB2 tables, tablespaces, partitions, indexes, and columns. It can place this info in the catalog tables with DB2 optimizer statistics or DBA monitoring statistics or with all statistics that have been gathered It can be used on specific SQL queries without updating the current usable statistics

Copyright 2004, Cognizant Academy, All Rights Reserved

161

3.7. DB2 Utilities : Reorg job streams


The total reorg schedule should include A RUNSTATS job or step : to record current tablespace and index statistics to DB catalog Two copy steps for each tablespace being reorganized : so that data is recoverable. The second copy job is required after the REORG if it was performed with a LOG NO option After a REORG is run with LOG NO option, DB2 turns on the copy pending status flag for tablespaces specified in the REORG. When LOG NO parameter is specified it is better to take a imagecopy of the tablespace being reorganized immediately after reorg a REBIND job for all plans using tables in any of the tblspaces being organized
Copyright 2004, Cognizant Academy, All Rights Reserved 162

3.7. DB2 Utilities : Explain


This feature can be detail the access paths chosen by the DB2 optimizer for SQL statements Used for performance monitoring When EXPLAIN is requested the access paths that the DB2 chooses are put in coded format into the table PLAN_TABLE, which is created in the default database To EXPLAIN a single SQL stmt precede that SQL stmt with the EXPLAIN Command EXPALIN ALL SET QUERYNO = integer FOR SQL stmt the other method is specifying EXPLAIN YES with the Bind command then PLAN_TABLE is to be queried to get the required information.

Copyright 2004, Cognizant Academy, All Rights Reserved

163

3.7. DB2 Utilities : Explain


The information provided include the type of access of particular tables used in the SQL or Package or Plan, the order in which the tables or joined in a JOIN, whether SORT is required and so on Since the EXPLAIN results are dependent on the DB catalog, it is better to run RUNSTATS before running a EXPLAIN

Copyright 2004, Cognizant Academy, All Rights Reserved

164

3.8. DB2 Security, Catalog tables and Optimizer : Security


LOCKING SERVICES : These are provided by an MVS subsystem called the IMS Resource Lock Manager(IRLM). It is used to control concurrent access DB2 data, regardless of whether IMS is present in a system or not.
L c a c y
Segmented Tablespace Table

Row

age

Copyright 2004, Cognizant Academy, All Rights Reserved

165

3.8. DB2 Security, Catalog tables and Optimizer : Security


The above is based on Transaction Processing - the system component that provides this is A TRANSACTION MANAGER COMMIT & ROLLBACK are key methods of implementing locking schemes

Copyright 2004, Cognizant Academy, All Rights Reserved

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

Copyright 2004, Cognizant Academy, All Rights Reserved

167

3.8. Catalog tables and DB2 directory


Repository for all DB2 objects - contains 43 tables Each table maintains data about an aspect of the DB2 environment The data refers to info about tablespaces, tables, indexes, privileges, on utilities run on DB2 and so on eg : SYSIBM.SYSTABLES, SYSINDEXES/SYSCOLUMNS ...... When standard DB2 SQL is used, the DB2 catalog is either accessed or updated. eg. When a CREATE TABLE stmt is issued the catalog tables SYSIBM.SYSTABLES, SYSIBM.SYSCOLUMNS & SYSIBM.SYSFIELDS are updated. However the DB2 catalog is semi active only. This is because updates to number of rows, the physical order of the rows for a set of keys and the like are updated only after running a RUNSTATS utility

Copyright 2004, Cognizant Academy, All Rights Reserved

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

Copyright 2004, Cognizant Academy, All Rights Reserved

169

3.8. DB2 Security, Catalog tables and Optimizer : Optimizer


Analyzes the SQL statements and determines the most efficient way to access data - gives Physical data independence It evaluates the following factors : CPU cost, I/O cost, DB2 catalog statistics & the SQL statement it estimates CPU time, cost involved in applying predicates, traversing pages and sorting It estimates the cost of physically retrieving and writing the data The information pertaining to the state of the tables that will be accessed by the SQL statements are provided by the Catalog
Copyright 2004, Cognizant Academy, All Rights Reserved 170

3.8. DB2 Security, Catalog tables and Optimizer : Performance Tuning


The performance of an application can be monitored and enhanced in the application, as well as database level In application side the SQLs can be tuned to make them more efficient, and avoid redundancy It is better to structure the SQLs so that they perform only the necessary operations On the database side, the major enhancements can be done to the definitions of tables, indexes & the distribution of tablespace and indexspace The application run statistics are obtained from EXPLAIN or DB2PM monitor report

Copyright 2004, Cognizant Academy, All Rights Reserved

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.

Copyright 2004, Cognizant Academy, All Rights Reserved

173

DB2: Next Step


The following items will provide more information on the subjects covered in this course: Resource Type Book Book PDF PDF URL White Paper Research article e-learning course Classroom training 2004, Cognizant Academy, All Rights Reserved Copyright
174

Description DB2 Developers Guide, Craig.S.Mullins

Reference Topic or Module

Congratulations! You have successfully completed

DB2 Basic

Vous aimerez peut-être aussi