Sei sulla pagina 1di 370

Business

Intelligence

Data
Centre

Cloud

Mobility

Enterprise Computing Solutions

Student Manual

Direccin General de Formacin


CONSEJERA DE EMPLEO,
TURISMO Y CULTURA

Comunidad de Madrid

UNIN EUROPEA
FONDO SOCIAL EUROPEO
El Fondo Social Europeo invierte en tu futuro

EDUCATION
S

V6.0

cover

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Front cover

DB2 Family
Fundamentals
(Course code CE031)

Student Notebook

pr

Ex

cl

ERC 7.0

IBM certified course material

Student Notebook

Trademarks
IBM is a registered trademark of International Business Machines Corporation.
The following are trademarks of International Business Machines Corporation in the United
States, or other countries, or both:
CICS
DB2
IMS
MQSeries
OS/390
QMF
REXX
z/OS

Cube Views
DRDA
Informix
MVS
OS/400
RACF
S/390
zSeries

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

AIX
DataPropagator
eServer
iSeries
MVS/SP
Parallel Sysplex
Rational
WebSphere

Alphablox is a registered trademark of Alphablox Corporation in the United States, other


countries, or both.
DataStage is a trademark or registered trademark of Ascential Software Corporation in
the United States, other countries, or both.
Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. in the
United States, other countries, or both.
Microsoft, Windows and Windows NT are trademarks of Microsoft Corporation in the
United States, other countries, or both.
UNIX is a registered trademark of The Open Group in the United States and other
countries.

Linux is a registered trademark of Linus Torvalds in the United States, other countries, or
both.

Ex

cl

Other company, product, or service names may be trademarks or service marks of others.

pr

October 2011 edition

The information contained in this document has not been submitted to any formal IBM test and is distributed on an as is basis without
any warranty either express or implied. The use of this information or the implementation of any of these techniques is a customer
responsibility and depends on the customers ability to evaluate and integrate them into the customers operational environment. While
each item may have been reviewed by IBM for accuracy in a specific situation, there is no guarantee that the same or similar results will
result elsewhere. Customers attempting to adapt these techniques to their own environments do so at their own risk.

Copyright International Business Machines Corporation 1997, 2011.


This document may not be reproduced in whole or in part without the prior written permission of IBM.
Note to U.S. Government Users Documentation related to restricted rights Use, duplication or disclosure is subject to restrictions
set forth in GSA ADP Schedule Contract with IBM Corp.

V6.0
Student Notebook

Contents
Trademarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
Course description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Agenda . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv

Unit 1. Understanding a Table. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-1


Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2
The ease of working with a relational database . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3
Load data into a DBMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-4
DB2 is a Relational Database Management System . . . . . . . . . . . . . . . . . . . . . . . 1-6
Table names in DB2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-7
Table creation - English . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-8
The language of Relational Database Management Systems . . . . . . . . . . . . . . . . 1-9
Creating tables in DB2 - We must provide ... . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10
From an Editor that talks to DB2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-11
A BIG picture look at running an application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-13
How DB2 handles an SQL Change request (part I) . . . . . . . . . . . . . . . . . . . . . . . 1-14
How DB2 handles an SQL Change request (part II) . . . . . . . . . . . . . . . . . . . . . . . 1-15
Potential Designer tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-16
Potential Systems Administrator tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-17
Potential DBADM tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-18
Potential Programmer tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-19
Potential End User tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-20
Potential Operator tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-21
Checkpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-22
Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-23

cl

Unit 2. Data Modeling and Database Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-1


Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
Business modeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3
Steps to Database Design - Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-5
Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7
Steps to Database Design - Conceptual View . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-8
Draw an Entity Relationship Diagram (ERD) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-9
Data Inventory - Purpose and Responsibilities . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-10
Example of Data Inventory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-11
Steps to Database Design - Logical View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-12
Process Inventory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-13
Examples of business processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-14
Benefits of business modeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-15
Steps to Database Design - Storage View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-16
Normalization - First normal form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-17
Normalization - Second normal form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-19

pr

Ex

TOC

Copyright IBM Corp. 1997, 2011


Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.

Contents

iii

Student Notebook

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Normalization - Third normal form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-21


Normalization recommendation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-22
Questions to answer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-23
Normalization/Denormalization example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-24
Denormalization example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-25
Costs and benefits of denormalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-26
From model to storage objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-27
One table per business entity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-28
One Column for each data element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-29
Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-30
Name examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-31
Table name construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-32
Column names - Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-33
Correlation names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-34
Data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-35
Numeric formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-36
Character string formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-37
Date/Time formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-38
Graphic string formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-40
Large objects - The need . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-42
Create table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-43
User-defined Distinct Data Types and User-Defined Functions . . . . . . . . . . . . . . .2-45
User-defined Distinct Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-46
Creating a User-defined Distinct Type (UDT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-47
Using the UDT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-48
Creating a User-Defined Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-49
Nulls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-51
Related data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-53
Identify Primary Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-54
Primary Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-55
Unique Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-56
Foreign Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-57
Which table gets the Foreign Key? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-58
Primary Keys, Foreign Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-59
Creating a relationship between tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-61
The rules DB2 enforces to in order to maintain Referential Integrity . . . . . . . . . . .2-63
Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-64
Processing rules enforced to maintain Referential Integrity . . . . . . . . . . . . . . . . .2-65
Primary Key violation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-66
Foreign Key violation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-67
Delete rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-68
Delete rules - ON DELETE CASCADE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-69
Delete rules - ON DELETE SET NULL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-70
Delete rules - ON DELETE RESTRICT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-71
Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-72
Triggers: A simple example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-73
Triggers: Another example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-74
Identify the need for Indexes and Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-75

iv

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V6.0
Student Notebook

Checkpoint (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-76
Checkpoint (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-77
Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-78

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Unit 3. How Does an End User Use DB2? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-1


Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2
Who is an End User of DB2? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-3
Running programs that talk to DB2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-4
And if I'm writing my own SQL? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-5
Learn about your table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-6
STAFF table description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-7
Example table - STAFF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-8
ORG table description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-9
Example table - ORG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-10
A SELECT to retrieve all columns for all rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-11
Four SELECT Statement clauses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-12
SELECT - Comparing Execution Order with Syntax . . . . . . . . . . . . . . . . . . . . . . . 3-14
To SELECT specified rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-16
Numeric comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-17
SQL comparison alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-18
Functions - Two types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-20
Column functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-21
Scalar functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-22
SELECT examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-24
SELECT clause - Column naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-26
CASE expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-27
JOIN - Retrieving data from multiple tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-28
Example tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-29
JOIN - Traditional syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-30
JOIN - Alternate syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-31
Inserting rows into the table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-32
Update columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-33
Delete rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-34
How do I talk to DB2? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-35
Command Line Processor - Interactive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-36
Command Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-37
SQL Assistant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-38
Data Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-40
z/OS SPUFI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-42
Compose query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-43
Execute the query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-44
Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-45
QMF for z/OS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-46
Compose query in QMF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-47
QMF tabular report . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-48
QMF report formatting instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-49
QMF tailored report . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-50
QMF procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-51

pr

Ex

TOC

Copyright IBM Corp. 1997, 2011


Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.

Contents

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

QMF for Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-52


Saving the data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-53
Format the report . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-54
Save data into a spreadsheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-55
QMF - A visual information platform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-56
Checkpoint (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-57
Checkpoint (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-58
Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-59

pr

Ex

cl

Unit 4. How Does a Programmer Use DB2? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-1


Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-2
Creating a test environment - Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-3
Creating a test environment - Indexes and Views . . . . . . . . . . . . . . . . . . . . . . . . . .4-5
Creating a test environment - Catalog statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-6
Prototyping Change SQL using a unit of work . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-7
Prototype SQL against test tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-9
EXPLAIN the queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-10
A program with SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-11
Steps to coding SQL in a program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-12
The SQL delimiters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-13
Host variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-14
In COBOL, C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-15
Selecting a single row . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-16
Selecting multiple rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-17
Fetch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-19
Scrollable cursors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-20
Handling exception conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-21
SQLCA codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-22
Precompile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-24
Compile and Link Edit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-26
Bind package (1 of 4) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-27
Bind package (2 of 4) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-28
Bind package (3 of 4) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-29
Bind package (4 of 4) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-30
Bind plan - z/OS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-32
Program execution - z/OS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-34
Program execution - Linux, UNIX, and Windows . . . . . . . . . . . . . . . . . . . . . . . . . .4-35
Program preparation summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-36
Why use Stored Procedures? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-37
Applications with Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-38
Stored Procedures - Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-39
Data Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-40
Static SQL - An SQL statement DB2 has! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-41
Dynamic SQL - Statements DB2 doesn't have . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-42
Static versus Dynamic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-43
Dynamic programming interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-44
Integration of XML and relational capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-45
What is XML? (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-47

vi

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V6.0
Student Notebook

What is XML? (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The XML document tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Two ways to query XML data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Checkpoint (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Checkpoint (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

4-48
4-49
4-51
4-53
4-54
4-55

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Unit 5. What does a System Administrator do in DB2? . . . . . . . . . . . . . . . . . . . . . . 5-1


Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2
What does a System Administrator do in DB2? . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-3
DB2 products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-4
Installation - Physical install . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-5
Create system level objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-6
Using the DB2 Control Center . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-7
Data Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-8
z/OS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-9
Linux, UNIX, and Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-11
DB2 system catalog tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-12
DB2 object hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-14
Granting privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-15
Monitor DB2's performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-16
Checkpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-17
Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-18

cl

Unit 6. What does an Administrator do in DB2? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-1


Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2
What does a Database Administrator do in DB2? . . . . . . . . . . . . . . . . . . . . . . . . . . 6-3
How does a DBA know what to create? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-4
Review - CREATE TABLE statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-5
Setting specific column default values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-6
Adding business rules to a table definition - (at CREATE TABLE time) . . . . . . . . . 6-7
Adding business rules to an existing table - (with the ALTER TABLE statement) . 6-8
Tables without indexes - From the table COMPANY.DIRECTORY . . . . . . . . . . . . 6-9
An index is: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-10
Locating rows in the table via RIDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-11
What is an index? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-12
Unique index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-13
Index structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-14
Non-clustered index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-15
Clustered index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-16
Clustered and non-clustered index access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-17
Creating indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-18
Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-19
Creating a view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-20
Using a view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-21
Updating through a view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-22
The DBA cleans up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-23
DROP dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-25

pr

Ex

TOC

Copyright IBM Corp. 1997, 2011


Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.

Contents

vii

Student Notebook

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Locking - Concurrency needs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-26


Concurrency - Table level (1 of 6) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-27
Concurrency - Table level (2 of 6) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-29
Concurrency - Table level (3 of 6) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-30
Concurrency - Table level (4 of 6) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-31
Concurrency - Table level (5 of 6) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-32
Concurrency - Table level (6 of 6) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-33
Concurrency - Page level - z/OS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-34
Concurrency - Row level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-35
Specifying LOCKSIZE (Concurrency level) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-36
Isolation Level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-37
Duration of Read Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-38
Utilities - DB2 tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-40
Setup and Move Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-41
Recovery-related Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-42
Forward recovery - Good data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-43
Speeding up recovery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-44
Point-In-Time recovery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-45
Version recovery - Linux, UNIX, and Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-46
Maintenance Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-47
REORG - Clustering the data on department . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-48
RUNSTATS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-49
To get to DB2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-50
DB2 security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-51
Everything is protected! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-52
Privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-53
Owner's (Implicit) table privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-54
Explicit table privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-55
Revoke . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-56
Indirect privileges - Static SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-57
Indirect privileges - Group . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-58
Administrative authorities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-59
Views and Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-60
Checkpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-61
Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-62

pr

Ex

Unit 7. Information Management with DB2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-1


Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-2
Planning for distributed data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-3
Planning - Multiple skills requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-5
Distributed terminology - Unit of work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-7
Types of access to distributed data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-8
No title . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-10
Information On Demand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-11
IBM Information Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-13
DB2 Warehouse integrated warehousing platform . . . . . . . . . . . . . . . . . . . . . . . . .7-15
Checkpoint (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-16
Checkpoint (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-17

viii

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V6.0
Student Notebook

Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-18


Appendix 8. Checkpoint solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-1
Appendix B. DB2 and Related Courses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . B-1

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Appendix C. More about DB2 and Other Products . . . . . . . . . . . . . . . . . . . . . . . . . . C-1

cl

DB2 Information Center . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . X-1

pr

Ex

TOC

Copyright IBM Corp. 1997, 2011


Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.

Contents

ix

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Student Notebook

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V6.0
Student Notebook

Trademarks
The reader should recognize that the following terms, which appear in the content of this
training document, are official trademarks of IBM or other companies:
IBM is a registered trademark of International Business Machines Corporation.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

The following are trademarks of International Business Machines Corporation in the United
States, or other countries, or both:
AIX
DataPropagator
eServer
iSeries
MVS/SP
Parallel Sysplex
Rational
WebSphere

CICS
DB2
IMS
MQSeries
OS/390
QMF
REXX
z/OS

Cube Views
DRDA
Informix
MVS
OS/400
RACF
S/390
zSeries

Alphablox is a registered trademark of Alphablox Corporation in the United States, other


countries, or both.
DataStage is a trademark or registered trademark of Ascential Software Corporation in
the United States, other countries, or both.
Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. in the
United States, other countries, or both.
Microsoft, Windows and Windows NT are trademarks of Microsoft Corporation in the
United States, other countries, or both.
UNIX is a registered trademark of The Open Group in the United States and other
countries.

cl

Linux is a registered trademark of Linus Torvalds in the United States, other countries, or
both.
Other company, product, or service names may be trademarks or service marks of others.

pr

Ex

TMK

Copyright IBM Corp. 1997, 2011


Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.

Trademarks

xi

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Student Notebook

xii

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V6.0
Student Notebook

Course description
DB2 Family Fundamentals

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Duration: 2 days
Purpose

This course provides you with information about the functions of IBM's
DB2, a relational database manager which may be installed under a
variety of operating systems on many hardware platforms.
DB2 runs under the z/OS, VM, Linux, UNIX, and Windows operating
systems, to name a few.

The course includes discussion of how the DB2 products provide


services. The focus is on the services DB2 provides and how we work
with DB2, not on its internal workings.

Audience

Persons needing an introductory knowledge of DB2, and persons


preparing for advanced and specialized DB2 education.

Prerequisites

Basic knowledge in data processing.

Objectives

cl

After completing this course, you should be able to:

pr

Ex

pref

List and describe the major components of IBM's relational


database, DB2
Explain the characteristics of a DB2 table

Relate the basic concepts of data modeling

Comprehend the processing instructions given to DB2 via simple


SQL statements
List and describe several ways to build (write) and execute SQL
statements
List and describe steps needed to imbed SQL statements in an
application program

Copyright IBM Corp. 1997, 2011


Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.

Course description

xiii

Student Notebook

Explain some of the functions performed by, and the


responsibilities of, database and system administrators
Establish a base for more specialized DB2 education

Contents

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Understanding a table

Data modeling and database design


How does a User use DB2?

How does a Programmer use DB2?

How does an Administrator use DB2?

pr

Ex

cl

Information Management with DB2

xiv

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V6.0
Student Notebook

Agenda
Day 1

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Welcome
Understanding a Table
Data Modeling and Database Design
How does a User use DB2?

Day 2

cl

How does a Programmer use DB2?


What does an Administrator do in DB2?
Information Management with DB2

pr

Ex

pref

Copyright IBM Corp. 1997, 2011


Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.

Agenda

xv

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Student Notebook

xvi

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Unit 1. Understanding a Table


What this unit is about

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

This unit introduces the DB2 Database Manager and the basic
concepts behind a table.

What you should be able to do

After completing this unit, you should be able to:

Identify the advantages of a relational database


Define a relation
Name the language used to talk to a relational database manager
(RDBM)
List three characteristics assigned to each column
Define the tasks performed by DB2 when running an application
Define the roles that are performed within DB2

How you will check your progress


Accountability:

pr

Ex

cl

Checkpoint questions

Copyright IBM Corp. 1997, 2011

Unit 1. Understanding a Table

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

1-1

Student Notebook

Unit objectives

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

After completing this unit, you should be able to:


Identify the advantages of a relational database
Define a RELATION

Name the language used to talk to a relational database


management system (RDBMS)
List three characteristics assigned to each column

Define the tasks performed by DB2 when running an


application
Define the roles that are performed within DB2

Copyright IBM Corporation 2007

Figure 1-1. Unit objectives

CE0317.0

Notes:

cl

Database managers are sophisticated software programs that maintain our data. Each
database manager provides a set of automated services. We will discuss some of the more
commonly used DB2 services in this course.

Ex

The word relation is simply a synonym for the word table. A relational database is one in
which the data is perceived as being stored in rows and columns. Relational database
managers understand SQL, the programming language for relational databases. End
users and applications access DB2 data through SQL.

pr

In this unit we will discuss the basic structure of a table, and will mention three
characteristics that are assigned to all columns. We will discuss potential tasks associated
with DB2, and the various I/T roles that support and use the DB2 environment.

1-2

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

The ease of working with a relational database

I need to call Larry Jones in the


company directory... now, where is his
phone number?

Phone book

COMPANY
DIRECTORY

Copyright IBM Corporation 2007

Figure 1-2. The ease of working with a relational database

CE0317.0

Notes:

cl

We work with tables of data every day. An Excel spreadsheet usually consists of rows and
columns. A phone book is another example of a table of data. Each page has the same
columns (name, address, and phone number), just different rows. Phone books are in
alphabetical sequence. The rows in a relational database may or may not be in a particular
order.

pr

Ex

A relational database is a table database, consisting of rows and columns. At each


juncture of a row and column, one and only one value may be present, according to
relational theory.

Copyright IBM Corp. 1997, 2011

Unit 1. Understanding a Table

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

1-3

Student Notebook

Load data into a DBMS

DB2, manage
the company phone
directory

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DB2

Tables - R - Us

Integrity and
Recovery

Phone book

COMPANY
DIRECTORY

Continuous
Operation

DBA

Logging

Optimizer

Lock
Manager

Security

Copyright IBM Corporation 2007

Figure 1-3. Load data into a DBMS

CE0317.0

Notes:

Database managers provide many services, such as:

cl

Logging Saving before and after images (called UNDO and REDO records) of any
DB2 rows changed by SQL (INSERT, UPDATE, DELETE).

Ex

Security Monitoring and controlling access to DB2 data

Optimization Minimizing the use of system resources (CPU cycles and physical I/O)
when performing SQL queries and doing other DB2 work

pr

Locking Serializing the access to DB2 data in order to allow concurrency, while
guaranteeing data integrity

Recovery Restoring DB2 data due to outages, system failures or corruption by incorrect
program logic
Data integrity See the discussion below

1-4

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Database theory demands that all changes to data follow the ACID model. From examples
during this course, you will see that DB2 does apply the characteristics of this model:
ATOMICITY refers to the ability of the DBMS to guarantee that either all of the tasks of a
transaction are performed or none of them are. A transfer of funds can be completed or it
can fail for a multitude of reasons, but atomicity guarantees that one account won't be
debited if the other is not credited as well. It is the concept of all or nothing.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

CONSISTENCY refers to the database being in a legal state when the transaction begins
and when it ends. This means that a transaction can't break the rules, or integrity
constraints, of the database. If an integrity constraint states that no salaries can exceed
$100,000, then any transaction violating this rule will be aborted. DB2 ensures that only
valid data is allowed within DB2.

ISOLATION refers to the ability of the application to make operations in a transaction


appear isolated from all other operations. This means that no operation outside the
transaction can ever see the data in an intermediate state; a bank manager can see the
transferred funds on one account or the other, but never on both even if they ran their
query while the transfer was still being processed. More formally, isolation means the
transaction history (or schedule) is able to be serialized. For performance reasons, this
ability is the most often relaxed constraint. Uncommitted Reads can be allowed in DB2, but
it is not the default behavior.
DURABILITY refers to the guarantee that once the user has been notified of success, the
transaction will persist, and not be undone. This means it will survive system failure, and
that the database system has checked the integrity constraints and won't need to abort the
transaction. Typically, all transactions are written into a log that can be played back to
recreate the system to its state right before the failure. A transaction can only be deemed
committed after it is safely in the log. DB2 ensures that no data is lost.

pr

Ex

cl

IBMs DB2 adheres to these guidelines, and therefore passes the ACID test!

Copyright IBM Corp. 1997, 2011

Unit 1. Understanding a Table

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

1-5

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DB2 is a Relational Database Management System

"RELATION"

TABLE

(Data is stored in
columns and rows)

OWNER1.TABLE_A
COL1
ABC
DEF
GHI
JKL
MNO

COL2 COL3
7
4
6
7
0

1987-11-14
1963-01-01
1946-07-12
1951-12-30
1992-08-12

PROD.PARTS

A
Relational
Database"
is a "Table
Database"

PROD
BOLT
NUT
SCREW
BOARD
WIRE

QTY
7
4
6
7
0

SUPPLIER
ABC CORP
THE NUT CO
FASTEN INC
ECM CORP
BINDINGS

Copyright IBM Corporation 2007

Figure 1-4. DB2 is a Relational Database Management System

CE0317.0

Notes:

The database management system we are working with in class is DB2.

Ex

cl

DB2 is a relational database management system (RDBMS). The word relation is a


synonym for the word table. A relational database management system is a database
manager that manages data perceived to be stored in a table format, that is, columns and
rows.

pr

IBM Informix is another example of a Relational Database Management System.

1-6

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Table names in DB2


Simple Table Name

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Seattle

PARTS

Chicago

Qualified Table Name

Atlanta

PROD. PARTS

Fully-Qualified Table Name

ATLANTA.PROD. PARTS

Copyright IBM Corporation 2007

Figure 1-5. Table names in DB2

CE0317.0

Notes:

cl

Every table in DB2 must be given a unique name. A table always has two parts, separated
by a period a schema/owner/creator name, followed by the table name. Optionally, if
there are multiple DB2 systems connected together, it may become necessary to qualify
the table with a location name as well. When that is the case, the location name comes
first.

pr

Ex

In the visual above, we refer to PARTS as the simple table name. There could be many
schemas or owners with a DB2 table named PARTS. There could be a TEST.PARTS, a
QA.PARTS and a PROD.PARTS a PARTS table each for a Test, a Quality Assurance
and a Production environment. PROD.PARTS, which is referred to as a qualified table
name, is the PARTS table that was created by the schema/owner known as PROD. Finally,
if our company had manufacturing facilities in Atlanta, Chicago and Seattle, and we needed
to maintain a PROD.PARTS table and DB2 system at each of those locations, we could
fully qualify the table name for example, ATLANTA.PROD.PARTS.
Each simple table name must be unique within a schema. Each qualified table name must
be unique within a location.
Copyright IBM Corp. 1997, 2011

Unit 1. Understanding a Table

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

1-7

Student Notebook

Table creation English

DB 2

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DB2, build me a table. Here are


the rules to enforce on the table.
Name the table
COMPANY.DIRECTORY. Name
the first column LASTNAME.
Ensure that only character data
be allowed in that column. Also,
ensure that every phone listing
has a value in the LASTNAME
column. Now, for the second
column...

Tables - R - Us

DB A

Copyright IBM Corporation 2007

Figure 1-6. Table creation - English

CE0317.0

Notes:

pr

Ex

cl

Talking to DB2 in English, French, or German, and so forth, would be too cumbersome.
What is needed in order to more effectively communicate with DB2 is a language that is
more precise (accurate and specific), and more concise (shorter, abbreviated).

1-8

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

The language of
Relational Database Management Systems

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Uempty

WHAT data to

TRUCTURED

retrieve,
NOT HOW to
retrieve it

UERY

ANGUAGE

Copyright IBM Corporation 2007

Figure 1-7. The language of Relational Database Management Systems

CE0317.0

Notes:

Ex

cl

SQL is a non-navigational language. What does that mean? With some databases, the
calls to access the data instruct the system to navigate the structure of the database
objects. But with DB2, through SQL, the user tells DB2 what data to access, but not how
to retrieve it. Rather, with SQL, the access path to the data is determined by the DB2
Optimizer. DB2 always strives to give the user the best possible performance when
accessing the data, by minimizing the consumption of system resources (I/Os and CPU
cycles).

pr

The SQL language is quite powerful, and is continually being enhanced with new functions.
Oftentimes, there may be more than one way to write a query in order to obtain the results
dictated by the logic requirements of the application program. So in that sense, the
language is somewhat flexible.
However, as the S implies in the acronym, the language is structured. There is a format
that needs to be followed, there are syntax rules, there are keywords that must be used,
etc.
We will go into some more detail about SQL later on during the course.
Copyright IBM Corp. 1997, 2011

Unit 1. Understanding a Table

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

1-9

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Creating tables in DB2 We must provide ...

Table name

Describe the columns


(give each column 3 attributes)
COLUMN NAME
DATATYPE / LENGTH
NULL CHARACTERISTIC

DBA
DBA

Copyright IBM Corporation 2007

Figure 1-8. Creating tables in DB2 - We must provide ...

CE0317.0

Notes:

Every table must be given a name.

Also, each of the table's columns must be assigned three attributes:

cl

Name

Ex

Data type (and optionally a length)


Null characteristic

pr

Some datatypes automatically imply a certain length, so for those datatypes, no length
specification is necessary (or allowed). With other datatypes, the user needs to specify the
desired length (and for decimal numbers, both a precision and scale).

1-10 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

From an Editor that talks to DB2

CREATE TABLE COMPANY.DIRECTORY

( ID

INTEGER

NOT NULL

LASTNAME

CHAR(15)

FIRSTNAME

CHAR(15)

ADDRESS

VARCHAR(45)

NOT NULL

NOT NULL

Column
description
area

WITH DEFAULT,

Copyright IBM Corporation 2007

Figure 1-9. From an Editor that talks to DB2

CE0317.0

Notes:

cl

Here is the basic syntax required for creating a table in DB2, using SQL. The CREATE
statement above tells DB2 to make a definition in the DB2 catalog. Here are some of the
details regarding this table.
Regarding the name of the table:

Ex

The schema/qualifier of the table is to be COMPANY.


The simple name of the table is to be DIRECTORY.
Regarding the columns that comprise the table:

pr

The name of the first column will be ID.

Only numeric data is to be allowed in the first column.


This column must always contain a valid value (i.e. not be NULL)
The name of the second column will be LASTNAME.
Only character data is to be allowed in the second column.
Copyright IBM Corp. 1997, 2011

Unit 1. Understanding a Table

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

1-11

Student Notebook

All values in the second column will be stored with 15 characters.


This column must always contain a valid value (i.e. not be NULL)
The name of the third column will be FIRSTNAME.
Only character data is to be allowed in the third column.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

All values in the second column will be stored with 15 characters.


If a first name is not available for a row, DB2 is to mark it NULL (unknown).
The name of the fourth column will be ADDRESS.

Only character data is to be allowed in the fourth column.

All values in the third column will be stored with the actual length of the value.

This column must always contain a valid value (i.e. not be NULL). If the INSERT
application does not provide a value for ADDRESS, DB2 will place blanks (which is the
default for CHAR and VARCHAR datatypes) into the column.

Notice that the column definitions immediately follow the table name, and are delimited by
an open parenthesis and a close parenthesis. If the user needs to specify other
parameters or keywords for this table, it must follow the column definitions. This is part of
the syntactical requirement of the CREATE TABLE statement.

pr

Ex

cl

Also notice that in the example above, the name, datatype and NULL characteristic for
each row is aligned with the other rows, for readabilitys sake. That is NOT a syntactical
requirement of SQL, but it is a highly recommended practice to follow.

1-12 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

A BIG picture look at running an application


DB2

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Tables - R - Us

Integrity
&
Recovery

Application
Manager

Continuous
Operation

Logging

Optimizer

Lock
Manager

Application

Application

Application

Security

Application

Application

Application

Data
Tables

Application
Programs

Copyright IBM Corporation 2007

Figure 1-10. A BIG picture look at running an application

CE0317.0

Notes:

The user must be authorized to log on to the system, which normally would involve a
security mechanism that is external to DB2.

cl

The application manager may have its own security.

The application manager loads the program into memory, and begins execution.

Ex

The application manager passes the authorization ID to DB2. DB2 then does an internal
security check, by reading the data in the DB2 directory tables that controls the access to
all DB2 objects in the system.

pr

If DB2 verifies that the authorization ID is authorized to do what it is trying to do, then the
SQL request is executed on behalf of the application.
DB2 returns messages (and perhaps data) to the application.

Copyright IBM Corp. 1997, 2011

Unit 1. Understanding a Table

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

1-13

Student Notebook

How DB2 handles an SQL Change request (part I)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

User wants to change a SALARY


User's application
connects to DB2

DB2

Tables - R - Us

Lock

Integrity
&
Recovery

Passes user's ID
to DB2

Continuous
Operation

Lock
Manager

Logging

Optimizer

Lock
Manager

Security

Read
into DB2
buffer

Data

Is user permitted
to update salary?

Catalog

Copyright IBM Corporation 2007

Figure 1-11. How DB2 handles an SQL Change request (part I)

CE0317.0

Notes:

Here the scenario is more complex, so we will explain it in the next two visuals.
The user logs on.

cl

The user's authorization ID is passed to DB2.

Ex

The user's request is passed to DB2.

DB2 verifies that user is permitted to UPDATE the column named SALARY.

DB2 locks the data that will be changed, since it must ensure that no other users can
change it at the same time, in order to maintain data integrity.

pr

DB2 reads the data that will be changed into the buffer pools.

1-14 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

How DB2 handles an SQL Change request (part II)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

After the update

Tables - R - Us

DB2

Integrity
&
Recovery

Release the lock

Continuous
Operation

Logging

Optimizer

Write

Lock
Manager

Data

Security

Write/Commit

Logging

LOG

Copyright IBM Corporation 2007

Figure 1-12. How DB2 handles an SQL Change request (part II)

CE0317.0

Notes:

DB2 writes a before update copy of the data to the log (an UNDO record).
DB2 updates the row.

cl

DB2 writes an after update copy of the data to the log (a REDO record).

Ex

DB2 writes a commit to the log.

DB2 writes the data to the table (the writes to external DASD may be batched and may
not actually occur at this time it may be an asynchronous, rather than a synchronous
process).

pr

DB2 releases the lock on the data, thereby making the DB2 data available once again to
other DB2 users and application programs.
DB2 returns a message to the user/application as to the success or failure of the SQL
statement.

Copyright IBM Corp. 1997, 2011

Unit 1. Understanding a Table

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

1-15

Student Notebook

Potential Designer tasks

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Business
Information

Dept

1 11

1 M1

Employees

M 1M

A
location
has many
departments

Many
employees
work on many
projects

Locations

Ah, so
that's how
it all works
together!

Projects

1 M

Skills

Long-term framework

Copyright IBM Corporation 2007

Figure 1-13. Potential Designer tasks

CE0317.0

Notes:

cl

The purpose of a database design or model is to create a formal representation of the


business, which will make it easier to visualize and comprehend the nature of the entities in
the business, the attributes of those entities, and the various relationships between the
entities. The processes within the enterprise that access the database can also be part of
an overall design.

Ex

Designers first analyze the business and identify business entities.

Data modeling then transforms the business entities into data entities. The
characteristics (attributes) of the business entities become columns in those tables.

pr

From data entities, tables are then created.

The characteristics (attributes) of the business entities become columns in the tables.
In order to implement business rules, tables may also then subsequently contain keys,
check constraints, referential integrity constraints, and triggers.

1-16 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Potential Systems Administrator tasks

Installation
of RDBMS

System
Admin

Original grantor
of all privileges

Creation of
system-level
objects

System
performance

Copyright IBM Corporation 2007

Figure 1-14. Potential Systems Administrator tasks

CE0317.0

Notes:

The system administrator does the physical installation of the DB2 product onto the
hardware platform, under the operating system.

cl

The system administrator creates the system level objects (databases).

Ex

The system administrator grants other authorized DB2 users the appropriate privileges
needed by them to perform their roles and responsibilities.
The system administrator monitors the overall DB2 system performance.

pr

Be aware that with the most current versions of DB2 for z/OS and LUW, there is now a new
security administrator authority, called SECADM. Customers may opt to choose this
alternative approach to administer the security function, instead of the approach listed in
bullet three above.

Copyright IBM Corp. 1997, 2011

Unit 1. Understanding a Table

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

1-17

Student Notebook

Potential DBADM tasks

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Grant privileges:
INSERT, SELECT,
UPDATE, DELETE,
LOAD, BIND, etc.

Creating
Database objects:
tables, indexes,
views, triggers, etc.

DBA

Maintenance of
database objects:
Loading data
gathering statistics
making backups,
recovering data,
Etc.

Binding:
packages,
plans

Copyright IBM Corporation 2007

Figure 1-15. Potential DBADM tasks

CE0317.0

Notes:

The Database Administrator (DBA) is responsible for building and maintaining the
application-related (as contrasted with system-level) DB2 objects within the database.

cl

The DBA grants to other authorized DB2 users, the privileges to access and/or change
DB2 objects to fulfill their responsibilities.

Ex

The DBA may perform the production binds (preparing application programs to work with
DB2).

pr

The DBA is usually responsible for the running of most DB2 utilities for purposes of
backup and recovery, re-organizing data, keeping statistics current, etc.

1-18 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Potential Programmer tasks

Creating
test
environment

Programmer

Coding:
programs,
stored
procedures,
SQL, and so on

Prototyping
SQL
"explaining"
SQL

Binding:
Packages,
Plans

Copyright IBM Corporation 2007

Figure 1-16. Potential Programmer tasks

CE0317.0

Notes:

Programmers may be involved with creating test environments for new applications.

cl

Programmers will prototype SQL (write and test the SQL before it is used in production),
to ensure proper logic and functioning of those SQL statements..

Ex

Programmers may be concerned with the efficiency of their SQL. EXPLAIN is a DB2
function that tells you how DB2 is going to execute our SQL.
Programmers physically embed SQL statements into host language programs.

pr

Programmers will prepare (pre-compile and bind) their programs for execution in the test
environment.

Copyright IBM Corp. 1997, 2011

Unit 1. Understanding a Table

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

1-19

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Potential End User tasks

Run business
applications

End User

Change
DB2 data

Produce
reports

Write SQL
queries

Copyright IBM Corporation 2007

Figure 1-17. Potential End User tasks

CE0317.0

Notes:

Anyone executing an SQL statement is a user.

SQL statements can be imbedded in the applications which users run.

cl

The SQL within the applications may be adding data (INSERT), reading data (SELECT),
changing data (UPDATE) or removing data (DELETE).

pr

Ex

The user may construct (write) their own queries that manipulate data.

1-20 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Potential Operator tasks

Load

Backup

Operator

Production
Application

Reorg

Copyright IBM Corporation 2007

Figure 1-18. Potential Operator tasks

CE0317.0

Notes:

cl

Operators are often granted the authority to run many application programs and utilities.
Operators take some of the maintenance burden off the shoulders of the DB2 system
administrators and DBAs, by running routine, thoroughly-tested, regularly- scheduled
utilities and production applications.

pr

Ex

Those utility and application programs were developed and tested by the DBAs and
application programmers for proper functioning, before becoming part of the regular
production schedule. If any problem arises during the execution of such programs or
utilities, the operator will contact the person responsible for the program or utility, before
proceeding further.

Operators normally do not have data access authority (they can't look at the data, unless
specifically authorized to do so).

Copyright IBM Corp. 1997, 2011

Unit 1. Understanding a Table

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

1-21

Student Notebook

Checkpoint

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

1. List some services provided by database managers.


2. What is the language that DB2 understands?

3. List the three characteristics assigned to every column.

4. What term is interchangeable with the term RELATION?


5. List some potential DBA tasks.

Copyright IBM Corporation 2007

Figure 1-19. Checkpoint

CE0317.0

Notes:

Write down your answers here:


1.

cl

2.

Ex

3.
4.

pr

5.

1-22 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Unit summary

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Having completed this unit, you should be able to:


Identify the advantages of a relational database
Define a RELATION

Name the language used to talk to a relational database


management system (RDBMS)
List three characteristics assigned to each column

Define the tasks performed by DB2 when running an


application
Define the roles that are performed within DB2

Copyright IBM Corporation 2007

Figure 1-20. Unit summary

CE0317.0

Notes:

A table format implies columns and rows.

Relational database managers understand SQL.

pr

Ex

cl

Every column has a column name, a data type assigned to it, and a null characteristic.

Copyright IBM Corp. 1997, 2011

Unit 1. Understanding a Table

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

1-23

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Student Notebook

1-24 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Unit 2. Data Modeling and Database Design


What this unit is about

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

This unit describes the basics of data modeling.

What you should be able to do

After completing this unit, you should be able to:

State the purpose of a business model


Identify an Entity-Relational Diagram (ERD) model
List several DB2 column data types
Identify non-standard column and table names
Identify the characteristics of a primary key and a foreign key
State the purpose of referential integrity
State the purpose of triggers

How you will check your progress


Accountability:

pr

Ex

cl

Checkpoint questions

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-1

Student Notebook

Unit objectives

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

After completing this unit, you should be able to:


State the purpose of a business model

Describe an Entity-Relational Diagram model (ERD)


Discuss the rules for normalization of databases
List several DB2 column data types

Describe User Defined data types and functions

Understand the features of a primary and foreign keys


State the purpose of referential integrity
State the purpose of triggers

Copyright IBM Corporation 2007

Figure 2-1. Unit objectives

CE0317.0

pr

Ex

cl

Notes:

2-2

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Business modeling

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Hmm, must
focus
on the business!

Business
Information

Copyright IBM Corporation 2007

Figure 2-2. Business modeling

CE0317.0

Notes:

cl

A business model is a formal representation of business information: its objects, each


object's properties or attributes, and the relationships of one object to another. It is
important to note that the model focuses on business, not technology, and therefore it is not
dependent upon:

Ex

The underlying database structure


A specific application

The hardware platform

pr

A certain operating system

Instead, the business model can span an entire enterprise, or a division of the organization;
it requires a solid understanding of the business, and can serve as a verification of the
users' view of the business before the database is even designed.
There are many parts in a business model, and there is no formal definition of what parts
must exist to be a valid model. It can be as simple as just naming the major business
entities or objects (customer, product, invoice, order, salesperson, vendor, purchase

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-3

Student Notebook

order, and so forth) and their relationship to each other (a customer has many invoices, but
an invoice has only one customer). This is commonly manifested in an Entity Relationship
Diagram (ERD): a pictorial representation of the user's view of the business.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

The ERD model and process may go further and convert the business entities into data
entities whereby many-to-many relationships are resolved, primary and foreign keys are
identified, and repeating data elements are discovered and result in additional data entities.
There are also ER tools that can transform the data model into the SQL DDL (data
definition language) statements that will create the necessary objects for a specified DB2
platform, to implement the data model.

2-4

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Steps to Database Design - Overview

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Problem Statement
Entity Relationship
Model

Process Inventory

Conceptual
View
Data Inventory

Tables

Indexes

Logical Data Structures

Integrity Rules

Logical View

Storage
View

Triggers

Copyright IBM Corporation 2007

Figure 2-3. Steps to Database Design - Overview

CE0317.0

Notes:

Ex

cl

When designing the database for an application domain, you start with a problem
statement for the application domain, that is, a document describing the types of business
objects for the application domain, the relationships between them, and the business
constraints for them. The problem statement must be established by an application domain
expert (analyst). In general, it is not produced by the database designer who does not have
the domain expertise; rather it is input for the data designer.

pr

Starting with the problem statement, a series of steps is performed during the design.
These steps look at the data of the application domain from three different angles, called
views:
The conceptual view
The storage view
The logical view

For each of these views, a set of results is produced during database design. You can
associate a view with its results and describe it by its results.
Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-5

Student Notebook

The conceptual view scrutinizes and structures the data of the application domain based
on their semantic meaning, that is, their meaning for the business (application domain). It
does this independently of the business processes accessing the data and without regard
to any existing or planned method for storing the data. Thus, during the conceptual view,
the process and implementation-independent architecture of the data of the
application domain is established.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

The storage view looks at the data of the application domain from a storage point of view.
During the storage view, in a series of steps, the objects of the conceptual view are
mapped into objects (in particular, tables and associated DB2 objects) of the relational
database management system chosen for the implementation of the data. Thus, the
storage view is not an implementation-independent view of the data. Rather, it is an
implementation-oriented view of the data during which the conceptual view is physically
implemented in the selected relational database management system.

The logical view looks at the data of the application domain from a process point of view.
Generally, a particular business process does not access all data of the application
domain, but only a part of the data. Thus, it has its own process-dependent view of the data
of the application domain. Accordingly, during the logical view, the process-dependent
views for the business processes of the application domain are established.

pr

Ex

cl

Of course, it is very important that you solicit and consider the opinions and input from
management, business analysts, end-users, DBAs, etc. during the modeling process.

2-6

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Problem Statement

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

GOAL:
Implement a
personnel database
system

Employees

Projects

Departments

Locations

Skills

Copyright IBM Corporation 2007

Figure 2-4. Problem Statement

CE0317.0

Notes:

cl

A problem statement declares the business purpose of the database and its related
applications in a few sentences. This problem or purpose statement can be referred to
during the modeling process. The statement may also be a dynamic document, since over
time there may be circumstances which arise that necessitate changes in your database
design and applications.

Ex

A problem statement should include:

A short textual description of the application domain: what it does, and what it proposes to
accomplish

pr

A listing of all business object types (know as entities), such as Employees, Departments,
Projects, etc.

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-7

Student Notebook

Steps to Database Design Conceptual View

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Problem Statement

Entity Relationship
Model

Process Inventory

Conceptual
View
Data Inventory

Tables

Indexes

Logical Data Structures

Integrity Rules

Storage
View

Logical View

Triggers

Copyright IBM Corporation 2007

Figure 2-5. Steps to Database Design - Conceptual View

CE0317.0

Notes:

cl

The development of an entity-relationship model for an application domain is the first step
of the conceptual view during which the process and implementation-independent
architecture of the data of the application domain is established.

Ex

To develop the database from here, you must then determine the data that should be
contained in the database. This means that you must establish a list of all data for the
application domain, that is, the data inventory. Also a list of all business processes for the
application domain must be established, in a process inventory. These both should be
independent of any implementation considerations.

pr

In order to determine what should go into the data inventory, a formal meeting (or series of
meetings) should be conducted, involving all interested parties. A list of all the data items
you want DB2 to store needs to be produced.

2-8

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Draw an Entity Relationship Diagram (ERD)


M

Many
employees
work on many
projects

Locations

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Dept

A
location
has many
departments

1 11

1 M1

Employees

M 1M

Projects

1 M

Skills

Copyright IBM Corporation 2007

Figure 2-6. Draw an Entity Relationship Diagram (ERD)

CE0317.0

Notes:

In entity-relationship (ER) modeling, you identify the components of the business, the
attributes of those entities, and the relationship between the various entities.

cl

Draw lines between related entities and notate the relationships. Relationships between
business entities can be one-to-one, one-to-many, or many-to-many.

Ex

Business modeling, then, includes at least two steps of the database design process:
Defining the entities

Defining the relationships between the entities

pr

In this diagram, we see that there are relationships between several pairs of tables. The
relationships can be verbalized by reading from the base of an arrow toward the point of
the arrow. (For instance, the first horizontal relationship line can be read "One location can
have many departments". The second horizontal relationship line can be read "One
department can have one location". The word following can have should represent the
maximum number of related occurrences.)

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-9

Student Notebook

Data Inventory Purpose and Responsibilities

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Detailed description of all data


for application domain
Data elements and data groups

Data element = Indivisible piece of data


Data group = Group of logically related
data elements and/or data groups

Independent of entity types

Multiple entity types may use same


data for different purposes

Jointly created by:

Application domain expert

Knows application domain

Database designer

Knows what is needed for database design


Knows entity types for data elements/data groups
Input for database designer
Copyright IBM Corporation 2007

Figure 2-7. Data Inventory - Purpose and Responsibilities

CE0317.0

Notes:

cl

The data inventory contains a detailed description of all data for the application domain: It
describes all abstract data types, data elements, and data groups for the application
domain.

Ex

Data elements are indivisible pieces of data. They cannot be divided into smaller pieces
meaningful for the application domain.

pr

In contrast, data groups are sets of logically related (for the application domain) data
elements and/or data groups. This is a recursive definition and implies that data groups can
contain data groups. The data elements or data groups of a data group are referred to as
components of the (owning) data group.
A data group can be viewed as a tree structure of one or more levels whose lowest level
nodes (terminal nodes) are data elements. An example of a data group may be Name of
Person, that is, the name of a person, consisting of data elements Last Name, First Name,
and Middle Initial. Another example of a data group would be Time, consisting of the data
elements Hour, Minute and Seconds.

2-10 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Example of Data Inventory

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Employee Number
Name:
Type:
Description:
Datatype:
Lengths:

Employee Number
Data element
Personnel Number of Employee
ALPHANUMERIC
Minimum Length: 6
Maximum Length: 6
Average Length: 6
Number of Digits: -

Domain:
Data Group:
Entity Type: EMPLOYEE
Role: Cardinality: Minimum = 1, Maximum = 1, Average = 1

Copyright IBM Corporation 2007

Figure 2-8. Example of Data Inventory

CE0317.0

Notes:

cl

The above visual illustrates a sample data element for our personnel system - the
employee number. The important attributes of each data element would need to be
determined and recorded.

Ex

Ideally, this information could be placed in a central repository, which would act as an
enterprise-wide data dictionary. In addition to the attributes shown in the visual above, it
may be very useful to also include the meaning of the data element, the location of the
data, any relevant naming standards, etc.

pr

If there is no data dictionary software product in the enterprise, this inventory would need to
be properly documented by other means, and made available to designers and other
persons involved in the processes of design, the creation of database objects and
application development.

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-11

Student Notebook

Steps to Database Design Logical View

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Problem Statement

Entity Relationship
Model

Process Inventory

Conceptual
View
Data Inventory

Tables

Indexes

Logical Data Structures

Integrity Rules

Logical View

Storage
View

Triggers

Copyright IBM Corporation 2007

Figure 2-9. Steps to Database Design - Logical View

CE0317.0

Notes:

pr

Ex

cl

The logical view involves the determination and listing of all the business processes that
will affect or impact our database. This part of the process needs to include the business
expert.

2-12 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Process Inventory

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Detailed description of all business


processes for application domain
Strictly business-oriented
Implementation independent

Created by application domain expert


Input for application programmers
Must allow application programmers to:
Understand processes for application domain
Develop programs for processes
Must identify all data for processes

References to data elements and data groups in data

inventory
Data inventory also input for application programmers

Allows to verify entity-relationship model


Database designer not involved
Copyright IBM Corporation 2007

Figure 2-10. Process Inventory

CE0317.0

Notes:

cl

As already mentioned before, the process inventory contains a detailed description of all
business processes for the application domain. The descriptions should be completely
business oriented so that they are independent of any implementation considerations.
These are the regular day-to-day activities, including entering new data and modifying
existing data.

Ex

The process inventory is input for the application programmers. It must allow them to
understand the business processes for the application domain and to develop the required
programs, queries, reports, etc.

pr

The descriptions for the business processes must identify all data elements and data
groups for the business processes. Since the data elements and data groups are process
independent, they are not described in the process inventory, but in the data inventory. The
business processes only refer to the data elements and data groups in data inventory.
Therefore, the data inventory is also input for the application programmers.

Except for assisting the application domain expert in verifying the entity-relationship model,
the data base designer is not involved in the establishment of the process inventory.
Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-13

Student Notebook

Examples of business processes

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Business processes that will access or alter the Employee entity:


Hiring a new employee

Terminating an existing employee


Promoting an employee

Transferring an employee from one department to another


Adjusting an employees salary

Performing an annual appraisal of an employee

Providing education or specific training for an employee


Running the weekly payroll for all employees
Changing the marital status of an employee

Granting year-end bonuses to certain employees


Etc.

Copyright IBM Corporation 2007

Figure 2-11. Examples of business processes

CE0317.0

Notes:

cl

A business-domain expert will need to identify and document all of the business processes
that impact the business entities within your enterprise. Some processes may impact only
one business entity. Other processes may impact multiple business entities.

pr

Ex

During the verification of the entity-relationship model for a business process, it is checked
to see which entity types would contain the data affected by the business process. It is also
verified that the entity-relationship model contains all relationship types necessary to
navigate from one entity type to another entity type as required by the business process.

2-14 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Benefits of business modeling

Ah, so
that's how
it all works
together!

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
A
location
has many
departments

Dept

1 11

Many
employees
work on
many
projects

1 M1

Employees

M 1M

Locations

Projects

1 M

Skills

Long-term framework

Copyright IBM Corporation 2007

Figure 2-12. Benefits of business modeling

CE0317.0

Notes:

Here are some of the principal benefits of modeling your data and the business processes
that access your data:

Ex

cl

It facilitates agreement. When everyone agrees that the model represents the way
business processes are really performed, it ensures that all participants have the same
understanding of those processes.

It enhances communication. The model enables clearer communications with all


participants involved in the modeling process.

pr

It provides a base for future growth and enhancement. Over time, the models can be
modified as new applications are developed, and when maintenance of existing
applications becomes necessary. The model serves as a long-term framework
(processes are usually stable and many applications will be developed relative to the
processes) that spans more than one application.

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-15

Student Notebook

Steps to Database Design Storage View

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Problem Statement
Entity Relationship
Model

Process Inventory

Conceptual
View
Data Inventory

Tables

Indexes

Logical Data Structures

Integrity Rules

Storage
View

Logical View

Triggers

Copyright IBM Corporation 2007

Figure 2-13. Steps to Database Design - Storage View

CE0317.0

Notes:

pr

Ex

cl

As part of the conceptual view, the entity-relationship model and the data and process
inventories for the application domain were established. The first step of the storage view
uses the data inventory to construct the entity types and relationship types of the
entity-relationship model and normalizes them. Thus we establish the tables - the
containers for the data of the application domain. Following that will be ancillary database
objects such as integrity rules, indexes, triggers and views (logical data structures).

2-16 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Normalization First normal form


(Eliminate repeating groups)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Denormalized EMPL

EMP
NO
000030
000250
000270
000300

LAST
NAME
KWAN
SMITH
PEREZ
SMITH

WORK
DEPT
GRE
BLU
RED
BLU

DEPTNAME

SKILL1

SKILL2

OPERATIONS
PLANNING 002
MARKETING
PLANNING 011

141
011
415
032

067
447

EMPL_SKILL TABLE

Normalized to 1NF - EMPL


EMP
NO
000030
000250
000270
000300

LAST

KWAN
SMITH
PEREZ
SMITH

WORK
NAME
GRE
BLU
RED
BLU

SKILLN...

DEPTNAME
DEPT
OPERATIONS
PLANNING
MARKETING
PLANNING

EMPNO
000030
000250
000250
000250
000270
000270
000300
000300

SKILL
141
002
011
067
415
447
011
032

SKILLDESC
RESEARCH
BID PREP
NEGOTIATION
PROD SPEC
BENEFITS ANL
TESTING
NEGOTIATION
INV CONTROL

Copyright IBM Corporation 2007

Figure 2-14. Normalization - First normal form

CE0317.0

Notes:

cl

Initial database design may contain anomalies, which could potentially cause data
inconsistencies during SQL change requests (i.e. INSERT, UPDATE and DELETE
statements). In order to ensure that our applications do not impact data integrity, we test
our database design with the rules of normalization.

Ex

First Normal Form: A table is in the First Normal Form if all its attributes, elementary or
composite, can have at most one value.

pr

Continuing in the process of database design: once the entities, their relationships, the
primary keys, and foreign keys are identified, the next step is to define all the non-key
elements or properties for a given entity. Once the columns are defined, it is time to
consider normalizing your tables. There are six different levels of normalization; the first
three levels are generally followed by serious designers seeking a robust data model.

1. Eliminate Repeating Groups - One of the first rules of relational tables is that all the rows
must have the same number of columns, and each column must mean the same thing in
the same sequence in each row.

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-17

Student Notebook

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

In our example of the employee table, if you wanted to represent the fact that each
employee has multiple skills, this would need to be represented in an additional table
where each skill for a given employee is represented in a row (the unique identifier being
the employee number and the skill number). If a new skill is created and an employee
already has the maximum number of skills allocated in the data structure, then the entire
file structure and all the programs have to be changed (in the denormalized file structure). If
this table is normalized, then the new skill simply becomes a new row.

2-18 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Normalization Second normal form

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

(Eliminate the columns that depend only on part of the key)

Normalized - 1NF

Normalized - 2NF

EMPL_SKILL TABLE

EMPNO
000030
000250
000250
000250
000270
000270
000300
000300

SKILL
141
002
011
067
415
447
011
032

SKILLDESC
RESEARCH
BID PREP
NEGOTIATION
PROD SPEC
BENEFITS ANL
TESTING
NEGOTIATION
INV CONTROL

EMPL_SKILL TABLE

EMPNO
000030
000250
000250
000250
000270
000270
000300
000300

SKILL
141
002
011
067
415
447
011
032

DATE
CERT

SKILL DESC TABLE

SKILL
002
011
032
067
141
415
447

SKILLDESC
BID PREP
NEGOTIATION
INV CONTROL
PROD SPEC
RESEARCH
BENEFITS ANL
TESTING

Copyright IBM Corporation 2007

Figure 2-15. Normalization - Second normal form

CE0317.0

Notes:

A table is in the Second Normal Form if:


It is in the First Normal Form

cl

All its elementary non-key attributes are functionally dependent on the entire primary key

Ex

2. Eliminate all attributes (columns) that depend only on part of a multi-valued primary key.

pr

To extend our example above: where we have taken the skills of the corporation and
matched them with the employees in a separate table, we have included the skill
description. Yet, that skill description depends only upon the skill number (not the employee
number/skill number combination). Three problems become apparent:
1. The skill description will be repeated every time the skill is repeated.
2. If this description for the skill changes, then multiple rows will need to be changed,
risking some update anomalies (data inconsistencies due to not changing all the rows).

3. If an employee is the sole source for a given skill and that person leaves the company,
that particular skill and skill description is lost forever (delete anomaly).
Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-19

Student Notebook

These problems can be solved by creating another table that holds the skill number and
skill description.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Notice that these new tables can have more columns than what is illustrated. For example,
the EMPL_SKILL table can have a column that holds information such as the date when
this employee acquired this particular skill. In this example, the new column
(DATE_CERTIFIED) depends upon both the employee and the specific skill.

2-20 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Normalization Third normal form


(Eliminate columns that don't depend on the key at all)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Denormalized EMPL

EMP
NO
000030
000250
000270
000300

LAST
NAME
KWAN
SMITH
PEREZ
SMITH

DEPTNAME

MGRNO ...

OPERATIONS
PLANNING
MARKETING
PLANNING

000080
000010
000020
000010

Normalized to 3NF

EMPL

EMP
NO
000030
000250
000270
000300

WORK
DEPT
GRE
BLU
RED
BLU

LAST
NAME
KWAN
SMITH
PEREZ
SMITH

WORK
DEPT
GRE
BLU
RED
BLU

DEPT

...

DEPTNO
BLU
GRE
RED

DEPTNAME
PLANNING
OPERATIONS
MARKETING

MGRNO
000010
000080
000020

Copyright IBM Corporation 2007

Figure 2-16. Normalization - Third normal form

CE0317.0

Notes:

A table is in the Third Normal Form if:


It is in the Second Normal Form

cl

None of its elementary non-key elements is functionally dependent on other non-key


attributes

Ex

3. Eliminate columns not dependent on the key at all: that is, the column is a fact about
another non-key column.

pr

An example of this is reflected in our Employee table. Notice that we are carrying the
Department Number in the Employee table (necessary to reflect the relationship between
the Employee and Department tables: many employees work for one department), and that
the Department Name column is a fact about the Department Number column (not the
primary key which is the employee number). The same three problems exist here as they
do with the second normal form example (redundant data, update, and delete anomalies)
and can be solved the same way: that is, an additional table where the Department
Number is the primary key, and the Department Name is dependent upon it.

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-21

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Normalization recommendation

Always normalize to Third Normal Form


during the initial design phase!

Then, analyze the table's usage in order


to evaluate the potential benefits of
denormalizing.

Copyright IBM Corporation 2007

Figure 2-17. Normalization recommendation

CE0317.0

Notes:

As in almost everything we do related to computers, there are some reasons to trade off
one thing for another; this is true with the process of normalization.

pr

Ex

cl

Normalization solves many problems, as demonstrated above (it eliminates redundancy,


which saves disk space as well as reducing update and delete anomalies). It also aids in
keeping your data model flexible: a key factor in today's world of rapidly changing business
requirements. However, the downside is that it may impact performance, depending upon
how often and in what manner the data is accessed by DB2 applications.

2-22 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Questions to answer

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

What is the nature of the SQL?


Is the data read-only?

Are updates frequent?

Will tables be frequently accessed together (i.e. joined)?


How often is the statement executed?

Is the SQL used in high-priority applications?


Copyright IBM Corporation 2007

Figure 2-18. Questions to answer

CE0317.0

Notes:

pr

Ex

cl

The decision whether to denormalize or not is very dependent upon an analysis of the SQL
statements that will be accessing the table you are desinging. The answers to the
questions above help to determine whether data should be denormalized. If data is
read-only, carrying some redundant data may improve access to the data. If tables are
often accessed together, carrying some redundant data may eliminate having to join tables
in some cases.

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-23

Student Notebook

Normalization/Denormalization example
(EMPNO, WORK DEPT, LAST, FIRST, JOB)
100,000 rows
87 chars / row

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EMPL

(DEPTNO, DEPTNAME, MGRNO)


400 rows
76 chars / row

DEPT

Transaction rate 20,000 per day, two tables accessed


SELECT LAST, FIRST, DEPTNAME
FROM EMPL A, DEPT B
WHERE A.DEPT = B.DEPT AND
EMPNO = :hv ;
Copyright IBM Corporation 2007

Figure 2-19. Normalization/Denormalization example

CE0317.0

Notes:

pr

Ex

cl

How could you reduce the number of two-table accesses?

2-24 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Denormalization example

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Carry DEPTNAME in both tables


EMPL

(EMPNO, WORKDEPT, LAST, FIRST, JOB, DEPTNAME)


100,000 rows
87 chars / row

DEPT

(DEPTNO, DEPTNAME, MGRNO)


400 rows
76 chars / row

Transaction rate 20,000 per day, one table accessed


SELECT LAST, FIRST, DEPTNAME
FROM EMPL
WHERE EMPNO = '000010'
Copyright IBM Corporation 2007

Figure 2-20. Denormalization example

CE0317.0

Notes:

pr

Ex

cl

Denormalization could improve the performance of our critical transaction. An alternative is


to carry DEPTNAME (column length = 36 bytes) in both tables: in DEPT, where it can be
used by other applications; and in EMPL, where it can improve the performance of our
critical transaction. The objective is to avoid the performance cost of a join, and thereby
improve the response time for this critical transaction.

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-25

Student Notebook

Costs and benefits of denormalization


Cost

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Storage: 100,000 rows

36 characters

= 3,600,000 characters (Approx 900 4K pages)

Additional updates if DEPTNAME changes


Benefit

Save 20,000 join operations per day

- Quicker response time on critical transaction

- More resources available for other users and applications


Copyright IBM Corporation 2007

Figure 2-21. Costs and benefits of denormalization

CE0317.0

Notes:

The costs are 900 additional pages of storage (4K each) and additional updates to the
second DEPTNAME column.

pr

Ex

cl

The benefit is avoiding joins of the EMPL and DEPT tables 20,000 times each day. This
will mean a faster response time for the critical transaction, and also more systems
resources being made available for other work in the system.

2-26 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

From model to storage objects

Business entity

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DB2 table

Table 1

Table 3

Entity attributes

Columns in table

Table 2

Entity instances

Rows in table

Entity 1

Entity 2

DDL created by an automated


tool or by a manual process

Entity 3

Copyright IBM Corporation 2007

Figure 2-22. From model to storage objects

CE0317.0

Notes:

pr

Ex

cl

When the ER model is complete, the next step is to transform the model objects and
specifications into storage objects in the DB2 catalog. With many ER tools, this is an
automatic process. The tool will use the specifications in the model and automatically
create the SQL DDL (Data Definition Language) statements to create the tables, with the
columns, proper relationships between the tables, etc.

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-27

Student Notebook

One table per business entity

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EMPL

DEPT

PROJ

Copyright IBM Corporation 2007

Figure 2-23. One table per business entity

CE0317.0

Notes:

pr

Ex

cl

Each of the business entities that were identified and documented during the modeling
process become the respective tables in DB2. The employee entity becomes the EMPL
table, the department entity becomes the DEPT table, etc.

2-28 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

One Column for each data element


LASTNAME WORKDEPT JOB ...

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EMPNO

EMPL

DEPTNO

DEPTNAME MGRNO

ADMRDEPT ...

DEPT

PROJNO

PROJNAME DEPTNO

RESPEMP ...

PROJ

Copyright IBM Corporation 2007

Figure 2-24. One Column for each data element

CE0317.0

Notes:

cl

Each of the business entities that were identified and documented during the modeling
process have certain characteristics or attributes. In other words, there are certain things
that we want to track for each entity. For an employee, we want to have their name, their
date of birth, home address, telephone numbers, etc. We will give them a salary,
determine their job responsibilities, assign them to a particular department, etc.

Ex

All of these data elements related to the employee become the columns in the EMPL table.

pr

After the table has been defined, with all of its column definitions, keys and relationships, it
will be populated with instances or occurrences of the business entity with which the table
is associated. So, Bob Jones and Sally Smith will become rows in the EMPL table.

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-29

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Names

First character
must be:
A-Z, $, @, #
Remaining:
A-Z, $, @, #, 0-9, _

Every table and


every column
must be named

Table name
unique to
owner/schema

Column
names
unique to
table

Copyright IBM Corporation 2007

Figure 2-25. Names

CE0317.0

Notes:

Column and table names must conform to the following rules, as shown in the visual
above:

cl

Every table, and every column within a table, must be given a name
Each table name must be unique within a schema

Ex

Each column in a table must be unique

Any name in DB2 may include the alphabetic characters A - Z, the numbers 0 9, and
the special characters @, #, $, and _ (These are known as the standard characters.)

pr

A DB2 name may begin with any of the characters in the bullet point above, EXCEPT
for the underscore

2-30 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Name examples
OK as is

SOC_SEC_#

OK as is

"SAL+COM"

Special character

"UPDATE"

A reserved word

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

TABLE_3

Copyright IBM Corporation 2007

Figure 2-26. Name examples

CE0317.0

Notes:

cl

When non-standard characters are used in table or column names, or when using a DB2
reserved word (i.e., a word that has a special meaning to DB2, part of the SQL syntax), the
names must be enclosed in double quotes. When DB2 sees the double quotes, it will
accept the non-standard character, and/or treat the name as a literal, rather than interpret it
as a keyword in SQL syntax.

pr

Ex

In the example above, the first two table names are valid as written, since they contain only
characters described as being included in the standard naming set on the previous slide.
But third and fourth names shown above require quotation marks. We need to enclose
SAL+COM in double quotes because the + is not part of the standard character set for DB2
names. Likewise, we need to use the double quotes around UPDATE, which is a reserved
word in DB2. UPDATE, when not enclosed by double quotes, will be interpreted as a
request to invoke the UPDATE statement in SQL.
For a complete listing of DB2 reserved words, refer to the SQL Reference manual for your
DB2 environment (z/OS or LUW).

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-31

Student Notebook

Table name construction


SIMPLE table-name

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Schema/Owner
PROD

EMPL

Qualified table-name
PROD.EMPL

Table user

(ANY_OTHER_ID)

PROD.EMPL
Copyright IBM Corporation 2007

Figure 2-27. Table name construction

CE0317.0

Notes:

cl

Table names consist of two parts, separated by a period. If your userid is identical to the
table qualifier (very often the case because you own the table), you can use just the simple
table name within an SQL statement. In the visual above, a user with an authorization id of
anything other than PROD, must use the qualified table name, PROD.EMPL, in his/her
SQL statements.

pr

Ex

For example, a user who is known to the system as PROD can refer to the simple table
name SELECT * FROM EMPL; A user that currently has an authorization id that is not
equal to PROD must enter the query as SELECT * FROM PROD.EMPL;

2-32 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Column names Construction

Assigned
Column name
LASTNAME

Table name
EMPL

Qualified column name


EMPL.LASTNAME

Copyright IBM Corporation 2007

Figure 2-28. Column names - Construction

CE0317.0

Notes:

pr

Ex

cl

Column names can be optionally prefixed (in any query) with their table name. In a join
operation (a SELECT that accesses multiple tables) the column names must be qualified if
those column names appear in more than one of the tables accessed in the query.

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-33

Student Notebook

Correlation names

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Given: table EMPL, with columns EMPNO, NAME, DEPTNO


and table DEPT, with columns DEPTNO, DEPTNAME
SELECT DEPTNO, DEPTNAME
FROM EMPL, DEPT
WHERE DEPTNO = DEPTNO

SQL Error! Ambiguous


column name DEPTNO

SELECT EMPL.DEPTNO, DEPT.DEPTNAME


OK column names
FROM EMPL, DEPT
are qualified by
WHERE EMPL.DEPTNO = DEPT.DEPTNO
the table names

SELECT E.DEPTNO, D.DEPTNAME


FROM EMPL E, DEPT D
WHERE E.DEPTNO = D.DEPTNO

OK column names
are qualified by the
correlation names

Copyright IBM Corporation 2007

Figure 2-29. Correlation names

CE0317.0

Notes:

cl

A correlation name is a substitute (usually abbreviated) name that you give to a table in an
SQL SELECT statement. In the FROM clause, if you code a separate character string after
the real table name (and before the comma delimiting the name of the next table
accessed), that becomes a correlation name. The other parts of your query can now refer
to the abbreviated table name, rather than the real table name.

pr

Ex

In the second statement above, the user is qualifying the column DEPTNO with the real
table names EMPL and DEPT. In the third statement, the user is substituting the letters E
and D for the tables EMPL and DEPT. This technique uses what we term correlation
names.

The qualification of column names in a join is required if any columns referred to within the
query are identically named. To do this, the query writer may employ either the technique
of using real table names, or correlation names, to qualify the columns.
It is commonly recommended to use either of the techniques in a join, even if there are no
identically-named columns in the query, to self-document and make the query easier to
read and understand.
2-34 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Data types

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Every column is assigned a specific data type and size


Categories of data types

Numeric
Character
Date/Time
Graphic
LOBs (Large Objects)
User-Defined Distinct Types
XML

Copyright IBM Corporation 2007

Figure 2-30. Data types

CE0317.0

Notes:

pr

Ex

cl

These are all of the general categories of DB2 data types. The next few visuals will
describe the more specific data types within each general category.

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-35

Student Notebook

Numeric formats

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Whole
numbers
SMALLINT
INTEGER
BIG INTEGER

Approximate
numbers
REAL - Single
FLOAT - Double

Fractional
numbers
DECIMAL
(p,s)

Copyright IBM Corporation 2007

Figure 2-31. Numeric formats

CE0317.0

Notes:

cl

There are three integer (whole number) data types. SMALLINT can be used for numbers
between minus 32K and plus 32K. INTEGER accommodates numbers between minus 2
GB and plus 2 GB. More recently BIGINT was added, in order to handle even larger
numbers from minus 8 exabytes to plus 8 exabytes.

Ex

Fractional numbers may be represented by the DECIMAL data type. Precision (p)
determines how many digits (up to a maximum of 31) to be reserved for storing/expressing
the number. Scale (s) must be a non-negative (i.e. either 0 or positive), and indicates how
many places to reserve to the right of the decimal point when displaying the result.

pr

Floating point describes a system for representing real numbers which supports a wide
range of values. Numbers are in general represented approximately to a fixed number of
significant digits and scaled using an exponent.

2-36 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Character string formats

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

CHAR (X)
Fixed length

X bytes

VARCHAR (X)
Variable, up to a
max. of X bytes

X bytes

Copyright IBM Corporation 2007

Figure 2-32. Character string formats

CE0317.0

Notes:

Ex

cl

Fixed character (CHAR) is always stored as a value with X number of bytes. Variable
character (VARCHAR) is stored as the real length of the value for the column, up to a
maximum of X bytes. In order for DB2 to determine the actual length and location of a
VARCHAR, DB2 first creates (at LOAD or INSERT time), and then examines (when
accessing the existing rows) a pointer/displacement within the row for each VARCHAR
column in the row.

pr

The general recommendation is to not user VARCHAR unless the nature of the data stored
in a column is truly going to be variable in nature (such as comments, or another type of
data that could be either quite long or quite short).

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-37

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Date/Time formats

Time
'HH:MM:SS'
'HH:MM AM / PM'
'HH.MM.SS'

Date
'YYYY-MM-DD'
'MM/DD/YYYY'
'DD.MM.YYYY'

Timestamp
'YYYY-MM-DD-HH.MM.SS.NNNNNN'

Copyright IBM Corporation 2007

Figure 2-33. Date/Time formats

CE0317.0

Notes:

cl

There are a number of formats for the display of date and time information: Some
common examples are ISO, USA, EUR (for Europe), and JIS (Japanese International
Standard).

pr

Ex

Internally, date, time and timestamp information is stored in a single format packed
decimal. The designations of ISO, USA, etc. only affects how the information is formatted
when displayed to the user or returned to the application program. At the time DB2 is
installed, the default format for date and time is set by the system programmer who is
configuring your DB2 system. That format can be overridden by the application
programmer or query writer, as needed.

DB2 accepts any of the formats (USA, ISO, EUR, JIS) in an SQL statement (character
string representation of dates, times, and timestamps). Thus, all three of the queries below
would execute successfully:
SELECT EMPNO, LASTNAME, BIRTHDATE
FROM EMPLOYEE
2-38 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

WHERE BIRTHDATE = 1950-12-18 ;


SELECT EMPNO, LASTNAME, BIRTHDATE
FROM EMPLOYEE
WHERE BIRTHDATE = 12/18/1950 ;

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SELECT EMPNO, LASTNAME, BIRTHDATE


FROM EMPLOYEE

pr

Ex

cl

WHERE BIRTHDATE = 18.12.1950 ;

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-39

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Graphic string formats

GRAPHIC (X)

VARGRAPHIC
(X)

For character data, not images

Copyright IBM Corporation 2007

Figure 2-34. Graphic string formats

CE0317.0

Notes:

cl

The word graphic may connote to some users the idea of images bit streams of data
representing the pixels in some kind of picture, map, diagram, etc. But in DB2 the term
graphic does not have this meaning at all.

Ex

Rather, graphic data types are analogous to the CHAR data types, except that in the
graphic data types it takes two bytes to represent each character being stored. The
alternate name for these data types is double byte character sets (DBCS).

pr

Perhaps the most common situation requiring the use of graphic data types is the nature of
Oriental languages, such as Japanese, Chinese, Korean, etc. The Indo-European
languages (French, Italian, Spanish, English, German, Russian, etc.) utilize a set of
characters that are not symbolic the do not represent anything intrinsically. Rather, the
letters only have meaning when strung together in specific combinations. For instance, the
letters A, E, L and P by themselves does not mean anything.
But when a person literate in English sees APPLE, then those same characters denote a
fruit that is somewhat spherical in nature, red/yellow/green when ripe, and grows on trees.
But in the Oriental languages, each object, each action, each word has a specific character
2-40 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

by which it is represented. Therefore, there are as many characters in those languages as


there are words in its vocabulary.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Linguists have judged that in order to have a detailed, effective means of communicating, a
language needs to have approximately 20,000 - 2,5000 words. Therefore, the Oriental
languages need that many characters in order to provide meaningful communication. In
contrast, the Indo-European languages can be accommodated by a much smaller number
of characters, as their alphabets consist in the neighborhood of 22 to 30 letters.

pr

Ex

cl

One byte, consisting of 8 bits, yields 256 (2 raised to the 8th power) combinations.
Therefore it will support all the letters A Z (both upper and lower case), the numbers 0
9, and a relatively robust set of punctuation marks and other special symbols. In contrast,
the Oriental languages need to support a much larger character set. Two bytes, consisting
of 16 bits, yields the necessary amount of combinations (64K, or 2 raised to the 16th power)
to support the larger character set.

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-41

Student Notebook

Large objects The need

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Action
News

DB2
by the Book

B L OB
I
N
A
R
Y

A
R
G
E

J
E
C
T

C L OB

D B C L OB

H
A
R
A
C
T
E
R

O
U
B
L
E

A
R
G
E

J
E
C
T

Y
T
E

H A
A R
R G
A E
C
T
E
R

J
E
C
T

DB2

Copyright IBM Corporation 2007

Figure 2-35. Large objects - The need

CE0317.0

Notes:

In today's world, there is a need to store large data objects.

cl

BLOBs (Binary Large Objects) can store images and sound data (images, video, audio,
etc).

Ex

CLOBs (Character Large Objects) can store large amounts of character data (usually
text).

pr

DBCLOBs (Double-Byte Character Large Objects) can store large amounts of


double-byte character data (usually text in an Oriental language).

2-42 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Create table

create table artists


(artno
smallint
name
varchar(50)
classification char(1)
bio
clob(100K)
picture
blob( 2M)

not null,
with default 'abc',
not null,
logged,
not logged compact)

Copyright IBM Corporation 2007

Figure 2-36. Create table

CE0317.0

Notes:

Ex

cl

The general format of the CREATE statement is similar, regardless of the type of DB2
object that you are defining. The syntax is CREATE object-type object-name options. To
create a table, you would code CREATE TABLE tablename, to create a view you would
code CREATE VIEW viewname, to create a trigger, you would code CREATE TRIGGER
triggername, etc. Of course, in order to give a complete definition, there are other options
and keywords that must follow the object name.

pr

All CREATE statements result in one or more rows being inserted into one or more of the
DB2 catalog tables (such as SYSTABLES, SYSVIEWS, SYSINDEXES, SYSKEYS,
SYSTRIGGERS, etc.)

In the visual above there is a relatively simple example of a CREATE TABLE statement. All
of the column definitions follow the tablename in the syntax, enclosed by parentheses.
Notice that a table can consist of many columns, each storing a different data type:
numeric SMALLINT,
character CHAR, VARCHAR,
Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-43

Student Notebook

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

large objects CLOB, BLOB.

2-44 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

User-defined Distinct Data Types


and User-Defined Functions
DB2 built-in
Functions

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DB2 built-in
Data Types

Customer
Unique
Data Types

Customer
Unique
Functions

Copyright IBM Corporation 2007

Figure 2-37. User-defined Distinct Data Types and User-Defined Functions

CE0317.0

Notes:

cl

DB2 automatically provides its users with a wide variety of data types (known as built-in
data types). However, often there arises a situation where the requirements of the
business application cannot be met with just those built-in or DB2-provided data types.

pr

Ex

For example, an enterprise may be international in scope, having to deal with the
currencies of different nations and different measuring systems (metric vs. English). In that
case, it may become necessary for the DBA to create a new data type, known as
user-defined distinct data type (abbreviated as UDT). It usually then also becomes
necessary for a programmer to write code (known as a user-defined function, or UDF) in
order to take full advantage of the newly created UDT. More about that later in this lecture.

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-45

Student Notebook

User-defined Distinct Types

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DECIMAL
(9,2)

EMPNO

LASTNAME

US_SAL

CAN_SAL

000010
000020
000030

HAAS
THOMPSON
KWAN

52750.00
41250.00
38250.00

31000.86
293.79
29843.29

I don't want the


users to try to add
Canadian dollars to
US dollars!

US_COMM CAN_COMM
4220.00
3300.00
3060.00

344.00
9487.00
4353.00

DBA

Copyright IBM Corporation 2007

Figure 2-38. User-defined Distinct Types

CE0317.0

Notes:

Ex

cl

In this example, we have a company whose employees receive parts of their salary and
parts of their commissions in two currencies (US and Canadian dollars). If the data type of
each currency column was DECIMAL, DB2 would let a user add together any of the
decimal columns, as well as allowing the user to compare a column holding US dollars
against a column containing Canadian dollars.

pr

Since the DBA knows that the data in these columns should not be allowed to mingle, the
User-Defined Distinct Types will be defined in order to address this problem.

2-46 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Creating a User-defined Distinct Type (UDT)

CREATE DISTINCT TYPE


CAN_DOL AS DECIMAL (9,2)
WITH COMPARISONS

DBA

Copyright IBM Corporation 2007

Figure 2-39. Creating a User-defined Distinct Type (UDT)

CE0317.0

Notes:

Ex

cl

A UDT is created with a CREATE SQL statement. The new data type must be based on an
existing data type. In this example, the DBA is creating a data type called CAN_DOL which
is to have the characteristics of (and be based on) the DECIMAL data type. This is known
as strong typing. In other words, the new data type CAN_DOL is very similar to or
behaves very much like DECIMAL (9,2).

pr

A column assigned this type will allow up to 9 total digits with 2 of those digits to the right of
the decimal point. The WITH COMPARISONS clause allows two columns of this data type
to be compared to each other with logical operators. (... WHERE CAN_SAL > CAN_COMM
...).

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-47

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Using the UDT

CREATE TABLE EMPL


(EMPNO
CHAR(6)
LASTNAME CHAR(15)
US_SAL
DEC(9,2)
CAN_SAL CAN_DOL
US_COMM DEC(9,2)
CAN_COMM CAN_DOL
.
.

DBA

NOT NULL,
NOT NULL,
,
,
,
,

Copyright IBM Corporation 2007

Figure 2-40. Using the UDT

CE0317.0

Notes:

pr

Ex

cl

UDTs can be used in a CREATE or ALTER statement just like other data types. In the
example above, the columns CAN_SAL and CAN_COMM have the data type of
CAN_DOL, which was previously defined by the DBA.

2-48 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Creating a User-Defined Function

CREATE FUNCTION "+"


(CAN_DOL, CAN_DOL)
RETURNS CAN_DOL
SOURCE "+" (DECIMAL, DECIMAL)

DBA

Copyright IBM Corporation 2007

Figure 2-41. Creating a User-Defined Function

CE0317.0

Notes:

cl

Functions can be created that will allow us to add, subtract, SUM, AVG, and so forth,
columns with the CAN_DOL data type. Creating a User-Defined Function (UDF) is a very
straightforward process.

pr

Ex

In this example, the DBA is creating a function called "+" (plus). It takes as input two
columns of the CAN_DOL data type and returns a value in the CAN_DOL data type. What
happens to the two inputs is the same thing that happens to any two decimal inputs to the
standard "+" function that comes with DB2 (the actions this new function takes are to be
SOURCEd on the existing "+" function). By specifying SOURCE some_function, we are
telling DB2 to make the new function do what the some_function does, but with these new
data types.
This kind of UDF is known as a sourced function, because it was based upon a built-in
function that comes natively with the DB2 software when the RDBMS is installed. So by
coding sourced functions, we manipulate Canadian dollars as we do US dollars (add,
subtract, multiply, divide, etc.) However, we cannot yet exchange or convert or compare
those two data types, as they are distinct from one another.
Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-49

Student Notebook

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

A programmer will need to write a conversion program (either in an external language, or in


SQL as a native procedure). Only then will applications be able to convert from CAN_DOL
to US_DOL, or compare those two different datatypes. So a stored procedure (SP) or UDF
must be coded in order to fully enable the function of the UDTs.

2-50 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Nulls

NULLS: A
column
constraint
telling DB2
what it should
do when the
value for a
column is
unknown

NOT NULL

NOT NULL WITH DEFAULT


"nullable"

Copyright IBM Corporation 2007

Figure 2-42. Nulls

CE0317.0

Notes:

cl

NULL has the meaning of unknown, or the empty result set. It does NOT mean blanks for
character date and zeroes for numeric data. Be aware that NULL is the default, so that
when you omit the third parameter in a column definition, that column will allow the NULL
value.

Ex

There are three null characteristics:

NOT NULL Column must always have a value.

NOT NULL WITH DEFAULT Column must always have a value - if we don't supply
one, DB2 does.

pr

"nullable" Column can be marked as having an 'unknown value'.

Please note that the way you code a column definition to allow the null attribute is to leave
the third parameter of the column blank. In other words, you simply code the column name
and data type, followed by a comma (delimiting the beginning of the next column to be
defined in the table). In fact, if you key in the word NULL as the third parameter for a
column definition, it results in a syntax error.
Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-51

Student Notebook

It is generally recommended that you do not define columns as being nullable, unless that
is specifically dictated by the database model or the application requirements. Here are the
reasons for avoiding nulls:
1. A null attribute byte takes up one byte of space for each column defined as nullable.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

2. More importantly, DB2 has to do extra work in navigating the row, and checking the null
attribute byte (CPU cycles)
3. A single null value can invalidate a major arithmetic calculation

pr

Ex

cl

4. Programmers usually need to write extra code for error checking and condition handling
when nulls are possibly present.

2-52 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Related data

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

A row in one table may be


logically associated with a
row in another table
Example: Each employee
works for one department

A ROW IN ONE TABLE can


carry a value from some
table's unique key

Example: Each employee row


contains the value of the
employee's department KEY

Copyright IBM Corporation 2007

Figure 2-43. Related data

CE0317.0

Notes:

pr

Ex

cl

During the modeling process, the relationships between tables were noted and
documented. The reason you create a relationship between two tables is so that data
entered into one table refers to corresponding data in another table. A row in one table
may be associated with a row or rows in another table. Those relationships can be
specifically defined in DB2.

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-53

Student Notebook

Identify Primary Keys

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Well, it's time to


assign each employee
a unique identifier.

We have two Larry


Smiths working here.
How are we going to
tell them apart in the
employee table?

Copyright IBM Corporation 2007

Figure 2-44. Identify Primary Keys

CE0317.0

Notes:

The primary key is the key that best identifies the data being stored in the table.

Ex

cl

Each occurrence or instance in a table (i.e. a row) must be uniquely identified. Some
thought and analysis should go into the decision as to how to uniquely identify each
instance. That column value (simple key), or that combination of column values
(compound key) which you choose to distinguish one row in the table from all of the other
rows in the table is called the primary key.

pr

For example, our data model might result in the creation of an EMPLOYEE table. How can
we distinguish one employee from every other employee? It is very possible that different
people in the enterprise have the same surname and the same given name. There may be
two Larry Smiths, therefore eliminating the columns LASTNAME and/or FIRSTNAME as
viable candidates for a primary key.
Perhaps a more suitable candidate for the primary key may be Social Security Number, or
state drivers license number. Better still, each employee at the time of hiring can be given
a unique identifier (number or character string).

2-54 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Primary Keys

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Types

SIMPLE - 1 column
COMPOUND multiple columns

Characteristics of a Primary Key

Must contain unique values


All columns in the key must be NOT NULL
A table may have only one primary key

Copyright IBM Corporation 2007

Figure 2-45. Primary Keys

CE0317.0

Notes:

A key is termed simple if it consists of values from only one column in the DB2 table. A key
is said to be compound if it consists of values from two or more columns in the DB2 table.

pr

Ex

cl

A primary key must contain unique values, and cannot be NULL. A DB2 table may have
one, and only one, primary key defined.

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-55

Student Notebook

Unique Keys

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Types
SIMPLE - 1 column
COMPOUND multiple columns

Characteristics of a Unique Key

Must contain unique values


A table may have multiple unique keys
Regarding the NULL value
UNIQUE one row may contain NULL
UNIQUE WHERE NOT NULL multiple rows may
contain NULL, and the key is still considered to be
unique by this definition
Copyright IBM Corporation 2007

Figure 2-46. Unique Keys

CE0317.0

Notes:

cl

A key is termed simple, if it consists of values from only one column in the DB2 table. A
key is said to be compound if it consists of values from two or more columns in the DB2
table.

pr

Ex

A unique key must contain unique values. A DB2 table may have more than one unique
key defined in it. If UNIQUE is specified, then only one row is allowed to contain the NULL
value. If UNIQUE WHERE NOT NULL is specified, then multiple rows may contain NULLs
in the key.

2-56 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Foreign Key

A column or set of columns that


contains values from some table's
unique key (called a parent) and is
dependent on them.

Copyright IBM Corporation 2007

Figure 2-47. Foreign Key

CE0317.0

Notes:

cl

Foreign keys are incorporated into our tables to define the relationships between tables. A
column from one table may contain the same information stored in the column of a different
table. It is our desire to track this relationship, and to define a set of rules for this
relationship. That is the very core of Referential Integrity (or RI, for short), which we will
discuss shortly.

pr

Ex

In essence, a foreign key is a column or set of columns that contains values from some
table's parent key (either a primary key or a unique key) and is dependent on them.
Parent key is the general term we give to the [either] primary or unique key upon which the
foreign key is dependent. A good analogy is to think of it as a parent/child relationship.
One parent may have many dependents, or children. A parent key often has many foreign
keys depending upon (and related) to it.

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-57

Student Notebook

Which table gets the Foreign Key?


A
location
has many
departments

Locations

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Dept

1 11

1 M1

Many
employees
work on many
projects

Employees

M 1M

1 M

One row in the parent


table has multiple rows
in the dependent table

Skills

Projects

The foreign key is


placed into the
dependent table

Copyright IBM Corporation 2007

Figure 2-48. Which table gets the Foreign Key?

CE0317.0

Notes:

Ex

cl

Which table carries the foreign key is determined from the business model. For example,
there is a relationship between the Employees table and the Dept table. Each employee is
assigned to a department. There is a column (WORKDEPT) in the Employee table that
stores the number of the department to which the employee is assigned. The DEPTNO
column in the Dept table is the primary key, and a parent key for WORKDEPT in the
Employee table. In other words, the WORKDEPT foreign key depends upon the DEPTNO.

pr

That relationship is called a referential constraint. Many Employees may work for the same
department, so there is a 1 to many relationship between Dept and Employee. The table
that contains the many is the table that contains the Foreign Key.

2-58 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Primary Keys, Foreign Key

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EMPL
PK parent
EMPNO

DEPT

FK

LASTNAME

FIRSTNAME

WORKDEPT

REMAINING
COLUMNS
SUPPRESSED

FK child/dependent

PK

DEPTNO

child/dependent

DEPTNAME

MGRNO

DIVISION

parent

Copyright IBM Corporation 2007

Figure 2-49. Primary Keys, Foreign Key

CE0317.0

Notes:

cl

The protocol of Referential Integrity states that when a foreign key is defined, DB2 will not
let a row be added to the dependent table unless the Foreign Key value for that row already
exists in the parent table.

Ex

In the diagram above are two tables, EMPL and DEPT, with two different referential
constraints defined between them.

pr

In the EMPL table, EMPNO is the primary key. It is also the parent key in an RI
constraint, because the MGRNO column in the DEPT table is the foreign key that
depends upon EMPNO. This relationship is highlighted in red in the visual above. In
this RI relationship, EMPL is the parent table and DEPT is the child or dependent table.
In the DEPT table, DEPTNO is the primary key. It is also the parent key in an RI
constraint, because the WORKDEPT column in the EMPL table is the foreign key that
depends upon DEPTNO. This relationship is highlighted in green in the visual above.
In this RI relationship, DEPT is the parent table and EMPL is the child or dependent
table.

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-59

Student Notebook

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

In the EMPNO/MGRNO relationship, DB2 will not allow a DEPT row to be inserted, unless
the value for MGRNO already exists in the EMPNO column of a row in the EMPL table. In
the DEPTNO/WORKDEPT relationship, DB2 will not allow an EMPL row to be inserted,
unless the value for WORKDEPT already exists in the DEPTNO column of a row in the
DEPT table.

2-60 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Creating a relationship between tables

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

CREATE TABLE klw{


(DEPTNO CHAR(3) NOT NULL,
DEPTNAME CHAR(20) NOT NULL,
..................
PRIMARY KEY (DEPTNO))
--CREATE TABLE ltws
(EMPNO CHAR(6) NOT NULL,
LASTNAME CHAR(20),
FIRSTNAME CHAR(15),
WORKDEPT CHAR(3),
..................
CONSTRAINT DEPT_VALUE
FOREIGN KEY (WORKDEPT)
REFERENCES DEPT
ON DELETE RESTRICT)
Copyright IBM Corporation 2007

Figure 2-50. Creating a relationship between tables

CE0317.0

Notes:

cl

You can define Referential Integrity when you CREATE the table. It can also be added after
the tables have been created, via the ALTER TABLE statement. In the visual above is an
example of defining a referential constraint between two tables.

Ex

The first CREATE TABLE statement defines the DEPT table with DEPTNO as the primary
key.

pr

The second CREATE TABLE statement defines the EMPL table with a foreign key. The
name of the referential constraint is DEPT_VALUE, and that name is determined by the
CONSTRAINT keyword. The WORKDEPT column is designated as the foreign key. The
REFERENCES keyword points to the parent table in the relationship (DEPT). By default,
the parent key is normally the primary key of the table (as in this case, the parent key is
the DEPTNO column in the DEPT table). Thus the WORKDEPT column in the EMPL table
is the foreign key, and the DEPTNO column in the DEPT table is the parent key.

If the parent key is a column (or set of columns) other than the primary key, then the
syntax must specify the column(s), enclosed in parentheses, immediately after the parent
tablename. For example, if the foreign key being defined depended upon a column name
Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-61

Student Notebook

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

PARENT_KEY in the DEPT table, that part of the clause would be . REFERENCES
DEPT (PARENT_KEY).

2-62 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

The rules DB2 enforces to in order to maintain


Referential Integrity

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

A foreign key value must match a unique


key value or be null
Foreign Key

EMPNO
000010
000020
000030

LASTNAME
HAAS
THOMPSON
KWAN

FIRSTNAME
CHRISTINE
MICHAEL
SALLY

WORKDEPT
RED
BLUE
GREEN

GOVT_ID
888-88-2794
888-89-4261
888-88-9456

SALARY
52750.00
31000.00
33000.00

Primary/Unique
(Parent) Key
DEPTNO
BLUE
GREEN
RED

DEPTNAME
PLANNING
OPERATIONS
MARKETING

MANAGER
000020
000080
000010

DIVISION
EASTERN
WESTERN
EASTERN

Copyright IBM Corporation 2007

Figure 2-51. The rules DB2 enforces to in order to maintain Referential Integrity

CE0317.0

Notes:

cl

Primary key values cannot be null. A foreign key contains a set of values that already exists
in a parent key, which is a table's unique or primary key (usually another table, but not
always the case).

pr

Ex

For instance, when a new employee is hired if the HR clerk attempted to insert the new row
and have YELLOW as the value for column WORKDEPT, he/she would receive an error
message from DB2. In order for the INSERT to work, the DEPT table would first need to
have a row with DEPTNO = YELLOW inserted into it.

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-63

Student Notebook

Terminology

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Referential constraint:
the limiting of a set of
foreign key values
to a set of
parent key values

Referential integrity:
the automatic enforcement
of referential constraints

Copyright IBM Corporation 2007

Figure 2-52. Terminology

CE0317.0

Notes:

cl

The referential constraint in essence defines the relationship between the two tables
how are they related to each other, which column from each table shares common data,
and which table is the parent and which is the child or dependent.

pr

Ex

Referential integrity is the enforcement of the rules regarding these relationships.

2-64 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Processing rules enforced to


maintain Referential Integrity

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Uempty

Every parent key


value remains unique
and is not null

Every foreign key


value matches a
parent key value
or is null

Copyright IBM Corporation 2007

Figure 2-53. Processing rules enforced to maintain Referential Integrity

CE0317.0

Notes:

When you define a referential constraint, referential integrity causes DB2 to enforce these
rules:

cl

1. The key values in a parent key (whether it is a primary key, or just a unique key), must
all be unique and not null.

pr

Ex

2. The key values in a foreign key must either match an existing value in the parent key, or
be null.

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-65

Student Notebook

Primary Key violation


LASTNAME
HAAS
THOMPSON
KWAN

FIRSTNAME
CHRISTINE
MICHAEL
SALLY

GOVT_ID
888-88-2794
RED
BLUE 888-89-4261
GREEN 888-88-9456

SALARY
52750.00
31000.00
33000.00

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EMPNO
000010
000020
000030

WORK
DEPT

DEPTNO
BLUE
GREEN
RED

DEPTNAME
PLANNING
OPERATIONS
MARKETING

MANAGER
000020
000080
000010

DIVISION
EASTERN
WESTERN
EASTERN

New employee - INSERT FAILS


| 000010 | DEAN | ANN | BLU | 888-32-3265 | 33250.00
Copyright IBM Corporation 2007

Figure 2-54. Primary Key violation

CE0317.0

Notes:

Why did the insert fail?

pr

Ex

cl

The failure is caused by a duplicate EMPNO value (primary key). 000010 is already
assigned to Christine Haas. Attempting to add Ann Dean with the same EMPNO value
returns an SQL error code.

2-66 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Foreign Key violation


LASTNAME
HAAS
THOMPSON
KWAN

FIRSTNAME
CHRISTINE
MICHAEL
SALLY

WORK
DEPT
DEP

GOVT_ID
888-88-2794
RED
BLUE 888-89-4261
GREEN 888-88-9456

SALARY
52750.00
31000.00
33000.00

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EMPNO
000010
000020
000030

DEPTNO
BLUE
GREEN
RED

DEPTNAME
PLANNING
OPERATIONS
MARKETING

MANAGER
000020
000080
000010

DIVISION
EASTERN
WESTERN
EASTERN

New employee - INSERT FAILS!


| 000110 | DEAN | ANN | BROWN | 888-32-3265 | 33250.00

Copyright IBM Corporation 2007

Figure 2-55. Foreign Key violation

CE0317.0

Notes:

Why did this insert fail? We have now specified a unique value 000110 for Ann Dean as a
new employee. However, now we have a different problem.

pr

Ex

cl

The value for the department to which Ann is being assigned, BROWN, does not exist in
the DEPT table. In order to add a row to EMPL, the value for column WORKDEPT must
already exist in the DEPTNO column of the DEPT table.

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-67

Student Notebook

Delete rules

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

When a row whose referenced unique key value is


deleted, what should be done with the referencing rows?

The table designer chooses one of the following DELETE RULES:


CASCADE
SET NULL

RESTRICT/NO ACTION

Copyright IBM Corporation 2007

Figure 2-56. Delete rules

CE0317.0

Notes:

There are three delete rules to choose from. We will discuss them in more detail on the
following visuals.

pr

Ex

cl

ON DELETE NO ACTION is the default in DB2 LUW. In DB2 z/OS, you can specify the
default to be either to ON DELETE RESTRICT or ON DELETE NO ACTION, by means of
setting the special register CURRENT RULES.

2-68 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Delete rules ON DELETE CASCADE


DEPT

DEPTNAME
PLANNING
OPERATIONS
MARKETING

MANAGER
000020
000080
000010

DIVISION
EASTERN
WESTERN
EASTERN

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DEPTNO
BLUE
GREEN
RED

EMPL

EMPNO
000010
000020
000030

DEPT

EMPL

LASTNAME
HAAS
THOMPSON
KWAN

FIRSTNAME
CHRISTINE
MICHAEL
SALLY

WORK
DEPT
DEP
RED

GOVT_ID
888-88-2794
888-89-4261
BLUE
GREEN 888-88-9456

DEPTNO DEPTNAME

MANAGER

DIVISION

GREEN
RED

000080
000010

WESTERN
EASTERN

OPERATIONS
MARKETING

EMPNO
000010

LASTNAME
HAAS

FIRSTNAME
CHRISTINE

WORK
DEPT
DEP
GOVT_ID
RED
888-88-2794

000030

KWAN

SALLY

GREEN 888-88-9456

SALARY
52750.00
31000.00
33000.00

SALARY
52750.00
33000.00

Copyright IBM Corporation 2007

Figure 2-57. Delete rules - ON DELETE CASCADE

CE0317.0

Notes:

pr

Ex

cl

In this example, the referenced table is the department table. The foreign key
EMPL.DEPTNO was defined with the ON DELETE CASCADE rule. What happens when
the BLUE WORKDEPT row is deleted from the department table with ON DELETE
CASCADE as the delete rule? All employees in the BLUE DEPTNO are also deleted.

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-69

Student Notebook

Delete rules ON DELETE SET NULL


DEPT

DEPTNAME
PLANNING
OPERATIONS
MARKETING

MANAGER
000020
000080
000010

DIVISION
EASTERN
WESTERN
EASTERN

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DEPTNO
BLU
GRE
RED

EMPL

EMPNO
000010
000020
000030

LASTNAME
HAAS
THOMPSON
KWAN

DEPT

WORK
DEP
FIRSTNAME DEPT
CHRISTINE RED
BLUE
MICHAEL
GREEN
SALLY

SALARY
52750.00
31000.00
33000.00

DEPTNO DEPTNAME

MANAGER

DIVISION

GRE
RED

000080
000010

WESTERN
EASTERN

OPERATIONS
MARKETING

EMPL

EMPNO
000010
000020
000030

GOVT_ID
888-88-2794
888-89-4261
888-88-9456

LASTNAME
HAAS
THOMPSON
KWAN

FIRSTNAME
CHRISTINE
MICHAEL
SALLY

WORK
DEPT
RED
NULL
GREEN

GOVT_ID
888-88-2794
888-89-4261
888-88-9456

UNKNOWN!

SALARY
52750.00
31000.00
33000.00

Copyright IBM Corporation 2007

Figure 2-58. Delete rules - ON DELETE SET NULL

CE0317.0

Notes:

pr

Ex

cl

In this example, the referenced table is the department table. The foreign key
EMPL.WORKDEPT was defined with an ON DELETE SET NULL rule. When the BLUE
DEPTNO row is deleted from the department table, DB2 will set the WORKDEPT value for
all BLUE employees to NULL.

2-70 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Delete rules ON DELETE RESTRICT


DEPT

DEPTNAME
PLANNING
OPERATIONS
MARKETING

MANAGER
000020
000080
000010
WORK
FIRSTNAME DEPT
DEP
CHRISTINE
RED
MICHAEL
BLUE
SALLY
GREEN

DIVISION
EASTERN
WESTERN
EASTERN

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DEPTNO
BLU
GRE
RED

EMPL

EMPNO
000010
000020
000030

DEPT

EMPL

LASTNAME
HAAS
THOMPSON
KWAN

DEPTNO
BLU
GRE
RED

EMPNO
000010
000020
000030

DEPTNAME
PLANNING
OPERATIONS
MARKETING

LASTNAME
HAAS
THOMPSON
KWAN

MANAGER
000020
000080
000010

FIRSTNAME
CHRISTINE
MICHAEL
SALLY

WORK
DEPT
DEP
RED
BLUE
GREEN

GOVT_ID
888-88-2794
888-89-4261
888-88-9456

SALARY
52750.00
31000.00
33000.00

DIVISION
EASTERN
WESTERN
EASTERN

SOC_SEC
888-88-2794
888-89-4261
888-88-9456

SALARY
52750.00
31000.00
33000.00

Copyright IBM Corporation 2007

Figure 2-59. Delete rules - ON DELETE RESTRICT

CE0317.0

Notes:

pr

Ex

cl

In this example, the referenced table is the department table. The foreign key
EMPL.WORKDEPT was defined with an ON DELETE RESTRICT rule. Before DB2 will
allow the BLUE DEPT row to be deleted from the department table, it checks to ensure that
there are no employee rows where WORKDEPT = BLUE. If DB2 finds even one employee
row with a value of BLUE in the WORKDEPT column, the delete fails (is restricted).

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-71

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Triggers

Defines a set of actions that are


executed before or after a change
on a specified table
Independent from applications
(stored in the database)

Copyright IBM Corporation 2007

Figure 2-60. Triggers

CE0317.0

Notes:

cl

If your application environment needs to enforce particular business rules, before or after
changes to DB2 tables take place, DB2 offers you the ability to automatically activate a
user-defined set of actions (called a trigger) which will implement your business rules.

Ex

When a trigger is fired (the set of actions is performed), the application that fired the
trigger will wait for its completion, as if the trigger had actually been called by the
application itself. Triggers are associated with specific tables, and will be activated no
matter how the table change took place.

pr

The advantage of using triggers is that they are database objects, associated with a
specific table. Therefore a trigger is maintained in one place, and is consistently applied,
regardless of the application which causes the trigger to fire. Without the use of triggers,
every application that issues a change SQL statement (INSERT, UPDATE or DELETE)
would have to design and code the logic necessary to implement the business rules
associated with the tables being changed.

2-72 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Triggers: A simple example

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

CREATE TRIGGER NEWHIRE


AFTER INSERT ON EMPL
FOR EACH ROW
MODE DB2SQL
UPDATE COMPANY_STATS SET NBEMP = NBEMP + 1

CREATE TRIGGER PRIOREMP


AFTER DELETE ON EMPL
FOR EACH ROW
MODE DB2SQL
UPDATE COMPANY_STATS SET NBEMP = NBEMP - 1

Copyright IBM Corporation 2007

Figure 2-61. Triggers: A simple example

CE0317.0

Notes:

This rather simple example shows how a set of two triggers can be used to keep the total
number of employees in the company current.

pr

Ex

cl

The NEW_HIRED trigger will perform an update each time a new employee row is added
to the EMPL table. The trigger adds 1 to a running count of employees. The second trigger
decrements the employee count each time a row is removed from the EMPL table. We will
present additional information about triggers in the DBA unit of this course.

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-73

Student Notebook

Triggers: Another example


Order Entry
JOHN

FAX

668723

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

CUST

ORDER AAAA
QTY

Insert Trigger

Orders File

40

Input Parameter

WRITE DOC:
SNDFAX for
Confirmation

Fax Machine

JOHN 558723 AAAA 40

Conf. #

DB2

E-mail msg

When a new order is inserted, a trigger starts

Triggers get information about order and customer


A confirmation fax (or email) is automatically sent
Copyright IBM Corporation 2007

Figure 2-62. Triggers: Another example

CE0317.0

Notes:

The picture shows an example of how triggers can be used to integrate a traditional
application, such as an order entry, with automation techniques.

pr

Ex

cl

This could be a very common case of a company acquiring orders over the phone,
validating them, and sending a confirmation fax or email message to their customer
afterwards. Using triggers, the fax or email could be automatically sent to the customer as
soon as the order is inserted into the database file, without changing the order entry
application. The insert trigger is invoked every time an order enters the orders file; the new
records are automatically passed to the trigger program as input parameters. The program
might produce a predefined document with the new order and customer data, which is sent
as a fax or email message to the customer.

2-74 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Identify the need for Indexes and Views


Managers should be able to see
all of it, but as for Payroll...

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Now...who should be able


to see the EMPL table?

And which EMPL


columns will we be
searching on a lot?

Hmmm...how about another


index on WORKDEPT?

Copyright IBM Corporation 2007

Figure 2-63. Identify the need for Indexes and Views

CE0317.0

Notes:

At this stage in the design process, you could also consider the following types of DB2
objects to add to a table:

Ex

cl

View: A view has many uses, but it is often used on a table containing information which
will interest a variety of users, as a means of ensuring that each group of users accesses
only the data that they are or authorized to see. A view enables users to see a particular
subset of the base data without the need to create copies of the tables for each group of
users. We will present more detailed information about views in the DBA unit of this course.

pr

Index: Basically, an index on a particular column can improve access performance on that
column. There are various types of index: a unique index was created as a result of
creating the primary key on the previous page. Apart from performance advantages, a
unique index also ensures there are no duplicate values in that column. Views and indexes
can be identified at design time, and created at the same time as the table; but they can
also be identified and created later on. Views and indexes are often implemented by the
DBA, therefore they are covered in more detail in the DBA unit of this course and on DB2
DBA courses.

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-75

Student Notebook

Checkpoint (1 of 2)
1. What is a business entity?

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

2. What are the two basic parts to a table name?


3. List some of the DB2 data types.

4. Suppose we are creating two tables. One, called


ORDER_TAB, will contain one row for each order taken by
our company. Its primary key will be ORDER_NO. The
second table is called ITEM_TAB, and will contain one row for
each item ordered (within an order a customer can order
many items). Into which table should the foreign key be
placed?
5. For a foreign key to be defined, the column (or columns) it
references must have what characteristic?
Copyright IBM Corporation 2007

Figure 2-64. Checkpoint (1 of 2)

CE0317.0

Notes:

Write down your answers here:


1.

cl

2.

Ex

3.
4.

pr

5.

2-76 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Checkpoint (2 of 2)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

6. From which SQL statements may primary and foreign keys


be defined?
7. What is a referential constraint?

8. What is the automatic enforcement of referential constraints


called?

Copyright IBM Corporation 2007

Figure 2-65. Checkpoint (2 of 2)

CE0317.0

Notes:

Write down your answers here:


6.

cl

7.

pr

Ex

8.

Copyright IBM Corp. 1997, 2011

Unit 2. Data Modeling and Database Design

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-77

Student Notebook

Unit summary

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

After completing this unit, you should be able to:


State the purpose of a business model

Describe an Entity-Relational Diagram model (ERD)


Discuss the rules of normalization of databases
List several DB2 column data types

Define User Defined data types and functions

Understand the features of a primary and foreign keys


State the purpose of referential integrity
State the purpose of triggers

Copyright IBM Corporation 2007

Figure 2-66. Unit summary

CE0317.0

pr

Ex

cl

Notes:

2-78 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Unit 3. How Does an End User Use DB2?


What this unit is about

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

This unit explains some of the tasks which a user may perform while
working with DB2.

What you should be able to do

After completing this unit, you should be able to:

List several ways to talk to DB2


List multiple ways to generate an SQL statement
List several ways to ADD, REMOVE, or CHANGE table rows
List several ways to READ data and produce reports

How you will check your progress


Accountability:

pr

Ex

cl

Checkpoint questions

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-1

Student Notebook

Unit objectives

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

After completing this unit, you should be able to:


List several ways to talk to DB2

List multiple ways to generate an SQL statement

List several ways to add, remove, or change table


rows

List several ways to read data and produce reports

Copyright IBM Corporation 2007

Figure 3-1. Unit objectives

CE0317.0

pr

Ex

cl

Notes:

3-2

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Who is an End User of DB2?

Run business
applications

End User

Produce
reports

Change
DB2 data

Write SQL
queries

Copyright IBM Corporation 2007

Figure 3-2. Who is an End User of DB2?

CE0317.0

Notes:

Anyone executing an SQL statement is a user of DB2. The end-user may or may not know
the SQL language. The end-user may not even be aware that he/she is using DB2.

cl

SQL can be embedded in the application programs which users run.

Ex

The SQL within the applications may be changing data, adding data, removing data, or
reading data.

SQL may return DB2 data, which may then be formatted to produce reports, with paging,
headers and footers, breakpoints, etc.

pr

The user may construct (write) their own queries that change data, add data, remove
data, or read data.

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-3

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Running programs that talk to DB2

Enter new customer


information
Cust name: _______________
Cust address: _____________
Cust phone _____________

End-Us
er

Copyright IBM Corporation 2007

Figure 3-3. Running programs that talk to DB2

CE0317.0

Notes:

pr

Ex

cl

Programs can contain SQL statements. People running those programs are users. The
users may or may not be aware that the applications that they are executing contain SQL
statements which access DB2 data.

3-4

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

And if I'm writing my own SQL?

INSERT clause
INTO clause
VALUES clause

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SELECT clause
FROM clause
WHERE clause

CREATE clause
(column description area)
IN clause

DELETE clause
FROM clause
WHERE clause

UPDATE clause
SET clause
WHERE clause

Copyright IBM Corporation 2007

Figure 3-4. And if I'm writing my own SQL?

CE0317.0

Notes:

pr

Ex

cl

SQL statements are made up of clauses. The sequence in which the clauses are written is
the structure of the language.

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-5

Student Notebook

Learn about your table

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

No Need to
Fly in the Dark!!

User
Guide?

DB2 Catalog

SYSTABLES

SYSINDEXES

Data Dictionary
Software ?

SYSVIEWS

Copyright IBM Corporation 2007

Figure 3-5. Learn about your table

CE0317.0

Notes:

Before writing any SQL, understand your tables!

What is the table called? What are the columns called?

cl

What do they mean in business terms?

pr

Ex

Ideally, you should be provided with a User Guide to the tables. Also, a customer
environment might deploy a software product that serves as an enterprise-wide data
dictionary. If you have neither a user guide nor a data dictionary to help you understand
the DB2 structures and the meaning of the data, the DB2 catalog tables provide a
significant amount of basic information about tables, columns, indexes, keys, views and
other DB2 objects.

3-6

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

STAFF table description

DATA TYPE

ID
NAME
DEPT
JOB
"YEARS"
SALARY
COMM

SMALLINT
VARCHAR
SMALLINT
CHAR
SMALLINT
DECIMAL
DECIMAL

LENGTH

NULLS

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

COLUMN NAME

(7,2)
(7,2)

NO
YES
YES
YES
YES
YES
YES

Copyright IBM Corporation 2007

Figure 3-6. STAFF table description

CE0317.0

Notes:

cl

The STAFF table is one of several sample tables that can be made available for users to
practice SQL statements and also relate to the SQL examples in the manuals. This table
may also be used as part of the IVP, Installation Verification Process. The IVP is a series of
jobs and queries that ensure that DB2 has been properly installed and is correctly
functioning.

pr

Ex

The implementation of the STAFF table (schema name, table contents, etc.) may differ
slightly depending on the DB2 platform. In this case, the schema of the table is Q (from
QMF - see later) and all the data is in upper case.

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-7

Student Notebook

Example table STAFF


NAME
--------SANDERS
PERNAL
MARENGHI
O'BRIEN
HANES
QUIGLEY
ROTHMAN
JAMES
KOONITZ
PLOTZ
NGAN
NAUGHTON
YAMAGUCHI
FRAYE
WILLIAMS
MOLINARE
KERMISCH
.
.

DEPT
-----20
20
38
38
15
38
15
20
42
42
15
38
42
51
51
10
15
.
.

JOB
----MGR
SALES
MGR
SALES
MGR
SALES
SALES
CLERK
SALES
MGR
CLERK
CLERK
CLERK
MGR
SALES
MGR
CLERK
.
.

YEARS
-----7
8
5
6
10
7
6
7
5
6
6
6
7
4
.
.

SALARY
---------18357.50
18171.25
17506.75
18006.00
20659.80
16808.30
16502.83
13504.60
18001.75
18352.80
12508.20
12954.75
10505.90
21150.00
19456.50
22959.20
12258.50
.
.

COMM
---------612.45
846.55
650.25
1152.00
128.20
1386.70
206.60
180.00
75.60
637.65
110.10
.
.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

ID
-----10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
.
.

Copyright IBM Corporation 2007

Figure 3-7. Example table - STAFF

CE0317.0

Notes:

cl

Here is a listing of some of the contents of the STAFF table, in column order as it was
defined at CREATE time. After the table was defined, users either executed SQL INSERT
statements or submitted a LOAD utility to populate the table.

pr

Ex

The primary key of the STAFF table is the column named ID. We will talk in more depth
about keys (primary, unique, compound, parent, foreign etc.) in a later unit during this
course.

3-8

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

ORG table description

COLUMN NAME

DATA TYPE

DEPTNUMB
DEPTNAME
MANAGER
DIVISION
LOCATION

SMALLINT
VARCHAR
SMALLINT
VARCHAR
VARCHAR

LENGTH

14

10
13

NULLS
NO
YES
YES
YES
YES

Copyright IBM Corporation 2007

Figure 3-8. ORG table description

CE0317.0

Notes:

pr

Ex

cl

Here is the structure of the ORG table. Q.ORG is another sample table that is installed on
most DB2 systems that are accessed by QMF. Like the STAFF table, it is used as part of
the IVP process, and also used to practice SQL statements and also relate to the SQL
examples in the manuals.

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-9

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Example table ORG

DEPTNUMB
-------10
15
20
38
42
51
66
84

DEPTNAME
-------------HEAD OFFICE
NEW ENGLAND
MID ATLANTIC
SOUTH ATLANTIC
GREAT LAKES
PLAINS
PACIFIC
MOUNTAIN

MANAGER
------160
50
10
30
100
140
270
290

DIVISION
---------CORPORATE
EASTERN
EASTERN
EASTERN
MIDWEST
MIDWEST
WESTERN
WESTERN

LOCATION
------------NEW YORK
BOSTON
WASHINGTON
ATLANTA
CHICAGO
DALLAS
SAN FRANCISCO
DENVER

Copyright IBM Corporation 2007

Figure 3-9. Example table - ORG

CE0317.0

Notes:

cl

Here is a listing of the contents of the ORG table, in column order as it was defined at
CREATE time. After the table was defined, users either executed SQL INSERT statements
or submitted a LOAD utility to populate the table.

pr

Ex

The primary key of the ORG table is the column named DEPTNUMB.

3-10 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

A SELECT to retrieve all columns for all rows

SELECT * -- AN ASTERISK SPECIFIES ALL COLUMNS


FROM Q.STAFF

-- NO WHERE CLAUSE MEANS RETURN ALL ROWS

Copyright IBM Corporation 2007

Figure 3-10. A SELECT to retrieve all columns for all rows

CE0317.0

Notes:

cl

The SQL SELECT statement is the means by which a user can read data in DB2 tables.
SELECT statements are commonly referred to as queries. An asterisk in a SELECT clause
requests that DB2 return all columns from this table. This could also be written: SELECT
Q.STAFF.*

Ex

A SELECT clause and a FROM clause is required in every query. All other clauses are
optional.

pr

The absence of a WHERE clause tells DB2: we want data from every row in the table. In
most SELECT statements, the user or application will return only a subset of the rows in
the table. Via the WHERE clause, the statement implements selection criteria (conditions
that the row must meet in order to be SELECTed). These criteria are referred to as local
predicates.

Two hyphens, side by side, denote a comment. Everything following to the right of the two
consecutive hyphens is treated as a comment. The SQL parser then resumes syntax
checking on the next line of the query.

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-11

Student Notebook

Four SELECT Statement clauses

FROM Q.STAFF

1 to N
TABLES,
VIEWS

WHERE DEPT = 38

1 to N TESTS,
CONDITIONS,
PREDICATES

ORDER BY NAME

OUTPUT ROW
SEQUENCE

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
YEARS, SALARY

1 to N COLUMNS,
FUNCTIONS,
LITERALS,
EXPRESSIONS

SELECT NAME,

Copyright IBM Corporation 2007

Figure 3-11. Four SELECT Statement clauses

CE0317.0

Notes:

Every SELECT statement must have a SELECT and a FROM clause. The remaining
clauses are optional, and only used when we need their services.

Ex

cl

The SELECT clause tells DB2 which column(s) of information you want on your report. In
addition to retrieving column values, the user may request literals, functions (either scalar
or column functions) and expressions. In the SELECT above, the user is requesting data
values from the NAME, YEARS and SALARY columns in the table.

pr

The FROM clause specifies which table(s) contain(s) the requested data. When
requesting data from more that one table, the SQL statement is know as a join. When
joining tables, the user should specify how the tables are related (what column information
they share in common). These join relationships are known as join predicates.
The WHERE clause tells DB2 from which row(s) the SELECT clause information is to be
retrieved, such as information about department 38. DEPT = 38 is an example of a local
predicate.

3-12 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

The ORDER BY clause specifies the sequence in which the qualified rows should be
presented to the user or application. There are additional clauses and options for the
SELECT statement, but these four (SELECT, FROM, WHERE, ORDER BY) are the most
commonly used.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

The sequence of the clauses must be coded as shown here: SELECT followed by FROM,
WHERE, then ORDER BY. If you do not code the clauses in this specific order, you will
receive a syntax error.

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-13

Student Notebook

SELECT - Comparing Execution Order with Syntax


SELECT
STATEMENT

EXECUTION
ORDER

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SYNTACTICAL
ORDER

VW

6(/(&7

UG

QG

)520

VW

UG

:+(5(

QG

WK

25'(5%<

WK

SELECT NAME, JOB, SALARY + BONUS AS INCOME

~
G
f

FROM EMPLOYEE

WHERE INCOME > 100000

z
lH

ORDER BY INCOME DESC;

Copyright IBM Corporation 2007

Figure 3-12. SELECT - Comparing Execution Order with Syntax

CE0317.0

Notes:

Although the user must write the clauses of the SELECT in a specific order, DB2 does not
execute them in the same sequence.

cl

DB2 must first locate the table(s) and/or view(s) that the user or application requires.
Therefore, the FROM clause is executed first.

Ex

Next, DB2 needs to apply the selection criteria, filtering out any rows that are not
needed or wanted by the user. So the WHERE clause is executed next.

As a third step in the process, DB2 is now in a position to retrieve the desired columns
from the qualified rows, which is accomplished by the SELECT clause.

pr

Finally, DB2 sequences the column values in the order designated by the user in the
ORDER BY clause.

In the SELECT statement in the visual above, we see an example of an expression


(SALARY + BONUS). That expression is given a label of INCOME. However, according
to the execution order of the clauses, the WHERE is evaluated before the SELECT. Since
there is no real column by the name of INCOME in the EMPLOYEE table, DB2 will return a
3-14 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

syntax error to the user. It would be necessary to rewrite part of the query (WHERE
SALARY + BONUS > 100000) in order for the query to run. Referring to the label
INCOME, rather than the full expression (SALARY + BONUS) in the ORDER BY clause is
valid syntax, since the ORDER BY clause is evaluated by DB2 after the SELECT clause.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Uempty

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-15

Student Notebook

To SELECT specified rows


Stored DB2 Table
ID
-----10
20
30
40
50
60
70
80
90
100
110
120
.
180
190
200
.

NAME
--------SANDERS
PERNAL
MARENGHI
O'BRIEN
HANES
QUIGLEY
ROTHMAN
JAMES
KOONITZ
PLOTZ
NGAN
NAUGHTON
.
ABRAHAMS
SNEIDER
SCOUTTEN
.

DEPT
-----20
20
38
38
15
38
15
20
42
42
15
38
.
38
20
42
.

JOB
----MGR
SALES
MGR
SALES
MGR
SALES
SALES
CLERK
SALES
MGR
CLERK
CLERK
.
CLERK
CLERK
CLERK
.

YEARS
-----7
8
5
6
10
7
6
7
5
.
3
8
.

SALARY
---------18357.50
18171.25
17506.75
18006.00
20659.80
16808.30
16502.83
13504.60
18001.75
18352.80
12508.20
12954.75
.
12009.75
14252.75
11508.60
.

COMM
---------612.45
846.55
650.25
1152.00
128.20
1386.70
206.60
180.00
.
236.50
126.50
84.20
.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SELECT JOB, NAME


FROM Q.STAFF
WHERE DEPT = 20
JOB
MGR
SALES
CLERK
CLERK

NAME
SANDERS
PERNAL
JAMES
SNEIDER

Final Results

Intermediate Table
10
20
80
190

SANDERS
PERNAL
JAMES
SNEIDER

20
20
20
20

MGR
SALES
CLERK
CLERK

NAME
2nd

7
8
8

18357.50
18171.25
13504.60
14252.75

612.45
128.20
126.50

JOB
1st

Copyright IBM Corporation 2007

Figure 3-13. To SELECT specified rows

CE0317.0

Notes:

pr

Ex

cl

This query returns two columns of data from the set of rows whose DEPT value is 20 (as
indicated by the small arrows). The WHERE clause identifies the set of rows from which
the data is to be returned. DB2 applies the SELECT clause against the conceptual
intermediate table retrieving the JOB and NAME values (in that left-to-right sequence) from
every row in the intermediate table.

3-16 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Numeric comparisons

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

TO QUALIFY ROWS BY NUMERIC DATA

WHERE DEPT = 20

-- (integer)

AND COMM = 612.45

-- (decimal point)

AND (SALARY = 18171.25

-- (no comma)

OR SALARY = 18000)

-- (same as 18000.00)

Copyright IBM Corporation 2007

Figure 3-14. Numeric comparisons

CE0317.0

Notes:

cl

Numeric data is either decimal, expressed as DECIMAL(p,s); integer (SMALLINT,


INTEGER or BIGINT); or floating-point (FLOAT, FLOAT(x), REAL, or DOUBLE
PRECISION).

Ex

For decimal numbers, the p and s designate precision and scale. Precision is the total
number of digits possible for the number, and the scale represents how many positions to
the Right of the decimal point (in other words, the fractional part of the number).

pr

Please note that when designating numeric data or values within SQL, you do NOT
enclose the numbers with apostrophes (single quotes). Also, the only punctuation allowed
in a number is the decimal point. There are no delimiters for thousands, millions, billions,
etc. Be advised that in some nations (especially Europe), the convention is to use the
period as the delimiter (for thousands, millions, etc.) and to use the comma as the decimal
point.

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-17

Student Notebook

SQL comparison alternatives

(>, <>, <, >=, <=)


(AND, OR)
(LIKE '_A%')
(COMM > SALARY)
(COMM/12)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Basic Operators
Boolean Logic
Partial Value Search
Values in other rows
Calculated values

SELECT *
FROM Q.STAFF
WHERE ((SALARY + COMM) > 20000)
AND JOB NOT LIKE 'M%'
Copyright IBM Corporation 2007

Figure 3-15. SQL comparison alternatives

CE0317.0

Notes:

cl

Be aware of some idiosyncrasies in SQL. For instance, when using GREATER THAN OR
EQUAL TO, and LESS THAN OR EQUAL TO, you must specify them as >= and <=. If you
use => or =< you will receive a syntax error.

Ex

Further, the <> (Less Than or Greater Than) is used to make comparisons that are NOT
EQUAL. It may not be intuitively obvious at first glance, but when you think about it if A
is either less than or greater than B, A is not equal to B. Perhaps this warrants some
further discussion.

pr

You can negate a condition or comparison by placing the SQL keyword NOT in front of the
entire condition. For instance, you can code WHERE NOT NAME = Jones; However, you
cannot code WHERE NAME NOT = Jones. The keyword NOT can be placed directly
before other comparison functions, such as LIKE, IN, and BETWEEN, but NOT can never
be coded immediately before EQUAL (whether you spell out the word or use the symbol
=). Therefore, the combination Less Than/Greater Than, <>, has become the
conventional way to express a not equal condition in SQL.

3-18 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Partial value searches using the LIKE keyword can only be performed on character data.
There are two special characters that enable the LIKE keyword to provide a generic search
function. The underscore represents exactly 1 character/byte position, and the percent
sign represents 0 or more character positions.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Uempty

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-19

Student Notebook

Functions Two types

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

FUNCTION_NAME(argument)

Column Functions
(AKA SUMMARY functions)

Scalar Functions
(most of the rest)

function
function
SUM
value
AVG
MIN
MAX
COUNT(*)
COUNT(DISTINCT column_name)

value

value
value

value

Copyright IBM Corporation 2007

Figure 3-16. Functions - Two types

CE0317.0

Notes:

Column functions produce one summary or aggregate row for each set of rows.

Scalar functions produce one value for each row within the set of qualified rows.

pr

Ex

cl

The above column functions are the most commonly-used column functions. Each
release of DB2 provides additional functions. Check the SQL manuals for your release of
DB2 to see the supported functions.

3-20 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Column functions
SUM(SALARY) AS SUM_SALARY,
COUNT(*) AS COUNT
Q.STAFF
DEPT = 20

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SELECT
FROM
WHERE

DEPT
20
20
20
20

SALARY
18357.50
18171.25
13504.60
14252.75

One
Summary
Row per
Set

SUM
SALARY
64286.10

COUNT
4

Copyright IBM Corporation 2007

Figure 3-17. Column functions

CE0317.0

Notes:

cl

Here is an example of using column functions in a SELECT statement. COUNT(*) counts


rows that meet the WHERE clause conditions. SUM(SALARY) adds up the known values
in the salary column for the set of rows that have a DEPT value of 20.
Conceptually, here is what happens:

Ex

1. DB2 searches the catalog for the table with a schema of Q and the name STAFF.

2. DB2 chooses only the rows where the DEPT = 20, by applying the local predicate in the
WHERE clause. This produces an intermediate result all of the column values for
qualified rows (DEPT = 20).

pr

3. According to the SELECT clause, DB2 is asked to compute the total of the salaries, and
to count the number of rows that meet the WHERE condition(s).

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-21

Student Notebook

Scalar functions

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SELECT ID, NAME, DEPT, SUBSTR(JOB,1,1) AS JOB, SALARY


FROM Q.STAFF
WHERE UPPER(JOB) = 'MGR'
ID
-----10
30
50
100
140
160
210
240
260
270
290

NAME
--------Sanders
Marenghi
Hanes
Plotz
Fraye
Molinare
Lu
Daniels
Jones
Lea
Quill

DEPT
-----20
38
15
42
51
10
10
10
10
66
84

JOB
----M
M
M
m
M
M
M
M
m
M
M

SALARY
---------18357.50
17506.75
20659.80
18352.80
21150.00
22959.20
20010.00
19260.25
21234.00
18555.50
19818.00

Copyright IBM Corporation 2007

Figure 3-18. Scalar functions

CE0317.0

Notes:

Here is an example of using scalar functions.

Ex

cl

Often, data is stored with mixed-case characters, either on purpose or because of an


undisciplined or inconsistent data entry process. The UPPER scalar function allows us to
perform case-insensitive searching. The function may also be invoked under the name
UCASE(). Check the reference manual for your environment to see if UPPER() is
supported in your DB2 release.

pr

SUBSTR is one of the most widely used of the scalar functions. SUBSTR is followed by
three arguments DB2 searches/reads the first argument, beginning at the nth byte
indicated by the second argument, and retrieves n bytes of characters as specified in the
third argument.

In the SELECT above, SUBSTR(JOB,1,1) retrieves the first letter of the JOB column,
regardless of the combinations of case for the letters MGR. In other words, the UPPER
scalar function would transform any case combinations into uppercase MGR before
comparing with the literal string MGR. Therefore, if the JOB column contained any of the
following, the row would be selected:
3-22 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

MGR
MGr
MgR
Mgr

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

mGR
mGr

mgR
mgr

Notice that some of the output rows in the report display a lowercase m (as indicated by
the red arrows). That means that the first character of the JOB column for those two rows
was a lowercase m. The SELECT clause simply asked for the first character. In order to
ensure that the output displayed JOB as uppercase, you would have to nest the scalar
functions like this .
SELECT ID, NAME, DEPT, SUBSTR(UPPER(JOB),1,1) AS JOB
FROM Q.STAFF

WHERE UPPER(JOB) = MGR

pr

Ex

cl

Some additional scalar functions are: MONTH, DAY, DAYS, COALESCE, SUBSTR, DATE,
DECIMAL, DIGITS, ROUND, TRIM, STRIP, NULLIF, to name a few. With the most current
versions of DB2, there are close to 200 scalar functions.

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-23

Student Notebook

SELECT examples
SALARY
18171.25
18006.00
16808.30
16502.83
13504.60
18001.75

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SELECT SALARY
FROM Q.STAFF
WHERE JOB <> 'MGR'

SELECT AVG(SALARY)
FROM Q.STAFF
WHERE JOB <> 'MGR'

SELECT DEPT, AVG(SALARY)


FROM Q.STAFF
WHERE JOB <> 'MGR'
GROUP BY DEPT
ORDER BY 2 DESC

ETC.

15240.986666

DEPT
66 16880.175000
51
84

16235.200000
15443.000000
ETC.

Copyright IBM Corporation 2007

Figure 3-19. SELECT examples

CE0317.0

Notes:

cl

First, a simple SELECT statement returning the SALARY values for all non-manager
employees in the STAFF table. Only the first six rows are displayed on the visual above.
The actual number of total rows returned by DB2 would equal the number of non-manager
employees in the entire STAFF table.

Ex

The second SELECT returns the average salary for all non-managers.

pr

The third SELECT demonstrates the ability of SQL to summarize data by a certain column
(in this case, DEPT). You can invoke that function by issuing the GROUP BY clause. This
produces a report with one line for each department (the DEPT column), with that
departments average salary for non-managers. The output for this third SELECT returns
the rows in sequence of highest to lowest average SALARY by DEPT (ORDER BY 2
DESC). The 2 in the ORDER BY clause refers to the 2nd argument found in the SELECT
(that is AVG SALARY), and DESC specifies descending order. As with the first SELECT,
not all of the rows returned by DB2 are displayed in the visual (i.e. there are more than
three departments).

3-24 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

There is a specific syntax rule that you should be aware of when you are invoking functions
in the SELECT clause: If there are column functions in the SELECT list along with columns
not in a column function, then every column in the SELECT list that is not an argument of a
column function must appear in the GROUP BY clause.
The clauses are evaluated in the following sequence (only SELECT and FROM are
mandatory, the other clauses are optional):

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

FROM

WHERE (if present)

GROUP BY (if present)


HAVING (if present)
SELECT

pr

Ex

cl

ORDER BY (if present)

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-25

Student Notebook

SELECT clause Column naming

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

In a SELECT statement:
SELECT
something AS new_name,
expression AS "multi part name" ...
SELECT DEP AS DEPARTMENT,
SAL + COMM AS EARNINGS
FROM ...

DEPARTMENT
BLU
RED
.

EARNINGS
64000
58000
.

Copyright IBM Corporation 2007

Figure 3-20. SELECT clause - Column naming

CE0317.0

Notes:

cl

In a SELECT statement, if an argument in the SELECT clause is something other than a


column (a literal, an expression, or a function), then by default, that argument does not
have a header or label in the output result set. If you want to have a heading or label for
literals, expressions or functions, you must use the AS keyword.

pr

Ex

In the example above, DEP is the actual column name, but we want the output to have
DEPARTMENT as the header for that column of information. In similar fashion, the query
shows us that the AS keyword is going to use EARNINGS as the header for the
compensation information, which represents the sum of columns SAL and COMM (salary
and commission). If you want to have the header include characters other than A-Z, 0-9,
@, # $ and _ (underscore), you must enclose the header name with double quotes. For
example, if you wanted SAL + COMM to display as TOTAL EARNINGS, you would code
your AS clause . AS TOTAL EARNINGS.

3-26 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

CASE expressions
'Small'
'Medium'
'Large'

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SELECT PARTNO, DESCRIPT, COLOR,


CASE
WHEN SIZE='S'
THEN
WHEN SIZE='M'
THEN
WHEN SIZE='L'
THEN
ELSE
'Unknown'
END
AS SIZE
FROM CATALOG
WHERE QTY <= REORDER_PT

PARTNO
12-234
12-235
14-276

DESCRIPT
SWEATER
SWEATER
JACKET

COLOR
BLUE
RED
GREEN

SIZE
Small
Medium
Unknown

Copyright IBM Corporation 2007

Figure 3-21. CASE expressions

CE0317.0

Notes:

CASE expressions give us if - then - else logic.

CASE expressions can be used anywhere an expression is allowed.

cl

CASE expressions must begin with the keyword CASE and conclude with the keyword
END.

Ex

The first WHEN condition that is found to be True results in the THEN value being
retrieved into the result set row.

pr

If none of the WHEN conditions is True, then the ELSE value is retrieved into the result
set row.

If none of the WHEN conditions is True, and no ELSE is coded, the NULL value is placed
into the result set row.
In the SELECT statement in the visual above, we are expecting to find an uppercase S, M,
or L in the one-byte column SIZE in the CATALOG table. If any other character value is
found in SIZE, then we place the literal string Unknown for that row in the results set.

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-27

Student Notebook

JOIN Retrieving data from multiple tables


Multiple tables are referenced in the FROM clause

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

The result table can contain columns from any table


named in the FROM clause

Relate the tables by columns containing common data


JOIN predicate

WHERE clause
ON clause, an extension of the FROM clause

Copyright IBM Corporation 2007

Figure 3-22. JOIN - Retrieving data from multiple tables

CE0317.0

Notes:

Ex

cl

Naming multiple tables in the FROM clause tells DB2 to join information from these
multiple tables into the result set rows. Adding a JOIN predicate tells DB2 to join only those
rows that are related in this way. Without the appropriate join predicate(s), DB2 will
produce a Cartesian product by matching each row in the first table with each and every
row of the other table(s). This results in a substantial consumption of system resources,
and is seldom (if ever) what the requirements of the application designated in the first
place.

pr

There are two formats or syntaxes for the join predicates. The first syntax employs an
equal predicate in the WHERE clause. The second syntax utilizes the ON keyword as an
extension in the FROM clause.

3-28 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Example tables
Empl

LASTNAME
HAAS
ADAMSON
THOMPSON
SMITH
SPENSER
PULASKI
NATZ

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EMPNO
000010
000150
000020
000250
000100
000070
200140

Dept

DEPTNAME

MGRNO

SPIFFY COMPUTER

000010

DEVELOPMENT

SOFTWARE

000100

ADMINISTRATION

000070

BRANCH OFFICE F2

Copyright IBM Corporation 2007

Figure 3-23. Example tables

CE0317.0

Notes:

cl

We can find the name of a department's manager by looking in the EMPNO column of the
Empl table for values that are contained in the MGRNO column of the Dept table. When
we find a matching value, reading across the row to the LASTNAME column provides us
with the manager's surname. These two tables are related by the MGRNO and EMPNO
columns (these columns carry common values).

pr

Ex

There may be departments with no manager yet assigned, and there may be employees
that are not assigned to any department. When the hyphen is displayed in a result set of
an SQL query, it indicated the NULL value. Null means unknown, or the empty set. It
does NOT mean blanks for character data, nor does it mean zeroes for numeric data. In
the Dept table above, there are two departments without an assigned manager.

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-29

Student Notebook

JOIN Traditional syntax

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SELECT LASTNAME, DEPTNAME


FROM EMPL , DEPT
WHERE EMPNO = MGRNO
LASTNAME
HAAS
SPENSER
PULASKI

DEPTNAME
SPIFFY COMPUTER
SOFTWARE
ADMINISTRATION

Copyright IBM Corporation 2007

Figure 3-24. JOIN - Traditional syntax

CE0317.0

Notes:

cl

These tables have common values between the EMPNO and MGRNO columns. MGRNO
contains the employee number of the person that manages that department. This join
predicate (EMPNO = MGRNO) is telling DB2 to connect department rows to the row of the
person who manages it.

pr

Ex

The syntax requires that you separate the table names with a comma. Forgetting the
comma between the table names tells DB2 to rename the first table (a correlation name)
by assigning it the name of the second table (it works in a similar fashion to the assigning of
custom column-header names in a SELECT list using the AS keyword).

3-30 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

JOIN Alternate syntax

SELECT LASTNAME, DEPTNAME


FROM EMPL INNER JOIN DEPT
ON EMPNO = MGRNO
LASTNAME
HAAS
SPENSER
PULASKI

DEPTNAME
SPIFFY COMPUTER
SOFTWARE
ADMINISTRATION

Copyright IBM Corporation 2007

Figure 3-25. JOIN - Alternate syntax

CE0317.0

Notes:

cl

Here is the second (more recent) syntax for a join statement. Instead of separating the
multiple tables in the join with commas, the JOIN keyword is used. JOIN implies INNER
JOIN, and either of those is permissible in this syntax. When the JOIN keyword is used in
the FROM clause, the join predicate must be written using the ON keyword, which is an
extension to the FROM clause.

pr

Ex

This second syntax is always required for OUTER JOINs. The first syntax can be used
only for INNER JOINs.

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-31

Student Notebook

Inserting rows into the table

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

INSERT INTO TESTEMP


VALUES ('000111', 'SMITH', 'C01', '1998-06-25', 25000, NULL);
OR

INSERT INTO TESTEMP


(EMPNO, LASTNAME, WORKDEPT, HIREDATE, SALARY)
VALUES ('000111', 'SMITH', 'C01', '1998-06-25', 25000);

EMPNO
000111

LASTNAME
SMITH

WORKDEPT
C01

HIREDATE
1998-06-25

SALARY
25000.00

BONUS
-

Copyright IBM Corporation 2007

Figure 3-26. Inserting rows into the table

CE0317.0

Notes:

cl

Use an INSERT statement to add rows to a table. Inserting a row via a view inserts the row
into the table on which the view is based. Therefore, inserts via a view are only possible if
the view contains all of the NOT NULL columns in the base table.

Ex

When using the syntax of the first example in the visual above, a value for each of the
table's columns must be provided, and those values must be in the sequence that the
columns were defined at the time of the CREATE TABLE or CREATE VIEW statement,
originally defining the DB2 object.

pr

In the second example, a column list is specified. The number of values provided for a row
via the VALUES clause must be equal to the number of column names in the column list.
The first value is inserted in the first column of the list, the second value in the second
column, and so on.

Some versions of DB2 enable you to add more than one row in your INSERT statement.
Instead of ending your one set of VALUES with a semi-colon, you simply put a comma and
then add another set of values, in brackets. Check the SQL Reference manual for your
environment to ensure support of multi-row inserts.
3-32 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Update columns
Before:
LASTNAME
SMITH
JONES
THOMPSON
HAAS
THOMPSON
KWAN
GEYER

WORKDEPT
C01
C01
C01
A00
B01
C01
E01

HIREDATE
1998-06-25
2001-06-25
2001-06-25
1965-01-01
1973-10-10
1975-04-05
1949-08-17

SALARY
25000.00
25000.00
25000.00
52750.00
41250.00
38250.00
40175.00

BONUS
1000.00
800.00
800.00
800.00

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EMPNO
000111
000113
000114
000010
000020
000030
000050

UPDATE TESTEMP
SET BONUS = 500,
SALARY = 26000
WHERE EMPNO = '000111'

After:

EMPNO
000111
000113
000114
000010
000020
000030
000050

LASTNAME
SMITH
JONES
THOMPSON
HAAS
THOMPSON
KWAN
GEYER

WORKDEPT
C01
C01
C01
A00
B01
C01
E01

HIREDATE
1998-06-25
2001-06-25
2001-06-25
1965-01-01
1973-10-10
1975-04-05
1949-08-17

SALARY
26000.00
25000.00
25000.00
52750.00
41250.00
38250.00
40175.00

BONUS
500.00
1000.00
800.00
800.00
800.00

Copyright IBM Corporation 2007

Figure 3-27. Update columns

CE0317.0

Notes:

cl

The UPDATE statement changes the values of the specified columns for one or more rows
of a table or view. Our new colleague, Mr. Smith (employee number 000111) receives a first
bonus and a salary raise. Therefore, we have been directed to set his BONUS to 500 and
his SALARY to 26000.

Ex

If more than one column is changed, the columns must be separated by commas. The SET
keyword must precede the first column and must not be repeated.
To set BONUS to NULL, the UPDATE statement must be coded as follows: UPDATE
TESTEMP SET BONUS = NULL WHERE EMPNO = '000111

pr

Be careful when constructing UPDATE and DELETE statements. If you do not include the
correct WHERE clause, you may be changing data that should not be altered. If you totally
omit the WHERE clause, then every row of the DB2 table will be updated or deleted.

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-33

Student Notebook

Delete rows
Before:
LASTNAME
SMITH
JONES
THOMPSON
HAAS
THOMPSON
KWAN
GEYER

WORKDEPT
C01
C01
C01
A00
B01
C01
E01

HIREDATE
1998-06-25
2001-06-25
2001-06-25
1965-01-01
1973-10-10
1975-04-05
1949-08-17

SALARY
27000.00
26000.00
26000.00
52750.00
41250.00
39250.00
40175.00

BONUS
500.00
1000.00
800.00
800.00
800.00

SALARY
26000.00
26000.00
52750.00
41250.00
39250.00
40175.00

BONUS
1000.00
800.00
800.00
800.00

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EMPNO
000111
000113
000114
000010
000020
000030
000050

DELETE FROM TESTEMP


WHERE EMPNO = '000111'

After:

EMPNO
000113
000114
000010
000020
000030
000050

LASTNAME
JONES
THOMPSON
HAAS
THOMPSON
KWAN
GEYER

WORKDEPT
C01
C01
A00
B01
C01
E01

HIREDATE
2001-06-25
2001-06-25
1965-01-01
1973-10-10
1975-04-05
1949-08-17

Copyright IBM Corporation 2007

Figure 3-28. Delete rows

CE0317.0

Notes:

cl

Mr. Smith left the company. Therefore, we delete the row with employee number 000111.
The DELETE statement removes entire rows from a table. Depending on the search
condition of the WHERE clause, the DELETE statement removes zero, one, or more rows
of a table.

pr

Ex

Be careful when constructing UPDATE and DELETE statements. If you do not include the
correct WHERE clause, you may be changing data that should not be altered. If you totally
omit the WHERE clause, then every row of the DB2 table will be updated or deleted.

3-34 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

How do I talk to DB2?


DB2 for Linux, UNIX, and Windows

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Command Line Processor (CLP)


IBM Data Studio
Optim Database Administrator

DB2 for z/OS


SPUFI
QMF

Other graphical tools (for example, QMF for Windows)

Copyright IBM Corporation 2007

Figure 3-29. How do I talk to DB2?

CE0317.0

Notes:

cl

In DB2 LUW, click the appropriate icons to invoke interfaces to DB2 such as the Command
Line Processor (CLP), the Command Editor, or QMF for Windows. The CLP is a
character-based interface, but other tools provide GUI function.

Ex

In z/OS, SPUFI and QMF are usually invoked from the ISPF menu. SPUFI and QMF on
the mainframe are tools that use a character-based, menu-driven interface.

pr

There are many GUI-based interfaces, available from numerous software vendors, that run
on workstations (Intel platform) and can access a wide variety of relational databases on
many different platforms.

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-35

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Command Line Processor Interactive

End Use
r

Copyright IBM Corporation 2007

Figure 3-30. Command Line Processor - Interactive

CE0317.0

Notes:

cl

The user above has invoked CLP in an interactive mode. This is accomplished by
entering db2 and pressing the Enter key. The user then receives a db2 => prompt, and
may key in and enter any valid SQL statement or DB2 command.

pr

Ex

The command line processor will also accept SQL statements in a non-interactive mode,
by prefixing the SQL statement with the term db2. Instead of typing db2 and pressing
Enter, which invokes the interactive mode of CLP, start each SQL statement with db2, as in
the following example: db2 connect to sample; db2 select * from department; Although
only required when multiple SQL statements are executed together, it is recommended
practice to end each SQL statement with the delimiter character (the semi-colon is the
default).

3-36 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Command Editor

Copyright IBM Corporation 2007

Figure 3-31. Command Editor

CE0317.0

Notes:

With the Command Editor you can perform the following actions:
Execute an SQL statement or DB2 CLP command

cl

Run and/or modify an existing script

Ex

Execute several commands in sequence

Save what you write as a script, store it in the Task Center and run it on demand, or even
schedule it to run at a specific time
See the results of your command or query, and save them if required

pr

Ask DB2 to show the Access Plan for an explainable query, in other words, how the query
was optimized by DB2

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-37

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SQL Assistant

Copyright IBM Corporation 2007

Figure 3-32. SQL Assistant

CE0317.0

Notes:

The SQL Assistant allows you to compose SQL statements easily. This software prompts
the user through the process of building a query, one step at a time:

cl

Choosing the source table(s) and/or view(s) (FROM clause)

Ex

Specifying what information (column values and other things) should be returned in
each row of output (SELECT clause)
Filtering out any rows not needed in the result set (WHERE clause)

pr

Telling DB2 how to group rows when the column function is invoked (GROUP BY
clause)

Setting conditions on the groups to be returned by DB2 (HAVING clause, which acts like
a WHERE, but for groups of rows, instead of single rows)
Instructing DB2 how to sequence the result set, when it is presented to the user or
returned to the application (ORDER BY clause)

3-38 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Remember that for a SELECT statement, only the SELECT and FROM clauses are
mandatory, The other clauses, although frequently used, are all optional, depending upon
the requirements of your query.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Uempty

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-39

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Data Studio

Copyright IBM Corporation 2007

Figure 3-33. Data Studio

CE0317.0

Notes:

cl

Data Studio is an Eclipse-based tool that provides similar functionality to Control Center
and SQL Assistant. Those older tools are being deprecated in the more recent versions of
DB2, so customers are encouraged to migrate to the newer tool.

Ex

IBM Data Studio provides foundational database development and administration support
for the DB2 family of products. Data Studio provides developers and DBAs with basic
capabilities for database management and development.
With Data Studio the user may:

pr

Launch the task launcher that guides users through common tasks and gives users
access to best practices
Invoke enhanced administration support for DB2 on Linux, UNIX, and Windows
including commands, utilities, and configuration support for DB2

Create, alter, and drop objects, manage privileges, view data distributions, and update
statistics on those objects

3-40 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Speed data development and improve enterprise standardization with customizable


templates and editors to create, test, debug, and deploy routines, such as stored
procedures and user-defined functions

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Create Web services that easily expose database operations (SQL SELECT and DML
statements, XQuery expressions, or calls to stored procedures) to client applications.

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-41

Student Notebook

z/OS SPUFI
SSID: DSNA

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SPUFI

Enter the input data set name:


(Can be sequential or partitioned)
1 DATA SET NAME ... ===> 'URID.DB2FUN.LIB(TEST)'
2 VOLUME SERIAL ... ===>
(Enter if not cataloged)
3 DATA SET PASSWORD ===>
(Enter if password protected)
Enter the output data set name:
(Must be a sequential data set)
4 DATA SET NAME ... ===> SPUFI.OUT
Specify processing options:
5 CHANGE DEFAULTS
===> NO
6 EDIT INPUT ...... ===> YES
7 EXECUTE ......... ===> YES
8 AUTOCOMMIT ...... ===> YES
9 BROWSE OUTPUT ... ===> YES

(Y/N
(Y/N
(Y/N
(Y/N
(Y/N

Display SPUFI defaults panel?)


Enter SQL statements?)
Execute SQL statements?)
Commit after successful run?)
Browse output data set?

For remote SQL processing:


10 CONNECT LOCATION ===>

Copyright IBM Corporation 2007

Figure 3-34. z/OS SPUFI

CE0317.0

Notes:

The user must navigate the ISPF menu structure to reach the SPUFI panels.

cl

SQL statements (SQL source) are written to the [sequential] file or member [of a
partitioned dataset, or PDS] named on line 1. This file must be pre-allocated before the
SPUFI session.

pr

Ex

The output of the SQL statement will be written to the file or data set named on line 4.
This sequential file is created automatically for the user, and therefore does not need to be
pre-allocated. This file is overwritten after each SPUFI execution, so if for some reason the
user needs to save the results (not the SQL source itself), he/she would need to save to a
different output dataset at the next execution, or copy the output to another file (sequential
or PDS) before the next execution.
A YES on line 6 tells SPUFI that you want to invoke an ISPF Edit session, to write or
change an SQL statement in the file named on line 1.
Pressing Enter while at the SPUFI panel causes the first function for which a YES is
specified to be invoked.
3-42 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Compose query

**********************************************
EDIT URID.DB2FUN.LIB(TEST) Columns 00001 00072
Command ===>
Scroll ===> PAGE
****** ********** Top of Data ****************
000100 SELECT DEPT, JOB, NAME, SALARY
000200 FROM Q.STAFF
000300 WHERE DEPT IN (15, 20)
000400 ORDER BY NAME, JOB
****** ********* Bottom of Data **************

Copyright IBM Corporation 2007

Figure 3-35. Compose query

CE0317.0

Notes:

Always end queries with a semicolon (default delimiter character) when more than one
query is in the data set.

pr

Ex

cl

When the query is complete, press F3 to leave the Edit session.

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-43

Student Notebook

Execute the query


SSID: DSNA

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SPUFI
===>

Enter the input data set name:


(Can be sequential or partitioned)
1 DATA SET NAME ... ===> 'URID.DB2FUN.LIB(TEST)'
2 VOLUME SERIAL ... ===>
(Enter if not cataloged)
3 DATA SET PASSWORD ===>
(Enter if password protected)
Enter the output data set name:
(Must be a sequential data set)
4 DATA SET NAME ... ===> SPUFI.OUT
Specify processing options:
5 CHANGE DEFAULTS
===> NO
6 EDIT INPUT ...... ===> *
7 EXECUTE ......... ===> YES
8 AUTOCOMMIT ...... ===> YES
9 BROWSE OUTPUT ... ===> YES

(Y/N
(Y/N
(Y/N
(Y/N
(Y/N

Display SPUFI defaults panel?)


Enter SQL statements?)
Execute SQL statements?)
Commit after successful run?)
Browse output data set?)

For remote SQL processing:


10 CONNECT LOCATION ===>

Copyright IBM Corporation 2007

Figure 3-36. Execute the query

CE0317.0

Notes:

With Option 6 set to *, pressing the Enter key causes the remaining YES options to be
invoked.

Ex

cl

Be careful about the AUTOCOMMIT setting, option 8. If a user sets it to NO, and executes
a query, and then does not respond to the prompt (a panel giving the user the option to
either COMMIT or to ROLLBACK the SQL they just executed), the locks are still being held
on the data, and this may cause serious contention for other users or applications
attempting to access the same data.

pr

Option 9 places the user into a BROWSE session on the dataset specified in option 4.

3-44 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Output

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SELECT DEPT, JOB, NAME, SALARY


FROM Q.STAFF
WHERE DEPT IN (15, 20)
ORDER BY NAME, JOB
---------+---------+---------+---------+---------+---------+
DEPT JOB
NAME
SALARY
---------+---------+---------+---------+---------+---------+
15 MGR
HANES
20659.80
20 CLERK JAMES
13504.60
15 CLERK KERMISCH
12258.50
15 CLERK NGAN
12508.20
20 SALES PERNAL
18171.25
15 SALES ROTHMAN
16502.83
20 MGR
SANDERS
18357.50
20 CLERK SNEIDER
14252.75
DSNE610I NUMBER OF ROWS DISPLAYED IS 8
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 100
---------+---------+---------+---------+---------+---------+
DSNE617I COMMIT PERFORMED, SQLCODE IS 0
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0
Copyright IBM Corporation 2007

Figure 3-37. Output

CE0317.0

Notes:

cl

The BROWSE output lists the SQL query, followed by the results set. If the results do not
totally fit on the screen, the user may scroll by pressing Function keys 8 (scroll DOWN) and
7 (scroll UP).

Ex

SQL Return codes and messages will be displayed at the bottom of the output, including
the number of rows in the output, and whether a COMMIT or ROLLBACK was performed.
Return code 100 tells the user that there were no more rows to return, essentially indicating
an end-of-file (EOF) condition.

pr

Function key F3 returns you to the SPUFI panel.

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-45

Student Notebook

QMF for z/OS

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

______________________________________________________________________________
IBM*
Licensed Materials - Property of IBM
5706-254 5706-255 5648-061
(c) Copyright IBM Corp. 1982, 1995 All Rights Reserved.
* Trademark of International Business Machines
______________________________________________________________________________
QMF HOME PANEL
Version X Release Y

******
**
**
*********
**
**
***
***
**
**
**
**** ****
*******
**
**
** ** ** **
**
**
* **
** **** ** **
******
**
**
** **
*
________________________________________

Query
Management
Facility

Connected to
DSNA

Type command on command line or use PF keys. For help, press PF1 or type HELP.
______________________________________________________________________________

1=Help
2=List
3=End
7=Retrieve
8=Edit Table 9=Form
OK, you may enter a command.
COMMAND ===>

4=Show
10=Proc

5=Chart
11=Profile

6=Query
12=Report

Copyright IBM Corporation 2007

Figure 3-38. QMF for z/OS

CE0317.0

Notes:

pr

Ex

cl

Here is the home panel for the Query Management Facility, a commonly-used reporting
tool for DB2 z/OS environments. Notice at the bottom of the panel, the various options that
can be invoked by the function keys. Pressing F6 will allow the user to enter an SQL
statement for execution.

3-46 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Compose query in QMF


MODIFIED

LINE

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SQL QUERY

SELECT DEPT, JOB, NAME, SALARY


FROM Q.STAFF
WHERE DEPT IN (15, 20)
ORDER BY DEPT, JOB

*** END ***

1=Help
2=Run
7=Backward 8=Forward
12=Report
OK, cursor positioned.
COMMAND ===>

3=End
9=Form

4=Print
10=Insert

5=Chart
11=Delete

6=Draw

SCROLL ===> PAGE

Copyright IBM Corporation 2007

Figure 3-39. Compose query in QMF

CE0317.0

Notes:

cl

SQL cannot be typed on the command linethe user must use F6 to navigate to the SQL
query panel. Likewise, QMF commands cannot be typed in the query area these
commands must be entered into the COMMAND line(s). A completed query is executed by
pressing F2.

pr

Ex

Reports requiring subtotaling of the data MUST be sorted by the columns for which we
want the subtotals. This means, if we want subtotals of the salary column for each
department, we must ORDER BY DEPT.

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-47

Student Notebook

QMF tabular report

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

REPORT
DEPT
---15
15
15
15
20
20
20
20

JOB
----SALES
MGR
CLERK
CLERK
SALES
MGR
CLERK
CLERK

NAME
--------ROTHMAN
HANES
KERMISCH
NGAN
PERNAL
SANDERS
JAMES
SNEIDER

SALARY
---------16502.83
20659.80
12258.50
12508.20
18171.25
18357.50
13504.60
14252.75

*** END ***

Copyright IBM Corporation 2007

Figure 3-40. QMF tabular report

CE0317.0

Notes:

pr

Ex

cl

By default, QMF produces a tabular report, similar to the SPUFI Browse output. Column
headings are the same names as the names specified in the SELECT list (either the actual
column name, or the pseudo-name given by the AS clause for each expression, column
function and/or literal in the SELECT clause).

3-48 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

QMF report formatting instructions


MODIFIED

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

FORM.MAIN

COLUMNS:
Total Width of Report Columns: 39
NUM COLUMN HEADING
USAGE
INDENT WIDTH EDIT
--- ---------------------- ------- ------ ----- ----1 DEPT._NUMBER
BREAK1 2
6
L
2 JOB
2
5
C
3 EMPLOYEE_NAME
2
9
C
4 SALARY
SUM
2
11
D2
*** END ***
PAGE:

HEADING
FOOTING
FINAL:
TEXT
BREAK1: NEW PAGE
FOOTING
BREAK2: NEW PAGE
FOOTING
OPTIONS: OUTLINE?

SEQ
--1
2
3
4

===> DIVISION EARNINGS REPORT


===> COMPANY CONFIDENTIAL
===> GRAND TOTAL
FOR BREAK? ===> NO
===> DEPT &1 TOTAL
FOR BREAK? ===> NO
===>
===> YES
DEFAULT BREAK TEXT? ===> YES
Copyright IBM Corporation 2007

Figure 3-41. QMF report formatting instructions

CE0317.0

Notes:

Pressing F9 brings the user to the main formatting panel in QMF. In the example above,
many of the formatting options are being used:

cl

Column headings are customized by typing over the current column headings.

Ex

An underscore in a column heading tells QMF to take the characters following the
underscore and print them on the next line.

The BREAK1 USAGE code tells QMF to generate a subtotal line any time the value in the
DEPT._NUMBER column changes.

pr

The column to be subtotaled (by specifying the SUM function) is SALARY.

The &1 on the BREAK1 FOOTING option is a variable pulling values from the first (1)
column and placing them in the break footing text.
There are various edit codes that are available to the QMF user. The edit code D2,
associated with the SALARY column, adds the currency symbol and decimal punctuation
(displaying 2 digits to the Right of the decimal point) to the SALARY values in the report.

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-49

Student Notebook

QMF tailored report


DIVISION EARNINGS REPORT
EMPLOYEE
NAME
--------ROTHMAN
HANES
KERMISCH
NGAN

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DEPT.
NUMBER
-----15

JOB
----SALES
MGR
CLERK
CLERK

DEPT 15 TOTAL

20

SALES
MGR
CLERK
CLERK

PERNAL
SANDERS
JAMES
SNEIDER

SALARY
----------$16,502.83
$20,659.80
$12,258.50
$12,508.20
----------$61,929.33

DEPT 20 TOTAL

$18,171.25
$18,357.50
$13,504.60
$14,252.75
----------$64,286.10

GRAND TOTAL

===========
$126,215.43

COMPANY CONFIDENTIAL

Copyright IBM Corporation 2007

Figure 3-42. QMF tailored report

CE0317.0

Notes:

cl

QMF gives the user interactive access to the report formatting instructions. By making
some simple changes to the formatting instructions, the tabular report on the previous page
is converted to the tailored report above. The queries and report formatting instructions
may be saved.

pr

Ex

QMF for Windows and mainframe QMF have access to the same queries when connected
to the same DB2 system.

3-50 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

QMF procedures

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

RUN QUERY1 (FORM = FORM1)


PRINT REPORT
PRINT REPORT (FORM = FORM1A)
RUN QUERY2 (FORM = FORM2)
PRINT REPORT
EXPORT DATA to DATA1
TSO EXEC7
COMMAND ==>

1=Help
7=Backward

2=Run
8=Forward

3=End
9=Form

4=Print
10=Insert

5=Chart
11=Delete

6=Query
12=Report

Copyright IBM Corporation 2007

Figure 3-43. QMF procedures

CE0317.0

Notes:

QMF procedures are comprised of stacks of QMF commands. The user must enter only
one command per line, and no SQL is allowed in a procedure, only valid QMF commands.

pr

Ex

cl

QMF commands can run saved queries, which contain SQL statements (QUERY1 and
QUERY2 above) and format the results with saved formatting instructions. Additional QMF
commands allow the user to print reports, save output data to MVS datasets, etc.

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-51

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

QMF for Windows

Copyright IBM Corporation 2007

Figure 3-44. QMF for Windows

CE0317.0

Notes:

pr

Ex

cl

QMF for Windows is a GUI interface that connects to any DB2 system. Click the button with
the piece of paper icon to open a query window. Multiple query windows can be open at
any time, each query window running a query against a different database. Click the button
with the exclamation point/report icon to run the query.

3-52 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Saving the data

Copyright IBM Corporation 2007

Figure 3-45. Saving the data

CE0317.0

Notes:

pr

Ex

cl

Data can be saved to the same database, a file, or a different database. From the Results
menu, click Save to Database.

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-53

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Format the report

Copyright IBM Corporation 2007

Figure 3-46. Format the report

CE0317.0

Notes:

By making changes to the report formatting instructions of QMF for Windows, we have
changed the format of the retrieved data.

pr

Ex

cl

The output in the visual looks very similar to the formatted report seen earlier for the QMF
mainframe product. The above report now has a page header and footer, the report has
been broken apart where the department numbers changed, and subtotals and grand
totals were added.

3-54 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Save data into a spreadsheet

Copyright IBM Corporation 2007

Figure 3-47. Save data into a spreadsheet

CE0317.0

Notes:

pr

Ex

cl

When QMF for Windows is installed, an add-on can be applied to make local copies of the
report and save them into Microsoft Excel. The spreadsheet is opened first, then QMF for
Windows is started by clicking the QMF for Windows add-on icon. Click File, Move Data to
Microsoft Excel.

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-55

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

QMF A visual information platform

Copyright IBM Corporation 2007

Figure 3-48. QMF - A visual information platform

CE0317.0

Notes:

cl

The example above is of QMF for Windows accessing DB2 for z/OS data and using
Business Information tools such as Cube Views and OLAP (OnLine Analytical Processing)
analytics to provide information from across the enterprise.
These products deliver the following capabilities:

Ex

OLAP analytics ('drag and drop')

Support for Cube Views

A QMF for Websphere interface

pr

Access to DB2 for z/OS data through IBM Office Connect


Easy-to-use visual query building

Reports in multiple formats, including HTML, PDF and plain text


The creation of virtual information appliances through the ability to display data from
multiple sources in a large number of intuitive formats and images

3-56 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Checkpoint (1 of 2)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

1. Who is an end user?


2. If the following clauses (FROM, GROUP BY, ORDER BY,
SELECT, and WHERE) are used in a single query, in which
sequence must they be coded?
3. What is a QMF procedure, and why would we use one?

4. Assume that you are going to be involved in a telemarketing


effort to increase sales. You have been asked to write a
SELECT statement that will return the CUSTOMER_NAME,
CUSTOMER_ADDRESS, CONTACT_NAME, and
PHONE_NUMBER from the MASTER.CUSTOMER table.
(continue on next page)
Copyright IBM Corporation 2007

Figure 3-49. Checkpoint (1 of 2)

CE0317.0

Notes:

Write down your answers here:


1.

cl

2.

pr

Ex

3.

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-57

Student Notebook

Checkpoint (2 of 2)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

4. (continued) Each person in your team will be responsible for


calling customers within specific POSTAL_CODEs. The
marketing campaign will focus on customers in the CITY of
PARIS who have had a YEAR_TO_DATE_SALES of more
than $2,000. Write the query:

5. Assume that the MASTER.PERSONNEL table has a


JOB_CODE column which contains 2-digit numeric values.
You want to produce a report and in this report you want the
JOB_CODE values to be replaced with a word description of
each job (for instance, assume that a JOB_CODE of 50
equates to the job manager, and a JOB_CODE of 20
equates to the job of analyst, and so on). Which aspect of
an SQL SELECT statement will simplify this translation?
Copyright IBM Corporation 2007

Figure 3-50. Checkpoint (2 of 2)

CE0317.0

Notes:

Write down your answers here:


4.

pr

Ex

cl

5.

3-58 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Unit summary

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Having completed this unit, you should be able to:


List several ways to talk to DB2

List multiple ways to generate an SQL statement

List several ways to ADD, REMOVE, or CHANGE


table rows

List several ways to READ data and produce reports

Copyright IBM Corporation 2007

Figure 3-51. Unit summary

CE0317.0

pr

Ex

cl

Notes:

Copyright IBM Corp. 1997, 2011

Unit 3. How Does an End User Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-59

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Student Notebook

3-60 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Unit 4. How Does a Programmer Use DB2?


What this unit is about

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

This unit describes how a programmer can code SQL statements


within their host language programs.

What you should be able to do

After completing this unit, you should be able to:

List the steps needed to create a test environment


List the necessary steps to coding SQL in a program
Describe the purpose of SQL delimiters
Describe the purpose of an SQLCA
List the steps involved in preparing a program for execution
State the differences between static and dynamic SQL

How you will check your progress

pr

Ex

cl

Accountability:

Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-1

Student Notebook

Checkpoint questions

Unit objectives
After completing this unit, you should be able to:

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Understand how to create a test environment

List the necessary steps to coding SQL in a program


Describe the purpose of SQL delimiters
Describe the purpose of an SQLCA

List the steps involved in preparing a program for


execution

State the differences between static and dynamic


SQL
Understand XML at the conceptual level

Copyright IBM Corporation 2007

Figure 4-1. Unit objectives

CE0317.0

pr

Ex

cl

Notes:

4-2

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Creating a test environment Tables


Same simple
table names,
same column
definitions

Test tables
with ~ 1000 rows

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Production tables
with millions of
rows

Programmer

Statistical
sample

Copyright IBM Corporation 2007

Figure 4-2. Creating a test environment - Tables

CE0317.0

Notes:

Ex

cl

In a test environment, we want our test tables to look exactly like the production tables. The
tables should have the same names (if the tables are on the same DB2 subsystem in z/OS
or in the same database on UNIX or Windows, then the schema/qualifier names will be
different), the same column names, lengths, data types, null characteristics, sequence,
constraints everything about the test tables and the production tables should be the
same.
This can be accomplished by the following SQL statements:

pr

1. CREATE TABLE new_table_name LIKE old_table_name

to create a table with the same structure, and then another SQL statement:
2. INSERT INTO new_table_name SELECT * FROM old_table_name with a WHERE
clause
to populate the newly defined smaller table with the test data. It is always wise to keep a
hard copy of your test data close at hand.

Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-3

Student Notebook

The test tables should be populated with a statistically accurate representation of the
production data, but with significantly fewer rows. The test data should be designed so
that:
1. All possible forms of input are verified/validated
2. All logic paths in the code are exercised

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

3. The most commonly occurring error conditions are handled

pr

Ex

cl

It is very common to privatize the data from production before loading it into test. A
product like Optim is ideal for performing that task.

4-4

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Creating a test environment Indexes and Views


X
DE
N
I
TE
EA EX_A
R
C IN D
.....

ATE
VIE
..... W V1

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

CRE

Programmer

CREATE INDEX
INDEX_B
.....

CREATE VIEW V2
.....

CR
EA
T
IND E IN
EX D E
.... _C X
.

3
WV
E
I
V
ATE ...
E
..
CR

Copyright IBM Corporation 2007

Figure 4-3. Creating a test environment - Indexes and Views

CE0317.0

Notes:

cl

When replicating a production system, be sure to create the same indexes and views as
are used on that system. Also, consider building indexes on additional columns to see if the
DB2 Optimizer (we will discuss that later in the course) would use them.

pr

Ex

If creating a test environment for a new system, meet with the DBA and users to determine
how the data will be accessed (which columns will be included in WHERE, ORDER BY,
and GROUP BY clauses, and which columns should contain unique values). These are
columns which are the primary candidates for use in creating indexes.

Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-5

Student Notebook

Creating a test environment Catalog statistics

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Updating
test
statistics

Gathering
production
statistics

Programmer

DBA

Catalog

Copyright IBM Corporation 2007

Figure 4-4. Creating a test environment - Catalog statistics

CE0317.0

Notes:

Ex

cl

Although our test tables have very few rows, and DB2 will process them very quickly when
we run queries against the test tables, we want the DB2 Optimizer to make the same
access strategy decisions (how to access the data most efficiently) it would make when
running against production data. We want DB2 to think that the test tables contain
production quantities of data. In other words, we are replicating the statistics from the
production system to test, in order to simulate the production-level volume of data.

pr

In most shops, only DBAs and system administrators (SYSADMs) have the authority to
update catalog values. The programmer can assist in gathering the production level
statistics that are needed.

4-6

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Prototyping Change SQL using a unit of work

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SPUFI / CLP / GUI Command Editor will accommodate


unit of work processing

Recommendation: Don't let your test environment change


Prototyping an INSERT, UPDATE, DELETE
SELECT ...;

INSERT ...; (or UPDATE, DELETE)


SELECT ...;

ROLLBACK ...;

Copyright IBM Corporation 2007

Figure 4-5. Prototyping Change SQL using a unit of work

CE0317.0

Notes:

cl

Let's take a minute to address the issue of change SQL statements i.e., INSERT,
UPDATE, and DELETE statements. These statements change the test tables. However,
remember that we want to stabilize our test environment change.

Ex

To avoid a permanent change to the data in our tables, we run several SQL statements as
a unit of work, which means that the work is not made permanent unless all SQL
statements finish successfully. If one statement fails, all work done by the statements that
completed up to that point will be undone. Semicolons are used to end a query when
multiple queries are stacked within a unit of work.

pr

Lets describe the statements above in a little more detail. The first SELECT statement
(known as the BEFORE SELECT) displays what the table data looks like before the
change SQL statement is executed. The second SELECT statement (known as the
AFTER SELECT) displays the table data as it is after INSERT, UPDATE or DELETE
statement has run. By comparing the results of the two SELECTs, the application
programmer should be able to determine if the effects of the SQL were what was expected.

Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-7

Student Notebook

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

The ROLLBACK statement will undo all of the changes to the data affected by the INSERT,
UPDATE or DELETE. Therefore, this unit of work approach is an effective technique for
testing the logic of change SQL statements, while at the same time preserving your test
data in its original state.

4-8

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Prototype SQL against test tables

SELECT ....
FROM ..., ..., ...
WHERE ..... AND
.....
ORDER BY ..., ...

SELECT .... FROM ..., ..., ...


WHERE ..... AND .....(SELECT ...
FROM ... WHERE ...)
ORDER BY ..., ...

Programmer

Copyright IBM Corporation 2007

Figure 4-6. Prototype SQL against test tables

CE0317.0

Notes:

cl

The application programmer should consider whether or not there are alternative SQL
statements that could be utilized to meet the logic requirements of the program. If so, then
he or she should try to choose the SQL statements that would perform most efficiently.

pr

Ex

When the program is prepared for execution, the DB2 optimizer will choose the access
path which is most likely to consume the fewest system resources (I/Os and CPU cycles).
If DB2 perceives that a different SQL statement will perform better than the one coded by
the programmer (and yet still guarantee the same results set), DB2 will actually change the
statement so that it will perform better this is called Automatic Query Rewrite, or AQR for
short.
The SQL Workshops (CE12 and CE13) teach alternate methods of coding SQL
statements. The Performance and Tuning Workshops deal with evaluating and monitoring
the performance impact of these methods. See Appendix B for further courses and details.

Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-9

Student Notebook

EXPLAIN the queries


For the first query, I'll read
every row in the table...

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DB2, here are


three queries
that produce the
same report. Tell
me how you will
execute each of
them.

For the second query, I'll use


Index NDX1 to find ...
For the third query, I can
find all the data by looking
in the index only ...

DB2

Tables - R - Us

Integrity
&
Recovery

Programmer

Continuous
Operation

Logging

Optimizer

Lock
Manager

Security

Copyright IBM Corporation 2007

Figure 4-7. EXPLAIN the queries

CE0317.0

Notes:

cl

Suppose that we've written three queries that will bring back the same data. Built into DB2
is a facility called EXPLAIN. We can use this facility to ask DB2 to tell us how it will go
about getting to the data we want. EXPLAIN can be invoked with SQL or from a graphical
user interface (GUI).

pr

Ex

Of the three proposed access paths in the visual above, which one do you think is the
best? Why?

4-10 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

A program with SQL

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Program
write to terminal
|
read from terminal
|
read/write - file
|
SQL - read/write - DB2
|
write to terminal
|
end

Data in a file

Data in DB2

An
additional
source of
data

Copyright IBM Corporation 2007

Figure 4-8. A program with SQL

CE0317.0

Notes:

pr

Ex

cl

Programs with embedded SQL have all their normal capabilities, plus access to relational
data managed by DB2. Such programs do require some additional preparation, because
of the SQL statements contained therein.

Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-11

Student Notebook

Steps to coding SQL in a program


Delimit and code SQL statements

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Utilize host variables

Declare the SQL communications area (SQLCA)


Handle exception conditions

Copyright IBM Corporation 2007

Figure 4-9. Steps to coding SQL in a program

CE0317.0

Notes:

Very briefly, here are the major considerations when coding SQL statements into a
program:

cl

Delimiters are used by the precompiler in order to identify the embedded SQL statements
for special processing.

Ex

Host variables are the means by which the program and DB2 pass data back and forth.
An SQLCA is normally included. This set of control blocks provides return codes and
other important information to the program, after each SQL statement is executed.

pr

If there was an error performing the SQL, the programmer needs to take appropriate
action for the error by coding routines for handling the various error conditions that may
occur.
The following visuals will explain these points in more detail.

4-12 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

The SQL delimiters

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Program A
Answering Machine

COBOL delimiters
...

Start flag

Precompiler,
SQL starts
here!

End flag

...

SQL - read a row

Start flag

Precompiler,
SQL ends
here!

.
.

EXEC SQL
DECLARE GLOBAL
TEMPORARY TABLE T1
AS (...)
END-EXEC.

End flag

EXEC SQL
SELECT ...
INTO ...
FROM T1
WHERE ...
END-EXEC.

...

End

Copyright IBM Corporation 2007

Figure 4-10. The SQL delimiters

CE0317.0

Notes:

The opening delimiter is always EXEC SQL. The ending delimiter varies by programming
language.

cl

The ending delimiter for COBOL is END-EXEC.

The ending delimiter for PL/I and C is a semicolon.

pr

Ex

In the program, note the DECLARE GLOBAL TEMPORARY TABLE T1 AS (...) SQL
statement. Within a program, SQL can be used to define temporary tables that may be
accessed by SQL statements in other areas of the same program. Between the
parentheses, an SQL SELECT statement is written that defines the set of columns and
rows that will be known as T1.

Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-13

Student Notebook

Host variables

3'

EXEC SQL

NEW
PHONE

UPDATE COMPANY.DIRECTORY

777'
'555-1

323
'555-

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EXEC SQL
UPDATE COMPANY.DIRECTORY
SET PHONE = '555-3233'
WHERE PHONE = '555-1777'
END-EXEC.

OLD
PHONE

SET PHONE =

WHERE PHONE =

END-EXEC.

Copyright IBM Corporation 2007

Figure 4-11. Host variables

CE0317.0

Notes:

Ex

cl

When imbedding an SQL statement in a host language program, our SQL statements
become more flexible. Instead of hardcoding values in the SQL statement (as in the first
UPDATE statement above), we can use host variables (as in the second UPDATE
statement) in their place. The first UPDATE is fixed, and can only be used once with the
hard-coded values. The second UPDATE is re-useable, because the program logic can
repeatedly replace the values in the host variables.

pr

As depicted in the visual, host variables can be thought of as buckets which can hold
information. Host variables are areas of storage that are defined within your program,
which can be used to pass data to DB2, or to receive data retrieved from DB2. The
programmer needs to define host variables with the same data type and length as the
columns with which they will be used.

4-14 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

In COBOL, C
COBOL

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

WORKING-STORAGE SECTION.

01 NEWPHONE PIC X(10)


01 OLDPHONE PIC X(10)
.
.
PROCEDURE DIVISION.
.
ACCEPT OLDPHONE.
.
ACCEPT NEWPHONE.
.
EXEC SQL
UPDATE FRANCE.PARIS
SET PHONE = :NEWPHONE
WHERE PHONE = :OLDPHONE
END-EXEC.

.
char newphone [10];
char oldphone [10];
.
fscanf (stdin, newphone, %s);
.
fscanf (stdin, oldphone, %s);

.
EXEC SQL
UPDATE FRANCE.PARIS
SET PHONE = :newphone
WHERE PHONE = :oldphone ;

Copyright IBM Corporation 2007

Figure 4-12. In COBOL, C

CE0317.0

Notes:

In COBOL, SQL statements must start in position 12 or beyond.

pr

Ex

cl

All host variables must begin with a colon when used within an SQL statement.
Elsewhere in your program logic (outside of the SQL statement), do not prefix the host
variable name with a colon.

Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-15

Student Notebook

Selecting a single row


UNIQUE
VALUES

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

MOVE '000010' TO ID.

EMPNO

LASTNAME

FIRSTNAME

EXEC SQL

000020

THOMPSON

MICHAEL

SELECT LASTNAME, FIRSTNAME

000010

HAAS

CHRISTINE

INTO :LASTNAME, :FIRSTNAME


FROM EMPL

WHERE EMPNO = :ID


END-EXEC.
.

Copyright IBM Corporation 2007

Figure 4-13. Selecting a single row

CE0317.0

Notes:

A query that returns a maximum of one row in the results set is known as a singleton
SELECT.

cl

Selects that return one or no rows are written with an additional clause, the INTO clause.
The INTO clause must name the proper number of host variables to hold the result row.

Ex

The first column in the result row will be placed in the first host variable named, the
second column in the result row will be placed in the second host variable named, and so
forth.

pr

If it is possible for the SELECT to return multiple rows in the results set, another
programming technique is required, called cursor processing.

4-16 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Selecting multiple rows


EXEC SQL

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DECLARE CUR1
CURSOR FOR
SELECT EMPNO, LASTNAME FROM EMPL
WHERE WORKDEPT = :DEP
END-EXEC.

MOVE 'E11' TO WORKDEPT.

EXEC SQL OPEN CUR1 END-EXEC.

EXEC SQL FETCH CUR1 INTO :ID, :NAME


.
.

END-EXEC.

EXEC SQL CLOSE CUR1 END-EXEC.000


Copyright IBM Corporation 2007

Figure 4-14. Selecting multiple rows

CE0317.0

Notes:

When retrieving multiple rows from DB2, the program uses a technique known as cursor
processing. It is comprised of the following four steps.

cl

1. DECLARE the cursor (define the SQL SELECT logic to be executed)

Ex

2. OPEN the cursor (DB2 executes the SQL and accesses the qualifying rows).
Sometimes the results set is placed into a temporary file, and at other times DB2 will
retrieve the rows when they are individually FETCHed by the application.

3. FETCH the rows from the stack. Conceptually, at OPEN time, you can conceive of the
rows as being placed into a stack, from which your program can retreive.

pr

4. CLOSE the cursor, when the application no longer needs to fetch any more rows.
Keep the following points in mind, regarding cursor processing:

SELECTs that could potentially return more than one row must be coded as a cursor.
A program may have any number of cursors.
Conceptually, the SELECT statement is executed at OPEN time.

Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-17

Student Notebook

Normally, your program will retrieve rows in an iterative loop, until you reach the end of
the stack of qualifying rows (SQLSTATE = 02000)

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

A cursor must be closed before it can be reopened.

4-18 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Fetch
EXEC SQL
FETCH CUR1
INTO :ID, :NAME
END-EXEC

1 st

LASTNAME

000020
000010

THOMPSON
HAAS

SQLCA
SQLSTATE

ID

NAME

First Fetch

'00000'

000020

THOMPSON

Second Fetch

'00000'

000010

HAAS

Third Fetch

'02000'

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EMPNO

2 nd

Copyright IBM Corporation 2007

Figure 4-15. Fetch

CE0317.0

Notes:

OPEN CURSOR positions the cursor at the top of the stack.


Each fetch moves the cursor down one row in the stack.

cl

The cursor position is lost when cursor is closed.

pr

Ex

SQLSTATE codes are character return codes. A return code of 00000 means successful
completion of the SQL statement. An SQLSTATE of 02000 means no more rows meet
our criteria (an end-of-file condition).

Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-19

Student Notebook

Scrollable cursors

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EXEC SQL
DECLARE CURSOR K9
SENSITIVE STATIC SCROLL FOR
SELECT ...
END-EXEC.
...
EXEC SQL OPEN K9 END-EXEC.
...
EXEC SQL FETCH LAST FROM K9 INTO :VN:VI END-EXEC.
...
EXEC SQL FETCH RELATIVE -10 FROM K9 INTO :VN:VI
END-EXEC.
...
EXEC SQL FETCH BEFORE FROM K9 INTO :VN:VI END-EXEC.
...
EXEC SQL CLOSE CURSOR K9 END-EXEC.

Copyright IBM Corporation 2007

Figure 4-16. Scrollable cursors

CE0317.0

Notes:

cl

Scrollable cursors allow us to fetch forwards, backwards, using a relative number forwards
and backwards, or using an absolute number. Scrollable cursors are currently only
available in DB2 for z/OS.
When the cursor is opened, DB2 builds the result table in a temporary database.

Ex

The word SCROLL in the cursor definition makes the cursor scrollable. The word STATIC
in this cursor's definition tells DB2 that the rows in the result set are to remain fixed. The
word SENSITIVE in the cursor's definition tells DB2 to keep the result set synchronized
with changes to the actual rows in the table (as opposed to INSENSITIVE).

pr

In the visual above:

The first fetch returns the last row in the result set.
The second fetch returns the row 10 rows above the current cursor position.
The third fetch returns the row above the current cursor position.

4-20 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Handling exception conditions

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Program A

Program Logic

SQL
read a row

A call

DB2

to DB
2

Tab l es - R - U s

Integrity
&
Recovery

SQLCA -

Continuous
Operation

Check SQL messages,


Take appropriate action

essage Return m
r failure?
Success o

Logging

Optimizer

Program Logic

Lock
Manager

Security

End

Copyright IBM Corporation 2007

Figure 4-17. Handling exception conditions

CE0317.0

Notes:

cl

The SQLCA is a structure of specifically named host variables to which DB2 writes after
each call to DB2. Many shops have standard error handling routines that can be copied
into your program and performed, or called, after each SQL statement. It is recommended
that a DB2 application program check the SQL return codes and information after each call
to DB2.

Ex

Within SQLCA, the first thing you will probably want to check is the SQL return code. You
have a choice of:
SQLCODE, the original return code, which may be platform-specific.

pr

SQLSTATE, added later as the ANSI standard, all-platform return code.

Both SQLCODEs and SQLSTATEs are documented in the DB2 Message Reference
manual or the DB2 Messages and Codes manual. The general categories of these codes
are explained on the next page.

Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-21

Student Notebook

SQLCA codes
CHAR(5)
SQLSTATE

REQUEST STATUS

<0

>"02nnn"

FAILED

"01nnn"

SATISFIED WITH
SPECIAL CONDITION
(SQLWARN0 = 'W')

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

INTEGER
SQLCODE

CONDITION
ERROR

WARNING

>0 &
<> 100

+100

NOTFOUND
SUCCESS

"02nnn"

(MORE)
DATA NOT FOUND

"00000"

SUCCESS

Copyright IBM Corporation 2007

Figure 4-18. SQLCA codes

CE0317.0

Notes:

cl

The SQLCA (SQL Communications Area) is set of control blocks that provides return
codes and other important information to the program, after each SQL statement is
executed.
All negative return codes are considered errors.

Ex

All positive return codes not equal to 100 are considered warnings. Also, a 'W' in
SQLWARN0 indicates a warning, even if SQLCODE = 0.

The meaning of SQLCODE values other than 0 and 100 is product specific.

pr

Here is a listing of some of the more commonly-encountered SQLCODEs in DB2 for z/OS:
Some common SQLCODE values:
-551 - <authorization-ID> does not have the privilege to perform operation <operation> on
object <name>. (SQLSTATE 42501)
-818 - A timestamp conflict occurred. (SQLSTATE 51013)
-805 - Package <package-name> was not found. (SQLSTATE 51002)
4-22 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

-904 - Unsuccessful execution caused by an unavailable resource. Reason code:


<reason-code>, type of resource: <resource-type>, and resource name: <resource-name>.
(SQLSTATE 57011)
-911 - The current transaction has been rolled back because of a deadlock or timeout.
Reason code <reason-code>. (SQLSTATE 40001)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

-305 - The NULL value cannot be assigned to a host variable in the SELECT or FETCH
statement because no indicator variable is specified. (SQLSTATE 22002)
-811 - The result of a scalar fullselect, SELECT INTO statement, or VALUES INTO
statement is more than one row. (SQLSTATE 21000)

+100 - No row was found for FETCH, UPDATE, or DELETE; or the result of a query is an
empty table. (SQLSTATE 02000)

pr

Ex

cl

Detailed descriptions of all error and warning messages can be found in the DB2
Messages and in the DB2 Codes reference manuals.

Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-23

Student Notebook

Precompile
Program A

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Answering Machine

Precompiler,
SQL starts
here!

DBRM or a bind file

Program A
Statement 27
200110271345050558
SQL - read a row
.
.
.
.

Precompiler
copies SQL

** SQL - read a row


**
Precompiler,
**
SQL ends
here!
**

Call DB2 - Run program A


statement 27
200110271345050558
.
End

Copyright IBM Corporation 2007

Figure 4-19. Precompile

CE0317.0

Notes:

There is one input to the precompiler (source code), and two outputs (modified source
code, and the DBRM or bind file).

cl

Some of the things that the precompiler does are:

Ex

Performs some syntax checking on the SQL statements (a more thorough check occurs
at BIND time)
Copies the source code to a new file

Comments out the SQL as it is copying the source code

pr

Includes code from include libraries

Numbers each statement in the program


Types in a CALL to DB2 after each commented out SQL statement (SELECT, INSERT,
UPDATE, DELETE, and so forth)
Adds the consistency token to each CALL and each SQL statement

4-24 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Copies the SQL statements to a new file (DBRM or bind file)

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Uempty

Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-25

Student Notebook

Compile and Link Edit

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Program A

Modified
source
code

Answering Machine

Compiler

Precompiler,
SQL starts
here!

** SQL - read a row


Precompiler,
**
SQL ends
**
here!
**

Link edit

Call DB2 - Run program A


statement 27
199710271345050558
.
End

Load
module

Copyright IBM Corporation 2007

Figure 4-20. Compile and Link Edit

CE0317.0

Notes:

pr

Ex

cl

After the precompile has successfully completed, the modified source code goes through
normal compile and link edit steps, to create an executable load module.

4-26 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Bind package (1 of 4)
Bind Package

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DBRM or bind file


Program A
Statement 27
199710271345050558
SQL - read a row
.
.
.
.

DB2

Tables - R - Us

Integrity
&
Recovery

Continuous
Operation

Logging

Optimizer

Lock
Manager

Security

Catalog

Syntax Check

Copyright IBM Corporation 2007

Figure 4-21. Bind package (1 of 4)

CE0317.0

Notes:

pr

Ex

cl

The bind package process can be thought of as a compile of the SQL portion of the
application program. During the bind, DB2 does a thorough check of the SQL syntax. If
there are errors that were not detected during the precompile, the SQL parser will find them
and flag them as the first major step of the BIND process.

Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-27

Student Notebook

Bind package (2 of 4)
Bind Package

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DBRM or bind file


Program A
Statement 27
199710271345050558
SQL - read a row
.
.
.
.

DB2

Tables - R - Us

Integrity
&
Recovery

Continuous
Operation

Logging

Optimizer

Lock
Manager

Security

Catalog

Object Validation

Copyright IBM Corporation 2007

Figure 4-22. Bind package (2 of 4)

CE0317.0

Notes:

pr

Ex

cl

It is very common for a programmer to misspell a table or column name when coding SQL
statements. This second major step of the BIND process checks to see if all of the tables
and columns referenced in the call actually exist in the DB2 Catalog. If no errors are
detected during this phase, then the next step is a security check.

4-28 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Bind package (3 of 4)
Bind Package

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DBRM or bind file


Program A
Statement 27
199710271345050558
SQL - read a row
.
.
.
.

DB2

Tables - R - Us

Integrity
&
Recovery

Continuous
Operation

Logging

Optimizer

Lock
Manager

Security

Catalog

Security Check

Copyright IBM Corporation 2007

Figure 4-23. Bind package (3 of 4)

CE0317.0

Notes:

pr

Ex

cl

DB2 checks the catalog authorization tables to ensure that the application and user has the
proper authorization to execute the program's SQL statements. If there are no
authorization errors or violations, then the BIND process can continue to the last step in
preparing the applications SQL for execution by DB2.

Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-29

Student Notebook

Bind package (4 of 4)
Bind Package

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DBRM or bind file

DB2

Program A
Statement 27
199710271345050558
SQL - read a row
.
.
.
.

Tables - R - Us

Integrity
&
Recovery

Continuous
Operation

Logging

Optimizer

Directory

Lock
Manager

Security

Packages

Catalog

Optimization

Copyright IBM Corporation 2007

Figure 4-24. Bind package (4 of 4)

CE0317.0

Notes:

It is during the last step of the BIND process that DB2's optimizer decides how it is going to
access the data.

Ex

cl

Earlier in this course, we mentioned that SQL is a non-navigational language, meaning that
we tell DB2 WHAT data we want to retrieve, but not HOW to retrieve that data. It is at
optimization time that DB2 determines how it will execute the SQL.

pr

To make the best access choices, DB2 must learn about the data that we're trying to
access. The optimizer goes to the catalog, and looks up information about the table(s) and
the table's indexes. The optimizer finds out how big the table is (how many pages and how
many rows). For each index, it finds how many unique values it contains, and for each
non-unique index DB2 finds, and what that index's most common values are.

With this, and additional information, the optimizer begins the task of finding the least cost
route to the data based on our SQL query's requirements. DB2 evaluates the WHERE
clause to see if, and which, indexes might be used to speed up access to the data. DB2
calculates the cost (in I/O and CPU cycles) of reading all the rows in the table (a table

4-30 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

scan). It calculates the cost of using index x, and of using index y. If appropriate, it
calculates the cost of using multiple combinations of indexes.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

The bottom line is that DB2 comes up with every route to the data it can think of, and then
calculates the cost of each route. It then compares the cost of each route, and the least
cost route is chosen as the access strategy for that SQL statement. Think of that route as
being a map to the data that our SQL statement requires. DB2 writes the map out to a
package which is kept in DB2's directory. This map will never change unless we rebind the
DBRM. A rebind asks DB2 to recalculate the cost of each possible route to the data, and
again, choose the least cost route and write it out to a package.

Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-31

Student Notebook

Bind plan z/OS

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Bind Plan
DB2

Tables - R - Us

Integrity
&
Recovery

Directory

Continuous
Operation

Optimizer

Logging

Plans
Plan A

Directory

Pkg A

Plans

Lock
Manager

Directory

Packages

Security

Catalog

Package Pointers

Copyright IBM Corporation 2007

Figure 4-25. Bind plan - z/OS

CE0317.0

Notes:

With DB2 z/OS, a plan is the means by which the SQL statements are executed, when the
application program (which is associated with that SQL) is run.

Ex

cl

There are two types of plans. The original plan consisted of a monolithic structure, into
which all of the SQL statements were directly bound. This structure could grow very large,
and even a slight modification to the SQL within the plan required that the entire plan go
through the BIND process. This proved to be cumbersome and resulted in the
consumption of considerable system resources.

pr

In version 2.3 of DB2 on the mainframe, an alternative method was made available to
users. A single program containing SQL could be bound into a structure called a package,
which provided users with a more modular approach. With this new capability, program
maintenance became easier and less resource consumptive, since only the program with
the changes to its SQL needed to be re-bound, rather than the entire plan structure.

However, there was still the requirement to execute the SQL by means of a plan. The new
plan could now consist simply of a directory list of the individual packages within the plan.
A plan which consists of a package list is analogous to a PDS directory pointing to
4-32 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

individual members. Inside a plan are the names of the packages with which the load
module may work. The programmer or DBA creates a plan, and in the plan they list the
packages that the load module is allowed to work with. Plans are stored in the DB2
directory.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Over time DB2 shops migrated to this new methodology of binding packages. With the
most current version of DB2 z/OS (DB2 10), the older method of binding DBRMs directly
into a plan is no longer supported. It is required of DB2 10 users to use the newer package
approach.

pr

Ex

cl

The use of plans and packages is explored in more detail in the Application Programming
Workshop courses.

Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-33

Student Notebook

Program execution z/OS

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

RUN PROGRAM (A) PLAN(A)

Directory

Load Module
(Program A)

Plans
Plan A

Pkg A

At the first
call to DB2

Directory

Packages
Pkg A

Copyright IBM Corporation 2007

Figure 4-26. Program execution - z/OS

CE0317.0

Notes:

cl

The PLAN option of the RUN command is not required if the plan name is the same as the
program name. In the example in the visual above, since the plan-name and the
program-name are both A, the PLAN keyword is not mandatory.

pr

Ex

In DB2 LUW (which was developed a number of years after the availability of DB2 for the
mainframe environment), the concept of a plan does not exist. In the LUW environment,
only packages are used .there are no plans.

4-34 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Program execution Linux, UNIX, and Windows

RUN PROGRAM (A)

Directory

Load Module
(Program A)

At the first
call to DB2

Packages
Pkg A

Copyright IBM Corporation 2007

Figure 4-27. Program execution - Linux, UNIX, and Windows

CE0317.0

Notes:

pr

Ex

cl

Since plans are not used by DB2 for Linux, UNIX, or Windows, running the program
instructs DB2 to locate the appropriate package. DB2 locates the package in the DB2
directory when the application program encounters the first CALL to DB2.

Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-35

Student Notebook

Program preparation summary


Source Program

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Include Libraries

Precompiler

Bind Package/Plan

Program A

Modified
Source
Code

Answering Machine

.
.

Tables - R - Us

DB2

Integrity
&
Recovery

Compiler

Continuous
Operation

Precompiler,
SQL starts
here!

** SQL - read a
row
**
**
**

Logging

Link Edit

Precompiler,
SQL ends
here!

DBRM Program A
Statement 27
199710271345050558
SQL - read a row
.
.
.
.

Optimizer

Directory

Lock
Manager

Security

Packages

Catalog

Call DB2 - Run Program


A
statement 27
199710271345050558

DBRM

Load
Module

End

At the first
call to DB2

Syntax Check
Security check
Access Strategies

RUN PROGRAM (A) Plan(A)


or
RUN PROGRAM (A)

Copyright IBM Corporation 2007

Figure 4-28. Program preparation summary

CE0317.0

Notes:

pr

Ex

cl

Here is the entire program preparation process on one page.

4-36 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Why use Stored Procedures?


Server

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Client
EXEC SQL INSERT ...

Process SQL

EXEC SQL SELECT ...

Process SQL

EXEC SQL UPDATE ...

Process SQL

EXEC SQL INSERT

Process SQL

Presentation Logic
+ Business Logic

Data Management

Copyright IBM Corporation 2007

Figure 4-29. Why use Stored Procedures?

CE0317.0

Notes:

cl

The visual above illustrates an approach to multi-tiered applications . That of a thick


client (where both the presentation logic and business logic reside), and a database server
location. Notice that each SQL statement at the client location results in both a send and
receive transmission across the network to the database server.

pr

Ex

Network traffic can have a very noticeable impact upon overall application performance.
Ideally, applications should be designed to minimize message traffic across the network.

Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-37

Student Notebook

Applications with Stored Procedures


Client

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Server
Parameters

EXEC SQL CALL PROCA ...

DB2

PROCA

SQL
SQL

Stored
Procedure

SQL

Presentation Logic

Returned values
from PROCA

Data Management
+ Business Logic

Copyright IBM Corporation 2007

Figure 4-30. Applications with Stored Procedures

CE0317.0

Notes:

cl

This visual illustrates a different programming approach. Here we have a thin client,
consisting primarily of the presentation logic. The business logic is encapsulated at the
database server location. The business logic is invoked by the client issuing a CALL to a
program resident at the database, called a stored procedure.

pr

Ex

The call across the network includes parameters required by the stored procedure. The
stored procedure uses the parameters to complete its logic. The stored procedure
executes, and, when finished, may return a set of values or rows to the calling program.
When comparing this programming approach with the previous one, it becomes obvious
that this model makes more efficient use of the network bandwidth, by significantly
reducing the number of transmissions between the client and server.

Additionally, program maintenance is enhanced. Since the main program logic resides at
the single server location, rather than at each of the clients, it is much easier to make
changes to the application, and we can ensure consistency of the logic for all of the clients
that execute the program.

4-38 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Stored Procedures Summary

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Define input and output parameters in both the calling


program and the Stored Procedure
An SQL statement calls the Stored Procedure

EXEC SQL CALL SPNAME(P1, P2, P3) END-EXEC.


Define the Stored Procedure to DB2 with the
CREATE PROCEDURE SQL statement

Precompile, compile, link edit, and bind the programs

Copyright IBM Corporation 2007

Figure 4-31. Stored Procedures - Summary

CE0317.0

Notes:

cl

The call to the stored procedure must be delimited. The above call example is calling a
stored procedure named SPNAME and passing three parameters (P1, P2, P3). The stored
procedure could be written in any number of languages, such as COBOL, C, C++, SQL
Procedure Language, Java, and so on.

pr

Ex

The stored procedure could have been written and tested by utilizing the Data Studio
software. Data Studio is an Eclipse-based GUI tool that makes it easier for the
programmer to develop stored procedures in DB2.

Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-39

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Data Studio

Copyright IBM Corporation 2007

Figure 4-32. Data Studio

CE0317.0

Notes:

cl

IBM Data Studio is an Eclipse-based tool that replaces the DB2 Developer Workbench
from DB2 for Linux, UNIX, and Windows. IBM Data Studio is a comprehensive
development environment for creating, editing, debugging, deploying, and testing DB2
stored procedures and user-defined functions. You can also use IBM Data Studio to
develop SQLJ applications, and to create, edit, and run SQL statements and XML queries.

Ex

The IBM Data Studio includes functionality that is comparable to Developer Workbench,
including these items:

pr

Database code designers for SQL statements, stored procedures, and user-defined
functions
Support for running statements in the database
Debugging support for DB2 stored procedures

The IBM Data Studio also provides cross-platform support for IBM DB2 Data Server and
Informix Dynamic Server.

4-40 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Static SQL An SQL statement DB2 has!

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Program A
Answering
Machine

Modified
Source
Code

.
.

Tables - R - Us

Compiler

DB2

Precompile
r,
SQL starts
here!

Integrity
&
Recovery

Link
Edit

** SQL - read a row


**
Precompile
**
r,
**
SQL ends

Continuous
Operation

here!

Optimizer

Logging

Call DB2 - Run Program A


statement 27
199710271345050558

Directory

Load
Module

.
End

Plans
Plan A

At the first
call to DB2

Pkg A

Lock
Manager

Security

Directory

Packages
Pkg A

Catalog

RUN PROGRAM (A) Plan(A)

Copyright IBM Corporation 2007

Figure 4-33. Static SQL - An SQL statement DB2 has!

CE0317.0

Notes:

To be a static SQL statement, DB2 must:

Be aware of the SELECT, INSERT, UPDATE, or DELETE statement at bind time.

cl

Store the statement's access strategy in a package (or plan).

pr

Ex

When the program is executed, DB2 locates the predetermined access strategy and
follows that strategy to the data. DB2 will not consider changing the access strategy until
the program is once again bound.

Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-41

Student Notebook

Dynamic SQL Statements DB2 doesn't have

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

COBOL WRITE MESSAGE

Write an SQL statement

ACCEPT

WHATS-IN-THIS-BUCKET.
.

UPDATE FRANCE.PARIS
SET PHONE = '555-2727'
WHERE PHONE = '555-1778'

** EXEC SQL
**

EXECUTE IMMEDIATE

**

:WHATS-IN-THIS-BUCKET

** END-EXEC.

CALL DB2 RUN STMT 45


DBRM X

199710270842542532.

Copyright IBM Corporation 2007

Figure 4-34. Dynamic SQL - Statements DB2 doesn't have

CE0317.0

Notes:

Dynamic SQL statements are statements that DB2 acquires during the execution of the
program: statements of which DB2 was not aware at bind time.

Ex

cl

In this example, the program contains some presentation logic that requests an SQL
statement from the user. The user's SQL statement (a string of characters) will be placed in
a host variable. The SQL that is hard-coded in the program tells DB2 to EXECUTE
IMMEDIATE the string of characters in the host variable. INSERT, UPDATE, and DELETE
statements can be handled almost this easily. SELECT statements, however, may require
much more complex coding.

pr

The SQL that DB2 evaluates at bind time is the EXECUTE IMMEDIATE
:host_variable_name. At bind time, the host variable contains nothing. At execution time,
the user supplies the SQL, the call to DB2 passes the contents of the host variable (the
user's SQL statement) to DB2, and DB2 must dynamically bind that statement (check the
syntax, check the user's authorization, and determine the least cost access strategy). That
access strategy does not become a permanent part of the package.

4-42 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Static versus Dynamic

Static SQL

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Coded in the program, always performs the same function


on the same tables and columns, but the rows affected are
often identified at execution time. Its access strategy is
placed permanently into the package by the bind process.

Dynamic SQL

SQL that DB2 is not aware of at bind time. Therefore, it


must go through a dynamic bind at execution time. The
dynamically created access strategy is not retained,
though it may be cached in memory.

Copyright IBM Corporation 2007

Figure 4-35. Static versus Dynamic

CE0317.0

Notes:

pr

Ex

cl

Above is a summary, comparing static to dynamic SQL.

Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-43

Student Notebook

Dynamic programming interfaces

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

CLIENT

APPLICATION PROGRAM

Embedded SQL
CLI/ODBC
Java

Run-Time Client

Static SQL
Dynamic SQL
APIs

DATABASE ENGINE

SERVER

Copyright IBM Corporation 2007

Figure 4-36. Dynamic programming interfaces

CE0317.0

Notes:

cl

A client/server application program gets access to data through the Run Time Client (RTC)
code (for Windows-based applications there is even DB2 RTCL (Lite)). This can run static
or dynamic programs from a client accessing a DB2 server. It can also run program APIs.

Ex

Application programs can issue SQL via the Run-Time Client component using one of
several possible programming interfaces.
Embedded SQL
CLI

pr

- ODBC
Java

- JDBC
- SQLJ

4-44 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Integration of XML and relational capabilities


Applications combine XML and relational data

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Native XML data type (server and client side)


XML capabilities in all DB2 components

DB2 SERVER

CLIENT

DB2 Storage:
Storage:
DB2

SQL/XML

Relational
Relational
Interface

DB2 Client /
DB2 Client /
Customer
Customer
Client
Application

Relational
Relational

DB2 Engine

XQuery

XML
XML

XML
Interface

Copyright IBM Corporation 2007

Figure 4-37. Integration of XML and relational capabilities

CE0317.0

Notes:

Beginning with DB2 9: Integration of XML and relational capabilities so that applications
can combine XML and relational data.

cl

A new DB2 datatype: XML type

The XML type is visible on the server and the client.

Ex

There is deep integration as the figure above shows. This is not an add-on, extender, or
wrapper.

pr

The DB2 infrastructure is reused wherever it made sense, with new system code where
needed.

DB2 9 for LUW supports a new query language: XQuery, with a new parser (side by side
of SQL parser).
XML data is natively storage: hierarchical, optimized for parent/child traversal.

XML permeates the engine. You can use SQL or XQuery to query a mix of relational and
XML data stored in DB2. The same complier handles both XML and relational data, so
Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-45

Student Notebook

SQL and XQuery queries can be mixed. Because XML is not just another numeric type,
but a significant change, special efforts are necessary in almost all system components,
tools and utilities to handle XML.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Xquery: Xquery is an emerging standard for access to XML data. It is designed, from the
ground up, to handle semi-structured data. DB2 supports both the Xquery language and
the SQL extensions for accessing XML data in a DB2 table.

4-46 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

What is XML? (1 of 2)
XML = eXtensible Markup Language
XML is self-describing data

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Here is some data from an ordinary delimited flat file:

47; John Doe; 58; Peter Pan; Database systems; 29; SQL;
relational

What does this data mean?

<book>
<authors>
<author id=47>John Doe</author>
<author id=58>Peter Pan</author>
</authors>
<title>Database systems</title>
<price>29</price>
<keywords>
<keyword>SQL</keyword>
<keyword>relational</keyword>
</keywords>
</book>

Copyright IBM Corporation 2007

Figure 4-38. What is XML? (1 of 2)

CE0317.0

Notes:

cl

The example of data above taken from a flat file shows a line of raw data. We see the
numbers 47, 58, 29 and the words John Doe, Peter Pan but we dont know what each of
these pieces of data means; the context of the data is external to the data itself.

Ex

The XML markup shown above is in blue and the actual data is in red. By viewing both, we
know what this data means. XML is eXtensible Markup Language. The markup shows the
tags used to describe the data. XML is a self describing kind of data.
The key difference between HTML and XML markup languages:
XML describes the relationship of the data.

pr

HTML describes how to display the data in a browser.

XML is extensible this means you can make up your own markup and tags to describe
the data. You can create your own tags if they are not tied to a certain schema.
HTML has a fixed vocabulary of tags and can only use those tags.

Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-47

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

What is XML? (2 of 2)

Copyright IBM Corporation 2007

Figure 4-39. What is XML? (2 of 2)

CE0317.0

Notes:

cl

Elements and attributes make up an XML document. These elements always have a start
and stop tag. Tags are always in angle brackets <> and the end tag is denoted by the slash
/. Elements can be nested. The root element is the outer most element for the document
as shown above, the book element is the root element.

Ex

Attributes can be specified for an XML element. As shown above, the author element has
an id attribute assigned to it. The attributes are always defined in the start tag. In the code
shown above, the value of the id attribute for the first author is 47 and assigned to the
author element in the first set of angle brackets.

pr

Note that the authors element (denoted by the authors tag) contains two nested elements
named author.

4-48 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

The XML document tree

Copyright IBM Corporation 2007

Figure 4-40. The XML document tree

CE0317.0

Notes:

cl

Every XML document can be viewed as a tree a hierarchy of nodes. Every XML
document can be seen as a higher key of nodes. It may be intuitive that this represents a
hierarchy of nodes. In the example above, book is the root node of the tree. The data
elements are always the leaf nodes in the tree (such as the title element in the example
above).

Ex

XML parsing typically produces this tree form. This is how XML can be manipulated by
software storing XML in trees in DB2 and not as text, which is more efficient and faster
to manipulate. This is done by parsing the tree.

pr

Serialization is reversing the parsing operation, which takes a tree and returns it to XML
document format.
In the graphic shown above, each node would have the following paths:
root node (book element)
/book
- authors node (authors element) /book/authors
Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-49

Student Notebook

author node (author element)


/book/authors/author (two members)
/book/authors/author/@id (author attribute = id)
- author text node (text element)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

/book/authors/author/text (values are John Doe, Peter Pan)


-title node (title element)
/book/title

title text node (text element)

/book/title/text (value is Database System)


- price node (price element)
/book/price

price text node (text element)


/book/price/text (value is 29)

- keywords node (keywords element)


/book/keywords

keyword node (keyword element)

/book/keywords/keyword (two members)


- keyword text node (text element)

pr

Ex

cl

/book/keywords/keyword/text (values are SQL, relational

4-50 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Two ways to query XML data


Using XQuery as the primary language

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

XQUERY db2-fn:xmlcolumn (CUSTOMER.INFO")


Returns all Documents in column INFO

Using SQL as the primary language

Select Pid from PRODUCT xmlp


WHERE XMLExists('declare default element namespace
"http://posample.org";$p/product/description[price
< 100]'
passing xmlp.description as "p");

Copyright IBM Corporation 2007

Figure 4-41. Two ways to query XML data

CE0317.0

Notes:

You may use a new query language XQuery to retrieve XML documents (and parts of
documents) from the database.

cl

Example 1 Xquery:

XQUERY db2-fn:xmlcolumn ("CUSTOMER.INFO")

Ex

Results - serial view:


...

<customerinfo xmlns="http://posample.org" Cid="1002">

pr

<name>

Jim Noodle
</name>

<addr country="Canada">
<street>
Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-51

Student Notebook

25 EastCreek
</street>
<city>
Markham

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

</city>
<prov-state>
Ontario

</prov-state>
<pcode-zip>
N9C 3T6

</pcode-zip>
</addr>

<phone type="work">
905-555-7258

</phone> </customerinfo> ...


Example 2 SQL/XML:
Select Pid

from PRODUCT xmlp

WHERE XMLExists('declare default element namespace

"http://posample.org";$p/product/description[price < 100]'


passing xmlp.description as "p");
Results - serial view:

cl

PID

----------

Ex

100-100-01
100-101-01
100-103-01

pr

100-201-01

4-52 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Checkpoint (1 of 2)
1. Why is it a good idea to have a test environment?

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

2. How can we determine what access path DB2 will take when it
executes an SQL statement?
3. Why must a host language program have an SQLCA (SQL
Communications Area)?
4. Why must SQL statements in a host language program be
delimited?

5. What is the opening delimiter for every SQL statement (except for
the Java language)?
6. List several things the PRECOMPILER does for us (minimum of
three things).

7. List several of the things DB2 does during the bind of a package.

Copyright IBM Corporation 2007

Figure 4-42. Checkpoint (1 of 2)

CE0317.0

Notes:

Write down your answers here:


1.

cl

2.

Ex

3.
4.
5.

pr

6.
7.

Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-53

Student Notebook

Checkpoint (2 of 2)
Name two benefits of using stored procedures.

9.

Host variables, when used within SQL delimiters, must all begin
with what?

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

8.

10. A SELECT statement that we know will return either 0 or 1 row (a


singleton SELECT) must have which additional clause when
embedded in a host language program?
11. A SELECT statement that could return more than one row to an
application program must be coded using which kind of
processing?

Copyright IBM Corporation 2007

Figure 4-43. Checkpoint (2 of 2)

CE0317.0

Notes:

Write down your answers here:


8.

cl

9.

Ex

10.

pr

11.

4-54 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Unit summary

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Having completed this unit, you should be able to:


Understand how to create a test environment

List the necessary steps to coding SQL in a program


Describe the purpose of SQL delimiters
Describe the purpose of an SQLCA

List the steps involved in preparing a program for execution


State the differences between static and dynamic SQL
Understand XML data at the conceptual level

Copyright IBM Corporation 2007

Figure 4-44. Unit summary

CE0317.0

pr

Ex

cl

Notes:

Copyright IBM Corp. 1997, 2011

Unit 4. How Does a Programmer Use DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-55

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Student Notebook

4-56 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Unit 5. What does a System Administrator do in


DB2?
What this unit is about

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

This unit describes the responsibilities of system and database


administrators.

What you should be able to do

After completing this unit, you should be able to:

List some of the tasks performed by a DB2 System Administrator:


-

Identifying the DB2 product


Installing DB2
Creating subsystems/instances, databases and table spaces
Authorizing
Monitoring

List some of the tasks performed by a DB2 Database


Administrator:
-

Creating, altering and dropping tables, views and indexes


Planning locking strategies
Running utility jobs
Authorizing

How you will check your progress


Checkpoint questions

pr

Ex

cl

Accountability:

Copyright IBM Corp. 1997, 2011

Unit 5. What does a System Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-1

Student Notebook

Unit objectives

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

After completing this unit, you should be able to list some


of the tasks performed by a DB2 System Administrator:

Identifying the appropriate DB2 product


Installing the DB2 software
Creating subsystems (z/OS) and/or instances (LUW)
Creating DB2 system objects
Granting initial authorizations in DB2
Monitoring overall DB2 performance

Copyright IBM Corporation 2007

Figure 5-1. Unit objectives

CE0317.0

Notes:

After completing this unit, you should be able to list some of the tasks performed by a DB2
System Administrator:

cl

Identifying the appropriate DB2 product


Installing the DB2 software

Ex

Creating DB2 subsystems (z/OS) and/or DB2 instances (LUW)


Creating DB2 system objects

Granting initial authorizations in DB2

pr

Monitoring overall DB2 performance

5-2

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

What does a System Administrator do in DB2?

Product
installation

System
Admin

Grantor
of initial
privileges

Creation of
system
objects

DB2 system
performance

Copyright IBM Corporation 2007

Figure 5-2. What does a System Administrator do in DB2?

CE0317.0

Notes:

System administrators may have responsibilities over many DB2 systems.

pr

Ex

cl

The are the ones who will be installing the RDBMS software on the system. They are
responsible for creating the system level objects, and for the initial granting of authorities
within the DB2 environment. They are also responsible for the overall performance of DB2.

Copyright IBM Corp. 1997, 2011

Unit 5. What does a System Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-3

Student Notebook

DB2 products

Linux/UNIX

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Windows
DB2

DB2

iSeries

zSeries

DB2

DB2

Copyright IBM Corporation 2007

Figure 5-3. DB2 products

CE0317.0

Notes:

cl

Designed to meet the information needs of small and large businesses alike, DB2 is
available on a variety of platforms, including large systems such as z/OS, VM, VSE and
Linux for zSeries; mid-sized systems such as OS/400, AIX, Linux, HP-UX, and Solaris; and
single or LAN-based systems such as Windows 2000, Windows NT, Windows XP, and
Windows .NET.

pr

Ex

Because of DB2s ability to run on so many different hardware platforms, DB2 is naturally
very scalable. DB2 is capable of supporting hardware platforms from uniprocessor laptops
to massively parallel systems with hundreds of nodes and many processors per node. In
between, it can support SMP machines or clusters of SMP machines. This provides both
extensive and granular growth.

All members of the DB2 family have similar external architecture and use many of the
same key algorithms, which enables DB2 on different platforms to integrate into a cohesive
system. Additionally, the SQL is highly standardized, thereby ensuring portability of DB2
applications across the various platforms.

5-4

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Installation Physical install

Study the documentation.


Check all the hardware and software prerequisites.
Plan DASD space requirements.
Verify the installation.

System
Admin

INSTALL
PANELS

Copyright IBM Corporation 2007

Figure 5-4. Installation - Physical install

CE0317.0

Notes:

The system programmer must ensure that all supporting software is at the proper release
level for the version or release of DB2 which you are installing.

cl

Calculations must be done to determine the amount of disk storage needed to contain the
projected data volumes.

pr

Ex

The system programmer may need to gather information that will affect their installation
(such as how many databases we'll be creating, the average number of expected tables
per database, the maximum number of concurrent users, etc.), in order to select the
appropriate settings and parameters when configuring the DB2 system environment

Copyright IBM Corp. 1997, 2011

Unit 5. What does a System Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-5

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Create system level objects

CREATE STOGROUP...

CREATE DATABASE ...

System
Admin

CREATE TABLESPACE ...

Copyright IBM Corporation 2007

Figure 5-5. Create system level objects

CE0317.0

Notes:

cl

The system programmer is normally the person responsible for defining certain
system-level objects (such as storage groups, databases, etc.) Once those objects are in
place, the database administrator (DBA) is usually the one who will create those DB2
objects that are related to the business applications (tables, indexes, views, triggers, etc.)

pr

Ex

Most DB2 objects are created via SQL, or through panels that generate the SQL under the
covers. Each CREATE statement that is successfully executed results in the insertion of a
row (or rows) into one or more DB2 tables comprising the DB2 catalog, where all of the
database objects within the DB2 system are defined.

5-6

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Using the DB2 Control Center

Copyright IBM Corporation 2007

Figure 5-6. Using the DB2 Control Center

CE0317.0

Notes:

cl

The Control Center tool could be used to define objects in the DB2 system. Using the DB2
Control Center GUI the system administrator could right-click on the type of an object of to
be defined, and then would click CREATE from the drop-down menu. Objects can be
created by typing the SQL statement in proper syntax, or created by a wizard and mouse
clicks.

Ex

The DB2 Control Center has the look and feel of the Windows file manager. Left-clicking
items in the left pane displays sub-items in the DB2 hierarchy of objects in the right pane.
Right-clicking an item opens a menu of actions that can be taken on those objects.

pr

Be aware that the Control Center is being phased out in the most recent versions of DB2,
and being replaced by Data Studio, an Eclipse-based GUI which will provide similar
functionality to the DB2 Control Center.

Copyright IBM Corp. 1997, 2011

Unit 5. What does a System Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-7

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Data Studio

Copyright IBM Corporation 2007

Figure 5-7. Data Studio

CE0317.0

Notes:

pr

Ex

cl

IBM Data Studio is an Eclipse-based tool that is intended to provide the functionality of
Control Center. It is a GUI tool for systems administrators, database administrators,
application programmers in fact, Data Studio provides features and functions that will
benefit nearly all DB2 users.

5-8

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

z/OS

Catalog

DB2 subsystem

Directory

Buffer pools
Storage groups
DB2 log

Database
Blue

Database
Green

Database
Red

JOIN

Copyright IBM Corporation 2007

Figure 5-8. z/OS

CE0317.0

Notes:

Installing DB2 on the z/OS platform involves the following major steps:

cl

1. Loading the DBMS software onto the system, using SMP/E (the RECEIVE, APPLY and
ACCEPT processes).

Ex

2. Setting all of the system configuration and application programming parameters via
interactive panels.

3. Running a series of jobs which are generated by the panel dialog in step 2, which builds
the DB2 subsystem.

pr

4. Executing a series of batch jobs, queries and transactions, to ensure the software was
properly installed and is functioning correctly. This step is know as the Installation
Verification Process, or IVP for short.
Each DB2 subsystem has its own catalog and directory, log, set of bufferpools, set of
storage groups, etc.
Very briefly, the DB2 catalog and directory contain the definitions of all DB2 objects within
the system. The DB2 log is a continuous record of all changes made to DB2 data. The
Copyright IBM Corp. 1997, 2011

Unit 5. What does a System Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-9

Student Notebook

bufferpools are allocations of virtual storage which act as a go-between external DASD and
the DB2 applications. Storage groups are sets of DASD volumes which are available for
storing DB2 data.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

In the z/OS environment, it is very easy to join tables (an SQL statement that needs to
access data from more than one table) from different databases. All that is required is for
the user or application to be authorized to access the tables in the databases, and (of
course) to code a syntactically correct SQL join statement.

5-10 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Linux, UNIX, and Windows

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Instance

Database Red

Database Blue

Catalog

Catalog

Buffer pools
DB2 log

Buffer pools
DB2 log

JOIN

(Requires federation)

Copyright IBM Corporation 2007

Figure 5-9. Linux, UNIX, and Windows

CE0317.0

Notes:

Ex

cl

To install DB2 on LUW, the system programmer must first run the install .exe in order to
establish what is known as a DB2 instance. There are certain parameters and defaults that
must be chosen in order to tailor DB2 to the customers specific needs and requirements.
This process is accomplished through a database manager configuration file, which
determines the setting for the entire instance. If a second instance of DB2 is installed, the
system programmer will again customize the instance by means of another database
manager configuration file.

pr

Within an instance of DB2, multiple databases may be configured. In DB2 for Linux, UNIX,
and Windows, each database is considered as a separate entity, and each has its own
catalog, set of bufferpools, etc. Each database undergoes further customization by means
of a database configuration file. Each data base may be tailored as needed by the
systems programmer.
In DB2 LUW, in order to accomplish joins of tables within separate databases, it is
necessary to federate the databases. This involves additional configuration of the
database parameters, setting up nicknames, etc.) in order to accomplish this federation.
Copyright IBM Corp. 1997, 2011

Unit 5. What does a System Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-11

Student Notebook

DB2 system catalog tables

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

A set of System Catalog Tables is


created and maintained to administer
the DB2 Objects.

The System Catalog Tables can be read with SQL.

Some of the System Catalog Tables include information on:

Table/Index definitions
Column data types
Defined constraints
Object dependencies
Object privileges
Statistical information about objects

Copyright IBM Corporation 2007

Figure 5-10. DB2 system catalog tables

CE0317.0

Notes:

Here are some key points to understand about the DB2 catalog:

cl

DB2 stores all information about your tables, indexes, columns, and other objects in the
DB2 catalog.

Ex

The DB2 catalog consists of normal tables and views, just like the ones in your own
databases. They can be read via SQL, just like your own tables.

pr

On some platforms, including DB2 for z/OS, catalog information is presented in tables. On
other platforms, including DB2 for Linux, UNIX and Windows and OS/400, catalog
information is also presented in views. The SQL Reference for that platform contains full
documentation of the catalog tables or views, whichever is preferred for that platform.

Catalog tables and views can be distinguished by their high-level qualifiers, that is,
authorization ID or schema names. The authorization ID used for catalog tables in DB2 for
z/OS is SYSIBM. SYSIBM.SYSTABLES is a commonly-used catalog table in DB2 for z/OS.
SYSCAT and SYSSTAT are the schema names used for catalog views in DB2 for Linux,
UNIX and Windows. SYSCAT.TABLES is a commonly-used catalog view in DB2 for Linux,
5-12 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

UNIX and Windows. SYSIBM is the schema name used for catalog views in DB2 for
iSeries.
SYSIBM.SYSTABLES/SYSCAT.TABLES contains information about every table and view.
SYSIBM.SYSCOLUMNS/SYSCAT.COLUMNS describes every column of every table and
view, and SYSIBM.SYSTABAUTH/SYSCAT.TABAUTH lists table privileges granted to
authorization IDs.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DB2 uses the catalog extensively as you work. For example, if you execute a query
against a table named EMPLOYEE, DB2 does the following:
- Looks in (SYS)TABLES to see if the EMPLOYEE table exists.

- Looks in (SYS)TABAUTH to see whether you are allowed to access the EMPLOYEE
table.
- Uses (SYS)COLUMNS to get the column names and the column sequence for the
EMPLOYEE table so that the data can be obtained for you.

If the table exists and you are authorized to use it, DB2 will present the result to you.
Otherwise, it will return an error message identifying the problem.

Since the DB2 catalog is such a critical component of the DB2 system, take good care of
the catalog tables:

1. Backup the catalog tables at least once daily, so that the catalog is readily recoverable.
2. REORG (a DB2 utility) the catalog tables on a regular basis for improved performance
3. Execute the RUNSTATS utility on the catalog tables, as needed, to ensure good
performance.

4. For z/OS, it is highly recommended to reserve the use of Bufferpool 0 for only the DB2
catalog and directory.

pr

Ex

cl

Note The DB2 Catalog is fully documented in the SQL Reference for each platform.

Copyright IBM Corp. 1997, 2011

Unit 5. What does a System Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-13

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DB2 object hierarchy

Database 1

Table1

Table2

Database 2

Table3

Table2

Index1

View1

View1

BLOBs

View2

Index1

Index2

View3

Copyright IBM Corporation 2007

Figure 5-11. DB2 object hierarchy

CE0317.0

Notes:

Within the DB2 system, there is a hierarchy of objects. Therefore be aware that:

cl

Each database may have multiple table spaces. Table spaces are used to manage
placement of data on physical storage. Table spaces contain indexes and table data.

Ex

A relational database presents data as a collection of tables. A table consists of a defined


number of columns and any number of rows.
Each table may have multiple indexes. Indexes may provide a faster way to access table
data.

pr

Each table may have multiple views. Views may be associated with more than one base
table.

5-14 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Granting privileges

GRANT DBADM ON
DATABASE RED
TO SALLY
WITH GRANT OPTION;
GRANT USE OF
TABLESPACE A
TO BOBBY

System
Admin

Copyright IBM Corporation 2007

Figure 5-12. Granting privileges

CE0317.0

Notes:

cl

Once the system-level objects have been created, the system administrator can grant
needed privileges to others (oftentimes the security personnel or the DBAs) Then the
security personnel or DBAs can GRANT privileges on objects to users, for those objects
that those users are authorized to access.

Ex

The granting of privileges or administrative authorities (logical groupings of multiple


individual privileges) can be taken away with the REVOKE SQL statement. For example,
REVOKE DBADM ON DATABASE RED FROM SALLY would undo the effects of the first
GRANT in the visual above.

pr

The WITH GRANT OPTION is a very powerful capability, but use with caution. In the first
GRANT statement above, the user SALLY has DBADM (database administration) authority
on database RED. She can also then GRANT that same authority to another user, for
example SAMMY. If SALLY includes the WITH GRANT OPTION in her GRANT statement,
then SAMMY can likewise pass the authority to another user, etc.

Copyright IBM Corp. 1997, 2011

Unit 5. What does a System Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-15

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Monitor DB2's performance

Performance Monitor
Trace Facility
Statistics

Locking Activity
etc...

System
Admin

Copyright IBM Corporation 2007

Figure 5-13. Monitor DB2's performance

CE0317.0

Notes:

Ex

cl

The system administrator is usually responsible for monitoring DB2's system-wide


performance. System administrators will typically use software tools like Omegamon for
DB2 in order to assist them in measuring overall performance and detecting performance
problems within the DB2 environment. The performance of specific DB2 applications or
databases would normally be the responsibility of the DBAs and programmers who
designed the database and coded the programs.
Here are some examples of things that the system administrator might want to know:
Do the bufferpools have sufficient storage allocated?

pr

Are the thresholds for those bufferpools set for good performance?
How efficient is the DB2 logging process?
How many threads are concurrently running?

Are we experiencing contention are there timeouts and/or deadlocks?

5-16 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Checkpoint

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

1. List a number of responsibilities for the DB2 system


administrator.
2. System-level objects are usually defined by the _________,
while application-related objects are normally the
responsibility of the ___________.
3. Users acquire privileges and authorities through the GRANT
statement. What SQL statement is used to take away those
privileges and authorities?
4. All objects defined in a DB2 system are stored in a set of
tables collectively called __________.

Copyright IBM Corporation 2007

Figure 5-14. Checkpoint

CE0317.0

Notes:

Write down your answers here:


1.

cl

2.

Ex

3.

pr

4.

Copyright IBM Corp. 1997, 2011

Unit 5. What does a System Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-17

Student Notebook

Unit summary

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Having completed this unit, you should be able to:


List some of the tasks performed by a DB2 System
Administrator:

Identifying the appropriate DB2 product


Installing the DB2 software
Creating DB2 subsystems (z/OS) and/or DB2
instances (LUW)
Creating DB2 system objects
Granting initial authorizations in DB2
Monitoring overall DB2 performance

Copyright IBM Corporation 2007

Figure 5-15. Unit summary

CE0317.0

pr

Ex

cl

Notes:

5-18 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Unit 6. What does an Administrator do in DB2?


What this unit is about

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

This unit introduces the student to the terminology and planning


requirements for a distributed data environment.

It also describes some of the other application and administration


products with which DB2 can interface.

What you should be able to do

After completing this unit, you should be able to:

List several planning considerations for distributing data


List some of the skills required to successfully distribute data
Differentiate between remote unit of work, distributed unit of work,
and distributed request
List some of the security concerns when dealing with distributed
data
List some of the other products that interface with DB2

How you will check your progress


Accountability:

pr

Ex

cl

Checkpoint questions

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-1

Student Notebook

Unit objectives

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

After completing this unit, you should be able to:


List some of the tasks performed by a DB2 Database
Administrator:

Creating, altering and dropping tables, views and indexes


Planning locking strategies
Running utility jobs
Preparing applications to run in production
Authorizing users to access DB2 data and functions
Monitoring the performance of DB2 databases and
applications, tuning them as necessary

Copyright IBM Corporation 2007

Figure 6-1. Unit objectives

CE0317.0

Notes:

After completing this unit, you should be able to list some of the tasks performed by a DB2
Database Administrator:

cl

- Creating, altering and dropping tables, views and indexes


- Planning locking strategies

Ex

- Running utility jobs

- Preparing applications to run in production

- Authorizing users to access DB2 data and functions

pr

- Monitor the performance of DB2 databases and applications, tuning them as


necessary

6-2

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

What does a Database Administrator do in DB2?

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Grant privileges:
select, insert,
update, delete,
and so on

Create objects:
tables, indexes,
views, and so on

DBA

Maintenance:
load data
gather statistics
copy data
reorganize data,
and so on

Bind:
packages,
plans

Copyright IBM Corporation 2007

Figure 6-2. What does a Database Administrator do in DB2?

CE0317.0

Notes:

pr

Ex

cl

A DBA normally works within the scope of a database or a few databases. Instead of
having responsibilities related to the overall DB2 system, the DBA performs the necessary
functions that pertain to a specific set of applications, or are related to a certain part of the
business or organization.

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-3

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

How does a DBA know what to create?

End

User

DBA

System
Admin

Programmer

Meetings!

Copyright IBM Corporation 2007

Figure 6-3. How does a DBA know what to create?

CE0317.0

Notes:

cl

DBAs meet with data owners, data users, and with people having system design skills,
database design skills, and application design skills. There may be many types of
meetings (data modeling, database design, application requirements, application design,
etc.) that the DBA should attend, in conjunction with those knowledgeable about the
business processes.

pr

Ex

During the course of these meetings, the resultant requirements documents and database
models provide the DBA with the knowledge of what tables and other DB2 objects will be
needed in order to meet the requirements.

6-4

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Review CREATE TABLE statement

CHAR(6)
CHAR(2)
CHAR(6)
SMALLINT
INT
CHAR(1)
DECIMAL(9,2)
DATE
TIMESTAMP

NOT NULL
NOT NULL
NOT NULL

,
,
,
,
,
,
,
,

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

CREATE TABLE CITIES


(CITY_ID
CITY_NAME
COUNTRY_CODE
MILES_TO_LA
POPULATION
HEMISPHERE
AVG_INCOME
LAST_CENSUS
LAST_UPDATED

NOT NULL
WITH DEFAULT

CITY_DESCRIPT
VARCHAR(300)
PRIMARY KEY (CITY_ID)
FOREIGN KEY (COUNTRY_CODE) REFERENCES COUNTRY

,
,
,
)

Copyright IBM Corporation 2007

Figure 6-4. Review - CREATE TABLE statement

CE0317.0

Notes:

cl

Tables are created via a CREATE statement. However the person creating the tables
might not need to physically type the CREATE statement and its syntax. There are various
Windows products that will build the CREATE statement for you as you fill out information
in panels via a prompting dialog.

Ex

Each column in the table created has three characteristics:


Column name

Data type designation

pr

NULL characteristic

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-5

Student Notebook

Setting specific column default values

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Assigning column default values:


.
.
COUNTRY
MILES_TO_LA

CHAR(30)
SMALLINT

DEFAULT
DEFAULT

'UNKNOWN',
-1

.
Default values may be:

constant
USER
CURRENT SQLID
NULL

Copyright IBM Corporation 2007

Figure 6-5. Setting specific column default values

CE0317.0

Notes:

Ex

cl

In DB2, we can specify a default value for any column, using the DEFAULT option. As part
of the table's definition, you can tell DB2 that, if someone INSERTS a row into this table
and doesn't provide a value for this column, you want DB2 to plug in this value. On the
visual, we are assigning the value of UNKNOWN to the column COUNTRY, and the value
-1 to the column MILES_TO_LA. Default values can be a constant of the column's data
type, the value of a DB2 special register, or null.

pr

The constant must be data type compatible, i.e. character constants must be in 'single
quotes'. DATE constants must be in 'single quotes' and in one of the acceptable date
formats. Numeric constants are not enclosed by quotes, and may include only the
numbers 0-9 and the period (decimal point).

USER and CURRENT SQLID in the visual above are two of the many special registers
available to DB2 users. These two special registers contain the default (USER) and
modifiable (CURRENT SQLID) value of the users current DB2 authorization ID.

6-6

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Adding business rules to a table definition (at CREATE TABLE time)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

At the column level:

( .
HEMISPHERE CHAR(1)
CHECK
(HEMISPHERE IN ('N','S')) ,

At the table level:

( .
PRIMARY KEY (CITY_ID)
,
CONSTRAINT POP_MILES CHECK
(POPULATION BETWEEN 1 AND 50000000
AND MILES_TO_LA >=-1 AND
MILES_TO_LA <=13500) )

Copyright IBM Corporation 2007

Figure 6-6. Adding business rules to a table definition - (at CREATE TABLE time)

CE0317.0

Notes:

Ex

cl

Business rules can be applied to specific columns or to the table itself. The word
CONSTRAINT in the bottom example tells DB2 that we are supplying a name for the
business rule. The name being supplied is POP_MILES. In the top example (the
hemisphere check), DB2 will generate the constraint name (usually the name of the
column), since we have coded the CHECK keyword, but have not provided the
CONSTRAINT keyword.

pr

When a constraint is simply associated with the values for a single column, it may be
referred to as a column constraint. If the constraint references more than one column, it
may referred to as a table constraint.

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-7

Student Notebook

Adding business rules to an existing table


(with the ALTER TABLE statement)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

ALTER TABLE CITIES

ADD CONSTRAINT POP_MILES CHECK


(POPULATION BETWEEN 1 AND 50000000
AND MILES_TO_LA >=-1 AND
MILES_TO_LA <=13500);

Copyright IBM Corporation 2007

Figure 6-7. Adding business rules to an existing table - (with the ALTER TABLE statement)

CE0317.0

Notes:

pr

Ex

cl

It may be very likely that long after a table has been defined in DB2, there are new
requirements for maintaining business rules in the data in that table. With an ALTER
statement, constraints can be added to a table after its creation, thus enforcing the new
rules. If there is already data in the table, DB2 must be sure that rows already in the table
conform to the business rules. If it does not, then the table will be left in CHECK PENDING
state.

6-8

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Tables without indexes


From the table COMPANY.DIRECTORY
First
Name
DIANA
LOIS
TERRY
SANDY
JOHN
TED
LARRY
SARA
LAVERNE
JEAN
BOBBY
LARRY
GRACE
SIMONE

Address
Phone
I need to call Larry Jones... now,
2253 DAHLIA
555-6098
where can I find his phone
6094 BEAKER
555-0945
number!
45 RIVER WALK
555-8495
214 CISCO LANE
555-4297
956 BILLINGS
555-4311
504 1ST AVE.
555-1778
567 YALE AVE.
555-6932
9023 COLORADO BLVD. 555-3242
SOUTH STAR ROUTE
555-2345
504 1ST AVE.
555-1777
1426 MAIN
555-1423
240 BOSTON BLVD.
555-5390
983 FAIRFAX
555-8764
122 42ND ST
555-5367

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Last
Name
SMITH
GAINES
BROWN
ADAMS
FRIDRICH
MITCHELL
JONES
WISE
JONES
MITCHELL
ADAMS
JONES
SMITH
CALDWELL

Copyright IBM Corporation 2007

Figure 6-8. Tables without indexes - From the table COMPANY.DIRECTORY

CE0317.0

Notes:

pr

Ex

cl

With no indexes to aid in the search for the information you need, every row in the table
must potentially be read. Keep in mind that row order is not guaranteed as rows in DB2
tables are inserted, updated and deleted. When returning a result set to the user, the
SELECT statement must specify the ORDER BY clause in order to ensure that the rows
are presented in any particular sequence.

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-9

Student Notebook

An index is:

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

A DB2 object used to


locate a row or rows
that meet a particular
condition

An ordered list of values with


one or more pointers
(called RIDs) to where
these values are found
within the rows of the table.
Copyright IBM Corporation 2007

Figure 6-9. An index is:

CE0317.0

Notes:

cl

An index is a DB2 object which consists of an ordered set of values, and pointers to the
table rows from which those index values were extracted. DB2 uses indexes for four basic
reasons:
To optimize access to data (faster response, fewer resources consumed)

Ex

To ensure uniqueness of values in a column or set of columns

To establish relationships between tables (referential constraints)

pr

To establish the sequence of the data rows, via clustering

6-10 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Locating rows in the table via RIDs

oy si

v
ec o
to fo
C rm
.F a
.T ci
.I. n
C
.

Page n
Page 5

Row 2
Row 1
Row 3
Row 4
Hdr

Page 5

Page 4

Page 3

Page 2

Page 1

RID = page, row

For example, RID = 5, 4

Copyright IBM Corporation 2007

Figure 6-10. Locating rows in the table via RIDs

CE0317.0

Notes:

cl

DB2 navigates to specific rows in a table by means of a RID (Record IDentifier). Each
index value is associated with a RID (or RIDs for non-unique indexes), which has two
components a page number, followed by a row number.

Ex

In order to illustrate how DB2 retrieves a particular row from an index, please refer to the
visual. DB2 does the following:
Read the RID value, in the format page, row (in this case the RID value is 5,4)

Retrieve page 5 of the table (which contains some header and trailer information, as
indicated in black)

pr

Locate the 2-byte pointer for row 4, which shows the displacement from the beginning
of the page to the first byte of row 4.

Use that displacement value in the pointer to read row 4.

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-11

Student Notebook

What is an index?

ADAMS,BOBBY

Multiple
RIDs
1,11

ADAMS,SANDY
BROWN,TERRY

1,4
1,3

CALDWELL,SIMONE
FRIDRICH,JOHN
GAINES,LOIS

1,14
1,5
1,2

JONES,LARRY
JONES,LAVERNE

1,7 - 1,12
1,9

MITCHELL,JEAN
MITCHELL,TED
SMITH,DIANA

1,10
1,6
1,1

SMITH,GRACE
WISE,SARA

1,13
1,8

Last
Name

First
Name

Phone

SMITH

DIANA
LOIS
TERRY
SANDY
JOHN
TED
LARRY
SARA
LAVERNE
JEAN
BOBBY
LARRY
GRACE
SIMONE

555-6098
555-0945
555-8495
555-4297
555-4311
555-1778
555-6932
555-3242
555-2345
555-1777
555-1423
555-5390
555-8764
555-5367

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

NON-UNIQUE INDEX

GAINES
BROWN
ADAMS
FRIDRICH
MITCHELL
JONES
WISE
JONES
MITCHELL
ADAMS
JONES
SMITH
CALDWELL

Copyright IBM Corporation 2007

Figure 6-11. What is an index?

CE0317.0

Notes:

pr

Ex

cl

For each unique set of values in an index (unique across the set of columns that comprise
the index), there are one or more pointers (RIDs). The above represents a non-unique
index. Conceptually, a RID is comprised of a page number and a row locator within the
page.

6-12 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Unique index
UNIQUE INDEX

ONE RID
PER UNIQUE
VALUE

First
Name

Phone

1,11

ADAMS,SANDY

1,4

SMITH

BROWN,TERRY

1,3

CALDWELL,SIMONE

1,14

FRIDRICH,JOHN

1,5

GAINES,LOIS

1,2

JONES,LARRY

1,7

JONES,LAVERNE

1,9

MITCHELL,JEAN

1,10

MITCHELL,TED

1,6

SMITH,DIANA

1,1

GAINES
BROWN
ADAMS
FRIDRICH
MITCHELL
JONES
WISE
JONES
MITCHELL
ADAMS

DIANA
LOIS
TERRY
SANDY
JOHN
TED
LARRY
SARA
LAVERNE
JEAN
BOBBY

555-6098
555-0945
555-8495
555-4297
555-4311
555-1778
555-6932
555-3242
555-2345
555-1777
555-1423

SMITH,GRACE

1,13

WISE,SARA

1,8

SMITH
CALDWELL

GRACE
SIMONE

555-8764
555-5367

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
ADAMS,BOBBY

Last
Name

Not a good choice for


a phone book!
Copyright IBM Corporation 2007

Figure 6-12. Unique index

CE0317.0

Notes:

cl

DB2 can use an index to ensure uniqueness of data values. A unique index allows only one
pointer (RID) per index value (no duplicate values). A unique index on LASTNAME,
FIRSTNAME would prevent a row from being added to the table if its LASTNAME,
FIRSTNAME values matched those of an existing row.

pr

Ex

Attempting to create or maintain a unique index on the combination of LASTNAME,


FIRSTNAME would be problematic, as there could be many individuals that have the same
first and/or lastname. However, a phone number (when including the area code) will be
unique.

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-13

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Index structure

Non-clustered Indexes

Clustered Indexes

Copyright IBM Corporation 2007

Figure 6-13. Index structure

CE0317.0

Notes:

An index in DB2 can be clustered or non-clustered. A table can have only one clustered
index.

pr

Ex

cl

A clustered index is one in which the physical sequence of the rows in the table matches
the sequence of the index entries. An index (by definition) is always ordered. Clustering
has to do with the order of the rows in the table, not the entries in the index.

6-14 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Non-clustered index
Howell, Thurston (1)
Peters, Joan (2)
Zidler, Bob (3)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Root
Page

L
e
a
f

P
a
g
e
s

Adams, Mark (2,1), (5,2)


Bennett, Barb (2,3)
Chapman, Mike (5,1)
Davis, Mac (3,1)
Howell, Thurston (3,2)

D
a
t
a

P
a
g
e
s

Jones, Bob

Adams, Mark

Davis, Mac

Jefferson, Tom

Reeves, Susan

Mitchell, David

Howell, Thurston

Smith, Steve

Peters, Joan

Bennett, Barb

Chapman, Mike Zidler, Bob

Smith, Stanley

Lee, Spike

Adams, Mark

Alviani, Carl

Lincoln, Abe

Jefferson, Tom (4,1)


Jones, Bob (1,1)
Lee, Spike (8,1)
Lincoln, Abe (8,2)
Mitchell, David (2,2)
Peters, Joan (1,3)

Quinn, Tony

Quinn, Tony (6,2)


Reeves, Susan (1,2)
Smith, Stanley (7,1)
Smith, Steve (4,2)
Thompson, Tommy (6,3)
Zidler, Bob (6,1)

Thompson, Tommy

Copyright IBM Corporation 2007

Figure 6-14. Non-clustered index

CE0317.0

Notes:

cl

The index is always in sequence, whether the index is clustered or not. For a non-clustered
index, DB2 does not care in which sequence rows are placed into the table. Lets observe
the consequences of retrieving rows from a table using a non-clustered index.

Ex

Lets retrieve all the rows for Mark Adams. For the non-clustered index, here are the I/Os
necessary to accomplish this:
One I/O to read the index root page

One I/O to read the index leaf page containing the key Adams, Mark. There are two
RIDs: 2,1 and 5,2

pr

One I/O to read the 2nd page, where row 1 is Mark Adams

One I/O to read the 5th page, where row 2 is Mark Adams

That is a total of four I/O operations in order to retrieve 2 rows, to satisfy the query:
SELECT FROM PHONEBOOK WHERE LASTNAME = Adams and FIRSTNAME =
Mark ;

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-15

Student Notebook

Clustered index

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Root
Page

Howell, Thurston (1)


Peters, Joan (2)
Zidler, Bob (3)

L
e
a
f

P
a
g
e
s

D
a
t
a

P
a
g
e
s

Adams,Mark (1,1),(1,2) Alviani,


Jefferson,
Carl (1,3)
Tom (3,2)
Bennett, Barb (2,1)
Jones, Bob (3,3)
Chapman, Mike (2,2)
Lee, Spike (4,1)
Davis, Mac (2,3)
Lincoln, Abe (4,2)
Howell, Thurston (3,1)
Mitchell, David (4,3)
Peters, Joan (5,1)

Quinn, Tony (5,2)


Reeves, Susan (5,3)
Smith, Stanley (6,1)
Smith, Steve (6,2)
Thompson, Tommy (6,3)
Zidler, Bob (7,1)

Adams, Mark

Bennett, Barb

Howell, Thurston

Lee, Spike

Adams, Mark

Chapman, Mike

Jefferson, Tom

Lincoln, Abe

Alviani, Carl

Davis, Mac

Jones, Bob

Mitchell, David

Peters, Joan

Smith, Stanley

Zidler, Bob

Quinn, Tony

Smith, Steve

Reeves, Susan

Thompson, Tommy

Copyright IBM Corporation 2007

Figure 6-15. Clustered index

CE0317.0

Notes:

cl

When data rows are clustered on a specific index, DB2 tries to maintain their sequence as
new rows are added to the table. In this visual the table rows are in order of LASTNAME,
FIRSTNAME.
Lets now observe the advantage of retrieving rows from a table using a clustered index.

Ex

Lets retrieve all the rows for Mark Adams. For the non-clustered index, here are the I/Os
necessary to accomplish this:
One I/O to read the index root page

pr

One I/O to read the index leaf page containing the key Adams, Mark. There are two
RIDs: 1,1 and 1,2
One I/O to read the 1st page, where rows 1 and 2 are both Mark Adams

That is a total of three I/O operations (not four as before) in order to retrieve 2 rows, to
satisfy the query: SELECT FROM PHONEBOOK WHERE LASTNAME = Adams
and FIRSTNAME = Mark ; This advantage would be even more pronounced when
retrieving more qualifying rows.
6-16 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Clustered and non-clustered index access


Non-clustered Index

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Clustered Index
Root Page

25 61 88

25 61 88

8 13 25

33 45 61

67 75 88

Non-leaf
Pages

8 13 25

33 45 61

67 75 88

Leaf Pages

data row
data row
data row
data row

data row
data row
data row
data row

data row
data row
data row
data row

data row
data row
data row
data row

data row
data row
data row
data row

data row
data row
data row
data row

data row
data row
data row
data row

data row
data row
data row
data row

Copyright IBM Corporation 2007

Figure 6-16. Clustered and non-clustered index access

CE0317.0

Notes:

pr

Ex

cl

Retrieval of a set of rows based on a clustering index is often faster than retrieval of a set of
rows based on a non-clustered index. There will be no advantage for a random read of one
table row. But for sequential processing, when an application or user is retrieving a range
of rows, the performance improvement using a clustered index can indeed be significant.

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-17

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Creating indexes

CREATE INDEX INX1


ON EMPL (LASTNAME,
FIRSTNAME)

DBA

Copyright IBM Corporation 2007

Figure 6-17. Creating indexes

CE0317.0

Notes:

pr

Ex

cl

When the DBA defines an index, he/she must provide a name for the index, specify the
table upon which the index is to be built, and list the column (or columns) whose values will
be used for the creation of the index keys. There are many additional index parameters
that may be specified when an index is created. These parameters are discussed in the
Database Administration Workshop courses offered by IBM Education.

6-18 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Views

A view is just a
description of a set of
columns and rows with
which we want to work

Copyright IBM Corporation 2007

Figure 6-18. Views

CE0317.0

Notes:

A view is a way to look at data that exists in one or more tables. It can be conceived of as
a virtual table, and enables user access to a subset of the data in DB2 tables.

pr

Ex

cl

However, a view does not involve copying any data. A view is not a physical object. Other
than the definition of the view in the DB2 catalog, creating a view does not consume any
additional storage space. There is no separate storage object created to contain view data.

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-19

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Creating a view

CREATE VIEW BLUEDEP AS


SELECT *
FROM EMPL
WHERE DEPT LIKE 'BLU%'

DBA

CATALOG

BLUEDEP =

'SELECT * FROM EMPL


WHERE DEPT LIKE 'BLU%'

Copyright IBM Corporation 2007

Figure 6-19. Creating a view

CE0317.0

Notes:

cl

When a view is created, its definition is stored in the DB2 catalog. When the view name is
used in an SQL statement, it is like telling DB2 to run my query against the set of data
defined by the view's SELECT statement.

Ex

DB2 lets us treat a view name as if it were a table that contains the data in the columns and
rows that the SELECT defines. The view definition may limit the columns of the table that
are accessible via the SELECT clause. The view definition may limit the rows of the table
that are accessible via the WHERE clause. A view may also limit both the columns and
rows that are accessible.

pr

In the example above, the view named BLUEDEP includes all of the columns of the real
or base table EMPL, because of the SELECT *, but the rows returned in the result set are
limited to those where the DEPT begins with the uppercase letters BLU.
A view can be based on another view. Of course, the top view in the hierarchy must be
referencing a real DB2 table.

6-20 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Using a view
EMPL
SAL
31000
32000
30000
27000
33000
31000
32000
28000
30000
33000
31000
35000
27000
29000
29000

DEPT
BLU
RED
BLU
GRE
GRE
BLU
RED
GRE
RED
GRE
RED
GRE
BLU
RED
BLU

SELECT JOB, SAL


FROM BLUEDEP
WHERE JOB <> 'M'

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

JOB
S
M
S
C
S
M
S
C
S
M
S
S
C
S
S

Columns and rows


defined by BLUEDEP
S
S
M
C
S

31000
30000
31000
27000
29000

BLU
BLU
BLU
BLU
BLU

JOB
S
S
C
S

SAL
31000
30000
27000
29000

Copyright IBM Corporation 2007

Figure 6-20. Using a view

CE0317.0

Notes:

When an SQL statement references a view, DB2 locates the SELECT that defines the view,
and then applies the SQL to the real table.

Ex

cl

In the visual above, we see three sets of data. The leftmost represents the entire contents
of table EMPL. The middle one displays the subset of the table data which is defined by
the view. The rightmost shows the final result set that is returned to the user issuing the
SELECT statement.

pr

In this example, using the view BLUEDEP tells DB2 to apply the SELECT statement to the
set of rows in table EMPL whose DEPT value begins with the letters BLU. There are only
five rows in EMPL where DEPT begins with BLU, as shown in the center of the visual.
The WHERE clause in the query filters out any rows where the job is equal to M. In the
SELECT, only columns JOB and SAL are specified, so the final result is a listing of the JOB
and SAL column values, for the four rows in the view where the JOB column value is not
equal to M.

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-21

Student Notebook

Updating through a view


EMPL

EMPL
SAL
31000
32000
30000
27000
33000
31000
32000
28000
30000
33000
31000
35000
27000
29000
29000

DEPT
BLU
RED
BLU
GRE
GRE
BLU
RED
GRE
RED
GRE
RED
GRE
BLU
RED
BLU

UPDATE BLUEDEP
UPDATE SAL =
SAL + 1000
WHERE JOB <> 'M'

JOB
S
M
S
C
S
M
S
C
S
M
S
S
C
S
S

SAL
32000
32000
31000
27000
33000
31000
32000
28000
30000
33000
31000
35000
28000
29000
30000

DEPT
BLU
RED
BLU
GRE
GRE
BLU
RED
GRE
RED
GRE
RED
GRE
BLU
RED
BLU

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

JOB
S
M
S
C
S
M
S
C
S
M
S
S
C
S
S

Copyright IBM Corporation 2007

Figure 6-21. Updating through a view

CE0317.0

Notes:

cl

In addition to reading DB2 data through a view, a user or application may also change data
by coding an INSERT, UPDATE or DELETE statement that refers to a view name rather
than the underlying table(s).

pr

Ex

In this example, DB2 will update the SAL of only those rows in the EMPL table whose
DEPT value begins with the letters BLU, and where the JOB value is not the letter M. The
four rows affected are the same rows that were SELECTed on the previous slide, since the
UPDATE statements is accessing the BLUEDEP view and has the same WHERE clause.

6-22 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

The DBA cleans up

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

3-word
cleanup!

OBJECT_TYPE

DROP

OBJECT_NAME

DROP TABLE EMPL


Copyright IBM Corporation 2007

Figure 6-22. The DBA cleans up

CE0317.0

Notes:

cl

It is very easy to remove a DB2 object (if you have the privilege to do so). The DROP
statement above is issued in order to delete the definition of the table EMPL from the DB2
catalog. But is this all that happens?

Ex

Earlier in this course we mentioned that there is a hierarchy of objects in DB2. Indexes,
views and other objects can be based on (and therefore dependent upon) DB2 tables.
Therefore, when you DROP an object, there may be other implications to consider.

For example, here is what happens when the DROP TABLE EMPL statement is executed:
1. The target object (EMPL table) is deleted.

pr

2. Any objects dependent on EMPL are deleted.

3. The security/authorization scheme for both target and dependent objects is deleted.
4. All data in both the target and dependent objects is deleted.
5. Any DB2 programs (Plans, Packages) that reference either the target or dependent
objects are invalidated.
Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-23

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Therefore, you want to be cautious with the use of the DROP statement. There is a
preventative measure that the DBA may choose in order to minimize the danger of
inadvertently deleting DB2 objects in the catalog. When you CREATE a table, after the
column specifications, the DBA can include the WITH RESTRICT ON DROP option. When
this option is in effect, DB2 disallows the DROPping of the table, and even disallows the
DROPping of the tablespace or database containing the protected table. This restriction
option can also be added to an already existing table, by issuing an ALTER TABLE
statement, with an ADD RESTRICT ON DROP clause.

pr

Ex

cl

In order to delete such a table, the DBA would first need to issue the statement ALTER
TABLE EMPL DROP RESTRICT ON DROP. If that statement executed successfully, then
the DBA could issue a DROP TABLE EMPL, and DB2 would delete the table definition and
all dependent objects, as described above.

6-24 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

DROP dependencies

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

TABLE
EMPL

INDEX
NDX1

VIEW
BLUEDEP

INDEX
NDX2

INDEX
NDX3

Copyright IBM Corporation 2007

Figure 6-23. DROP dependencies

CE0317.0

Notes:

pr

Ex

cl

As mentioned earlier, dropping the table causes DB2 either to drop or make inactive all
objects dependent on the table (for example, indexes or views). If you are certain that you
do indeed wish to delete the table EMPL and all of its dependents, it is advantageous that
DB2 automatically takes care of the cleanup. Attempts to cleanup using manual methods
would be much more error-prone and incomplete.

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-25

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Locking Concurrency needs

Hmm, what kind of locking


is best for these tables?

DBA

Copyright IBM Corporation 2007

Figure 6-24. Locking - Concurrency needs

CE0317.0

Notes:

Ex

cl

The most important function of a RDBMS is to maintain data integrity. But it is also very
important to allow multiple users and applications to access the same data at the same
time (This is known as concurrency). Therefore, DB2 must employ methods or techniques
to serialize access to that data. There are a number of serialization techniques used by
DB2 (locking, CLAIMS and DRAINS, etc.). In this next set of visuals, we will illustrate some
of the basic concepts of locking.

pr

An important part of application design is to formulate a locking strategy. It often depends


on the type of application, and is aimed at satisfying the needs for both data integrity and
concurrency. The DBA can then tell DB2 what level of locking to maintain on a resource.

The other consideration is cost. The higher the level of locking, the more system resources
which are needed to support the locking. Each unit of data (whether a row, page, or table)
that is being locked consumes memory. Each lock request (such as acquire a lock, check
a lock status, upgrade a lock, release a lock, etc.) requires CPU cycles.

6-26 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Concurrency Table level (1 of 6)

Read
Only
App

Read
Only
App

Read
Only
App

__________
__________
__________
__________
__________

__________
__________
__________
__________
__________

Applicatio
n

Read
Only
App

Read
Only
App

__________
__________
__________
__________
__________

__________
__________
__________
__________
__________

Application

Read
Only
App

Read
Only
App

Read
Only
App

Applications
Queue Here
For Data Access

Lock
Manager

Copyright IBM Corporation 2007

Figure 6-25. Concurrency - Table level (1 of 6)

CE0317.0

Notes:

For the following visuals, some explanation of what is being illustrated is needed:

cl

1. The computer display looking at a book represents an application that wants to READ
DB2 data.

Ex

2. The computer display with glasses and a handbag represents an application that needs
to CHANGE DB2 data.
3. The DB2 table that the applications are accessing consists of four pages, with 5 rows
on each page.

pr

4. The lock manager uses the letters R and C to indicate whether an application is simply
Reading DB2 data, or Changing DB2 data.
5. The wooden structure indicates that initially, DB2 is locking at the table level. In
subsequent visuals, the size or scope of the lock is reduced to the page or row level.

If the DBA tells DB2 to maintain locking at the table level, conceptually DB2's lock enforcer
will look at the first application in the queue to see what it wants to do to the data in the

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-27

Student Notebook

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

table. In the above example, the first application wants to READ the data. The lock
enforcer sets up a sign indicating that the gate to the table is open for reading purposes.

6-28 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Concurrency Table level (2 of 6)

Read
Only
App

Read
Only
App

Read
Only
App

__________
__________
__________
__________
__________

__________
__________
__________
__________
__________

__________
__________
__________
__________
__________

__________
__________
__________
__________
__________

Read
Only
App

Read
Only
App

Applicatio
n

Read
Only
App

Read
Only
App

Application

Applications
Queue Here For
Data Access

Read
Only
App

App. 1
App. 2
App. 3

Lock
Manager

Copyright IBM Corporation 2007

Figure 6-26. Concurrency - Table level (2 of 6)

CE0317.0

Notes:

For each consecutive application in the queue that wants to read, DB2 will let it into the
table. DB2 keeps track of which applications it has let into the table.

pr

Ex

cl

In the above example, the first three applications in the queue wanted to read only, so DB2
allows them access to the table. The fourth application wanted to make changes to the
table, so it and the applications behind it in the queue must wait until all the readers
finish their work and leave the table.

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-29

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Concurrency Table level (3 of 6)

Read
Only
App

__________
__________
__________
__________
__________

__________
__________
__________
__________
__________

Read
Only
App

Read
Only
App

Read
Only
App

Applicatio
n

__________
__________
__________
__________
__________

__________
__________
__________
__________
__________

Read
Only
App

Read
Only
App

Read
Only
App

App. 1
App. 2
App. 3

APPS
WORKING

Application

Applications
Queue Here For
Data Access

Lock
Manager

Copyright IBM Corporation 2007

Figure 6-27. Concurrency - Table level (3 of 6)

CE0317.0

Notes:

pr

Ex

cl

As each of the reading applications finishes its work and leaves the table, the Lock
Manager crosses it off the blackboard.

6-30 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Concurrency Table level (4 of 6)

Read
Only
App

__________
__________
__________
__________
__________

Read
Only
App

Read
Only
App

__________
__________
__________
__________
__________

Applicatio
n

__________
__________
__________
__________
__________

Read
Only
App

Read
Only
App

Application

Applications
Queue Here For
Data Access

__________
__________
__________
__________
__________

App. 1
App. 2
App. 3

APPS
WORKING

Lock
Manager

Copyright IBM Corporation 2007

Figure 6-28. Concurrency - Table level (4 of 6)

CE0317.0

Notes:

pr

Ex

cl

When the last read application leaves, the lock enforcer will change the access sign to the
table. Applications that change data require EXCLUSIVE access to that data. Since the
DBA told DB2 that locking was to be maintained at the table level for this table, only one
lock enforcer was hired. The lock enforcer will let in only one change application at a time.

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-31

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Concurrency Table level (5 of 6)

__________
__________
__________
__________
__________

Read
Only
App

Read
Only
App

Application

__________
__________
__________
__________
__________

Read
Only
App

__________
__________
__________
__________
__________

Application

Read
Only
App

Read
Only
App

Applications
Queue Here For
Data Access

APPS
WORKING

__________
__________
__________
__________
__________

App. 1
App. 2
App. 3
App. 4

Lock
Manager

Copyright IBM Corporation 2007

Figure 6-29. Concurrency - Table level (5 of 6)

CE0317.0

Notes:

pr

Ex

cl

Application 4 is making its changes while all other applications remain in the queue. The
lock enforcer indicates this with a C on his sign. Only one change application is allowed
into the table at a time.

6-32 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Concurrency Table level (6 of 6)

Read
Only
App

__________
__________
__________
__________
__________

__________
__________
__________
__________
__________

__________
__________
__________
__________
__________

__________
__________
__________
__________
__________

Read
Only
App

Read
Only
App

Read
Only
App

Read
Only
App

Application

Applications
Queue Here For
Data Access

APPS
WORKING

App. 1 App. 6
App. 2
App. 3
App. 4
App. 5

Lock
Manager

Copyright IBM Corporation 2007

Figure 6-30. Concurrency - Table level (6 of 6)

CE0317.0

Notes:

cl

Once the change application has completed its work, the lock enforcer checks what the
next application in the queue wants to do. Since it wants to read, as does the next
application in the queue, both applications are let into the table, and the sign reverts to R.

pr

Ex

Application 7 wants to make changes to the data (requiring EXCLUSIVE access), so it and
subsequent applications remain in the queue.

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-33

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Concurrency Page level z/OS

__________
__________
__________
__________
__________

Change
Mode

Read
Only
__________
App
__________
__________
__________
__________

Application

Read
Only
App

App. 4

Read
Mode

This
application
wants
exclusive use
of the table

Lock
Manager

App.
1
App.
2

Application

Lock
Manager

Read
Only
App

Read
Only
__________
App
__________

Read
__________
Only
__________
App
__________
__________
__________

Read
Only
App

__________
__________
__________

Read
Only
App

Read
Mode

Read
Only
App

Read
Mode

App. 5
App. 6

App. 3

Lock
Manager

Lock
Manager

Applications
Queue Here For
Data Access

Application

R/C

App. 1 - R
App. 2 - R
App. 3 - R
App. 4 - C
App. 5 - R
App. 6 - R

App. 7 - C
App. 8 - R
App. 9 - R
App. 10 - R

Lock
Manager

Copyright IBM Corporation 2007

Figure 6-31. Concurrency - Page level - z/OS

CE0317.0

Notes:

cl

Suppose that, instead of choosing TABLE locking, the DBA chose page locking. Page
locking is like hiring a lock enforcer for the table, along with a lock enforcer for each page.
Again, all applications queue up for access. The lock enforcer for the table sets the sign for
both reading and change activity to occur concurrently within the table, so long as all
applications are willing to work together.

pr

Ex

In this example, the first 10 applications in the queue are willing to work concurrently. The
11th application is demanding EXCLUSIVE access to the table. The table lock enforcer has
documented which applications are working within the table. Inside the table, each page
has its own lock enforcer that is ensuring concurrency at the page level.

6-34 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Concurrency Row level

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

App. 4

Application

App. 4

App. 4

Read
Only
App

Read
Only
App

App. 4

Read
Only
App

Read
Only
App

App. 4

Application

App. 4

Read
Only
App

This
application
wants
exclusive use
of the table

App. 4

Read
Only
App

App. 4

Read
Only
App

App. 4

Read
Only
App

Application

Applications
Queue Here For
Data Access

App. 1 - R
App. 2 - R
App. 3 - R
App. 4 - C
App. 5 - R
App. 6 - R

R/C

App. 7 - C
App. 8 - R
App. 9 - R
App. 10 - R

Lock
Manager

Copyright IBM Corporation 2007

Figure 6-32. Concurrency - Row level

CE0317.0

Notes:

cl

This example assumes that the DBA chose row level locking for this table. When row level
locking is in place, it is like hiring a lock enforcer for the table and one lock enforcer for
each row.

pr

Ex

The example shows that there is one application waiting outside the table because it
demands EXCLUSIVE access to the table; however, within the table, all applications are
working because, for this example, there is no conflict at the row level. Conflict at the row
level would occur if a change application and any other application wanted access to the
same row at the same time. When this occurs, the lock enforcer gives access to the first
application queued for the row.

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-35

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Specifying LOCKSIZE (Concurrency level)

CREATE TABLESPACE TS1


IN DATABASE RED
LOCKSIZE PAGE
...

DBA

Copyright IBM Corporation 2007

Figure 6-33. Specifying LOCKSIZE (Concurrency level)

CE0317.0

Notes:

Defining LOCKSIZE is a straight-forward task, done as a parameter on a CREATE


TABLESPACE statement or an ALTER TABLESPACE statement on z/OS.

cl

There are some differences in the type of locking supported on the various DB2 platforms.

Ex

DB2 for z/OS supports LOCKSIZE ROW, TABLE, or TABLESPACE. The default setting
is PAGE.
DB2 for Linux, UNIX, and Windows supports ROW or TABLE level locking. You define
LOCKSIZE via ALTER TABLE statement. The default setting is ROW.

pr

The DBA should monitor DB2 applications for timeouts and deadlocks (these are two
events that indicate that there is some contention for the shared data). Some minor
amount of contention is normal, but at some point contention may become a major
performance issue. In that case, the DBA and programmer will need to re-evaluate the
locking strategy.

6-36 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Isolation Level

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

How long will a READ row lock be held?

Controlled by the Isolation Level BIND parameter

RR - Repeatable Read - lock rows read until commit

RS - Read Stability - lock qualifying rows until commit

CS - Cursor Stability - hold lock on row until next read

UR - Uncommitted Read - no READ lock is held

Copyright IBM Corporation 2007

Figure 6-34. Isolation Level

CE0317.0

Notes:

pr

Ex

cl

When a DB2 application program is prepared for execution via the BIND process, one of
the BIND parameters that the DBA can set is known as the isolation level. This controls the
duration of read locks during the execution of the program. The BIND option for isolation
sets the default for the program, but an individual SQL statement can override this default
by using the WITH clause (i.e. with RR, with CS, etc.)

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-37

Student Notebook

Duration of Read Locks


1 row

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

No row locks

acquired for
R/O cursors

COMMIT point

RR or RS

UR

CS

Copyright IBM Corporation 2007

Figure 6-35. Duration of Read Locks

CE0317.0

Notes:

Ex

cl

The visual above shows the various isolation levels, from most restrictive on the left, to
least restrictive on the right. More restrictive reduces both the concurrency and data
integrity exposure. The least restrictive increases both the concurrency to the data and the
data integrity exposure. The programmer and DBA need to understand the business
requirements of the applications and database structures in order to select the appropriate
isolation level.

pr

As shown above, with both RR and RS, the row locks are held until the next COMMIT. The
difference between them is that with RR, all of the rows read are locked, whereas with RS,
only the rows that qualify (meeting the WHERE clause criteria) are locked. With CS, the
lock on the row is held only when it is FETCHed. When the cursor fetches the next row, the
previous rows lock is released. With UR (sometimes called dirty read), no read locks are
held.

Although UR enables the highest level of concurrency, you must ensure that the application
can tolerate the reading of uncommitted DB2 data.
Note
6-38 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Starting with DB2 LUW 9.7, the option to use 'currently committed' with CS locking allows
read-only requests to bypass row locking and avoid lock contention with write requests.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Uempty

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-39

Student Notebook

Utilities DB2 tools

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Setup
or
Move

Maintenance

Recovery

Copyright IBM Corporation 2007

Figure 6-36. Utilities - DB2 tools

CE0317.0

Notes:

cl

Most DB2 utilities work at the table space (data set or file) level. So if there is more than
one table in a table space, a single execution of a utility will operate against all of those
tables.

pr

Ex

For the purposes of our discussion the utilities are grouped into the above three rather
broad categories.

6-40 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Setup and Move Utilities

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

z/OS

Unload

Populate

DSNTIAUL

LOAD

(SAMPLE)

DSN1COPY

Linux, UNIX, Windows

Populate

IMPORT

LOAD

UNLOAD

EXPORT

Copyright IBM Corporation 2007

Figure 6-37. Setup and Move Utilities

CE0317.0

Notes:

Unload programs download the data that is in a DB2 table into a sequential file.

EXPORT (LUW) and DSNTIAUL (z/OS) are based on the SQL SELECT statement.

cl

UNLOAD and DSN1COPY (z/OS) are based more on the underlying DB2 objects and
VSAM data sets.

Ex

Both IMPORT and LOAD upload data from a sequential file of various formats into a DB2
table. They can insert or replace data in tables. The LOAD programs in particular have high
performance and can load millions of rows very quickly.

pr

Note

With DB2 LUW the load utility can use a defined Cursor for input data, which would retrieve
local data or remote data using a nickname.

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-41

Student Notebook

Recovery-related Utilities

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

z/OS
COPY

MERGECOPY

QUIESCE

REPORT

RECOVER

REPAIR

CHECK

COPYTOCOPY

Linux, UNIX, Windows

BACKUP

QUIESCE

RESTORE

ROLLFORWARD

Copyright IBM Corporation 2007

Figure 6-38. Recovery-related Utilities

CE0317.0

Notes:

cl

Recovery in principle consists of taking a frequent, periodic COPY (z/OS) or BACKUP


(LUW) of your data (for example, at a database or a table space level). When the data
needs to be recovered, RECOVER (z/OS) or RESTORE, optionally followed by
ROLLFORWARD (LUW) can recover your data to a point in time or to the end of the
current logs.

Ex

Note

pr

There is a RECOVER DATABASE command in DB2 LUW that combines Restore and
Rollforward processing for simple database level recovery

6-42 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Forward recovery Good data


Time

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Linear Log

Changes
to data

Data

Data not
accessible

Log

Recovered
data

Copy

Copyright IBM Corporation 2007

Figure 6-39. Forward recovery - Good data

CE0317.0

Notes:

pr

Ex

cl

In the first scenario (a device is damaged, but data is good), we want to move all of the
data, as it is at the current time, to a different device. The approach to this is to restore the
data, from the latest copy taken, to another device, and then to apply any committed
changes that were made after the most recent copy. These changes are on the DB2 log,
and the process of applying the changes that are on the log is called forward recovery. This
is sometimes called full recovery.

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-43

Student Notebook

Speeding up recovery

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Time

Data

Copy
data

Copy
data

Copy
data

Copy

Copy

Copy

Incr
copy

Incr
copy

Incr
copy

Data not
accessible

Copy

Full
copy

Merge
copy

New
Image
Copy

Log

Recovered
data

Recover

Report

Copyright IBM Corporation 2007

Figure 6-40. Speeding up recovery

CE0317.0

Notes:

In DB2 for z/OS, you can take either full or incremental copies of a table space. The
MERGECOPY utility allows you to merge either:

cl

Full copies with incremental copies or

Just incremental copies in advance to reduce recovery time

pr

Ex

In DB2 for Linux, UNIX, and Windows, full, incremental and delta backups are also
available at a database or tablespace level.

6-44 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Point-In-Time recovery
Time

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Lost
processing

Changes
to data

Data

Logically
damaged

Recovery
point

Log

Recovered
data

Copy

Copyright IBM Corporation 2007

Figure 6-41. Point-In-Time recovery

CE0317.0

Notes:

cl

Point-in-time recovery is used when data has been logically damaged (as opposed to the
storage medium being physically damaged), normally by a DB2 application program with
flawed logic. The goal for logically-damaged data is to recover the data to a point-in-time
just prior to where the processing that caused the logical damage occurred.

pr

Ex

Once the data has been restored, there is another issue to deal with . Even though the
flawed program damaged the DB2 data, the business transactions which invoked the
program needed to be processed. Now that the recovery has been done, all of those
transactions have been lost, and must somehow be processed again, as soon as the
program is corrected.

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-45

Student Notebook

Version recovery Linux, UNIX, and Windows

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Time

Backup

Backup

Backup

Backup

Circular Log

Backup

Log

Backup

Backup

Log

Log

Backup

Log

Copyright IBM Corporation 2007

Figure 6-42. Version recovery - Linux, UNIX, and Windows

CE0317.0

Notes:

cl

Version recovery is a DB2 for Linux, UNIX, and Windows option. With version recovery,
implemented recovery is limited to the point-in-time of the last copy of the database.
Logging is used for application rollback processing and crash recovery.
Note

pr

Ex

For DB2 LUW, version recovery could be performed on a database using archive logging
as long as the backup was produced offline. There is an option to rollforward to the END
OF BACKUP which could use a set of logs imbedded in the backup to recover a copy of a
database.

6-46 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Maintenance Utilities

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

z/OS
Reorganize
the data

Gather statistics

REORG

RUNSTATS

Linux, UNIX, Windows

Do we need to?

Reorganize
the data

Gather statistics

REORGCHK

REORG

RUNSTATS

Copyright IBM Corporation 2007

Figure 6-43. Maintenance Utilities

CE0317.0

Notes:

cl

Here is a brief description of those DB2 utilities which enable the DBA to keep the data in
good shape. These maintenance utilities keep the data well-organized and statistically
accurate, so that optimum performance is achieved, and DASD is most efficiently utilized.

Ex

REORG frees up space in tables or indexes, eliminates fragmentation, and may also
sequence the data rows based on a clustering index.

pr

REORGCHK (LUW only) is a report that can be run advising whether a REORG is
advisable, based on certain calculations. For the z/OS environment, there is an option on
the REORG utility that can inform the DBA as to whether a REORG is recommended,
based upon the criteria that the DBA passes to the REORG job. This is known as
conditional REORG.

RUNSTATS updates DB2 catalog statistics with current information on tables and indexes
that normal database activity does not update automatically. Afterwards the DB2 optimizer
can take advantage of more up-to-date information (although static SQL may need to be
rebound to do this).

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-47

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

REORG Clustering the data on department

________________
________________
_____|___________
_|_______________
_________|_______
_|____________|__
________________|
___________|_____
________________
_______|_________
___|_____________
_________|_______
__________|______
___|_____________
____________|
____|
Free space
Copyright IBM Corporation 2007

Figure 6-44. REORG - Clustering the data on department

CE0317.0

Notes:

pr

Ex

cl

After the execution of the REORG utility, all rows have been put into their clustering
sequence, and free space has been reestablished in order to accommodate additional
rows being added to the table. The frequency with which a DBA needs to run the REORG
is primarily dependent upon the volatility (i.e. how often does it change?) of the table data.

6-48 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

RUNSTATS

Table
space

# pages
# rows
# unique values
% free
# overflow rows
cluster ratio

Catalog

Copyright IBM Corporation 2007

Figure 6-45. RUNSTATS

CE0317.0

Notes:

pr

Ex

cl

The RUNSTATS utility examines the actual table data and indexes, gathers statistics and
makes calculations, and then writes that information into the DB2 catalog tables. The
statistical information in the catalog provides significant input to the DB2 optimizer, so that
an efficient access path is chosen to access the data.

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-49

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

To get to DB2

Tables - R - Us

DB2

Integrity
&
Recovery

RA
CF
Op or
e
Sy ratin
ste g
m

Continuous
Operation

Logging

Optimizer

Password
Required

Lock
Manager

Security

Copyright IBM Corporation 2007

Figure 6-46. To get to DB2

CE0317.0

Notes:

pr

Ex

cl

It is necessary to get through the operating systems security manager before we can get to
DB2. This authentication processes normally requires a valid ID and current password.
This is often referred to as external security. Once a person has been authorized to use
the system, there is another layer of security, pertaining specifically to accessing DB2
objects and functions. This is referred to as DB2 internal security, and that is the focus of
the next set of visuals.

6-50 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

DB2 security
Peter

No way,
Peter!

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

UPDATE EMPL
SET SALARY =
47000
WHERE ID =
'000030'

DB2

Tables - R - Us

Integrity
&
Recovery

CATALOG

Continuous
Operation

Sally is a DBA for DBASE17


Loraine is a SYSADM
Peter can SELECT from EMPL
Peter can UPDATE the LASTNAME
column in EMPL
Jan owns EMPL

Logging

Optimizer

Lock
Manager

Security

Copyright IBM Corporation 2007

Figure 6-47. DB2 security

CE0317.0

Notes:

Before a user can access a DB2 object, DB2 goes to the catalog and checks to see if the
user has the authority or privilege to do what they are trying to do.

pr

Ex

cl

Lets look at the example above. DB2 has several catalog tables that are involved in
maintaining DB2 security. DB2 objects and users are registered in the catalog, along with
the actions each user may take against an object. In this example, Peter is trying to update
a SALARY in the EMPL table. DB2's security manager goes to the catalog and looks up
Peter's authorizations against the EMPL table. Peter only has the privilege to update the
LASTNAME column in the EMPL table, thus his request is rejected.

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-51

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Everything is protected!

Plans

Utilities

Packages

Tables

Buffer
Pools

Commands

Copyright IBM Corporation 2007

Figure 6-48. Everything is protected!

CE0317.0

Notes:

DB2 internal security determines whether or not a particular user or application can:
Define, modify or delete objects in the DB2 catalog

cl

Execute DB2 programs (plans and packages)

Ex

Run DB2 utilities

Access tables with SQL

Access certain columns in a table with SQL

pr

What kind of SQL can be executed on a DB2 table


Use specific buffer pools
Issue DB2 commands

Objects on DB2 for z/OS can be more granularly controlled than in DB2 for Linux, UNIX,
and Windows.
Note Plans are DB2 for z/OS objects.
6-52 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Privileges
IMPLICIT - Owner's privileges

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

EXPLICIT - Specifically GRANTed privileges

INDIRECT - Group, or execute acquired privileges

Copyright IBM Corporation 2007

Figure 6-49. Privileges

CE0317.0

Notes:

pr

Ex

cl

These are the three ways to acquire DB2 privileges. These will be explained in more detail
on the following visuals.

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-53

Student Notebook

Owner's (Implicit) table privileges

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

All DML

TABLE
ALTER
DROP

SELECT
INSERT
UPDATE
DELETE

INDEXES
CREATE
DROP

CONSTRAINTS

UTILITIES

Copyright IBM Corporation 2007

Figure 6-50. Owner's (Implicit) table privileges

CE0317.0

Notes:

cl

The individual who CREATEs an object is referred to as the owner, and the authorization
ID used at the time of the CREATE becomes the qualifier for the table name. The owner is
automatically granted privileges such as:
ALTERing that table, when changes are needed

Ex

DROPping the table, when it is no longer being used


Issuing DML statements on the table

Adding additional objects related to the table (indexes, constraints, etc.)

pr

Running DB2 utility programs on the table data

DML stands for Data Manipulation Language, and is the subset of SQL statements which
contains SELECT, INSERT, UPDATE, and DELETE.

6-54 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Explicit table privileges

GRANT SELECT, UPDATE


(LASTNAME,
FIRSTNAME)
ON TABLE EMPL
TO PETER, SUSAN

DBA

CATALOG

Sally is a DBA for DBASE17


Loraine is a SYSADM
Peter can SELECT from the EMPL table
Susan can SELECT from the EMPL table
Peter can UPDATE the LASTNAME, FIRSTNAME in EMPL
Susan can UPDATE the LASTNAME, FIRSTNAME in EMPL
Copyright IBM Corporation 2007

Figure 6-51. Explicit table privileges

CE0317.0

Notes:

Explicit table privileges are given via a GRANT SQL statement. Privileges can be taken
away with the REVOKE SQL statement.

pr

Ex

cl

In the example above, the DBA is giving Peter and Susan the privilege to read all of the
data in the EMPL table. However, they can only update the LASTNAME and FIRSTNAME
columns within the table. When the GRANT statement is executed, DB2 writes those
privileges into the catalog.

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-55

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Revoke

REVOKE SELECT, UPDATE


(LASTNAME, FIRSTNAME)
ON TABLE EMPL
FROM PETER

DBA

CATALOG

Sally is a DBA for DBASE17


Loraine is a SYSADM
Peter can SELECT from the EMPL table
Susan can SELECT from the EMPL table
Peter can UPDATE the LASTNAME, FIRSTNAME in EMPL
Susan can UPDATE the LASTNAME, FIRSTNAME in EMPL
Copyright IBM Corporation 2007

Figure 6-52. Revoke

CE0317.0

Notes:

REVOKE removes privilege(s) from the catalog which had been previously given
(GRANTed) to a DB2 user or group of users.

pr

Ex

cl

In the example above, the REVOKE statement removes both the SELECT and UPDATE
privileges that user PETER had on the EMPL table. Susans privileges on EMPL remain in
tact.

6-56 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Indirect privileges Static SQL


DBRM or Bindfile

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Bind Package

Program A
Statement 27
199710271345050558
SQL - read a row
.
.
.
.

DB2

Tables - R - Us

CATALOG

Integrity
&
Recovery

Continuous
Operation

Logging

Optimizer

Lock
Manager

Security

Catalog

If Sally Binds..

Sally is a DBA for DB1


Sally owns TEST.A
Loraine is a SYSADM
Rita can run TEST.A

Security Check

If Sally GRANTS...
GRANT EXECUTE ON PACKAGE TEST.A TO RITA
Copyright IBM Corporation 2007

Figure 6-53. Indirect privileges - Static SQL

CE0317.0

Notes:

pr

Ex

cl

Rita gets to execute package TEST.A which contains static SQL, because Sally granted
her the execute authority on the package. Sally did not have to GRANT Rita the privilege to
execute the SQL within the package. However, it is only from package TEST.A that Rita
can execute those SQL statements. If she attempts to submit the same SQL statements to
DB2 through another means (CLP in LUW, SPUFI in z/OS, etc.) those statements will fail,
returning an error to Rita explaining that she is not authorized to execute the statements.

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-57

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Indirect privileges Group

Alice

Bob

Public

MGRGROUP
Bob
Carol
Ted
Alice

TED

Carol

GRANT SELECT
ON TABLE EMPL
TO MGRGROUP
Copyright IBM Corporation 2007

Figure 6-54. Indirect privileges - Group

CE0317.0

Notes:

cl

Everyone with access to DB2 is part of the PUBLIC group. To GRANT a privilege to
everyone, GRANT it to PUBLIC. A privilege can be GRANTed to a group level ID
(MGRGROUP) which will make that privilege available to each member of the group,
without having to grant the privilege to specific users.

Ex

Note

pr

Starting with DB2 LUQ 9.5 database ROLES can be defined to support group type
authorization that is defined in the database rather than outside of the database like OS
Groups.

6-58 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Administrative authorities

SYSADM

SYSCTRL
DBADM DB1
DBADM DB2

Data
access

Data
access

Copyright IBM Corporation 2007

Figure 6-55. Administrative authorities

CE0317.0

Notes:

An administrative authority consists of a set of privileges, logically grouped together for


various roles in DB2 (such as system administrator, DBA, operator, etc.).

cl

Note

pr

Ex

Starting with DB2 LUW 9.7 and DB2 for z/OS 10, the new SECADM authority can be the
primary grantor of access and authority to DB2 databases in the system. The DBADM user
can be granted with or without authority to access data and with or without the ability to
perform grants.

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-59

Student Notebook

Views and Security


EMPL
SAL
31000
32000
30000
27000
33000
31000
32000
28000
30000
33000
31000
35000
27000
29000
29000

DEPT
BLU
RED
BLU
GRE
GRE
BLU
RED
GRE
RED
GRE
RED
GRE
BLU
RED
BLU

CREATE VIEW BLUEDEP AS


SELECT *
FROM EMPL
WHERE DEP = 'BLU'

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

JOB
S
M
S
C
S
M
S
C
S
M
S
S
C
S
S

GRANT SELECT,
UPDATE (SALARY)
ON BLUEDEP
TO CLERK

Copyright IBM Corporation 2007

Figure 6-56. Views and Security

CE0317.0

Notes:

Views play an important part of the DB2 security scheme for most customers. Views allow
a more granular control of privileges in DB2.

pr

Ex

cl

In the example above, our view BLUEDEP limits the rows that can be accessed. When
combined with the example GRANT, we have limited the column(s) (SALARY) and rows in
which SALARY can be changed (those rows whose DEPT value begins with the characters
BLU).

6-60 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Checkpoint
Who is responsible for creating DB2 databases?
What is a DB2 index?
List a couple of reasons why indexes are used in DB2.
What is a view?
To recover data that is no longer accessible, the DBA must
start with what?
Which DB2 utility gathers information about our tables and
indexes?
What must be true if USER1 can successfully update the
ADDRESS column in table USER7.TAB_1?
List some of the things a table's owner can do to/with their
table.
Name a couple of ways in which you can get privileges
indirectly.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

1.
2.
3.
4.
5.
6.
7.
8.
9.

Copyright IBM Corporation 2007

Figure 6-57. Checkpoint

CE0317.0

Notes:

Write down your answers here:


1.

cl

2.

Ex

3.
4.
5.

pr

6.
7.
8.
9.

Copyright IBM Corp. 1997, 2011

Unit 6. What does an Administrator do in DB2?

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-61

Student Notebook

Unit summary

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Having completed this unit, you should be able to:


List some of the tasks performed by a DB2 Database
Administrator:

Creating, altering and dropping tables, views and indexes


Planning locking strategies
Running utility jobs
Preparing applications to run in production
Authorizing users to access DB2 data and functions
Monitoring the performance of DB2 databases and
applications, tuning them as necessary

Copyright IBM Corporation 2007

Figure 6-58. Unit summary

CE0317.0

pr

Ex

cl

Notes:

6-62 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Unit 7. Information Management with DB2

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

What you should be able to do


After completing this unit, you should be able to:

List several planning considerations for distributing data

List some of the skills required to successfully distribute data


Differentiate between remote request, remote unit of work,
distributed unit of work, and distributed request

List some of the security concerns when dealing with distributed


data

List some of the other products that interface with DB2

How you will check your progress


Accountability:

pr

Ex

cl

Checkpoin questions.

Copyright IBM Corp. 1997, 2011

Unit 7. Information Management with DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

7-1

Student Notebook

Unit objectives

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

After completing this unit, you should be able to:


List several planning considerations for distributing data
List some of the skills required to successfully distribute
data
Differentiate between remote request, remote unit of
work, distributed unit of work, and distributed request
List some of the security concerns when dealing with
distributed data

List some of the other products that interface with DB2

Copyright IBM Corporation 2007

Figure 7-1. Unit objectives

CE0317.0

pr

Ex

cl

Notes:

7-2

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Planning for distributed data


Remote Server

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Local Server
DB2

Tables - R - Us

Tables - R - Us

DB2

Integrity
&
Recovery

Integrity
&
Recovery

Continuous
Operation

Continuous
Operation

Application

Logging

Logging

Optimizer

Optimizer

Lock
Manager

A DRDB (distributed
relational database)
can be a server,
a requester, or both!

Lock
Manager

Security

Security

Remote
Data

Local
Data

Copyright IBM Corporation 2007

Figure 7-2. Planning for distributed data

CE0317.0

Notes:

cl

An application can access data at the local server. When the application needs data stored
in a remote database manager, the local database manager becomes a requester (client)
to the remote database manager. To the user of the data, it appears that the data is at the
local system. The actual location of the data is transparent to the user.

Ex

Distributing data and providing secure access to that data with adequate performance is
not a trivial task. It requires thorough planning, implementation, monitoring, and
maintenance.
Here are some of the questions that the enterprise should attempt to answer:

pr

How is your organization structured centralized or decentralized?

How is your Information Technology department structured; your network; your user
community (are LANs already in place?)
How do you perform systems management, data backup, and recovery?
If distributed, will you replicate or actually move data to a single remote site?

Copyright IBM Corp. 1997, 2011

Unit 7. Information Management with DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

7-3

Student Notebook

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Do you want to allow multiple site updates within a single logical unit of work?

7-4

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Planning Multiple skills requirements


Communications
Specialist

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Applications
Personnel

DBA

LAN
Administrator

Strategist

User

Security?

Management?
Backups?

Copyright IBM Corporation 2007

Figure 7-3. Planning - Multiple skills requirements

CE0317.0

Notes:

cl

The strategist must have the global perspective (big picture view) of what needs to be
accomplished. The strategist must have an understanding of all the pieces of the
distributed processing puzzle, and must know how the pieces fit together. People with
many diverse skills must be coordinated by a person with strong leadership skills.

Ex

Policies and the appropriate software must be put into place to ensure the security of:

Hardware: Additional servers at remote locations now in closets instead of on a raised


floor.

pr

Network: How will we ensure that an outside access (a phone call from a hotel room) is
legitimate?
Workstations: These are now palmtops and Web-enabled phones; how do we ensure
their security?
Data access: Document who has access to the data.
Auditing: Ensure that access to the data can be tracked.

Copyright IBM Corp. 1997, 2011

Unit 7. Information Management with DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

7-5

Student Notebook

Security tools: Plug holes in the security network.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Coordination across the distributed systems is also important. Backups and recovery must
be coordinated. System date and time across systems must be synchronized.

7-6

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Distributed terminology Unit of work

SAVINGS

FUNDS
TRANSFER

CHECKING

Copyright IBM Corporation 2007

Figure 7-4. Distributed terminology - Unit of work

CE0317.0

Notes:

Ex

cl

A unit of work (also known as a logical unit of work or unit of recovery) is a sequence of
SQL operations that is handled in an all or nothing way. Either all the operations are
successfully performed, or the sequence as a whole is considered unsuccessful. If all the
operations are successful, changes to data are put into effect. If the operations are
unsuccessful, changes to data are removed (as though the changes were never made). To
put the changes into effect, the RDBMS performs a commit operation; to remove the
changes, it does a rollback operation. In either case, the data remains at a point of
consistency.

pr

The ability to commit or roll back is important in maintaining the integrity of related data. In
the example, the application subtracts from the savings account table, and adds to the
checking account table. All the subtract and add operations either happen (they are
committed), or do not happen (they are rolled back). During a unit of work, updates to data
are seen only by the updating program.
Only committed work can be seen by applications not requesting an uncommitted read.

Copyright IBM Corp. 1997, 2011

Unit 7. Information Management with DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

7-7

Student Notebook

Types of access to distributed data


SQL

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Remote Request

Remote
Unit of Work
Application

SQL
SQL

Distributed
Unit of Work
Application

SQL
SQL
SQL

Boston

Distributed
Request
Application

SQL
SQL
SQL

Chicago

Atlanta

Copyright IBM Corporation 2007

Figure 7-5. Types of access to distributed data

CE0317.0

Notes:

Remote request allows a user or DB2 application to read or update data at one remote
server. Only one SQL statement is allowed in a remote request.

Ex

cl

Remote unit of work lets a user or application program read or update data at one remote
location per unit of work. There may be multiple SQL statements within the unit of work. All
DB2 environments support remote unit of work.

pr

Distributed unit of work lets a user or an application program read or update data at
multiple locations within a single unit of work. There may be many SQL statements within
the unit of work. Distributed unit of work requires two-phase commit (the ability of the
various DB2s to coordinate the committing process). DB2s that connect via distributed
relational database architecture (DRDA) connections support distributed unit of work.

Distributed request is significantly more sophisticated than either remote unit of work or
distributed unit of work. It will let you access data at multiple locations with a single SQL
statement (such as JOIN operations). With distributed request, there may be many SQL
statements within a unit of work, and individual SQL statements can join tables from
multiple locations. This is currently supported by WebSphere Federated Server. This level
7-8

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

of distributed data requires global optimization: the ability for DB2 to analyze statistics
about all of the tables in all of the locations as well as information about each location's
CPU capacity, network, and other resources, in order to determine the most efficient way to
handle a distributed request.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Uempty

Copyright IBM Corp. 1997, 2011

Unit 7. Information Management with DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

7-9

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Types of access to distributed data

Type of Distributed
Database Request

Number of
Number of Server Servers accessed
SQL statements
Locations
per SQL statement

Remote Request

Special
Requirements

Remote Unit of Work

>1

Distributed Unit of Work

>1

>1

Two Phase Commit

Distributed Request

>1

>1

>1

Two Phase Commit,


Global Optimization

Copyright IBM Corporation 2007

Figure 7-6. No title

CE0317.0

Notes:

pr

Ex

cl

Here is another way to look at the various levels of distributed data access, ranging from
the most simple (remote request) to the most complex (distributed request). The four
levels are determined by the combination of the number of SQL statements allowed, the
number of server locations accessed, and the number of server databases accessed within
a single SQL statement.

7-10 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Information On Demand

Unified, Insightful Information in Business Context


Insightful
Relationships

Business
Context

Unified Data
& Content

Standards-based

Information On Demand

For example,
XQuery, JCR,
JDBC, Web
Services, ...

Optimize, Visualize, Integrate, Accelerate

Master Data,
Entity Analytics,
Information
Warehouses,
Industry Models

Extracted or
Real-time

Databases and Content Repositories


Copyright IBM Corporation 2007

Figure 7-7. Information On Demand

CE0317.0

Notes:

cl

Information On Demand is about getting the right information to the right people or
processes at the right time to act on emerging opportunities and competitive threats. It is
about creating business value and reducing risk by integrating, analyzing and optimizing all
types and sources of information throughout its lifecycle to address the following customer
needs:

Ex

Lower costs through an optimized infrastructure


Gain insight through business intelligence

Manage risk and streamline compliance

pr

Leverage information to drive growth


Gain control over master data
Manage information complexity

At the bottom of this chart, you see repositories typical of the multitude of application silos
many companies have:
Copyright IBM Corp. 1997, 2011

Unit 7. Information Management with DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

7-11

Student Notebook

SAP running on DB2,


A Content Management application running on an IBM Content Manager
Or other applications running on other repositories

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

And there's various disparate sources from external suppliers and business partners. Its
all information that needs to be available to any process or person that can benefit from it.
As people build new tools and applications for example, executive dashboards or next
generation procurement applications they do not need to really understand or do not
need to depend upon what sources that information is coming from.

pr

Ex

cl

In fact, information sources change every day and consumption of sources change every
day. Therefore, to make business flexible, and not based on a specific integration project
but a flexible information architecture, we need to deliver information as a service. This
virtualizes access to the information. By separating information from the processes and
applications it is easier and faster to change either.

7-12 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

IBM Information Server

Copyright IBM Corporation 2007

Figure 7-8. IBM Information Server

CE0317.0

Notes:

cl

The IBM Information Server is designed to help companies leverage their information
across all its sources. The IBM Information Server delivers all of the functions required to
integrate, enrich and deliver information you can trust for your key business initiatives. The
Information Server allows you to:

Ex

Understand all sources of information within the business, analyzing its usage, quality,
and relationships
Cleanse it to assure its quality and consistency

Transform it to provide enriched and tailored information

pr

Federate it to make it accessible to people, processes, and applications

All of these functions are based on a parallel processing infrastructure that provides
leverage and automation across the platform. The Information Server also provides
connectivity to nearly any data or content source, and the ability to deliver information
through a variety of mechanisms.

Copyright IBM Corp. 1997, 2011

Unit 7. Information Management with DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

7-13

Student Notebook

Underlying these functions is a unified metadata management foundation that provides


seamless sharing of knowledge throughout a project lifecycle, along with a detailed
understanding of what information means, where it came from, and how it is related to
information in other systems. Integration logic built within the Information Server can easily
be deployed and managed as a shared service within a SOA (service-oriented
architecture).

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

The IBM Information Server includes the following:

IBM Metadata Workbench manages metadata, depicts the relationship between sources
and consumers
WebSphere Information Analyzer profiles source systems and monitors data rules
WebSphere Business Glossary creates/manages/searches metadata definitions

WebSphere QualityStage standardizes/matches information across heterogeneous


sources
WebSphere DataStage moves data between sources and targets

WebSphere Federation Server defines integrated views across sources

WebSphere Information Services Director allows information access and integration


processes to be published as reusable services (SOA)

The breadth and flexibility of the platform enable it to address many types of business
problems and meet the requirements of many types of projects. This optimizes the
opportunities for reuse, leading to faster project cycles, better information consistency, and
stronger information governance.

pr

Ex

cl

Regarding Service-Oriented Architectures, information integration enables information to


be made available as a service, publishing consistent, reusable services for information
that make it easier for processes to get the information they need from across a
heterogeneous landscape.

7-14 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

DB2 Warehouse integrated warehousing platform


Eclipse Design Studio

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Ease of Design
and Manage
via Tooling

Web Administration Console

Data Mining

Extend Value
via Analytics

Simplify
Warehouse
Infrastructure

SQW:
Design and
Deploy

In-Line
Analytics

Cubing
Services

DB2 ESE/DPF/Compression

Unstructured
Analysis

WLM
Workload
Management

Data Warehouse

BCU 1

BCU 2

BCU N

Copyright IBM Corporation 2007

Figure 7-9. DB2 Warehouse integrated warehousing platform

CE0317.0

Notes:

Ex

cl

DB2 Data Warehouse is an integrated platform for warehouse-based analytics that runs on
the DB2 LUW platforms. The Warehouse Tool component is the SQL Warehousing Tool
(SQW) that is designed to move data already in DB2 into data marts. The mining
component is DB2 intelligent Miner and includes a Miningblox component to facilitate
reporting from a mining run via Alphablox. The OLAP component is Cubing Services, which
is designed to be an MDX gateway and an OLAP accelerator for applications accessing a
star schema stored in DB2. Alphablox is used as the in line analytics component.

pr

The SQW, the data mining component, Cubing Services and the new Alphablox blox
builder component all share the same Eclipse-base development environment and all,
except Alphablox, utilize the same common Administration Console, a WebSphere
Application Server application, for administration purposes.
A Rational Data Architect plug-in is used to do physical data modeling.

Copyright IBM Corp. 1997, 2011

Unit 7. Information Management with DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

7-15

Student Notebook

Checkpoint (1 of 2)

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

1. Which of the following must be planned for to ensure a


successful distributed data environment?
a. The goals and objectives of distributing the data
b. Obtaining the right people/skill set to implement and maintain
the distributed data
c. Building the infrastructure
d. Security
e. Back up and recovery
f. Data synchronization
g. All of the above

2. List several of the people (skills) that are involved with the
distribution of data.
3. List some of the security concerns with distributed data.
Copyright IBM Corporation 2007

Figure 7-10. Checkpoint (1 of 2)

CE0317.0

Notes:

Write down your answers here:


1.

cl

2.

pr

Ex

3.

7-16 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Checkpoint (2 of 2)
4. Match the following with their definitions.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

__ Within a single SQL statement, data


a. Remote Unit of Work
at multiple sites may be accessed.
b. Distributed Unit of Work
__ Within a unit of work, ALL SQL
c. Distributed Request
statements must access data at the
same site.
__ Within a unit of work, data may be
read or changed at multiple
locations. However, individual SQL
statements must access data at one
site only. This process requires that
all participating sites support twophase commit.

Copyright IBM Corporation 2007

Figure 7-11. Checkpoint (2 of 2)

CE0317.0

pr

Ex

cl

Notes:

Copyright IBM Corp. 1997, 2011

Unit 7. Information Management with DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

7-17

Student Notebook

Unit summary

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Having completed this unit, you should be able to:


List several planning considerations for distributing data
List some of the skills required to successfully distribute
data
Differentiate between remote unit of work, distributed
unit of work, and distributed request
List some of the security concerns when dealing with
distributed data

List some of the other products that interface with DB2

Copyright IBM Corporation 2007

Figure 7-12. Unit summary

CE0317.0

pr

Ex

cl

Notes:

7-18 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V6.0
Student Notebook

Appendix 8. Checkpoint solutions


Unit 1 - Understanding a Table
1. Logging, optimization, locking, recovery, data integrity, security

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

2. SQL
3. Column name
Data type/length
Null characteristic
4. Table

5. Creating objects (tables, indexes, views, and so on)


Grant privileges (select, update, delete, and so on)
Maintenance (load, statistics, copy, recover, and so on)

Unit 2 - Data Modeling and Database Design

1. A business entity is something fundamental to the business that can be uniquely


identified.
2. The creator/owner/schema of the table (high level qualifier)
The simple table name

cl

3. INTEGER
SMALLINT
DECIMAL
REAL
FLOAT
CHAR
VARCHAR
LONG VARCHAR
DATE
TIME
TIMESTAMP
BLOB
CLOB
DBCLOB
User-defined Distinct Types

pr

Ex

AP

4. ITEM_TAB - Each item has only one order.


5. It must contain unique values, ensured by a unique index created over the referenced
column (or columns).
6. CREATE TABLE and ALTER TABLE statements.
7. The limiting of a set of foreign key values to a set of values in a unique key.
Copyright IBM Corp. 1997, 2011

Appendix 8. Checkpoint solutions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

8-1

Student Notebook

8. Referential integrity.

Unit 3 - How does a User use DB2?


1. A user is anyone who, through their actions, causes an SQL statement to be executed.
2. SELECT, FROM, WHERE, GROUP BY, ORDER BY

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

3. When we wish to retrieve, in addition to data from related rows, data from unmatched
rows in table A, data from unmatched rows in table B, or data from unmatched rows in
both tables.
4. A linear procedure is a stack of QMF commands. Procedures are used to automate
data processing tasks, such as creating the monthly reports by running a single
command (RUN PROCX).
5. SELECT CUSTOMER_NAME, CUSTOMER_ADDRESS, CONTACT_NAME, PHONE_NUMBER,
POSTAL_CODE
FROM MASTER.CUSTOMER
WHERE CITY = 'PARIS' AND
YEAR_TO_DATE_SALES > 2000
ORDER BY POSTAL_CODE
6. A CASE expression.

Unit 4 - How does a Programmer use DB2?

1. So that testing will not interfere with the production environment.

2. Use the EXPLAIN function, through which DB2 tells us its access strategy for each SQL
statement.
3. It is the vehicle through which DB2 talks to our program (gives it return codes and
messages).
4. So that they can be found by the PRECOMPILER.

cl

5. EXEC SQL

pr

Ex

6. Comments out SQL statements (modified source code)


Numbers the program's statements
Adds host language calls to DB2 after commented SQL statements
Copies SQL into a separate file (DBRM or bind file)
Adds a consistency token to the modified source code and DBRM (bind file)
Includes code from other libraries
Does minor SQL syntax checking

7. Performs an in-depth syntax check of the SQL (using the DB2 catalog)
Checks to ensure that the owner of the program has the authority to execute the

8-2

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V6.0
Student Notebook

embedded SQL statements


Develops the access strategies for each SQL statement
8. Reduced network traffic
Program changes are easier to control (business logic is stored in one place, at the
host, rather than in many client applications).

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

9. A colon
10. An INTO clause

11. Cursor processing

Unit 5 - What does an Administrator do in DB2?


1. SYSADM (or system administrator)

2. An ordered list of values with one or more pointers (called RIDs) to where these values
are found within the table.
3. Improve performance
Uniqueness
Clustering

4. A view is a description of a set of columns and rows with which we want to work. That
description is given a name, and we may treat that name as if it were a table that
actually contained the columns and rows described.

5. A copy of the data taken at a time when we knew that the data was good (primary keys
and foreign keys were in sync).
6. RUNSTATS

7. The owner (USER7) of the table, or an ID with the appropriate authority (SYSADM,
DBADM, and so on) has granted the UPDATE or UPDATE(ADDRESS) privilege to
USER1.

cl

8. ALTER (add columns or business rules)


DROP (remove the table from the system)
SELECT (read data from the table)
UPDATE (change data in the table)
INSERT
DELETE
CREATE INDEXES
LOAD (utility)

pr

Ex

AP

9. Being a member of a group and the privilege is granted to the group


Being granted EXECUTE authority on a program that contains SQL

Unit 6 - Information Management with DB2


1. g
Copyright IBM Corp. 1997, 2011

Appendix 8. Checkpoint solutions

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

8-3

Student Notebook

2. Strategist, database administrator, communications specialist, LAN administrator,


applications personnel, users

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

3. Hardware security
Network security
Workstation security
Data access: who is allowed access to the data
Auditing accesses
Security tools

pr

Ex

cl

4. c, a, b

8-4

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V6.0
Student Notebook

Appendix B. DB2 and Related Courses


This list is only meant as a general guide to the courses available. Please refer to the IBM
Course Catalog to see specific offerings in your country.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

N.B. Course codes are currently being updated. The course listed may be known in your
country by either the course code inside or outside the brackets.

New Versions of DB2:


z/OS:
CG38
CV30

DB2 UDB for z/OS V8 Transition


DB2 9 for z/OS Transition Workshop

LUW:

CG09(CL31)

DB2 9 for Linux, UNIX, and Windows Transition

Note

Course code changes for the Transition Course for every new version of DB2.

Design:

CF18(CE81)

Relational Database Design

User:

DB2 SQL Workshop


DB2 SQL Workshop for Experienced Users
QMF on S/390 Workshop

cl

CF12(CE12)
CF13(CE13)
CF81(CV74)

Ex

AP

Programmer:

pr

z/OS:

CF82(CV72)
CG14(CV14)

DB2 Application Programming Workshop


DB2 for z/OS and OS/390 Programming Using Java

Copyright IBM Corp. 1997, 2011

Appendix B. DB2 and Related Courses

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

B-1

Student Notebook

LUW:
DB2 Programming Workshop for Linux, UNIX and Windows
DB2 Advanced Programming

CF71(CL71)

DB2 Stored Procedures Programming Workshop

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

CF10(CL10)
CF11(CL11)

Administration:
z/OS:

CF83(CV83)
CF84(CV84)

DB2 for z/OS and OS/390 Database Administration Workshop 1


DB2 for z/OS and OS/390 Database Administration Workshop 2

CF85(CV85)
CG85(CV86)
CF89(CV89)
CF95(CV95)
CF96(CV96)

DB2 for z/OS and OS/390 System Administration


DB2 for z/OS Implementation Workshop
DB2 for z/OS and OS/390 Application Data Recovery
DB2 for z/OS System Performance Tuning and Analysis
DB2 for OS/390 and z/OS Application Performance and Tuning

CF91(CV91)

DB2 for z/OS and OS/390 System/Disaster Recovery Workshop

CF31/35(CV21/25) DB2 for z/OS and OS/390 Data Sharing


CF32/36(CV92/93) DB2 for z/OS and OS/390 Data Sharing Recovery and Restart

LUW:

DB2 9 Database Administration Workshop for Linux


DB2 9 Database Administration Workshop for UNIX
DB2 9 Database Administration Workshop for Windows

CF10(CL10)
CF41(CL41)
CF46(CF46)
CF49(CL49)

DB2 Programming Workshop for Linux, UNIX and Windows


DB2 Performance Tuning and Monitoring Workshop
DB2 for LUW Advanced Database Administration for Experts
DB2 Advanced Recovery for Single Partition Databases

Ex

cl

CF20/CL201
CF21/CL211
CF23/CL231

Fastpath to DB2 for Experienced Relational DBAs


Fastpath to DB2 Advanced Administration for Experienced Relational
DBAs

pr

CF28(CL28)
CF48(CL48)

CF24(CL24)
CF44(CL44)

DB2 Multiple Partition DBA Workshop for UNIX


DB2 for UNIX Multi Partition Performance Workshop

CF60(CL60)
CF63(CL63)

DB2 Connect DRDA Implementation with TCP/IP


DB2 Connect to DB2 for z/OS Problem Determination and Performance

B-2

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V6.0
Student Notebook

DB2 and XML:


CG10(CL12)
CG13(CL13)
CG16(CL14)

Query XML Data with DB2 9


Query and Manage XML Data with DB2 9
Manage XML Data with DB2 9

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Other Products with DB2:

Changing Business with Data Insight

DW35
L130

DB2 Data Warehouse Edition Components


DB2 Alphablox Essentials

DW14
DW15
DW24

Data Replication: Basic Usage


DB2 DataPropagator Advanced Topics
Using Q Replication

MQ01

A Technical Introduction to WebSphere MQ

CM01
CI01

IMS Fundamentals
CICS Fundamentals

613
757

Informix Structured Query Language Part 1


Informix Structured Query Language Part 2

846
848

IBM Informix Dynamic Server Administration


Informix Database Administration

cl

DW03

pr

Ex

AP

Copyright IBM Corp. 1997, 2011

Appendix B. DB2 and Related Courses

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

B-3

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Student Notebook

B-4

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V6.0
Student Notebook

Uempty

Appendix C. More about DB2 and Other Products

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Please note that due to the diversity of subjects covered in Appendix C, and the
ever-changing world of Information Management, product details may have been updated
since these notes were published. The material here is therefore provided on as-is basis
and should be checked against the most recent documentation.

Copyright IBM Corp. 1997, 2011

Appendix C. More about DB2 and Other Products

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

C-1

Student Notebook

IBM Informix
TABLE_A

PARTS
"Relational
Database"
is a "Table
Database"

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

COL1
ABC
DEF
GHI
JKL
MNO

COL2

7
4
6
7
0

COL3
1987-11-14
1963-01-01
1946-07-12
1951-12-30
1992-08-12

PROD
BOLT
NUT
SCREW
BOARD
WIRE

QTY
7
4
6
7
0

SUPPLIER
ABC CORP
THE NUT CO
FASTEN INC
ECM CORP
BINDINGS

Informix Dynamic Server Express Edition

Informix Dynamic Server Workgroup Edition


Informix Dynamic Server Enterprise Edition

http://www-306.ibm.com/software/data/informix
Copyright IBM Corporation 2007

Figure C-1. IBM Informix

CE0317.0

Notes:

pr

Ex

cl

IBM Informix is another relational database system offering similar facilities to DB2. The
various editions available are:

C-2

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V6.0
Student Notebook

Uempty

Informix Dynamic Server Express Edition


Linux and Windows
Suitable for small to mid-market companies
Full-function, object-relational data server
Includes important capabilities such as:
- High reliability
- Security
- Usability
- Manageability
- Performance.
Continuous Log Restore for high-availability server recovery
Self-healing manageability features
Near-zero administration
Allows integration into Rational Application Developer and Microsoft Visual Studio
Spatial Datablade
Support for transparent silent installation
Supports wide array of development paradigms
Minimal disk space requirements
Simplified installation

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Informix Dynamic Server Workgroup Edition

Linux, UNIX and Windows


Suitable for at mid-market and departments within larger companies
Includes all of the features of IDS Express, plus features to handle high transaction
volume including parallel Performance for Workgroup Feature (add-on)
High-Availability Data Replication (HDR) can be purchased as an add-on option
Datablades can be purchased as an option

cl

Informix Dynamic Server Enterprise Edition

pr

Ex

Linux, UNIX and Windows


Suitable for large enterprises
Includes all of the features of IDS Workgroup Edition plus features required to provide
the scalability to handle high user loads and provide 24x7x365 high availability,
including:
- Enterprise Replication (ER)
- High-Availability Data Replication (HDR) and global availability with HDR Multiple
Remote Secondary Servers
- Datablades can be purchased as an option
- Continuous Availability Feature can be purchased as an option

Copyright IBM Corp. 1997, 2011

Appendix C. More about DB2 and Other Products

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

C-3

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SOA reference architecture

Copyright IBM Corporation 2007

Figure C-2. SOA reference architecture

CE0317.0

Notes:

Ex

cl

Service-Oriented Architecture (SOA) is an IT architectural style that supports the


transformation of your business into a set of linked services, or repeatable business tasks,
that can be accessed when needed over a network. This may be a local network, it may be
the Internet, or it may be geographically and technologically diverse, combining services in
New York, London, and Hong Kong as though they were all installed on your local desktop.
These services can coalesce to accomplish a specific business task, enabling your
business to quickly adapt to changing conditions and requirements.

pr

When SOA implementation is guided by strategic business goals, you ensure the positive
transformation of your business and can realize the chief benefits on an SOA, as follows:
Alignment of IT with the business
Maximal reuse of IT assets

Together, these help assure that investment in expensive IT projects result in lasting value
to the business.

C-4

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V6.0
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Data services within Information Services

Copyright IBM Corporation 2007

Figure C-3. Data services within Information Services

CE0317.0

Notes:

cl

Information as a Service is about accelerating the delivery of information through an


optimized, virtualized, and integrated infrastructure based upon a Service Oriented
Architecture which provides a flexible and agile conduit.

pr

Ex

Information as a service is about providing a new level of services that helps add value to
the information. Services that integrate information both data and content, regardless of
location - to provide a unified view. Services that add business context to the raw
information; so transaction data takes the form of a policy, for example. And services that
expose insightful relationships in the information through advanced analysis, so you can
make better decisions.
To achieve this, information as a service must be flexible, therefore standards like XML,
XQuery, JSR170, which is an unstructured content standard, or web services are critical.
Information needs to be delivered in real time, like our federation capabilities allow for
up-to-date information in a call center, or extracted, or to support warehouse or
consolidation activities.

Copyright IBM Corp. 1997, 2011

Appendix C. More about DB2 and Other Products

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

C-5

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Adding value to information takes demand capabilities, above and beyond traditional data
management, for example, Master Data to help organize key information domains like
product or customer data, and provide business context. Or, entity analytics to identify
relationships in data so you really know who is who and can detect AML in Finance or Slip
and Fall fraud in Retail. Or, embedded analytic capabilities so dashboard analysis can be
embedded in a business process and extend analytic insight to a wider audience of
users.and so forth.
So Information as a Service, delivering information more flexibly, is critical to meeting the
goals of any On Demand Business.

pr

Ex

cl

C.1. Autonomic Computing

C-6

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V6.0
Student Notebook

Uempty

Autonomic computing Now that's SMART!


DB2 A system that self-monitors
and self-regulates

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Human Body:
A System that self-monitors
and self-regulates

A Sympathetic DB2

Monitors itself looking for suboptimal performance, design, etc.


Looks out for subsystems that
require routine maintenance
Seeks out resource inadequacies

And a Calming DB2


Provides expert advice on improving
performance, capacity planning,
appropriate windows of opportunity
to do maintenance
Automatically balances resource
usage, compensates for outages,
adjusts designs based on workload,
and so on ...

Copyright IBM Corporation 2007

Figure C-4. Autonomic computing Now thats SMART!

CE0317.0

Notes:

In a similar way, a DB2 system should be both:


Looking for situations that need action.

pr

Ex

cl

Balancing resources when high levels are not required.

Copyright IBM Corp. 1997, 2011

Appendix C. More about DB2 and Other Products

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

C-7

Student Notebook

A SMART DB2: Objectives


Self Managing And Resource Tuning (SMART) Databases

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Technology for reducing human intervention/cost in the operation of


a DBMS that includes one or more of the qualities of automation,
decision making, or expert advice.

Focus areas:
Up and running
Expert design
Self-maintaining
Self-healing
Auto-recovering

Copyright IBM Corporation 2007

Figure C-5. A SMART DB2: Objectives

CE0317.0

Notes:

cl

Just as the human body manages itself without conscious intervention, the objectives of
SMART databases is to make the e-business infrastructure, and in particular, IBM eServer,
storage and software, a self-managing system, one that can "take a lickin' and keep on
tickin' " with the least possible human intervention.

Ex

Some tools that DB2 uses to achieve its SMART objectives are:

Developer Workbench
Health Center
Wizards for creating some DB2 objects, backup, restore, load, etc.
Memory Visualizer, Activity Monitor
Configuration Advisor, Design Advisor
and many multiplatform tools

pr

C.2. Web Services and Java

C-8

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V6.0
Student Notebook

Uempty

Web services applications


New programming paradigm

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Enables development and deployment of loosely coupled


applications within a company or within an industry

Based on emerging technologies:

Simple Object Access Protocol (SOAP)


Web Services Description Language (WSDL)
Universal Description, Discovery, and Integration (UDDI)

DB2 can be accessed as Web service provider

Usually teamed with IBM WebSphere family products


Complete Web services framework

Copyright IBM Corporation 2007

Figure C-6. Web services applications

CE0317.0

Notes:

Web Services Applications

Ex

cl

Web Services is a powerful new programming paradigm that enables the development and
deployment of loosely coupled applications within a company or across industries. Web
services are based on emerging technologies including Simple Object Access Protocol
(SOAP), Web Services Description Language (WSDL), and Universal Description,
Discovery, and Integration (UDDI).

pr

A Web service is created by wrapping an application so that it can be accessed using XML
messages which, in turn, are wrapped to mask the underlying transport protocol. The
service is publicized in a standard-format registry, allowing people and applications to find
and use the service over the Web.
DB2 can be accessed as a Web service provider, and it is usually teamed with IBM
WebSphere family products to provide a complete Web services framework.

Copyright IBM Corp. 1997, 2011

Appendix C. More about DB2 and Other Products

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

C-9

Student Notebook

An easy way to develop data-intensive Web services applications is through DB2s


document access definition extension (DADx) programming model. DADx files are simply
XML documents that contain stored procedure references, DB2 XML Extender references,
or traditional SQL. Best of all, DADx files can be created quickly without any Java
programming.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

For a demonstration of DB2 as a Web service provider, see the IBM Video Central for
e-business tutorial at:

pr

Ex

cl

http://www.ibm.com/developerworks/db2/library/samples/videocentral/videocentral.html

C-10 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V6.0
Student Notebook

Uempty

IBM DB2 Driver for JDBC and SQLJ

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Provided functions of DB2 for Linux, UNIX, and Windows and


those for z/OS are the same
Improves Family compatibility - true portability

Functionality enhancements for Type 2 and Type 4 drivers


Fully compliant with JDBC 3.0 standard

Provides a full Java application development process for


SQLJ
Ease of installation and deployment for Type 4 driver
No dependencies on a runtime of DLL
Installation merely a copy of a .jar and .zip file

Reduces the client footprint for Linux, UNIX and Windows


platforms
Delivers functional and performance enhancements quicker
Improves trace capabilities
Copyright IBM Corporation 2007

Figure C-7. IBM DB2 Driver for JDBC and SQLJ

CE0317.0

Notes:

DB2 has a new common runtime environment.

cl

It has a single Java driver with a common code base for Linux, UNIX, Windows and z/OS.
The functions on all of these platforms are now the same.

pr

Ex

This largely improves DB2 Family compatibility - allowing development across Linux, UNIX,
and Windows, and deployment on z/OS without any changes, eliminating the major cause
of Java porting problems.

Copyright IBM Corp. 1997, 2011

Appendix C. More about DB2 and Other Products

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

C-11

Student Notebook

IBM DB2 Driver for JDBC and SQLJ Architecture

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

SQLJ application JDBC application

SQLJ runtime

Abstract JDBC machine


(Java common layer)

Java DRDA
Application
Requester

DB2 for
LUW native
libraries
T2

DB2 for z/OS


native libraries
RRS/RDI
Requester

JCC

T2

T4

DB2

DB2 for z/OS

DRDA

DDF

Direct

T4

Tx Driver
type

DB2 for LUW

Copyright IBM Corporation 2007

Figure C-8. IBM DB2 Driver for JDBC and SQLJ Architecture

CE0317.0

Notes:

cl

Whereas before the DB2 Universal Driver, each DB2 client platform came with its own
driver files for JDBC and SQLJ support, the new driver is a single set of Java Archive (.jar)
and .zip files, which can be used on UNIX, Windows, and z/OS. It is a combined driver
providing both JDBC Type 2 and Type 4 connectivity, depending on the URL used in the
getConnection() call (or the underlying data source definition).

Ex

Of course, selecting Type 2 functionality requires that native code has been installed on the
client.

pr

C.3. WebSphere Products

C-12 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V6.0
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

WebSphere Application server

Copyright IBM Corporation 2007

Figure C-9. WebSphere Application server

CE0317.0

Notes:

cl

WebSphere Application Server is IBMs Middleware Java Enterprise platform, as well as


IBMs Web Application Server giving a robust flexible environment to run Java Enterprise
Applications.

Ex

IBM Middleware connects applications simply and powerfully to support complex,


distributed applications and is especially useful with modern information technologies such
as XML, Web Services and Service-Oriented Architecture.

pr

Integration Technology is included in the product for linking to Business critical applications
and services such as Messaging Providers including WebSphere MQ and Databases
including DB2.
WebSphere Application Server is the underlying technology platform used by many IBM
WebSphere products, such as WebSphere Portal, WebSphere Process Server and many
more.

Copyright IBM Corp. 1997, 2011

Appendix C. More about DB2 and Other Products

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

C-13

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Rational Application Developer

Copyright IBM Corporation 2007

Figure C-10. Rational Application Developer

CE0317.0

Notes:

Rational Application Developer is the IBM tool for developing application code for the IBM
WebSphere platform.

cl

It allows developers to create, test and deploy Java code, alongside XML Documents,
WebSphere Portal code, SOA Applications, Database schemas and much more.

Ex

Rational Application Developer is based on the universal open source Eclipse Integrated
Development Environments. The Eclipse tool is designed to give a standard Development
Environment for all software development.

pr

It is based around a series of Views, each of which provide specific functionality. Each can
be thought of as a separate program, but within the greater whole of Eclipse.

Plug-ins can be added to Eclipse to give extra functionality, often by providing extra Views.
Eclipse is also the basis of many other IBM Development tools, including WebSphere
Integration Developer, WebSphere Message Broker Toolkit and Rational Software
Architect (which includes Rational Application Developer).

C-14 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V6.0
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DB2 and WebSphere MQ

DB2 MQListener

DB2

MQSeries
Networks

DB2 Workload

MQ
MQ UDF
UDF // SPs
SPs

trigger

SP

MQSeries
Networks

Send
Receive
Read
Publish
Subscribe

DB2 Clients

Copyright IBM Corporation 2007

Figure C-11. DB2 and WebSphere MQ

CE0317.0

Notes:

WebSphere MQ is a middleware messaging product that provides program- to-program


communication between over 40 different platforms.

Ex

cl

Data is sent via a message on a queue and can be received either synchronously or
asynchronously from that queue, locally or remotely. Delivery of the message is assured by
WebSphere MQ.
A set of MQSeries functions are provided with DB2 to allow DB2 applications to interact
with asynchronous messaging operations. This means that MQSeries support is available
to applications written in any programming language supported by DB2.

pr

In a basic configuration, an MQSeries server is located on the database server machine


along with DB2. The MQSeries functions are available from a DB2 server and provide
access to other MQSeries applications. Multiple DB2 clients can concurrently access the
MQSeries functions through the database. The MQSeries operations allow DB2
applications to asynchronously communicate with other MQSeries applications. For
instance, the new functions provide a simple way for a DB2 application to publish database
events to remote MQSeries applications, initiate a workflow through the optional MQSeries

Copyright IBM Corp. 1997, 2011

Appendix C. More about DB2 and Other Products

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

C-15

Student Notebook

Workflow product, or communicate with an existing application package with the optional
MQSeries Integrator product.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

C.4. Yet More Products!

C-16 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V6.0
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Convergence: Enterprise Content Management

Copyright IBM Corporation 2007

Figure C-12. Convergence: Enterprise Content Management

CE0317.0

Notes:

Ex

cl

Today, the lines of demarcation have blurred. With the influx of multimedia and graphics,
office documents are becoming so large as to require some of the caching and local
storage techniques used in imaging systems. Scanned images are required to work in
concert with forms and other editable office documents in such a way as to demand the
version capabilities of a document management system. Corporations have both the
historic imaging and document management concerns but they want them in one system.
They require a single system to control all of their documentscradle to grave. Starting
with the creation and on through the editing, publishing, archival and ultimate disposition or
destruction.

pr

The concept of Enterprise Content Management is to bring under the control of one system
a platform independent integrated solution for storing and managing large quantities of
multimedia content as well as the traditional relational database data. This allows database
management facilities such as referential integrity, transactional integrity, robustness and
archival and retrieval to be utilized to manage the full range of data objects within the
modern enterprise.

Copyright IBM Corp. 1997, 2011

Appendix C. More about DB2 and Other Products

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

C-17

Student Notebook

We classify Document management systems as those applications where users interact


with some authoring or editing tools to create or enhance electronic format data files, and
store them into a safe place. The authoring or editing tools can be word processing
packages, CAD systems, spreadsheets. The system provides a repository of documents
and easy tools to find documents meeting certain criteria.

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

In these systems, the number of documents tends to be fewer than in imaging systems.
Each document takes a relatively long time and thought to create. The number of users
using the system can be relatively high, but small at any one time. The value of the
documents is also relatively high, as indicated by the cost of creating and storing them. The
Document Management system must provide value by delivering many ways to categorize
the document holdings and provide tools to easily search for documents of interest. This
implies that the repositories typically have many ways of indexing the data including
possibly full text index of its content.

C-18 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V6.0
Student Notebook

Uempty

IBM DB2 Document Manager


A client and set of services for DB2 Content Manager

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Exploits much of the V8 data model


Offers renditioning, notification, lifecycle, process, print/plot services

An easy-to-use, drag-and-drop, admin and configuration tool


Object based admin model
No programming necessary

An object broker between the repository and the user

Intercepts requests, interprets, clarifies, gathers and serves


Maintains a whole repository view of objects and users, their roles
and relationships

Copyright IBM Corporation 2007

Figure C-13. IBM DB2 Document Manager

CE0317.0

Notes:

cl

Document Manager provides a configurable layer of document management services on


top of Content Managers robust repository. Accessing Content Manager through its API,
DM is able to leverage the Content Manager architecture as it assists to address the needs
of an Enterprise Content Management System.

pr

Ex

Content Manager provides all of the repository back-end services necessary to address an
Enterprise Content Management system. It offers a scalable architecture that can
cost-effectively address the needs of a small implementation up to one that incorporates
thousands of employees across dozens of locations. It contains a rich set of APIs that
cover the full spectrum of content management functionality needed for an enterprise
solution. All of this architecture is secure and managed through a common system
administration interface that helps reduce operational costs.
DM enables the Content Management system to interface with the author through the
application itself. When it comes to application integration, DM goes well beyond the
ODMA standard and allows the user to have access to any DM command from within their
application. For example, a user creates a document in MS Word. Upon closing or saving

Copyright IBM Corp. 1997, 2011

Appendix C. More about DB2 and Other Products

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

C-19

Student Notebook

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

the document, the content management system can either insist or suggest (based on the
configuration) that the document be stored in the corporate repository. It is not a matter of a
user saving a file to their local PC and then launching a separate application to store it
away. It is integrated: it is not an application, it is infrastructure. This dramatically increases
user acceptance and will serve to shorten any time required to prove usability of the
system.

C-20 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V6.0
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

DM and CM architecture

Copyright IBM Corporation 2007

Figure C-14. DM and CM architecture

CE0317.0

Notes:

cl

Document Manager provides a configurable layer of document management services on


top of Content Managers robust repository. Accessing Content Manager through its API,
DM is able to leverage the Content Manager architecture as it assists to address the needs
of an Enterprise Content Management System.

pr

Ex

Content Manager provides all of the repository back-end services necessary to address an
Enterprise Content Management system. It offers a scalable architecture that can
cost-effectively address the needs of a small implementation up to one that incorporates
thousands of employees across dozens of locations. It contains a rich set of APIs that
cover the full spectrum of content management functionality needed for an enterprise
solution. All of this architecture is secure and managed through a common system
administration interface that helps reduce operational costs.

Copyright IBM Corp. 1997, 2011

Appendix C. More about DB2 and Other Products

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

C-21

Student Notebook

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Document Manager Architecture: Three-tiered

Copyright IBM Corporation 2007

Figure C-15. Document Manager Architecture: Three-tiered

CE0317.0

Notes:

pr

Ex

cl

The Content Manager tier incorporates a DB2 database to manage items. Content
Manager stores documents on a designated server and uses the DB2 database to manage
indexing and access control. Users can navigate to files through folders or alternatively, the
user can search the Content Manager System for an item based on metadata contained in
the DB2 Database.

C-22 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V6.0
Student Notebook

Uempty

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Transaction Managers with DB2

IMS

CICS

DB2

Copyright IBM Corporation 2007

Figure C-16. Transaction Managers with DB2

CE0317.0

Notes:

pr

Ex

cl

CICS and IMS are both DB (Database) and DC (Data Communication) products. When
dealing with DB2 it is they who are responsible for the entire transaction - the Transaction
Manager. Therefore, when you have an IMS-DB2 or CICS-DB2 application, you will find no
DB2 COMMIT or ROLLBACK statements. The unit of work is coordinated by IMS or CICS,
and it is their SYNCPOINT or ROLLBACK equivalents that will force DB2 to COMMIT or
ROLLBACK at the same time as the transaction manager. WebSphere MQ can also be
used as a Transaction Manager with DB2.

Copyright IBM Corp. 1997, 2011

Appendix C. More about DB2 and Other Products

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

C-23

Student Notebook

DB2 z/OS data sharing


DB2A

DB2B

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Sysplex
Timer

Coupling
Facility

Local
Buffer
Pools

Shared DASD
DB2
CATALOG

DB2
DIRECTORY

DB2A

IRLM

WORK
FILES

Local
Buffer
Tools

BSDS

Sysplex
Query
Parallelism

USER
DATA

DB2B

ACTIVE
LOG

BSDS

IRLM

ACTIVE
LOG

Sysplex
Query
Parallelism

Copyright IBM Corporation 2007

WORK
FILES

Figure C-17. DB2 z/OS data sharing

CE0317.0

Notes:

Data sharing allows concurrent R/W access to shared DB2 data from multiple DB2
subsystems.

cl

Sharing is done within a data sharing GROUP.

A group consists of one or more data sharing MEMBERS.

Ex

Data sharing requires a z/OS or S/390 Parallel Sysplex environment.

pr

Existing applications can take advantage of data sharing.

C-24 DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V6.0
Student Notebook

DB2 Information Center


IBM product information (including DB2) is available at the following Web site:

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

http://www.ibm.com/support/publications/us/library/index.shtml#software

pr

Ex

bibl

Copyright IBM Corp. 1997, 2011

DB2 Information Center

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

X-1

pr

Ex

cl

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

Student Notebook

X-2

DB2 Family Fundamentals

Copyright IBM Corp. 1997, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

backpg

pr

cl

Ex
V6.0

Back page

pr

u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.

cl

Ex

CONTACTO
Telfono
91 761 21 78
Pngase en contacto con nuestro equipo y le
informaremos de cualquier duda o cuestin
que pueda surgirle.

Email
formacion@arrowecs.es
Mndenos un email y le atenderemos
enseguida.

Online
@Arrow_Edu_ES
O bien puede contactarnos a travs de
nuestro perfil en Twitter.

Vistenos
Arrow ECS Education Services
Avenida de Europa 21,
Parque Empresarial La Moraleja
28108 Alcobendas, Madrid

EDUCATION
S

Potrebbero piacerti anche