DB2 Good One
DB2 Good One
Day 1 - Session 1
DB2 4
Introduction to Databases
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
DB2 6
• Hierarchical Model
• Network Model
• Relational Model
• Object-Oriented Model
DB2 9
HIERARCHICAL
• Top down structure resembling an upside-down
tree
• Parent child relationship
• First logical database model
• Available on most of the Mainframe computers
• Example - IMS
DB2 10
NETWORK
• Does not distinguish between parent and child. Any
record type can be associated with any number of
arbitrary record types
• Enhanced to overcome limitations of other models
but in reality, there is minimal difference due to
frequent enhancements
• Example - IDMS
DB2 11
RELATIONAL
• Data stored in the form of tables consists of
multiple rows and columns.
• Examples - DB2, Oracle, Sybase, Ingres etc.
RELATIONAL DB CONCEPTS
DB2 13
Relational Properties
• Why Relational ? - 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
DB2 14
Relational Concepts (Terminology)
• 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/empty value
• Atomic Value: Smallest unit of data; the individual
data value
DB2 15
Normalization (contd...)
Types of Integrity
Table ‘CUSTOMER’ -
Attributes - CUST_NO, CUST_NAME,
CUST-LOCATION, CUST_ID, ORDER_NO...
Day 1 - Session 2
DB2 36
An introduction to SQL
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
DB2 38
DB2 Objects
Stogroup
Database
Database (contd...)
Tablespaces
Simple Tablespace
Segmented 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.
DB2 52
VCAT Option
Day 2 - Session 1
DB2 62
Other Clauses
In Clause
E.g. SELECT CUST_NO, CUST_NAME, CUST_ADDR
FROM CUSTOMER
WHERE CUST_NO IN(1000, 1001,1002);
DB2 66
Functions
Column Functions
Scalar Functions
Subqueries
Subqueries (contd...)
E.g. SELECT CUST_NO, CUST_NAME
FROM CUSTOMER
WHERE ORDER_NO IN (SELECT ORDER_NO
FROM ORDER WHERE NO_PRODUCTS <5);
Subqueries (contd...)
Correlated Subquery
WHERE A.ORDER_NO IN
(SELECT ORDER_NO
FROM CUSTOMER B
WHERE A.CUST_ID = B.CUST_ID)
ORDER BY A.CUST_ID, A.CUST_NO ;
DB2 78
Joins
INSERT
Day 2 - Session 2
DB2 86
Static SQL
Dynamic SQL
Indexes
What is an Index ?
‘An index is an ordered set of pointers to rows of a
base table’.
Or
‘An Index is a balanced B-tree structure that orders
the values of columns in a table’
Why an Index ?
‘One can access data directly and more efficiently’
DB2 89
Indexes (contd...)
• Each index is 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.
• When you define an index using the CREATE INDEX
statement, DB2 builds this structure and maintains it
automatically.
• Indexes can be used by DB2 to improve performance
and ensure uniqueness.
• In most cases, access to data is faster with an index.
• A table with a unique index cannot have rows with
identical keys.
DB2 90
Indexes (contd...)
Syntax : For creation of an Index
VIEWS
• It is 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
DB2 98
Synonym
• Also means another name for the table, but is private to
the user who created it.
DB2 99
SQL Guidelines
- Refer handout
- Mullins, chapter 2
DB2 101
Day 3 - Session 1
DB2 102
Host Variables
DCLGEN
SQLCA
• An SQLCA 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.
DB2 111
SQLCA (contd...)
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).
DB2 112
Day 3 - Session 2
DB2 113
Cursors
Cursors (contd...)
The four (4) Cursor control statements are -
• Declare : name assigned for a particular SQL
statement
• Open : readies the cursor for row retrieval; sometimes
builds the result table. However it does not assign
values to the host variables
• Fetch : returns data from the results table one row at a
time and assigns the value to specified host variables
• Close : releases all resources used by the cursor
DB2 115
Cursors (contd...)
DECLARE
E.g. - For the Declare statement
EXEC SQL
DECLARE EMPCUR CURSOR FOR
SELECT Empno, Empname,Dept, Job
FROM EMP
WHERE Dept = 'D11'
FOR UPDATE OF Job
END-EXEC.
DB2 116
Cursors (contd...)
OPEN
E.g. - For the Open statement
EXEC SQL
OPEN EMPCUR
END-EXEC.
DB2 117
Cursors (contd...)
FETCH
E.g. - For the Fetch statement
EXEC SQL
FETCH EMPCUR
INTO :Empno, :Empname, :Dept, :Job
END-EXEC.
DB2 118
Cursors (contd...)
CLOSE
E.g. - For the Close statement
EXEC SQL
CLOSE EMPCUR
END EXEC.
DB2 119
Cursors (contd...)
WHENEVER
E.g. - For the Whenever Clause
EXEC SQL
WHENEVER NOT FOUND
Go To Close-EMPCUR
END EXEC.
Day 4 - Session 1
DB2 127
• Program Preparation
• Precompile, Compile, Linkedit and Bind
• Plan & Packages
DB2 128
Precompile
Precompile (contd...)
Bind
Bind Types
What is a Package ?
Advantages of Package
What is a Plan ?
• An application plan contains one or both of the
following elements:
• A list of package names
• The bound form of SQL statements taken from one
or more DBRMs.
• Every DB2 application requires an application plan.
• Plans are created using the DB2 subcommands BIND
PLAN
DB2 136
Day 4 - Session 2
DB2 138
• DB2 Utilities
DB2 139
DB2 UTILITIES
• Check
• Copy/Mergecopy
• Recover
• Load
• Reorg
• Runstats
• Explain
DB2 140
Check
Copy
Mergecopy
Recover
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
DB2 145
Load (contd...)
• 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.
DB2 146
Reorg
Runstats
Explain
Explain (contd...)
Explain (contd...)
Day 5 - Session 1
DB2 154
DB2 Locking
Why Locking ?
‘Locking is used to provide multiple user access to the
same system’
Day 5 - Session 2
DB2 166
DB2 Optimizer
Performance Tuning
Thank You