Documente Academic
Documente Profesional
Documente Cultură
Intelligence
Data
Centre
Cloud
Mobility
Student Manual
Comunidad de Madrid
UNIN EUROPEA
FONDO SOCIAL EUROPEO
El Fondo Social Europeo invierte en tu futuro
EDUCATION
S
V5.4.0.3
cover
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Front cover
Student Notebook
pr
Ex
cl
ERC 7.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
DRDA
Notes
QMF
z/OS
CICS
IMS
OS/390
RACF
zSeries
DB2
Lotus
Parallel Sysplex
RETAIN
1-2-3
Java and all Java-based trademarks and logos are trademarks of Sun Microsystems, Inc.
in the United States, other countries, or both.
Linux is a registered trademark of Linus Torvalds in the United States, other countries, or
both.
Windows is a trademark 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.
Ex
cl
Other product and service names might be trademarks of IBM or other companies.
pr
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.
V5.4.0.3
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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
cl
pr
Ex
TOC
Contents
iii
Student Notebook
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
iv
V5.4.0.3
Student Notebook
TOC
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
2.6.
cl
2.7.
Ex
2.8.
pr
2.9.
Contents
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
V5.4.0.3
Student Notebook
4-24
4-25
4-27
4-28
4-29
4-30
4-31
4-32
4-33
4-34
4-36
4-38
4-40
4-41
4-43
4-44
4-46
4-48
4-49
4-50
4-51
4-52
4-53
4-54
4-55
4-56
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
cl
pr
Ex
TOC
Contents
vii
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
viii
V5.4.0.3
Student Notebook
8-25
8-27
8-28
8-30
8-31
8-32
8-33
8-34
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Establishing ownership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Ownership examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Checking authorization to execute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Controlling access for dynamic DML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Controlling access for static SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Authorizations for BIND (overview) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
DB2 authorization summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
cl
pr
Ex
TOC
Contents
ix
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Student Notebook
V5.4.0.3
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
DRDA
Notes
QMF
z/OS
CICS
IMS
OS/390
RACF
zSeries
DB2
Lotus
Parallel Sysplex
RETAIN
1-2-3
Java and all Java-based trademarks and logos are trademarks of Sun Microsystems, Inc.
in the United States, other countries, or both.
Linux is a registered trademark of Linus Torvalds in the United States, other countries, or
both.
Windows is a trademark 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
Other product and service names might be trademarks of IBM or other companies.
pr
Ex
TMK
Trademarks
xi
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Student Notebook
xii
V5.4.0.3
Student Notebook
Course description
DB2 10 for z/OS Database Administration Workshop Part 1
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Duration: 5 days
Purpose
Audience
Future DB2 for z/OS database administrators who need to acquire the
basic skills required to administer a DB2 database.
Prerequisites
You should also take DB2 Family Fundamentals (CE03) and SQL
Workshop (CE12), or have equivalent experience.
cl
Objectives
pr
Ex
pref
Contents
Course description
xiii
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Security
Serialization
Curriculum relationship
CV851
CV870
CV891
CV940
CV950
CV960
pr
Ex
cl
CV841
xiv
V5.4.0.3
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Administrators of DB2 10 for z/OS can acquire a view of the architecture and fundamental
processes required to manage a DB2 10 for z/OS subsystem. Engage in lectures and
hands-on labs to gain experience to:
Relate the z/OS IPL process to a DB2 subsystem
Explain effects of stopping and starting DB2
Explain how DB2 sets and use Integrated Catalog Facility (ICF) catalog names
The use of DSN command processor running in batch and foreground
See how the catalog (through grant activity) controls access to data
Search the catalog for problem situations
Use the catalog and DB2 utilities to determine data recovery requirements
Describe Internal Resource Lock Manager (IRLM) in a DB2 environment
cl
Ex
pref
See how the SQL Processor Using File Input (SPUFI) AUTOCOMMIT option defers the
COMMIT/ROLLBACK decision
pr
Describe available DB2 utilities to manage system and user page sets
Course description
xv
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
This course is designed to teach you how data and index recovery is supported by DB2 9
for z/OS in order to develop the knowledge and skills you need to design and implement
backup and recovery procedures for your installations. Practice using the utility functions
and techniques included in the lecture material during extensive interactive machine
exercises on a z/OS system. The main topics covered by the course are as follows:
Backup and recovery basics
Normal backup of table and index spaces
Normal recovery of table and index spaces
Point-in-time recovery
Generic utility jobs
Using nonstandard copies
Special recovery situations
Application recovery
pr
Ex
cl
xvi
V5.4.0.3
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
All of the index utilization possibilities are illustrated as well as the desired column
sequence of a multiple column index. Pros and cons of DPSI and NPSI indexes are
explained together with the lesser known concepts like index look-aside, implicit
uniqueness, sparse indexes, and combination of index screening with list prefetch. The
course also clarifies the meaning of many DB2 catalog columns pertaining to the indexes
and shows how to correctly use the different RUNSTATS options related to indexes. The
main topics covered by this course are as follows:
Generalities
Index versioning
DB2 9 for z/OS System Performance Analysis and Tuning (CV950 - 4 days)
This classroom course explains how to monitor and tune DB2 9 for z/OS from a system
point of view. The main topics covered by this course are as follows:
DB2 system performance analysis and tuning introduction
The DB2 trace
cl
Ex
pref
pr
Course description
xvii
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
V5.4.0.3
Student Notebook
pref
Course objectives
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
After
Aftercompleting
completingthis
thiscourse,
course,you
youshould
shouldbe
beable
ableto:
to:
Implement
ImplementaaDB2
DB2database
databasedesign
design
Use
Usedatabase
databaseutilities
utilitiesto
toload
loadand
andreorganize
reorganizedata
data
Define
Defineand
andimplement
implementaaDB2
DB2database
databaserecovery
recoverystrategy
strategy
Control
Controlaccess
accessto
todatabase
databaseusing
usingDB2
DB2authorization
authorizationfacilities
facilities
CV8317.0
pr
Ex
cl
Notes:
Course description
xix
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Student Notebook
xx
V5.4.0.3
Student Notebook
Agenda
Day 1
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Welcome
Introduction
Unit 2 (topic 1) - Overview of setting up a DB2 database
Unit 2 (topic 2) - Storage groups, databases, table spaces
Exercise 1 and review
Unit 2 (topic 3) - Tables
Exercise 2 and review
Unit 2 (topic 4) - Data compression
Day 2
Day 3
cl
Day 4
pr
Ex
pref
Agenda
xxi
Student Notebook
Day 5
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
V5.4.0.3
Student Notebook
Uempty
Unit 1. Introduction
What this unit is about
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Classroom discussion
Student Notebook
pr
Ex
cl
References
Unit 1. Introduction
1-1
Student Notebook
Unit objectives
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
After
Aftercompleting
completingthis
thisunit,
unit,you
youshould
shouldbe
beable
ableto:
to:
Describe
Describethe
themain
maincomponents
componentsof
ofaaDB2
DB2environment
environmentand
and
the
theroles
rolesthey
theyplay:
play:
Users
Users
Data
Data
SQL
SQLstatements
statements(static,
(static,dynamic)
dynamic)
System
System
CV8317.0
pr
Ex
cl
Notes:
1-2
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Simple concepts
Dynamic relationships
RDBMS facilities
Integrity
Dynamic definition of DB2 objects
Active catalog
Recovery / restart
Continuous operations
Security
Interactive tools
CV8317.0
Notes:
This is the view of DB2 for z/OS from 37,000 feet. A high-level view in one visual before we
get into any detail.
cl
Ex
The Structured Query Language (SQL) provides you with capabilities to access, define and
control access to these tables. The main language statements fall broadly into 3
categories.
pr
Unit 1. Introduction
1-3
Student Notebook
You tell DB2 WHAT columns, rows and sorting sequence you want and DB2 will decide
HOW to satisfy your request with the best performance.
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
As we shall see in this course, DB2 has facilities to maintain the integrity of your data and
to allow your definitions to take effect immediately. You can query DB2s catalog to find out
what tables have been defined and who owns them etc. There are extensive recovery and
restart facilities and many enhancements in recent years to help provide 24X7 operations.
DB2 has its own security mechanism. DB2 provides interactive tools for you to carry out
your database administration tasks.
1-4
V5.4.0.3
Student Notebook
Uempty
Haas
A00
3978
1965-01-01
000020
Mike
Thomson
B01
3476
1973-10-10
000030
Sally
Kwain
C01
4738
1975-04-05
000060
Irving
Stern
D11
6423
1973-09-14
...
000050
John
Geyer
E01
6542
1972-02-02
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
000010
SELECT LASTNAME
FROM EMP
WHERE EMPNO = 000050
QMF / SPUFI
A00
B01
C01
D11
...
D01
000010
000020
000030
000060
...
CV8317.0
Notes:
cl
Most of you have attended a SQL DML course. During the machine labs, you were asked
to code SQL statements and to execute them. You learned the syntax of the Data
Manipulation Language (DML) part of the SQL language. While you were practicing, you
were DB2 users.
Ex
How does a DB2 user "see" the world around them? They see tables and issue SQL DML
statements against those tables. They do not see other programs; they do not have to
worry about data integrity and so on.
pr
The interactive user communicates with a dynamic program. Such programs don't have
complete built-in SQL statements, but ask for the SQL statements (or parts of SQL
statements, such as a WHERE clause) at execution time. Once the complete statement is
known, it will be passed on to DB2 for access path selection and then executed.
The access path could not be determined at program preparation time, since some
fundamental part of the statement, or the entire statement, was unknown until execution of
the program.
Unit 1. Introduction
1-5
Student Notebook
Haas
A00
3978
1965-01-01
000020
Mike
Thomson
B01
3476
1973-10-10
000030
Sally
Kwain
C01
4738
1975-04-05
000060
Irving
Stern
D11
6423
1973-09-14
...
000050
John
Geyer
E01
6542
1972-02-02
GET
MOVE
...
...
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
000010
A00
000010
B01
C01
D11
...
D01
Planning
Information Center
Manufacturing System
000020
000030
000060
Development Center
SELECT LASTNAME
FROM EMP
WHERE EMPNO = :EMPNO
...
EMP NR ?
EMP NR ?
LASTNAME :
000050
Geyer
CV8317.0
Notes:
cl
Besides interactive (or dynamic) users who enter SQL statements at their terminals, DB2
also supports static users. The static users communicate with static programs. These users
use specific programs (transactions) to perform specific actions on DB2 data, such as
getting information about a customer, making a flight reservation, modifying product data,
and so on.
pr
Ex
These users need not know SQL, and might not even be aware that they access data
stored in DB2. The programs with which they work do not expect SQL statements to be
entered from a terminal. Such programs contain built-in or embedded SQL statements that
were written into the program when it was written. The access path is determined at
program preparation time. At execution time, those SQL statements will be executed by
loading the needed access paths in storage and completing the missing variables with
input from a data set or terminal.
Typically, static programs will be used in transactional or batch environments where the
same data access logic has to be executed over and over again. The only information to be
1-6
V5.4.0.3
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
provided at execution time will be the actual employee number or a specific product
number, but the access logic will always be the same.
pr
Ex
cl
Uempty
Unit 1. Introduction
1-7
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
UPDATE A
COMMIT
A
B
2000 1000
3000 3000
1000
4000
UPDATE A
1000
4000
UPDATE B
ROLLBACK
A
B
2000 1000
3000 3000
1000
4000
2000
3000
CV8317.0
Notes:
A major responsibility of any program is to group its statements in Logical Units of Work
(LUW).
Ex
cl
A program must tell the system when a LUW starts and ends. This is part of the business
logic. The typical banking transaction (debit/credit) doesn't make sense unless it moves
money from one account to another, thereby updating both accounts. It must tell DB2 by
COMMITing that a LUW has successfully ended, or by ROLLBACKing that the entire LUW
should be backed out.
pr
The actual statement used to COMMIT depends on the execution environment of the
program: EXEC SQL COMMIT under TSO or batch; EXEC CICS SYNCPOINT under
CICS; GU IOPCB or CHKP calls under IMS.
DB2 uses the term Unit of Recovery for LUW.
1-8
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Utility
Utility
WebSphere
Application
Server
TSO
BATCH
CICS
DB2
IMS-TM
(aka IMS-DC)
Trxb
Trxa
Prog2
Appl2
Appl1
Trx2
Trx1
Prog1
CV8317.0
Notes:
There are many different ways that DB2 data can be accessed concurrently such as:
User-written applications
cl
Ex
DB2 utilities
pr
The visual shows the allied (or user application) address spaces where application
programs run. The dashed lines between the allied address spaces and DB2 are
connections or threads. Think of them as pipelines through which any kind of DB2 request
must flow; that is true for SQL, commands, DSN subcommands, utilities, and so forth. To
communicate with DB2, allied address spaces must have an authorized connection.
A DB2 subsystem is composed of the following address spaces:
System Services or MSTR (mandatory)
Database Services or DBM1 (mandatory)
Internal Resource Lock Manager (IRLM) (mandatory)
Copyright IBM Corp. 1993, 2011
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Unit 1. Introduction
1-9
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Distributed data is data that resides on a DB2 system other than your local DB2 system.
Your local DB2 system is the one on which you bind (see later). All other DB2 systems are
remote.
When you request services from a remote DB2 system, the remote DB2 system is a server
and your local DB2 system is a requester or client. A remote server can be many miles
away or it can run under the same operating system as the local client. The remote server
can be another DB2 for z/OS system or another member of the DB2 Family.
pr
Ex
cl
Although only DB2 systems are mentioned above, you should note that any kind of
relational DRDA system is able to connect to a DB2 for z/OS system.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Utilities
COPY
IMAGE COPY
SEQUENTIAL
DATA SET
LOAD
REORG
Run in BATCH
CV8317.0
Notes:
cl
Utilities are "special" programs that can be run by submitting JCL (or calling the DSNUTILS
or DSNUTILU stored procedure). They run in batch and get their control information via
control statements (not SQL).
pr
Ex
Utilities are used, for example, to take backups of tables (COPY utility), to reorganize table
data (REORG), or to load them with data from sequential data sets (LOAD utility).
Unit 1. Introduction
1-11
Student Notebook
DB2
UPDATE
T1 SET ...
Pierre
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
STOP
SELECT ...
FROM T1
OK
Jan
GRANT SELECT
ON TABLE T1
TO JAN, PIERRE ;
GRANT UPDATE ON
TABLE T1 TO JAN ;
Table
T1
T1
DB2 CATALOG
User
Select
Jan
Pierre
Y
Y
Update
Y
N
CV8317.0
Notes:
cl
Every SQL call, every utility execution, and every command execution must be authorized,
otherwise access will be denied. Security definitions are made using the GRANT and
REVOKE SQL statements.
pr
Ex
The resulting security definitions are stored in the DB2 catalog (set of tables containing
DB2's own control information).
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Prog1
UPDATE
EXCLUSIVE
LOCK
Row
STOP
..
.
RELEASE
LOCK
SELECT
OK
SHARED
LOCK
COMMIT
CV8317.0
Notes:
Step 1
Ex
cl
While this data is being modified within a logical unit of work (LUW) it is potentially
inconsistent and must therefore be hidden from all other processes until program 1s
update completes with a COMMIT or ROLLBACK.
To protect the modified data from all other processes DB2 automatically uses a locking
mechanism. DB2s locking mechanism is covered in detail in a later unit. For now, here is a
brief introduction.
pr
Before allowing a program to access a piece of data, DB2 may ask for a shared lock (if the
program only reads the data), or an exclusive lock (if it also modifies the data) on behalf of
that program.
If no one is using the data, a lock will be granted. If another program already has a shared
lock on the data, only other shared locks will be granted. If another program already has an
exclusive lock on the data, no other locks will be granted.
Copyright IBM Corp. 1993, 2011
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Unit 1. Introduction
1-13
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Step 3
DB2 releases the exclusive lock and the committed update is now visible to other
programs.
Step 4
DB2 "wakes up" program 2, acquires a shared lock on the data and allows program 2 to
read the committed update. Other programs will be able to read the same data
concurrently.
SELECT.........WITH UR
Some types of request don't really worry about integrity of the data.
pr
Ex
cl
An example is the business analyst who wants to know how much of product Y was
already sold. He just needs a ballpark figure. He can specify the request using
uncommitted read (UR) isolation, and is fully aware that he can potentially receive
uncommitted data. If he were to run the query with the standard integrity, he could have
problems running concurrently with transactions that add new sales to the table. In this
case, the waiting would be inappropriate as the analyst just needs an approximate number.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Data Buffer
DATA
ASYNC
Program
UPDATE
INSERT
Log Buffer
COMMIT
LOG
DATA SET
XXX
SYNC
X X X
before
after
CV8317.0
Notes:
cl
When a program modifies DB2 data, this actually happens in storage, in the data buffers.
Writing those changes to disk can happen at any time, before or after the changes are
COMMITted or ROLLBACKed. The only synchronous process is the I/O to the log data set
at COMMIT/ROLLBACK time.
pr
Ex
The log data set is the master copy of the data, and enables DB2 to recover from whatever
might happen in the system.
Unit 1. Introduction
1-15
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
COPY/RECOVER utilities
UPDATE
UPDATE
RECOVER
DELETE
Log Time
COPY
DISK
CRASH
IMAGE COPY
CV8317.0
Notes:
Ex
cl
The basic principle is very straightforward; every once in a while, an image copy will be
taken of the data (when somebody submits a COPY job). In the meantime, DB2 continues
recording all changes on the log data set. If, for some reason, data was destroyed by a
non-DB2 disaster (disk crash, for example), the DB2 RECOVER utility will be able to
reconstruct the data by restoring the image copy and then applying all log records that
were created since the copy was taken.
pr
For DB2 to be able to recover data, changes to data must be copied to an image
copy data set (using the COPY utility), or be logged (always happens with SQL;
some utilities can bypass), or both.
V5.4.0.3
Student Notebook
Uempty
DB2
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
BIND
WHAT
HOW
Optimizer
Load
Module
EXECUTION
Access
Path
Catalog
Statistics
CV8317.0
Notes:
The system will have to find out HOW to get to the data the most efficient way.
Ex
cl
The traditional program compilation process will have to perform some additional work if
SQL statements are included in the program. The most important one is called the BIND,
during which DB2 will do access path selection.
pr
BIND will create an executable access path that will be stored in DB2's control data sets.
Later, at execution time, DB2 will simply "load" that access path into storage and execute it.
Unit 1. Introduction
1-17
Student Notebook
Application
Programmer
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
System
Administrator
SQL
DB2
CATALOG
Data
Definitions
Security
Definitions
Recovery
Information
CV8317.0
Notes:
cl
It therefore stores all metadata concerning objects and programs, in its catalog and
directory.
Ex
The Catalog
This is a set of DB2 tables at the heart of the DB2 system used by both the DB2 system
and authorized SQL users
pr
Data definitions of tables, views, columns, indexes etc. These definitions are used, for
example, by DB2 when processing SQL
V5.4.0.3
Student Notebook
Uempty
Statistical data such as the number of rows in tables, key ranges etc. These statistics
are updated by the RUNSTATS utility and they are used by the DBA to decide, for
example, whether to REORG and they are used by DB2, for example, to choose the
best access path to data.
You can query the catalog tables with SQL
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
You can access parts of the directory using commands. For example, you can monitor the
status of a utility as it runs.
pr
Ex
cl
Unit 1. Introduction
1-19
Student Notebook
z/OS
DB2
DB2
DB2
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
z/OS
z/OS
z/OS
SHARED
DASD
DB2
DB2
z/OS
z/OS
z/OS
DB2
DB2
DB2
Improved availability
Huge queries feasible
SYSPLEX hardware required
Single shared catalog
CV8317.0
Notes:
The data sharing function of DB2 for z/OS enables applications that run on more than one
DB2 for z/OS system to read from and write to the same set of data concurrently.
Ex
cl
DB2 for z/OS systems that share data must belong to a DB2 data sharing group which runs
on a Parallel Sysplex. A Parallel Sysplex is a cluster of z/OS systems that communicate
and co-operate with each other.
Each DB2 for z/OS system that belongs to a particular data sharing group is a member of
that group. All members use the same shared catalog.
You dont need to change the SQL in your applications to use data sharing.
pr
The primary advantage of data sharing is improved availability. If one system goes down
temporarily then you can continue to access the same DB2 data via the other members.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Security
Administrator
Operations
System
Administration
Functional
Designer
Data
Administrator
Capacity
Planner
Technical
Designer
Database
Administrator
z/OS
Sysprog
CICS/IMS
Sysprog
Programmer
TP
Specialist
CV8317.0
Notes:
cl
In development:
pr
Ex
Unit 1. Introduction
1-21
Student Notebook
In production:
Support the applications as they are put into production
Manage the data resources and programs (compilations)
Tune the database objects using the DB2 utilities such as REORG and RUNSTATS
Cooperate with the development DBAs to define recovery and security procedures
Be responsible for loading data into the production tables
Be responsible for adhering to the security procedures established with the
development DBA and the SYSADM
- Ensure that all system problems are correctly reported, and that fixes do not impact
the production environment.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
System administrators:
pr
Ex
cl
Are responsible for monitoring and controlling the DB2 subsystem operation and
resources
Provide information to capacity planners
Define and adjust system parameters
Tune DB2 at the system level
Define acceptable performance criteria
Are responsible for directing the DB2 operator and system programming activities
Are responsible for directing DB2 Recovery, data integrity, and data security
Are responsible for the backup and maintenance of the DB2 catalog
Define naming standards for programs, data objects, and so on.
Install, update, and migrate DB2 subsystems
Generally serve as the help desk, as a last resort.
V5.4.0.3
Student Notebook
Uempty
Unit summary
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Having
Havingcompleted
completedthis
thisunit,
unit, you
youshould
shouldbe
beable
ableto:
to:
Describe
Describethe
themain
maincomponents
componentsof
ofaaDB2
DB2environment
environmentand
and
the
theroles
rolesthey
theyplay:
play:
Users
Users
Data
Data
SQL
SQLstatements
statements(static,
(static,dynamic)
dynamic)
System
System
CV8317.0
pr
Ex
cl
Notes:
Unit 1. Introduction
1-23
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Student Notebook
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Machine exercises
GC19-2985
SC19-2968
SC19-2983
GC18-9856
SC18-9840
SC18-9854
GC18-7428
SC18-7413
SC18-7426
pr
Ex
cl
References
2-1
Student Notebook
Unit objectives
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
After
Aftercompleting
completingthis
thisunit,
unit,you
youshould
shouldbe
beable
ableto:
to:
Describe
Describethe
theDB2
DB2objects
objects that
thatmake
makeup
upaaDB2
DB2database
database
Select
Selectparameters
parametersfor
forthese
theseobjects
objectsso
sothat
thatthey
theyare
are
implemented
implementedwith
withthe
themost
mostappropriate
appropriateattributes
attributes
Create
Createstorage
storagegroups,
groups,databases,
databases,table
tablespaces,
spaces,tables,
tables,
views,
views,synonyms,
synonyms,aliases,
aliases,materialized
materialized query
query tables,
tables, and
and
indexes
indexes
Alter
Alter the
the attributes
attributesof
ofDB2
DB2database
databaseobjects
objectsas
asrequirements
requirements
change
change over
over time
time
Describe
Describehow
howdata
dataisisstored
storedin
inaaDB2
DB2database
database
CV8317.0
pr
Ex
cl
Notes:
2-2
V5.4.0.3
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
Uempty
2-3
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
...
Hi-level
qualifier
Hi-level
qualifier
Table
Database
Table Space
Table
Views
and
MQTs
Table
Table
SELECT * FROM T1
Index
Space
Table
Index
V_LONG_TABLE_NAME
CV8317.0
Notes:
This is an introduction to storage groups, databases, table spaces, tables, indexes (in
index spaces), views, synonyms, aliases, and materialized query tables (MQT).
cl
Database
Ex
Table space
- An area of external storage used to store the records of one or more tables
pr
Index space
Index
- An ordered set of key values and pointers to the rows containing those keys
2-4
V5.4.0.3
Student Notebook
Uempty
Storage group
- A set of volumes from which DB2 can assign space for table spaces and index
spaces
View
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
- A virtual table defined by a select statement that identifies some or all of the rows /
columns of one or more tables
Synonym
- A private nickname for a local table, local view, or local materialized query table
Alias
- A system-wide nickname for a table, view, or materialized query table. Can be used
for local or remote objects
pr
Ex
cl
- A materialized query table is a table that contains pre-computed data from other
tables / views
2-5
Student Notebook
Hi-level
qualifier
SYSSTOGROUP
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Create
Stogroup
DB2 CATALOG
DSNDB06
Create
Database
Create
Tablespace
Database
Table Space
SYSDATABASE
SYSTABLESPACE
SYSTABLEPART
Create
Table
VSAM
CATALOG
SYSTABLES
SYSCOLUMNS
Table
Index
Space
Create
Index
Index
Create
View
Create
Synonym
Create
Alias
SYSINDEXES
SYSINDEXPART
SYSVIEWS
SYSSYNONYMS
SYSTABLES
CV8317.0
Notes:
cl
You use the SQL language to create, alter, and drop DB2 objects such as storage groups,
databases, table spaces, tables, indexes, views, materialized query tables, aliases, and
synonyms.
Ex
DB2 automatically records information about the DB2 objects you set up or change in one
or more of the DB2 catalog tables. In some cases, information is also recorded in the
VSAM catalog. When you drop a DB2 object, the information is automatically removed.
Some of the catalog tables are shown in the visual. All of the DB2 catalog table names are
prefixed with SYSIBM.
pr
For a complete description of the DB2 catalog tables and their contents, you should refer to
the SQL Reference manual.
These objects can be set up using SPUFI, QMF, DSNTEP2, DSNTEP4, etc.
2-6
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
BART
BART.EMP
CV8317.0
Notes:
cl
For example, a DB2 table has an owner and is recorded in the OWNER column of the DB2
catalog table SYSIBM.SYSTABLES.
Ex
The complete table name has three qualifiers: first the location, second the schema name,
and last the table name. When the table is at your local DB2 subsystem, you do not have to
specify the location.
pr
2-7
Student Notebook
Owners privileges
SELECT
FROM EMP ;
GRANT SELECT
ON EMP
TO LISA
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SELECT ...
FROM BART.EMP
BART
LISA
BART.EMP
CV8317.0
Notes:
cl
An owner is a concept that relates to DB2 security. The owner of an object has the right of
life and death over that object. The owner can use, modify, or destroy the object which they
own. The owner can also decide who else should be able to work with the object.
pr
Ex
The visual illustrates this by showing how table owner (Bart) grants the SELECT privilege
on his table to another person (Lisa).
2-8
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DATABASE
TABLESPACE
Qualifies
SCHEMA
TABLE
Qualifies
COLUMN
VIEW
SYNONYM
ALIAS
INDEX
Qualifies
DATABASE
INDEXSPACE
CV8317.0
Notes:
The DATABASE name and STORAGE GROUP name must be unique within the DB2
system. For other objects indicated above, the qualified name must be unique.
Ex
cl
SCHEMA qualifies the object and by default is the same as the object owner (authorization
ID). The relevant columns In the DB2 catalog are CREATOR and OWNER. Some catalog
tables have only CREATOR column and some have both CREATOR and OWNER
columns.
Begin all object names with an alphabetic character (A-Z, #, $, @) followed by any
alphanumeric characters (A-Z, #, $, 0-9, @, _)
pr
The name for a database and table space can be up to a maximum of 8 characters. The
name of other objects on the visual can be up to a maximum of 128 characters. Index
space name is generated by DB2 when the index is created.
2-9
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Student Notebook
V5.4.0.3
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
Uempty
2-11
Student Notebook
DB2
storage
group:
hlq1
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Purpose:
Simplify VSAM data set creation
by specifying:
high-level qualifier
list of volumes
-
Tablespace
Table
Syntax:
SMS
nonSMS
-- also possible:
CREATE STOGROUP SG1 VOLUMES(*) VCAT hlq1
DATACLAS(dc1) MGMTCLAS(mc1) STORCLAS(sc1);
...
VOL001
VOL002
...
Default: SYSDEFLT
CV8317.0
Notes:
cl
Defines, extends, alters and deletes the necessary VSAM data sets
Ex
The volumes are associated with an ICF catalog or VCAT. The ICF catalog stores entries
for all data sets that DB2 creates on behalf of a storage group.
VCAT
pr
The VCAT parameter is used to specify the ICF high-level qualifier of the catalog name or
alias.
SYSDEFLT
SYSDEFLT is the default DB2 storage group and is created when DB2 is installed. It is
used whenever a storage group is not explicitly specified in CREATE DATABASE,
CREATE TABLESPACE, or CREATE INDEX.
V5.4.0.3
Student Notebook
Uempty
The two main options for managing DB2 data sets in DB2 storage groups are:
1. SMS-managed DB2 data sets
When defining DB2 storage groups, use the VOLUMES(*) attribute on the CREATE
STOGROUP statement to let SMS (a z/OS component) control the selection of volumes
during allocation.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
If your SMS setup (more precisely the SMS ACS routines written by your SMS
administrator) is such that DB2 data is SMS-managed, the volumes in the DB2 storage
groups may be overridden by SMS at allocation time. If this is the case, it is strongly
recommended to define your storage groups with VOLUMES(*) to avoid having
volumes in catalog table SYSIBM.SYSVOLUMES that do not correspond to the
volumes that are really used to store DB2 data sets.
SMS ACS routines could be written in the following way:
- If VOLUMES(*) is used, SMS assigns a volume.
- If VOLUMES(volser,...) is used, SMS tries to allocate the DB2 data set on the
specified volumes.
Note
The data class, management class and storage class for these SMS managed data sets
are specified in SMS ACS routines. You can also specify these classes in the
CREATE STOGROUP statement. There are three keywords in the syntax. You can specify
just one, two or all three of them in the CREATE STOGROUP statement:
DATACLAS dc-name
cl
SMS data classes influence characteristics such as the data set control block (DCB),
striping, extended format usage, extended addressability usage and so on.
Ex
MGMTCLAS mc-name
Management classes define the data sets frequency of volume backups, migration
requirement and things like that.
pr
STORCLAS sc-name
Among other things, storage classes can for example define guaranteed space
requirements.
2-13
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
A volume assigned to a storage group is not dedicated to it. Non-DB2 data sets can be
allocated on the volume and a volume can be assigned to more than one storage group.
The maximum number of volumes that can be managed for a storage group is 133. The
volumes must be of the same device type. The maximum number of volumes that can be
allocated per data set is 59.
pr
Ex
cl
V5.4.0.3
Student Notebook
Uempty
DB2 database
Default Storage Group
Database
Table Space
VSAM
LDS
VSAM
LDS
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Table Space
Table
Default BUFFERPOOL
BP1
Table
Table Space
Index
Space
Default INDEXBP
BP2
Index
Table
Index
Space
Index
Space
Default
Encoding Scheme
Index
SpaceIndex
Index
ASCII,
EBCDIC,
Unicode
Index
CV8317.0
Notes:
A database is a collection of table spaces and index spaces grouped together for
organizational purposes and operational convenience.
cl
It is recommended that you organize databases by application or by, say, QMF user.
Ex
pr
The defaults for the database may be overridden for the individual objects defined in the
database. But note that an index always has the same encoding scheme as its associated
table and that all tables in a table space always have the same encoding scheme.
Copyright IBM Corp. 1993, 2011
2-15
Student Notebook
The STOGROUP, BUFFERPOOL, INDEXBP, and CCSID parameters can be altered using
ALTER DATABASE.
ALTER DATABASE DB1
STOGROUP SG3
BUFFERPOOL BP4
INDEXBP BP6;
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DSNDB04
pr
Ex
cl
V5.4.0.3
Student Notebook
Uempty
Program
BP2
BP8K2
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Long Row
SELECT...
...
Short Row
SELECT...
BUFFERPOOL BP2
CI_1
CI_2
CI_3
CI_4
TS
1
CI_5
CI_6
CI_7
CI_8
TS2
CI_9
CI_1
CI_3
CI_A
CI_B
CI_C
CI_2
CI_4
CI_5
CI_5
CI_6
BUFFERPOOL BP8K2
CV8317.0
Notes:
Buffer pools are areas of virtual storage that temporarily store pages of:
cl
Ex
Indexes.
pr
When an application program accesses a row of a table, DB2 places the data page that
contains that row in a buffer. If the requested data is already in a buffer, the application
program does not have to wait for it to be retrieved from disk. Avoiding the need to retrieve
data from disk results in faster performance.
The data remains in the buffer until DB2 decides to use the space for another page. Until
that time, the data can be read or changed without a disk I/O operation.
If the row is changed, the data in the buffer must be written back to disk eventually. But that
write operation might be delayed until, for example, a time determined by DB2.
2-17
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
cl
Up to 50 buffer pools called BP0, BP1, BP2,........, BP49 which contain 4 KB buffers and
Ex
Up to 10 buffer pools called BP8K0, ..... , BP8K9 which contain 8 KB buffers and
Up to 10 buffer pools called BP16K0, ...... , BP16K9 which contain 16 KB buffers and
pr
Do not confuse BP32 which contains 4 KB pages with BP32K which contains 32 KB pages.
V5.4.0.3
Student Notebook
Uempty
You can set the size of each of these buffer pools (and many other buffer pool parameters)
separately using the -ALTER BUFFERPOOL command.
DB2 stores buffer pool attributes in a DB2 data set called the DB2 bootstrap data set
(BSDS).
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
You may choose a single 4 KB buffer pool, say, for a test DB2 system or you may choose
more than one buffer pool, say, to isolate indexes from data.
Whether you choose to set up one or many 4 KB buffer pools, DB2 also requires you to
define BP32K because some SQL operations, such as joins, can create a result row that
does not fit into a 4 KB page. You must also have BP0, BP8K0 and BP16K0 defined for the
catalog.
Assigning a table space or index to a buffer pool
- Can specify the default buffer pool to be used for table spaces and indexes created
in the database.
A default buffer pool for user data and one for user indexes can also be specified at install
time.
The buffer pool is actually allocated the first time a table space or index assigned to it is
opened.
DB2 catalog uses buffer pools BP0, BP8K0, BP16K0, and BP32K.
Choosing a buffer pool for user data and indexes other than those used by the Catalog is a
good idea. It is much more difficult to monitor and tune if user data and indexes share the
same buffer pools.
cl
Ex
The size of a data page or an index page is determined by the buffer pool to which you
assign the table space or index. For example:
A table space has 4 KB data pages when it is defined in a 4 KB buffer pool and 8 KB
data pages when it is defined in an 8 KB buffer pool.
pr
Table spaces and indexes can be in 4 KB, 8 KB, 16 KB, or 32 KB buffer pool.
A good starting point is to use 4 KB page sizes when access to the data is random and only
a few rows per page are needed. If row sizes are very small, using the 4 KB page size is
recommended.
As a row cannot be split over two or more pages, the page size must be able to contain at
least one row.
2-19
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
This is controlled with a DSNZPARM parameter DSVCI. With the default, a DB2managed
data set is created with a VSAM control interval that corresponds to the size of the buffer
pool that is used for the table space. Otherwise, you can specify that a DB2managed data
set is always created with a fixed VSAM CI of 4 KB, regardless of the size of the buffer pool
used for the table space.
TS Page Size
4 KB
8 KB
16 KB
32 KB
Default CI Size
4 KB
8 KB
16 KB
32 KB
Compatible CI Sizes
4 KB
4 KB, 8 KB
4 KB, 16 KB
4 KB, 32 KB
For example, a table space with pages 16 KB in size can have a VSAM CI of 4 KB or
16 KB.
Control interval sizing has no impact on indexes. VSAM data sets for indexes have always
a CI size of 4 KB.
One of the biggest benefits of this change is an improvement in query processing
performance.
ALTER BUFFERPOOL command
Authorized users can change the sizes and other characteristics of a buffer pool at any
time while DB2 is running, by using the ALTER BUFFERPOOL command.
DISPLAY BUFFERPOOL command
pr
Ex
cl
Authorized users can display the current status of one or more buffer pools by using the
DISPLAY BUFFERPOOL command.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
ASCII
UNICODE
CV8317.0
Notes:
cl
Ex
Multinational corporations
e-commerce
Goal is to provide:
pr
Single, unique definition (code point) for every character in the world
2-21
Student Notebook
Table Space
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Table
Table
Table Space
Tablespace
Index
Space
Index
Table
Index
Table
Space
Index
Space
Index
Index
Space
Index
Index
CV8317.0
Notes:
pr
Ex
cl
If you have DBADM authority, you can create, alter, and drop objects in the database, use
utilities on the objects, and execute commands on the objects such as DISPLAY, STOP,
and START.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Table space
Table
CV8317.0
Notes:
cl
DB2 tables are stored in z/OS data sets. A table space serves as a means to specify within
DB2 how this logical structure of a table should be mapped to the physical structure of a
data set. Therefore, a table is related to (resides in) a table space which in turn controls
(resides in) a data set. To be precise, many tables can be stored in a table space, or a table
space can be stored in many data sets.
pr
Ex
Typically, you do not define these data sets by yourself. Instead, when you create a table
space in DB2, you define, for example, how many data sets DB2 should define for a given
table, perhaps where these data sets should reside, how much data should be transferred
by a single I/O operation, and other parameters (like locking, freespace) on how DB2
should process the data.
DB2 only uses VSAM LDS (Virtual Storage Access Method Linear Data Set) data sets for
your table spaces.
Table spaces are divided into pages, and you can specify a page size of 4, 8, 16, or 32 K.
For the table spaces we discuss in this course, a table row must completely fit into one
page. Thus, only large table rows require page sizes of more than 4 K.
Copyright IBM Corp. 1993, 2011
2-23
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
cl
You need to decide which type of table space is best for each of your tables. When you
have made a decision, there are then many parameters to select so that your table spaces
are managed by DB2 in the best way.
Ex
It is always best to explicitly specify parameters when you create a table space, otherwise
defaults apply (including which type of table space is used), and these may not always be
the best for your table spaces.
pr
Since V9, this type of table space cannot be created any more. But because existing
simple table spaces can still be used and as its internal structure is used in classic
partitioned table spaces, this type is described here. A simple table space can hold one or
more tables. A page can contain rows from different tables.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Enhanced version of a segmented table space in that it can hold more data and offers
many advantages due to partitioning (similar to classic partitioned table space or
UTS PBR). Can only hold one table.
Enhanced version of a classic partitioned table space. Each partition has the internal
structure of a segmented table space (whereas each partition of a classic partitioned table
space has the structure of a simple table space).
pr
Ex
cl
You can store large movies, pictures, text documents (so called large objects, LOBs) or
XML documents in specialized table columns with a data type of LOB or XML. Because the
values in those columns do not fit into one page, such objects are stored in separate, either
LOB or XML table spaces. Depending on its size, a LOB can now reside completely in the
base table space along with other non-LOB columns. They are explained in other courses.
2-25
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Dept Table
Employee Table
Data Page
Space
Map
Header
=DEPT Row
=EMPLOYEE Row
CV8317.0
Notes:
cl
Rows from multiple tables can be interleaved on each data page. For example, a row from
the DEPT table can be followed by multiple rows from the EMPLOYEE table. This would
result in more efficient processing if you wanted to select, say, rows from each department
table together with the corresponding rows from the employee table.
Ex
The interleaving of rows is something you would need to set up and manage. DB2 does not
maintain the interleaving for you during SQL INSERT.
For a simple table space with multiple tables:
pr
A table space scan scans rows of all tables, even if you are interested in the rows of
only one table.
There is no lock at the table level.
REORG is needed to reclaim space after DROP TABLE.
If your installation has been using DB2 for many years, you may find some simple table
spaces still in use.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Segment 3
Segment 2
Data Page
Space Map
Segment 1
Header
=DEPT Row
=EMPLOYEE Row
CV8317.0
Notes:
cl
A segmented table space is divided into SEGMENTS. A segment can consist of 4, 8, 12,
16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, or 64 pages. You specify the segment size
with the SEGSIZE parameter. Your choice of SEGSIZE should be determined by the table
size. The SEGSIZE applies to all tables in the table space. Each segment is dedicated to a
table.
Ex
A table space scan scans only the segments of the table being processed.
You can specify a lock size of TABLE (see later).
pr
The space map is used to avoid scanning empty segments and segments containing only
deleted rows.
When you take a full image copy, the COPY utility does not copy empty pages due to a
dropped table or mass delete.
2-27
Student Notebook
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Detailed information is available in the space map to locate free space for variable length
rows and to maintain the clustering sequence (see later).
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Multiple
CV8317.0
Notes:
With single-table table spaces you can give each table its own attributes.
cl
Utilities, such as REORG, RUNSTATS, COPY, and RECOVER, operate at the table space
or partition level. With single-table table spaces, you can run these utilities for individual
tables.
pr
Ex
With multiple-table table spaces, you need to set up and run fewer utilities, and it can be
easier to keep related tables in step.
2-29
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
cl
The USING STOGROUP clause specifies the storage group that is used for space
allocation of the table space.
Ex
pr
You can execute the CREATE TABLESPACE statement without specifying the SEGSIZE
clause successfully. However, what is created is not a simple table space, but a segmented
table space with SEGIZE 4.
If you execute the CREATE TABLESPACE statement without specifying the IN clause, the
table space is created in the default database DSNDB04.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
2 GB
2 GB
2 GB
GB
64 GB
CV8317.0
Notes:
cl
If more data is added to its tables, DB2 automatically creates a continuation data set, if
PRIQTY and SECQTY are big enough.
pr
Ex
But DB2 does not allocate more than 32 data sets for a segmented table space, thus the
maximum size of a segmented table space is 64 GB.
2-31
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
EMPLOYEE Table
(Partitioned by EMPNO)
Data Partition 1
Data Page
Space Map
Header
Data Partition 2
Data Partition 3
Data Page
Space Map
Header
Data Partition 4
Data Page
Space Map
Header
CV8317.0
Notes:
pr
Ex
cl
A classic partitioned table space contains one table only. It is divided into parts (partitions).
Each partition is stored in a separate VSAM linear data set. CREATE TABLE specifies the
partitioning key and the partition boundary values.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
The CREATE TABLESPACE statement in the visual creates a classic partitioned table
space with four partitions. This is shown by the NUMPARTS parameter.
cl
The storage group, space allocation, and free space required for all partitions are also
specified.
Ex
However, partitions 1 and 2 have been named separately. Different storage group, space
allocation, free space, and compression parameters have been specified explicitly for
these two partitions.
pr
In DB2 10, if SEGSIZE is not specified, the value of DSNZPARM DPSEGSZ applies. If it is
set to 0, a classic partitioned table space is created. If the DPSEGSZ value is greater than
0, a partition-by-range universal table space is created.
In DB2 9, SEGSIZE 0 is invalid and creating a partitioned table space without specifying
SEGSIZE clause results in a classic partitioned table space.
The definition of the table space is incomplete until the partitioning key and partition
boundaries are specified in the CREATE TABLE statement.
Copyright IBM Corp. 1993, 2011
2-33
Student Notebook
Advantages of partitioning
Good for large volumes of data
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Partition scan
CV8317.0T
Notes:
pr
Ex
cl
There are many reasons why you would choose a partitioned table space to implement
your data, and they are listed in the visual.
V5.4.0.3
Student Notebook
Uempty
1-16
Maximum size
per partition
4 GB
Maximum size
of table space
64 GB
17-32
2 GB
64 GB
33-64
1 GB
64 GB
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
NUMPARTS
65-254
[n=1,2,4,8,16,32, or 64],
Figure 2-23. Classic partitioned table space - Maximum size for NUMPARTS < 255
CV8317.0
Notes:
cl
You may specify the maximum data set size per partition (DSSIZE option in the
CREATE TABLESPACE statement). If you specify (or DB2 chooses) a DSSIZE value
greater than 4G, the data sets for the table space must be associated with a DFSMS data
class that has been specified with extended format and extended addressability.
Ex
For a classic partitioned table space, if DSSIZE is omitted, the default for the maximum
size for each partition depends on the value of NUMPARTS. For example, if NUMPARTS is
65 to 254, the default partition size is 4 GB.
pr
If you specify NUMPARTS 254 and DSSIZE 64G, the classic partitioned table space can
grow up to almost 16 TB.
2-35
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Page size
4K
8K
16 K
32 K
1-4 GB
4096
4096
4096
4096
8 GB
2048
4096
4096
4096
16 GB
1024
2048
4096
4096
32 GB
512
1024
2048
4096
64 GB
256
512
1024
2048
CV8317.0
Notes:
If you specify NUMPARTS > 254, but do not specify DSSIZE, DB2 chooses DSSIZE
according to the rule: DSSIZE = n GB if page size = n KB (for n = 4, 8, 16, or 32).
pr
Ex
cl
According to the above table, if you rely on the default DSSIZE, you can specify up to 4096
partitions for any page size.
V5.4.0.3
Student Notebook
Uempty
Max TS size
4 TB
16 TB
16 TB
16 TB
16 TB
16 TB
4 TB
32 TB
32 TB
64 TB
64 TB
128 TB
128 TB
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Page size
4 KB
4 KB
4 KB
4 KB
4 KB
4 KB
8 KB
8 KB
8 KB
16 KB
16 KB
32 KB
32 KB
Figure 2-25. Classic partitioned table space - Maximum size for NUMPARTS > 254
CV8317.0
Notes:
cl
Previously, we saw the maximum size of a classic partitioned table space with up to 254
partitions. This table lists the maximum size of a classic partitioned table space with more
than 254 partitions. This maximum size depends on the page size, the DSSIZE and the
number of partitions (remember, the maximum number of partitions itself depend on page
size and DSSIZE).
pr
Ex
For example, reaching a table space size of 128 TB with the maximum DSSIZE of 64 GB is
possible only with 32 K page and 2048 partitions.
2-37
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
Ex
cl
pr
V5.4.0.3
Student Notebook
Uempty
The example on the visual creates a partition-by-range universal table space, USTS5, in
database USDB1 using storage group USCV831S. The table space has 64 pages per
segment and has 10 partitions.
In DB2 10, if SEGSIZE is not specified, the value of DSNZPARM DPSEGSZ applies. If it is
set to 0, a classic partitioned table space is created. If the DPSEGSZ value is greater than
0, a partition-by-range universal table space is created.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
In DB2 9, SEGSIZE 0 is invalid and creating a partitioned table space without specifying
SEGSIZE clause results in a classic partitioned table space.
Note
pr
Ex
cl
With a partition-by-range universal table space, you can also control the partition size,
choose from a wide array of indexing options, and take advantage of partition-level
operations and parallelism capabilities. You can immediately reuse all or most of the
segments of a table after a mass delete has been performed.
2-39
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
What is it?
DSSIZE
4 KB Page
8 KB Page
16 KB Page 32 KB Page
1 4 GB
4096
4096
4096
4096
8 GB
2048
4096
4096
4096
16 GB
1024
2048
4096
4096
32 GB
512
1024
2048
4096
64 GB
256
512
1024
2048
CV8317.0
Notes:
cl
If you want to have a segmented table space that can hold more than 64 GB, you must use
a Universal Table Space (UTS) of type Partition-by-Growth (PBG).
pr
Ex
Using ALTER TABLESPACE, you can increase, but not decrease the MAXPARTITIONS
value.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
cl
Ex
Begins small as a one-partition table space. Grows only if needed. You do not have to
allocate too much space at the beginning.
Additional partitions may become empty, but are not purged after a ROLLBACK or REORG
or LOAD REPLACE.
pr
2-41
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Create explicitly
CREATE TABLESPACE USTS4 IN USDB1
USING STOGROUP USCV831S
DSSIZE 4G MAXPARTITIONS 24 SEGSIZE 64;
Create implicitly
CV8317.0
Notes:
cl
The first example on the visual creates a partition-by-growth universal table space that has
a maximum size of 4 GB for each partition, 64 pages per segment with a maximum of 24
partitions for the table space.
Ex
Important
pr
It is not possible to use ALTER TABLESPACE to reduce MAXPARTITIONS after the table
space has been created.
Implicit creation: The default type of an implicitly create table space is partition-by-growth.
If you want to influence the DSSIZE of your implicitly created table space, you can do so,
as shown in the second example on the visual, by using the PARTITION BY SIZE clause in
your CREATE TABLE statement. The syntax is PARTITION BY SIZE EVERY integer G. If
you omit the EVERY integer G part of the syntax, the implicit table space defaults to
V5.4.0.3
Student Notebook
Uempty
SEGSIZE 4, Maximum size of each partition 4 GB, and Maximum number of partitions set
to 256.
If the limit of maximum 256 partitions is inadequate, you can increase it using ALTER
TABLESPACE statement. Note as stated earlier, you cannot decrease the value.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Note
pr
Ex
cl
You have the option to partition according to data growth, which enables segmented tables
to be partitioned as they grow, without the need for key ranges. As a result, segmented
tables benefit from increased table space limits and SQL and utility parallelism that were
formerly available only to partitioned tables, and you can avoid needing to reorganize a
table space to change the limit keys.
2-43
Student Notebook
Explicitly specified
MAXPARTITIONS
clause
Explicitly specified
Explicitly specified
Not specified
Not specified
Explicitly specified
Not specified
Not specified
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SEGSIZE clause
DSSIZE = 4 GB
CV8317.0
Notes:
The default type of a table space is segmented when issuing CREATE TABLESPACE
(assuming you have specified neither NUMPARTS, nor MAXPARTITIONS, nor LOB).
pr
Ex
cl
Some applications may issue CREATE TABLE without specifying a table space. In this
case, a table space is automatically created and it is not a segmented, but a
partition-by-growth universal table space with MAXPARTITIONS 256. Note that this value
cannot be decreased, only increased using ALTER TABLESPACE. Therefore, even if you
want a PBG UTS, consider first to create the table space explicitly with a smaller
MAXPARTITIONS value and then specify this table space in your CREATE TABLE
statement in order to prevent run-away applications.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SYSIBM.SYSTABLESPACE
SELECT NAME, DBNAME,CREATOR,BPOOL,PARTITIONS,MAXPARTITIONS,
SEGSIZE,PGSIZE,TYPE,STATUS,IMPLICIT,NTABLES,DSSIZE
FROM
SYSIBM.SYSTABLESPACE
WHERE DBNAME = USDB1
---------+---------+---------+---------+---------+---------+
NAME DBNAME CREATOR BPOOL PARTITIONS MAXPARTITIONS
---------+---------+---------+---------+---------+---------+
USTS1
USTS2
USTS3
USTS4
TB2
USTS5
USDB1
USDB1
USDB1
USDB1
USDB1
USDB1
TSOUD01
TSOUD01
TSOUD01
TSOUD01
TSOUD01
TSOUD01
BP1
BP2
BP1
BP1
BP0
BP1
0
0
4
1
1
10
0
0
0
24
256
0
4
4
4
4
4
4
G
G
R
T
T
T
T
A
T
N
N
N
N
Y
N
0
0
0
0
1
0
0
0
0
4194304
1048576
4194304
CV8317.0
Notes:
cl
Ex
USTS1 and USTS2 are segmented table spaces, because columns PARTITIONS and
MAXPARTITIONS contain zero values, SEGSIZE contains a non-zero value, and TYPE
is blank.
pr
USTS4 and TB2 are partition-by growth universal table spaces, because column TYPE
shows G. For a partition-by growth universal table space, PARTITIONS shows the
current number of partitions and MAXPARTITIONS shows the maximum number of
2-45
Student Notebook
partitions to which it can grow. SEGSIZE shows the segment size and never contains
zero value. For table space TB2, column IMPLCIT shows Y indicating it was created
implicitly.
USTS5 is a partition-by-range universal table space, because column TYPE shows R.
For a partition-by-range universal table space, PARTITIONS and SEGSIZE always
contain non-zero values and MAXPARTITIONS always contains zero value.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
For segmented table spaces USTS1 and USTS2, and classic partitioned table space
UTS3, initially the value is T and later changed to A when tables are created in these
table spaces.
For table space USTS4, initially the value is T, indicating that the definition is
incomplete because no table has been created in this table space. The value changes
from T to A when the table is created.
For table space TB2, the value is A indicating for the implicitly created
partition-by-growth universal table space, the table space is available since the table is
already created. Notice for the implicitly created partition-by-growth universal table
space, SEGSIZE is 4 and MAXPARTITIONS is 256.
For table space USTS5, initially the value is T and later changed to A when a
table-controlled partitioned table is created in the table space.
pr
Ex
cl
Notice the value in DSSIZE shows the maximum size of a data set in kilobytes. A zero
value indicates that the table space is not greater than 64 GB.
V5.4.0.3
Student Notebook
Uempty
Space allocation
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
INSERT, LOAD
CV8317.0
Notes:
cl
The components of the USING STOGROUP clause are discussed below, first for
nonpartitioned table spaces and then for partitioned table spaces. This is applicable to
classic partitioned table spaces and universal table spaces (PBG and PBR).
Ex
For nonpartitioned table spaces, USING STOGROUP indicates the data set for the table
space is defined by DB2. This clause also gives space allocation parameters.
PRIQTY
pr
PRIQTY integer specifies the minimum primary space allocation for a DB2-managed data
set.
1. If you specify PRIQTY with a value other than -1, the primary space allocation is at least
n kilobytes, where n is the value of integer with the following exceptions:
- For 4KB page sizes, if integer is less than 12, n is 12.
2-47
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
- If the TSQTY subsystem parameter value is specified and is greater than 0, actual
primary quantity is at least the value of TSQTY.
SECQTY integer specifies the minimum secondary space allocation for a DB2-managed
data set.
1. If you do not specify SECQTY, the following formulas determine actual secondary
quantity:
- If the maximum size of a data set in the table space is < 32 GB, the formula is:
- If the maximum size of a data set in the table space is >= 32 GB, the formula is:
cl
The calculated extent in cylinders is arrived at by DB2 using a sliding scale. A sliding
scale means that the first secondary extent allocations are smaller than later secondary
allocations. For example, for the sliding scale of secondary extent allocations that DB2
uses for a 64-GB data set, the size of each secondary extent is larger for each
secondary extent that is allocated up to the 127th extent. For the 127th secondary
extent and any subsequent extents, the secondary size allocation is 559 cylinders.
Ex
3. If you specify SECQTY and the value is not 0 or -1, the following applies;
This is the only rule that depends on the value of subsystem parameter MGEXTSZ
(field OPTIMIZE EXTENT on installation panel DSNTIP7).
pr
If MGEXTSZ is YES:
V5.4.0.3
Student Notebook
If the maximum size of a data set in the table space is >= 32 GB, the formula is:
Uempty
If MGEXTSZ is NO:
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Executing the CREATE TABLESPACE statement causes only one data set to be created.
However, you might have more data than this one data set can hold. DB2 automatically
defines more data sets when they are needed. Regardless of the value in PRIQTY, when a
data set reaches its maximum size, DB2 creates a new one. To enable a data set to reach
its maximum size without running out of extents, it is recommended that you allow DB2 to
automatically choose the value of the secondary space allocations for extents.
If the table space is partitioned, there is a USING clause for each partition; either one you
give explicitly or one provided by default. Except as explained below, the meaning of the
clause and the rules that apply to it are the same as for a nonpartitioned table space.
The USING clause for a particular partition is the first of these choices that can be found:
A USING clause in the PARTITION clause for the partition
An implicit USING STOGROUP clause that identifies the default storage group of the
database and accepts the defaults for PRIQTY and SECQTY.
cl
If you omit USING STOGROUP, DB2 defines the data sets using the default storage group
of the database and the defaults for PRIQTY and SECQTY.
Ex
Preformatting
pr
Two cylinders or tracks are preformatted at a time for table spaces when they are created.
Preformatting means writing X'00' on the pages. Further preformatting can occur when an
application is issuing, for example, SQL INSERTs, and this can be resource-intensive. You
can minimize this by having the LOAD or REORG utilities preformat allocated space by
specifying the PREFORMAT parameter.
2-49
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Page
Data
Page
Space Map
Header
One 2-bit
entry
for each page
xx .. .. ..
xx
00
01
10
11
space
> max
< max
< avg
< min
for :
row size
& >= avg row size
& >= min row size
row size
Seg 1
P1
....
Pn
Seg 2
P1
....
Pn
x :
0
not modified
1
modified
One 1-bit
entry
for each page
x :
0
not modified
1
modified
Page Status
Page Status
1-byte trailer
1-byte trailer
CV8317.0
Notes:
All pages of a table space have the same size, determined by the buffer pool.
After the header page (page 0) comes the space map page (page 1).
cl
The space map pages tell DB2 where to find free space on the data pages that follow.
Ex
DB2 uses two bits to indicate whether space is available on a particular page for the
largest, average, or smallest row, or whether the page is full.
pr
The space map pages also tell the COPY utility which pages have been changed since the
last copy. This information is used to determine which pages should be copied when
making an incremental copy.
Each space map covers 10,760 4K pages. Space map pages may recur throughout the
table space.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
0 1 2 3 4-10
11-14
15 -
The space map page is also used to identify the segments in a segmented table space.
pr
Ex
cl
The improved space map gives segmented table spaces an advantage over
nonsegmented ones.
2-51
Student Notebook
Data page
Data Page
Data Page
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Data Page
Data Page
Space Map
Header
6 bytes
Header ...
Row Data
Flags
Length
and so forth
ID Map
2 bytes/row
max 255
2-byte trailer
CV8317.0
Notes:
The first data page has page number 2 in the absence of other pages (for example,
dictionary pages, see later).
cl
Data pages have the same layout in all types of table space.
A table row consists of all the columns in the row plus a 6-byte prefix.
Ex
The row prefix has a 1-byte row type, a record length of 2 bytes, a 2-byte OBID, and a
1-byte map ID for the directory entry at the bottom of the page.
pr
The page directory contains up to 255 map IDs, each of which is 2 bytes in length. This
maximum number of directory entries corresponds to the maximum number of rows you
can specify for a page with the MAXROWS parameter.
The last 2 bytes on the page contain the ID of the first free ID map entry (1 byte) and a
check byte (1 byte).
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Free space
ts
Large
hole
Header
LL
Space
Small
hole
Next
Large
hole
Free Space
ID Map
CV8317.0
Notes:
Free space
cl
The right amount of free space in data pages has a favorable influence on, for example,
keeping rows in clustering sequence (see later) and managing variable length rows during
SQL INSERT and UPDATE processing. This can improve performance.
Ex
On the other hand, over-allocation of free space can mean, for example, more pages to
scan and less information transferred per I/O.
How do I specify my free space requirements?
pr
You can use the PCTFREE and FREEPAGE clauses of the CREATE TABLESPACE
statement to improve the performance of INSERT and UPDATE operations.
You can change the values of PCTFREE and FREEPAGE for existing table spaces using
the ALTER TABLESPACE statement but the change has no effect until you load or
reorganize the table space.
The space made available by deleted rows is eligible for reuse. Deleted rows are chained
together, and the chain is anchored from the 20-byte page header.
Copyright IBM Corp. 1993, 2011
2-53
Student Notebook
PCTFREE
This specifies what percentage of each page in a table space is left free when loading or
reorganizing the data.
The first record on each page is loaded without restriction. When additional records are
loaded at least the PCTFREE percentage of free space is left on each page.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
The percentage can range from 0 to 99. The default for a table space is 5.
FREEPAGE
This clause specifies how often DB2 leaves a full page of free space when loading data or
when reorganizing data. For example, if you specify 30 for FREEPAGE, DB2 leaves a free
page for every 30 pages populated with data.
The default is 0. The maximum value you can specify for FREEPAGE is 255. However, in a
segmented table space, the maximum value is 1 less than the number of pages specified
for SEGSIZE.
Leaving and Using free space
The LOAD (except LOAD RESUME YES SHRLEVEL CHANGE) and REORG utilities
leave free space, whereas INSERT and UPDATE use the free space in order to maintain
the clustering sequence of the data.
Insufficient free space
When there is insufficient free space for SQL INSERTs and UPDATEs, DB2 may have to
hold some of your data on a page other than the best page. This can cause performance
problems. For example:
An SQL INSERT causes DB2 to search for the best page to hold the row so that it can
maintain the rows in clustering sequence (see later). If there is insufficient free space on
the best page then DB2 has to choose a less favorable page.
cl
An SQL UPDATE can increase the length of a variable length row and if there is
insufficient free space on the page to hold the expanded row then DB2 may have to
place it on a neighboring, less favorable page.
Ex
Occurrences of records that have been inserted into or overflowed to pages other than their
best pages are reported by real time statistics tables (see later) in REORGLEAFFAR and
REORGNUMLEVELS in SYSIBM.SYSINDEXSPACESTATS, and REORGUNCLUSTINS
and REORGFARINDREF in SYSIBM.SYSTABLESPACESTATS.
pr
Free space is not necessary if, for example, the table space is read-only, inserts are in
ascending order, and updates are only on fixed-length columns with non-compressed data.
V5.4.0.3
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
2.3. Tables
pr
Ex
cl
Uempty
2-55
Student Notebook
Null Attribute
Default Attribute
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
DESCRIPTION
LENGTH/RANGE
----------------------Numbers
SMALLINT
WHOLE NUMBERS
(See note below)
INTEGER
WHOLE NUMBERS
(See note below)
BIGINT
WHOLE NUMBERS
(See note below)
DECFLOAT
DECIMAL
(See note below)
DECIMAL(x,y)
PRECISION/SCALE
1 TO 31 DIGITS
SINGLE PRECISION
(See note below)
REAL
DOUBLE
DOUBLE PRECISION
(See note below)
Strings
CHARACTER(x)
FIXED LENGTH
1 TO 255 BYTES
VARCHAR(x)
VARYING LENGTH
0 TO PAGE SIZE
BINARY(x)
FIXED LENGTH
1 TO 255 BYTES
VARBINARY(x)
VARYING LENGTH
1 TO 32704 BYTES
GRAPHIC(x)
FIXED LENGTH
1 TO 127 CHARS
VARGRAPHIC(x) VARYING LENGTH
0 TO PAGE SIZE
DateTime values
DATE
ISO,USA,EUR,JIS
4 BYTES
TIME
ISO,USA,EUR,JIS
3 BYTES
TIMESTAMP
ISO,USA,EUR,JIS
10 BYTES
pr
Ex
cl
DATATYPE
--------
STORED AS
---------BINARY
BINARY
BINARY
PACKED DECIMAL
32 BITS
64 BITS
YYYYMMDD
HHMMSS
YYYYMMDDHHMMSSNNNNNN
TIMESTAMP may be defined with a user-defined number of positions after the seconds.
2-56 DB2 10 for z/OS DB Admin Workshop Part 1
V5.4.0.3
Student Notebook
Uempty
Note
Numbers
A small integer (SMALLINT) is a binary integer with a precision of 15 bits. The range of
small integers is -32768 to +32767
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
A large integer (INTEGER) is a binary integer with a precision of 31 bits. The range of large
integers is -2147483648 to +2147483647.
A big integer (BIGINT) is a binary integer with a precision of 63 bits. The range of big
integers is -9223372036854775808 to +9223372036854775807.
DECFLOAT is similar to both packed decimal and floating point. For precision of 16
decimal digits use DECFLOAT(16) and for precision of 32 decimal digits use
DECFLOAT(32). DECFLOAT processing deals with exact numbers.
REAL is a single precision floating point number. DOUBLE (or FLOAT) is a double
precision floating point number. Number of significant digits is 1 - 21 for REAL and 22 - 53
for DOUBLE, and the range is -7.2E75 to +7.2E75 for both REAL and DOUBLE.
Strings
Character strings are represented by CHAR, VARCHAR, and CLOB. CLOB (character
large object) is discussed in CV841.
Binary strings are represented by BINARY, VARBINARY and BLOB. BLOB (binary large
object) is discussed in CV841.
Graphic strings are represented by GRAPHIC, VARGRAPHIC, and DBCLOB. DBCLOB
(double-byte character large object) is discussed in CV841.
ROWID
cl
The data type ROWID is supported. The ROWID allows you to uniquely identify a row in a
table. A ROWID column enables queries to be written that navigate directly to a row in the
table because the column implicitly contains the location of the row. ROWID data type is
discussed in CV841.
XML
Ex
The data type XML is supported. The XML data type allows you to store XML data in your
table. XML data type is discussed in CV120 or CV260.
NULL
pr
NULL indicates the absence of a value. NULL is not considered for column function
evaluation (AVG...), except COUNT. Multiple NULLs are not considered equal, except for
UNION, INTERSECT, EXCEPT, GROUP BY, ORDER BY, and unique indexes. The next
visual discusses how nulls are handled in DB2.
2-57
Student Notebook
If the CREATE TABLE statement specifies IN DBX.TSX, the table EMP is created in the
table space DBX.TSX.
If the CREATE TABLE statement specifies IN DATABASE DBX, the table EMP is created in
an implicitly created table space and associated with the database DBX.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
If the CREATE TABLE statement does not specify the IN clause, the table EMP is created
in an implicitly created table space and associated with an implicitly created database.
The implicitly created table space is PBG UTS if the CREATE TABLE statement does not
specify PARTITION BY clause, or a PBR UTS if the CREATE TABLE statement specifies
PARTITION BY clause.
pr
Ex
cl
More information on implicitly created database is provided at the end of this unit.
V5.4.0.3
Student Notebook
Uempty
Row format
Nullable fields have a flag to indicate whether the field is present (X'00') or null (X'FF')
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
fld1
00
fld2
FF
fld3
fld3 is null
fld2 is present
fld1 cannot be null
fld1
00
fld2
03
00
fld3
ALTER TABLE ... ADD columnname does not update the existing records
-> Updated when values are inserted for the new column or at REORG time
Copyright IBM Corporation 2011
CV8317.0
Notes:
Nullable fields
cl
Ex
The use of nulls requires a 1-byte flag to be added to each nullable column. Another 1-byte
flag is added to each value in an index created from a nullable column.
The null prefix is HEX '00' if the value is present, and HEX 'FF' if it is not.
pr
Application programs that select data from nullable columns should be coded with null
indicators.
The extra bytes for null flags and the additional processing for null indicators can represent
an overhead.
2-59
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Although VARCHAR can use space efficiently, remember that a VARCHAR entry carries a
2-byte length prefix. The benefits of space savings should outweigh the overhead of the
length prefixes.
If a VARCHAR column permits nulls, then there is the 1-byte null flag overhead as well as
the 2-byte length overhead.
pr
Ex
cl
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
C2
8000000A 0006 WILSON
C3
ANDREW
C4
0008 SAN JOSE
2-byte length
O2
O4
12
offset to C2
C2
18 WILSON
C4
SAN JOSE
offset to C4
CV8317.0
Notes:
pr
Ex
cl
Prior to V9, each variable length column in a data row is stored with its length preceding it.
If you need to access a column that is preceded by a variable length column, DB2 has to
traverse through all the columns that precede it, starting with the first variable length
column until it reaches the column you want to process. If you updated a variable length
column, DB2 logs the changed column and everything to the end of the row. For these
reasons, we recommend that all variable length columns be placed at the end of the row
and the most frequently changed ones at the very end of the row. In todays world of ERP,
CRM and many other canned applications, it is impossible to control the placement of
columns. V9 introduces a new format for data rows called reordered row format that helps
reduce the impact and overhead of processing of variable length columns.
In V9 NFM and later versions, you do not have to worry about the order of columns within
the row. You can specify the order however you wish, and DB2 automatically reorders the
columns within the row and places all of the variable length columns at the end of the
physical row within the data page. Instead of storing the columns with their length, DB2
stores all of the offsets for the variable length columns in an area following the last fixed
length column and prior to the first variable length column. DB2 can now directly access
Copyright IBM Corp. 1993, 2011
2-61
Student Notebook
each variable length column and know its length by doing simple calculations with the
offsets. This is a much more efficient way to process individual varying length columns.
This enhancement may also help reduce the amount of logging performed for canned
applications since all the variable length columns are physically located at the end of the
row no matter how the applications define the order of the columns.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Let us take a look at an example of how this is done. We use the statement shown in the
visual to create a table with four columns, two variable length character columns, and two
fixed length character columns. In our example, the variable length columns are in the
middle and end of the row.
DB2 internally rearranges the columns in such a manner that all the fixed length columns
go to the front of the row. All the length fields from VARiable columns get turned into
offsets. All the data parts from VARiable columns get stored at the end of the row. Net
result is 2-byte offset replaces 2-byte length field for VARCHAR meaning row length
remains unchanged.
C2 offset: 4 bytes (for two x two-byte offset fields) + 4 bytes for an integer column +
10 bytes for the char field = 18 bytes = x12 bytes.
C4 offset: 18 bytes for C2 offset + 6 bytes for VARCHAR column C2 = 24 bytes = x18
bytes.
Note
Even though the physical order of the columns within the row has changed, if you do a
SELECT * to retrieve all the columns, DB2 still returns them in the order specified in your
CREATE TABLE statement.
cl
In Basic Row Format, for a varying length field, DB2 logs from the first changed byte to the
end of the row. In Reordered Row Format, the first changed byte may be the offset field for
the first varying length field following the one being updated.
Information
pr
Ex
The value of the DSNZPARM parameter RRF (field REORDERED ROW FORMAT on
Install panel DSNTIP7, Install DB2 - Sizes Panel 2) specifies whether most newly created
table spaces are to store data in reordered row format (RRF) or basic row format (BRF) by
default. Acceptable values are ENABLE and DISABLE. The default is ENABLE. In DB2 9
this parameter is not on the installation panel.
ENABLE - Newly created table spaces or newly added partitions that are created by
ALTER ADD PARTITION statements on partition-by-growth table spaces are created in
RRF. Existing BRF table spaces are converted to RRF by running LOAD REPLACE or
REORG TABLESPACE.
V5.4.0.3
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DISABLE - Newly created table spaces, including universal table spaces, and newly added
partitions that are created by ALTER ADD PARTITION statements on partition-by-growth
table spaces are created in BRF. Existing BRF table spaces are not converted to RRF by
LOAD REPLACE or REORG TABLESPACE.
pr
Ex
cl
Uempty
2-63
Student Notebook
DEFAULT attributes
Default can be either:
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
A constant
USER (special register)
System defaults
Examples:
WITH
WITH
WITH
WITH
WITH
DEFAULT
DEFAULT
DEFAULT
DEFAULT
DEFAULT
MY OWN VALUE
USER
CURRENT SQLID
NULL
CV8317.0
Notes:
cl
It is possible to provide your own default value. It is used by DB2 if you fail to supply a value
in the INSERT statement. The current timestamp, time, and date are supported through the
traditional system default. There are no additional keywords required.
pr
Ex
DATA TYPE
DEFAULT VALUE
-------------------------------------NUMERIC
0
CHARACTER
Blanks
VARCHAR
ZERO LENGTH
GRAPHIC
Blanks
BINARY
X00
DATE
CURRENT DATE
TIME
CURRENT TIME
TIMESTAMP
CURRENT TIMESTAMP
Note [VAR]CHAR is padded with spaces (X40 for EBCDIC, and X20 for ASCII
and Unicode. BINARY is padded with X00. VARBINARY is not padded even
during comparisons,
2-64 DB2 10 for z/OS DB Admin Workshop Part 1
V5.4.0.3
Student Notebook
Uempty
The USER special register always contains the primary authorization ID of the process.
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
WITH DEFAULT NULL specifies that the default is the null value. NOT NULL and
DEFAULT NULL cannot both be specified. If NOT NULL is omitted and DEFAULT is
omitted, the effect is equivalent to an implicit specification of DEFAULT NULL.
2-65
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
Table check constraints enhance your ability to control the integrity of your data.
cl
A table check constraint designates the values that specific columns of a base table can
contain. It is checked whenever the content of the data is changed.
Ex
The constraints are included in the table DDL. The presence of the constraint impacts the
behavior of DB2 for that table. DB2 automatically enforces the constraint at all times.
The following is the complete list of check constraint restrictions as documented in SQL
Reference:
pr
It can refer only to columns of this table; however, the columns cannot be LOB, ROWID,
or security label columns (including distinct types that are based on LOB and ROWID
data types).
It can be up to 3800 bytes long, not including redundant blanks.
It must not contain any of the following:
- Subselects
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
- Special registers
- Columns that include a field procedure
- CASE expressions
- Row expressions
- DISTINCT predicates
If a check condition refers to a LOB column (including a distinct type that is based on a
LOB), the reference must occur within a LIKE predicate.
The AND and OR logical operators can be used between predicates. The NOT logical
operator cannot be used.
The first operand of every predicate must be the column name of a column in the table.
The second operand in the check condition must be either a constant or a column name
of a column in the table.
- If the second operand of a predicate is a constant, and if the constant is:
A floating-point number, then the column data type must be floating point.
A decimal number, then the column data type must be either floating point or
decimal.
An integer number, then the column data type must not be a small integer.
A small integer number, then the column data type must be small integer.
cl
A decimal constant, then its precision must not be larger than the precision of the
column.
Identical descriptions, with the exception that the specification of the NOT NULL
and DEFAULT clauses for the columns can be different, and that string columns
with the same data type can have different length attributes.
pr
Ex
- If the second operand of a predicate is a column, then both columns of the predicate
must have:
Using CONSTRAINT to name a CHECK is optional. If you do not provide a name, then
DB2 generates one for you.
2-67
Student Notebook
Note
WITH RESTRICT ON DROP:
Cannot drop table. Also the database or table space that contains the table cannot be
dropped.
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SELECT NAME,OWNER,TYPE,DBNAME,TSNAME,DBID,OBID,COLCOUNT,CHECKS,CLUSTERTYPE
FROM
SYSIBM.SYSTABLES
WHERE NAME
= 'EMPLOYEE' AND
CREATOR = 'TSOUD01'
---------+---------+---------+---------+---------+---------+---------+------+
NAME
OWNER TYPE DBNAME TSNAME DBID OBID COLCOUNT CHECKS CLUSTERTYPE
---------+---------+---------+---------+---------+---------+---------+------+
EMPLOYEE TSOUD01
T USDB1
USTS2
264
8
8
4 Y
SYSIBM.SYSCOLUMNS
SELECT NAME,TBNAME,COLNO,COLTYPE,LENGTH,SCALE,NULLS,DEFAULT
FROM
SYSIBM.SYSCOLUMNS
WHERE TBNAME
= 'EMPLOYEE' AND
TBCREATOR = TSOUD01'
---------+---------+---------+---------+---------+---------+---------+-NAME
TBNAME
COLNO COLTYPE
LENGTH
SCALE NULLS DEFAULT
---------+---------+---------+---------+---------+---------+---------+-EMPNO
EMPLOYEE
1 INTEGER
4
0 N
N
NAME
EMPLOYEE
2 CHAR
10
0 N
N
DEPT
EMPLOYEE
3 INTEGER
4
0 Y
Y
JOB
EMPLOYEE
4 CHAR
10
0 Y
Y
YEARS
EMPLOYEE
5 INTEGER
4
0 Y
Y
SALARY
EMPLOYEE
6 DECIMAL
10
2 Y
Y
INVESTP
EMPLOYEE
7 INTEGER
4
0 N
Y
MAXINVEST EMPLOYEE
8 INTEGER
4
0 N
Y
Copyright IBM Corporation 2011
CV8317.0
Notes:
All DB2 tables, including the catalog tables, are described in the DB2 catalog table
SYSIBM.SYSTABLES.
pr
Ex
cl
2-69
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SYSIBM.SYSCHECKDEP
---------+---------+---------+---------+---------+SELECT TBOWNER,TBNAME,CHECKNAME,COLNAME
FROM
SYSIBM.SYSCHECKDEP
WHERE TBOWNER
= 'TSOUD01' AND
TBNAME = 'EMPLOYEE'
---------+---------+---------+---------+---------+TBOWNER
TBNAME
CHECKNAME COLNAME
---------+---------+---------+---------+---------+TSOUD01
EMPLOYEE
DEPT
DEPT
TSOUD01
EMPLOYEE
INVESTP
INVESTP
TSOUD01
EMPLOYEE
INVESTP
MAXINVEST
TSOUD01
EMPLOYEE
JOB
JOB
TSOUD01
EMPLOYEE
NOINVEST
INVESTP
TSOUD01
EMPLOYEE
NOINVEST
YEARS
Copyright IBM Corporation 2011
CV8317.0
Notes:
pr
Ex
cl
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
Ex
cl
pr
The example shows the table is created in the classic partitioned table space
USDB1.USTS3. If you specify IN USDB1.USTS5, the table is created in the
partition-by-range universal table space USDB1.USTS5.
2-71
Student Notebook
ALTER TABLE
You can:
Add / remove Table check / Referential integrity constraints
Add extra column or partitioning key
Change a column name
Specify ADD RESTRICT ON DROP
Specify DROP RESTRICT ON DROP
Increase data type length (old values must fit in) such as:
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CHAR(30) to CHAR(50)
VARCHAR(30) to VARCHAR(50)
SMALLINT to BIGINT
DECIMAL (9,2) to DECIMAL(15,4)
You cannot:
CV8317.0
Notes:
When a column is added via ALTER, it becomes the rightmost, and contains NULL values
or the default value for existing rows.
Ex
cl
If the table is not created by specifying WITH RESTRICT ON DROP, the table definition
can be altered using ALTER TABLE specifying ADD RESTRICT ON DROP.
pr
If there is a need to drop the table, the table definition can be altered using ALTER TABLE
specifying DROP RESTRICT ON DROP.
You can use the RENAME COLUMN clause to rename columns. Numerous restrictions
apply. Details are discussed in CV841.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
pr
Ex
cl
Notes:
2-73
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
For DB2
CV8317.0
Notes:
cl
The CURRENT RULES special register is set initially to the value specified by the BIND
SQLRULES option. The special register can be changed dynamically by using the SET
CURRENT RULES SQL statement.
Ex
Check integrity exists when each row of a table conforms to the check constraints defined
on that table. Check Pending (CHKP) is set when check integrity cannot be guaranteed.
pr
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
-DIS DB (INVDB11)
DSNT360I # *********************************************************
DSNT361I # * DISPLAY DATABASE SUMMARY
* GLOBAL
DSNT360I # *********************************************************
DSNT362I # DATABASE = INVDB11 STATUS = RW
DBD LENGTH = 4028
DSNT397I #
NAME
TYPE PART
STATUS
PHYERRLO PHYERRHI CATALOG PIECE
......
.... ..... .......... ....... ....... ....... .....
INVSINV TS
RW
INVSINL TS
RW,CHKP
INVXINV0 IX
RW
INVXINLO IX
RW
******* DISPLAY OF DATABASE INVDB11 ENDED
*************************
DSN9022I
# DSNTDDIS DISPLAY DATABASE NORMAL COMPLETION
***
CV8317.0
Notes:
pr
Ex
cl
The -DISPLAY DATABASE command can be used to show the status of all objects in your
database. As you can see, the status is set at the table space (not table) and index level.
We see some other restrictive states later in the course.
2-75
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Student Notebook
V5.4.0.3
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
Uempty
2-77
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Without
data compression
I/O
Buffer pool
I/O
Buffer pool
Program
Program
ENCODE
DECODE
CV8317.0
Notes:
cl
The decision whether to use data compression for a specific table space is a tradeoff
between resource savings (disk space, I/O, and buffer pool requirements) and the incurred
CPU overhead of the compression or decompression.
pr
Ex
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CREATE/ALTER TABLESPACE
COMPRESS YES
Data Page
Row
Header
Dictionary
Row
Data
Space Map
Header Page
Dictionary Anchor
CV8317.0
Notes:
cl
When a row is read for compression, the algorithm evaluates the compression space
savings before actually doing the compression. A bit in the row header indicates whether a
row has been compressed or not.
Ex
Data compression uses a dictionary that is stored with the data and is anchored in the table
space's header page. The dictionary is a structure which defines the rules used to encode
and decode strings of characters. The dictionary has at most 4096 entries and a maximum
size of 64 KB. Depending the page size of the table space, it can therefore be stored, for
example, in 16 4K pages or two 32K pages.
pr
2-79
Student Notebook
REORG TABLESPACE
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
1.
2.
3.
Compress at RELOAD
2.
Starts compressing
CV8317.0
Notes:
cl
Since different data can have different characteristics; the best compression requires
customization to the specific data.
Ex
This customization of encoding and decoding to the data is the purpose of the compression
dictionary. Each table space (or partition of a partitioned table space) in DB2 has a
dictionary that is custom-built to the characteristics of the data within it to maximize the
compression of that data.
pr
V5.4.0.3
Student Notebook
Uempty
1+1
=2
Example
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Allows the LOAD utility to copy an existing compression dictionary from the specified
partition to other partitions on a partitioned table space. LOAD copies the current
compression dictionary from the specified partition and uses it for compressing the input
data for the partitions being replaced.
This option provides a method to copy a compression dictionary to an empty partition that
normally would not have a compression dictionary built.
This keyword only copies the compression dictionary to partitions being replaced that have
the COMPRESS YES attribute. A valid dictionary must exist for the partition specified with
the COPYDICTIONARY keyword.
You can specify any valid partition number that is not being replaced for the partitioned
table space. The default value is 1.
cl
Ex
Prevents the LOAD utility from building a new compression dictionary. LOAD retains the
current compression dictionary and uses it for compressing the input data. This option
eliminates the cost that is associated with building a new dictionary.
pr
DB2 ignores the KEEPDICTIONARY option if the REORG utility changes the table space
from basic row format to reordered row format.
If the table space or partition is empty, DB2 performs one of these actions:
DB2 builds a dictionary if a compression dictionary does not exist.
DB2 keeps the dictionary if a compression dictionary exists.
2-81
Student Notebook
If RESUME NO and REPLACE are specified when the table space or partition is not empty,
DB2 performs the same actions as it does when the table space or partition is empty.
If the table space or partition is not empty and RESUME YES is specified, DB2 performs
one of these actions:
DB2 does not build a dictionary if a compression dictionary does not exist.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
You must use KEEPDICTIONARY to ensure that the compression dictionary is maintained.
If the data has changed significantly since the last dictionary was built, rebuilding the
dictionary might save a significant amount of space.
If the current dictionary was built either by the LOAD utility or automatically by DB2
based on records that have been inserted over time, rebuilding the dictionary by using
REORG might produce a better compression dictionary.
If the data is being converted from basic row format to reordered row format, REORG
builds a new dictionary for the new format. DB2 ignores the KEEPDICTIONARY option
if the REORG utility changes the table space from basic row format to reordered row
format.
cl
You must use KEEPDICTIONARY to ensure that the compression dictionary is maintained.
Ex
Note
pr
The first time you REORG a compressed table space, DB2 automatically rebuilds a new
compression dictionary even if you specify the KEEPDICTIONARY keyword in your
REORG/LOAD control statement.
V5.4.0.3
Student Notebook
Uempty
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DSN1COMP does not require DB2 authorization checking (data sets maybe RACF
protected) therefore, can run when DB2 is up or down.
2-83
Student Notebook
Compress on INSERT
Data compression occurs when a dictionary exists
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Prior to DB2 10
CV8317.0
Notes:
cl
Prior to DB2 10, if you turn on compression for a table space using the ALTER
TABLESPACE command, DB2 needs to build the compression dictionary. Compression
dictionaries are built as part of REORG TABLESPACE or LOAD utility runs. You might not
be able to run LOAD or REORG when you decide to turn on compression for a given table
space.
Ex
With DB2 10 NFM, you can turn on compression with ALTER any time, and the
compression dictionary is built when you execute the following statements:
pr
INSERT statements
MERGE statements
LOAD SHRLEVEL CHANGE
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
cl
The threshold is about 1.2 MB, which is determined by reading the RTS statistics in
memory. When the threshold is reached, DB2 builds the dictionary asynchronously and
issues the message shown below:
Ex
pr
The DSNU241I message is issued in this case because the table space is partitioned.
DSNU231I is written out in case of a non-partitioned table space. These messages are
issued on the console only by compress on insert. The LOAD and REORG utilities have
the same messages, but these messages are written in the utility output, not on the
console.
After the dictionary is built, DB2 inserts the data in compressed format. At least 1.2 MB of
data in the table space is not compressed. The additional data is compressed.
2-85
Student Notebook
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Data rows that you insert while the dictionary is still under construction are inserted
uncompressed. When building the dictionary asynchronously, DB2 reads the existing data
with isolation level uncommitted read.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DSNU1232I COMPRESSED
ROW
IS IGNORED
BECAUSE THE
DICTIONARY
IS NOT
AVAILABLE
FOR TABLE
table-name
DI
D ..
..
D
DI
DI
H SM D
DI
D ..
..
D
DI
DI
H SM D
COPY TS...
SYSTEMPAGES NO
Dictionary pages
not necessarily
after space map page
nor contiguous
COPY TS...
SYSTEMPAGES YES
(option makes DB2 copy
the dictionary pages
after space map page)
H SM DI DI ..
..
DI D D D
DI
DI
UNLOAD
DATA....
FROMCOPY
DI
CV8317.0
Notes:
Ex
cl
If the compression dictionary is built using LOAD REPLACE or REORG TABLESPACE, the
dictionary pages follow the system pages (header and space map), which is not the case
when the compression dictionary is built on-the-fly. Because there must be at least 1.2 MB
worth of data in the table space before the new compression functionality kicks in, the
compression dictionary gets any page numbers. Furthermore, the dictionary pages might
not be contiguous, as illustrated in above visual.
pr
The left side in the above visual shows the dictionary pages (DI) in the table space. When
the table space is image copied with option SYSTEMPAGES YES the dictionary pages are
replicated after the header and space map pages.
If you use image copies as input for the UNLOAD utility and if you plan on using this
automatic compression without REORG or LOAD, you must run the COPY utilities with
option SYSTEMPAGES YES. This option requests the collection of all SYSTEMPAGES
during utility execution and places a copy of those directly behind the first space map page.
The original dictionary pages remain at their original location. So, the SYSTEMPAGES
YES option on COPY TABLESPACE produces duplicates of those system pages.
Copyright IBM Corp. 1993, 2011
2-87
Student Notebook
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
If you specify SYSTEMPAGES NO, DB2 copies only the table space as is. When you
attempt to unload from this image copy, the UNLOAD utility recognizes that the rows in the
table space are compressed but cannot uncompress the rows because it looks only for
dictionary pages at the beginning of the table space prior to first data page.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
If COMPRESS YES:
CV8317.0
Notes:
cl
If you run REORG later, depending on your choice regarding the KEEPDICTIONARY utility
control keyword, REORG either moves the dictionary pages behind the space map page
and before the first data page or creates a new dictionary, which is then located at this
point.
Ex
Generally, you can use the DSN1COMP utility to estimate the space savings that can be
achieved by DB2 data compression in table spaces and indexes.
pr
If you run the DSN1COMP utility without any special option, the utility calculates the
estimated space savings based on the algorithms that are used for building compression
during LOAD. If you use the LOAD utility to build a new compression dictionary, the
compression ratio is likely to be a bit less effective than during REORG. If you specify the
REORG keyword on the compression utility, DB2 calculates the compression ratio based
on what is accomplished by the REORG utility. There is no specific keyword for the
COMPRESS on INSERT method. Compress on INSERT reaches similar compression
ratios as the LOAD utility for table spaces, considerably larger than 1.2 MB. As a
2-89
Student Notebook
consequence, if you do not specify REORG for your estimate, the calculated savings is
about the same as COMPRESS ON INSERT.
Let us assume that you have turned on compression for your table space and now you
want to know whether COMPRESS on INSERT actually worked and if the data is now
compressed.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
The easiest way to verify whether the table space is compressed is to check the SYSLOG
for the following message:
DSNU241I -DB0B DSNUZLCR - DICTIONARY WITH 4096 755
ENTRIES HAS BEEN SUCCESSFULLY BUILT FROM 598 ROWS FOR TABLE SPACE
SABI6.TS6, PARTITION 1
If you can find this message, you know that the dictionary was built for a given partition of a
table space. For a non-partitioned table space, the message is DSNU231I.
If you cannot find this message in the SYSLOG, check whether the data volume is large
enough so that the threshold of 1.2 MB of data is passed and compression can
theoretically begin. You can for example use the following SQL query to check the RTS
tables:
SELECT DATASIZE
FROM SYSIBM.SYSTABLESPACESTATS
WHERE DBNAME='yourdb'
AND NAME='yourts';
Remember that column DATASIZE contains the amount of data stored in your table space
in bytes, that is you need at least 1,310,720 bytes. However, the time that the current value
is reflected in the catalog depends on the interval specified for externalizing RTS statistics
(STATSINT DSNZPARM default is 30 minutes).
pr
Ex
cl
In some cases, it might appear that you turn on compression and that the data volume is
larger than 1.2 MB but that COMPRESS on INSERT did not take place. This situation can
occur if DB2 cannot build a usable compression dictionary because of the data contents. If
this is the case, you do not get an error message. An error message, such as DSNU235I or
DSNU245I, displays on the console if there are issues, such as out of space conditions or
similar.
V5.4.0.3
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
Uempty
2-91
Student Notebook
DB2 views
View V1 (Subset of columns)
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
EMP Table
EMP Table
EMP Table
EMPLOYEE Table
DEPT Table
DPT#
EMP#
LAST
MI
FIRST
JOB
DPTNME
MGR
F01
020
DAVIS
CHARLES
52
F01
ACCT
087
F01
090
QUINTO
DOLORES
55
B11
ADMIN
177
F01
087
EAST
MORIS
99
F02
SALES
358
F01
333
MARY
52
F02
358
MALLET
MIKE
99
B11
130
PARKER
JUDITH
62
B11
200
WILSON
SMITH
BETTY
40
B11
298
KANDOS
PHILLIP
47
B11
177
SECOND
MARC
99
DPT#
DEPT Table
CV8317.0
Notes:
cl
Views are virtual tables made up of columns and rows from real tables and other views.
They provide alternative ways of looking at data in one or more tables.
Ex
DB2 does not store any data for the view itself because the data already exists in the real
(base) table or tables.
You define a view in terms of a SELECT. For example, you can define a view as:
A subset of columns of a real table
pr
V5.4.0.3
Student Notebook
Uempty
Ease of use
A single view can provide easy access to several underlying base tables.
Your installation naming standards may mean that some table and column names are long
or difficult to remember. You can create a view that uses alternative names that are much
easier to remember.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
You can create views for users which execute complex SQL and which perform well, rather
than expect users to code complex SQL.
Note
pr
Ex
cl
Do not be concerned if you cannot easily read the data within the table cells shown on this
visual. The details are not important at this stage. The purpose of the diagram is to highlight
what is visible through the view as opposed to what is actually in the table.
2-93
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
cl
Ex
Cost of views
In most cases a view costs only a few microseconds of CPU time when the SQL
referencing the view is bound. For static SQL this is a one-time cost and for dynamic SQL
this cost is incurred each time the view is used.
pr
View materialization
In some cases the qualifying rows from a view definition must be written to a work fIle. For
example, if the SELECT in the view definition has a GROUP BY with a column function.
V5.4.0.3
Student Notebook
Uempty
Read-only views
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
In some cases, a view is read-only. For example, when a view is defined on the JOIN of two
or more tables (and no INSTEAD OF TRIGGER is defined).
2-95
Student Notebook
SQL3
SQL4
SQL5
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SQL1
VIEW1
TABLE1
VIEW2
TABLE2
TABLE3
TABLE4
CV8317.0
Notes:
You can create a view on views as long as the view references no more than 15 base
tables directly or indirectly.
cl
In the visual, VIEW1 is defined on some rows and columns of TABLE1 and VIEW2. Also,
VIEW2 is defined on some rows and columns of TABLE3 and TABLE4.
Ex
Higher level views depend on those at lower levels. If a lower level view or table is dropped
then higher level views depending on it are also dropped automatically. For example, if
TABLE4 is dropped, then VIEW2 and VIEW1 are dropped.
pr
REVOKE SELECT ON base-table FROM view-creator drops the view. This is driven by the
DSNZPARM REVOKE_DEP_PRIVILEGES setting.
Security
Views provide security for your data. Rather than allow users to access all rows and
columns in a base table, you can grant them access to data via views that define a subset
of columns and rows.
2-96 DB2 10 for z/OS DB Admin Workshop Part 1
V5.4.0.3
Student Notebook
Uempty
Data independence
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
If you ALTER an underlying base table and add a column, users selecting columns through
an existing view do not see the new column even when using SELECT *. This way,
programs using views can run unchanged.
2-97
Student Notebook
DEPT
A10
E10
E11
E11
E11
ROOM
1018
1003
1012
1034
1022
TELEPHONE
4388
4407
4112
4234
4419
SALARY
7500
4900
5500
4300
4100
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
EMPNO
110
220
290
300
310
CREATE
AS
SELECT
FROM
WHERE
VIEW EMPE11
EMPE11
EMPNO
290
300
310
NAME
OBERHAUS
SCHMIDT
MUELLER
ROOM
1012
1034
1022
TELEPHONE
4112
4234
4419
Two rows with name SCHMIDT in the table. What is the effect of DELETE?
Copyright IBM Corporation 2011
CV8317.0
pr
Ex
cl
Notes:
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
BASE TABLE
D
C
B
E
SELECT
and
UPDATE
INSERT
DELETE
Copyright
CopyrightIBM
IBMCorporation
Corporation2011
2007
CV8317.0
Notes:
cl
UPDATE: The change of values in the view changes the values in the corresponding
columns the base table. However, base table columns which are not part of the view are
not changed.
Ex
INSERT: Inserting a row into a view is only possible, if all columns of the base table, which
are not part of the view, are nullable or have defaults.
DELETE: The deletion of a row from the view deletes the entire row from the base table,
even if some columns are not part of the view. However, the rows that are not part of the
view cannot be deleted.
pr
2-99
Student Notebook
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
V5.4.0.3
Student Notebook
Uempty
LASTNAME
HAAS
KWAN
O'CONNELL
QUINTANA
NICHOLLS
WORKDEPT
SALARY
A00
C01
A00
C01
C01
52750.00
38250.00
29250.00
23800.00
28420.00
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
EMPLOYEE Table
000010
000030
000120
000130
000140
UPDATE SALARIES_2
SET SALARY = 42000
WHERE EMPNO = '000030'
WORKS!
FAILS!
CV8317.0
Notes:
cl
In the first example, view SALARIES_1 is created. An update that raised the yearly salary
to 40000 or more, which is beyond the scope of the view, would succeed. After the
update, the changed row is no longer visible through the view, but still exists in the
base table. We call this effect the disappearing row.
pr
Ex
In the second example, view SALARIES_2 is created. It has the same definition as
SALARIES_1, except that WITH CHECK OPTION is specified. This time, an update that
tried to raise the yearly salary to 40000 or higher would fail. WITH CHECK OPTION
ensures that the update is not permitted if it would remove the changed row from the scope
of the view.
The CHECK OPTION also constraints INSERT through the view, not just UPDATE as in the
example in the visual.
2-101
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Verify only your own WHERE clause and any underlying views
that also have the WITH CHECK OPTION
Ignore definitions of underlying views that do not have the
WITH CHECK OPTION
CV8317.0
Notes:
cl
The difference between these two forms is meaningful only when a view is defined on top
of another view. If a view called V2 is defined by a query on another view called V1, we
refer to V1 as the underlying view.
Ex
When V2 is defined using WITH LOCAL CHECK OPTION, operations on V2 must satisfy
the definitions of V2 and of all underlying views that also have WITH CHECK OPTION;
however, they need not satisfy the definitions of underlying views that do not have WITH
CHECK OPTION.
pr
On the other hand, when V2 is defined using WITH CASCADED CHECK OPTION, all
operations on V2 must satisfy the definitions of V2 and of all underlying views, whether
they have WITH CHECK OPTION or not.
If a CREATE VIEW statement simply specifies WITH CHECK OPTION, the default is WITH
CASCADED CHECK OPTION.
V5.4.0.3
Student Notebook
Uempty
If the CHECK OPTION for view V2 is LOCAL, only WHERE condition 2 is checked during
UPDATE and INSERT.
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
If the CHECK OPTION for view V2 is CASCADED, both WHERE condition 2 and WHERE
condition 1 are checked during UPDATE and INSERT.
2-103
Student Notebook
DEPT
A10
E10
E11
E11
E11
ROOM
1018
1003
1012
1034
1022
1. CREATE VIEW V1
SALARY
7500
4900
5500
4300
4100
3. CREATE VIEW V2
AS
SELECT EMPNO, NAME, SALARY
FROM
V1
WHERE SALARY > 4200;
AS
SELECT EMPNO, NAME, SALARY
FROM
EMPLOYEE
WHERE SALARY < 5000
WITH CHECK OPTION;
V1
TELEPHONE
4388
4407
4112
4234
4419
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
EMPNO
110
220
290
300
310
V2
EMPNO
220
300
310
NAME
ABELE
SCHMIDT
MUELLER
SALARY
4900
4300
4100
EMPNO
220
300
NAME
ABELE
SCHMIDT
SALARY
4900
4300
4. UPDATE V2
2. UPDATE V1
FAILS!
FAILS!
CV8317.0
Notes:
cl
In this example, view V1 is created on table EMPLOYEE using WITH CHECK OPTION.
Notice the WHERE clause that allows only rows where SALARY < 5000 to be retrieved
through this view. Updating the table through view V1 by increasing the SALARY by 200
would fail because SALARY for EMPNO 220 would go over 5000.
Ex
View V2 is created on view V1 without the use of WITH CHECK OPTION. Notice the
WHERE clause that allows only rows where SALARY > 4200 to be retrieved. Thus, out of
three rows visible through view V1, only two rows can be retrieved through view V2,
because the SALARY for EMPNO 310 is < 4200.
pr
Updating the table through view V2 by increasing the SALARY by 200 would fail because
SALARY for EMPNO 220 would go over 5000. Although WITH CHECK OPTION is not
used when creating view V2, since view V1 is created using WITH CHECK OPTION, the
search condition of view V1 is inherited by view V2.
V5.4.0.3
Student Notebook
Uempty
The rules for the two WITH CHECK OPTIONS can be summarized as follows:
CASCADED
When a row is inserted or updated using a view defined WITH CASCADED CHECK
OPTION, then the search condition for the view is checked along with any search
conditions defined for underlying views.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
This is regardless of whether or not the WITH CHECK OPTION was specified in the
definition of the underlying views.
LOCAL
pr
Ex
cl
When a row is inserted or updated using a view defined WITH LOCAL CHECK
OPTION, then the search condition for the view is checked along with any search
conditions for underlying views defined with the WITH CHECK OPTION clause.
2-105
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SYSIBM.SYSTABLES
---------+---------+---------+---------+-----SELECT NAME,OWNER,TYPE,COLCOUNT
FROM
SYSIBM.SYSTABLES
WHERE TYPE = 'V' AND
CREATOR = ' TSOUD01';
---------+-----+---------+---------+-----NAME
OWNER
TYPE COLCOUNT
---------+-----+---------+---------+-----EMPLOYEEV
TSOUD01
V
3
SYSIBM.SYSCOLUMNS
SELECT NAME,TBNAME,COLNO,COLTYPE,LENGTH,NULLS
FROM
SYSIBM.SYSCOLUMNS
WHERE TBNAME = 'EMPLOYEEV';
---------+---------+---------+---------+---------+---------+---------+-NAME
TBNAME
COLNO COLTYPE
LENGTH NULLS
---------+---------+---------+---------+---------+---------+---------+-EMPNO
EMPLOYEEV
1 INTEGER
4 N
DEPT
EMPLOYEEV
2 INTEGER
4 Y
YEARS
EMPLOYEEV
3 INTEGER
4 Y
Copyright IBM Corporation 2011
CV8317.0
Notes:
Since views are "virtual" tables, DB2 stores information about them as if they were tables.
cl
In SYSIBM.SYSTABLES a row is inserted with TYPE V indicating this row is for the view
EMPLOYEEV shown under column NAME.
pr
Ex
In SYSIBM.SYSCOLUMNS, a row is inserted for each column in the view definition. In this
example, the column names in the table are used for the view column names by default.
The view name is shown under column TBNAME.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SYSIBM.SYSVIEWS
SELECT NAME,TYPE,SEQNO,TEXT
FROM
SYSIBM.SYSVIEWS
WHERE NAME = 'EMPLOYEEV';
---------+---------+---------+---------+---------+---------+---------+---------+---NAME
TYPE SEQNO TEXT
---------+---------+---------+---------+---------+---------+---------+---------+---EMPLOYEEV V
1 CREATE VIEW EMPLOYEEV AS SELECT EMPNO,DEPT,YEARS FROM EMPLOYEE
SYSIBM.SYSVIEWDEP
SELECT DNAME,DTYPE,BNAME,BTYPE
FROM
SYSIBM.SYSVIEWDEP
WHERE DNAME = 'EMPLOYEEV';
---------+---------+---------+---------+---------+-DNAME
DTYPE BNAME
BTYPE
---------+---------+---------+---------+---------+-EMPLOYEEV
V
EMPLOYEE
T
CV8317.0
Notes:
Ex
cl
The view definition is stored in SYSIBM.SYSVIEWS catalog table in one or more rows.
Column TEXT defined as VARCHAR(1500) NOT NULL contains text or portion of the text
of the statement that was used to create the view. Column SEQNO contains the sequence
number of this row; the first portion of the view is on row one and successive rows have
increasing values of SEQNO.
pr
2-107
Student Notebook
1.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
EMPLOYEE
DEPARTMENT
MQT1
SUM(E.SALARY) AS TOTAL_SALARY,
SUM(E.BONUS) AS TOTAL_BONUS,
2.
SUM(E.COMMISSION) AS TOTAL_COMMISSION
Subselect
MAINTAINED BY SYSTEM
4.
3. User query
SELECT
FROM EMPLOYEE E, DEPARTMENT D
WHERE E.DEPTNO = D.DEPTNO
AND .
GROUP BY D.DEPTNO, D.DEPTNAME
CV8317.0
Notes:
cl
2. Populate the MQT with pre-computed result data (for example, using REFRESH)
Ex
3. Users then run heavyweight decision support queries targeting massive operational
tables.
4. The optimizer automatically rewrites the queries to access the MQT to satisfy the
queries much more cheaply and with better performance.
pr
V5.4.0.3
Student Notebook
Uempty
The demand on response times has reached a very high level and traditional optimization
techniques often fail to meet these new requirements.
The solution
Precompute the whole or parts of each query beforehand and materialize the results
into an MQT.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Use these materialized results to provide a timely answer when queries are submitted.
The precomputed data is the result of a query that is a subselect associated with the table,
specified as part of the CREATE/ALTER TABLE statement.
The source for a materialized query table can be base tables and views.
Automatic Query Rewrite (AQR)
MQTs can either be accessed:
Directly using SQL or
A view is only a logical definition while a materialized query table actually contains
materialized data.
Creating MQTs
You can create an MQT from scratch using the CREATE TABLE statement
pr
Ex
cl
2-109
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
REFRESH DEFERRED
This means the data in the table is not refreshed on update of the base table (except by
use of triggers), but can be refreshed at any time using the REFRESH TABLE
statement. The data in the table only reflects the result of the query as a snapshot at the
time when the REFRESH TABLE statement is processed or when it was last updated
for a user-maintained materialized query table.
MAINTAINED BY SYSTEM
This means the materialized query table is maintained by the system. Only the
REFRESH TABLE SQL statement is allowed on the table. This is the default.
MAINTAINED BY USER
This means the materialized query table is maintained by the user, who can use the
LOAD utility, an SQL data change statement, a SELECT from data change statement,
or REFRESH TABLE SQL statements on the table.
ENABLE QUERY OPTIMIZATION
This means the materialized query table can be used for query optimization. If the
Subselect specified does not satisfy the restrictions for query optimization, an error
occurs. This is the default.
DISABLE QUERY OPTIMIZATION
This means the materialized query table cannot be used for query optimization. The
table can still be queried directly.
pr
Ex
cl
MQTs are registered in the DB2 catalog table SYSIBM.SYSTABLES. The identifier in
column TYPE is M for MQTs.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Can populate:
System-maintained MQT
User-maintained MQT
Logging
Mass delete if segmented table space
Subselect executed
Catalog updated
Refresh timestamp
MQT cardinality
CV8317.0
Notes:
cl
You can use the REFRESH TABLE SQL statement to populate a system-maintained or a
user-maintained MQT.
Whenever you issue the REFRESH TABLE statement, the following actions are performed:
Ex
1. All rows are deleted from the MQT. As for regular tables, deletes are much faster if the
data is stored in a segmented table space or universal table spaces.
pr
2. The MQTs Subselect is executed to recalculate the data from the tables which are
specified in this fullselect. Access to the MQT itself is blocked during the execution of
the REFRESH TABLE statement.
3. The calculated data is then inserted into the MQT.
4. The catalog is updated.
The four steps described above are all done within a single commit scope.
2-111
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Special registers
SYSTEM
CURRENT
REFRESH ANY
AGE
All systemmaintained
query
optimization
enabled
MQTs
USER
All usermaintained
query
optimization
enabled
MQTs
None
None
ALL
NONE
All
query
optimization
enabled
MQTs
None
None
None
CV8317.0
Notes:
Special registers
cl
In addition to specifying ENABLE QUERY OPTIMIZATION there are two special registers
that govern the selection of the MQT by automatic query rewrite at run time. They are:
1. CURRENT REFRESH AGE
pr
Ex
V5.4.0.3
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
Uempty
2-113
Student Notebook
SYNONYM
CREATE SYNONYM
SEMP
FOR BART.EMP
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CREATE TABLE
EMP
BART
LISA
SELECT *
FROM SEMP
GRANT SELECT
ON EMP
TO LISA,BETH
BART
LISA
SELECT *
FROM SEMP
LISA
SELECT *
FROM LISA.SEMP
BETH
CV8317.0
Notes:
Synonym
cl
Ex
One of your own tables or views perhaps using a more meaningful name
Someone elses table or view so that you can then refer to that table or view without
specifying the first qualifier.
pr
1.
2.
3.
4.
5.
6.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SYSIBM.SYSSYNONYMS
SELECT NAME,CREATOR,TBNAME,TBCREATOR
FROM
SYSIBM.SYSSYNONYMS
WHERE CREATOR = 'TSOUD01';
---------+---------+---------+---------+---------+---------+--NAME
CREATOR
TBNAME
TBCREATOR
---------+---------+---------+---------+---------+---------+--SEMP1
TSOUD01
EMPLOYEE
TSOUD01
SEMP2
TSOUD01
EMPLOYEEV
TSOUD01
CV8317.0
Notes:
pr
Ex
cl
2-115
Student Notebook
ALIAS
CREATE ALIAS
AEMP FOR
BART.EMP
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CREATE TABLE
EMP
BART
LISA
SELECT *
FROM AEMP
GRANT SELECT
ON EMP
TO LISA,BETH
BART
LISA
SELECT *
FROM AEMP
LISA
SELECT *
FROM LISA.AEMP
BETH
CV8317.0
Notes:
Alias
cl
An alternative name that can be used in SQL statements to refer to a table or view in the
local or remote DB2 subsystem.
Points to remember about aliases
pr
Ex
1.
2.
3.
4.
V5.4.0.3
Student Notebook
Uempty
In the visual, for Lisa to create the Alias successfully, she requires the CREATEALIAS
privilege, unless either she is a DBADM or DBCTRL (see later) on the database that
contains the table BART.EMP, or she is a SYSADM or SYSCTRL (see later), or she has
System DBADM authority (see later).
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Note
pr
Ex
cl
ALIAS is used for remote table or view and then the three part name becomes relevant. We
do not discuss this here.
2-117
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SYSIBM.SYSTABLES
SELECT NAME,OWNER,TYPE,TBNAME,TBCREATOR,LOCATION
FROM
SYSIBM.SYSTABLES
WHERE TYPE
= 'A' AND
CREATOR = 'TSOUD01';
---------+---------+---------+---------+---------+---------+--NAME
OWNER
TYPE
TBNAME
TBCREATOR LOCATION
---------+---------+---------+---------+---------+---------+--AEMP1
TSOUD01
A
EMPLOYEE TSOUD01
CV8317.0
Notes:
pr
Ex
cl
V5.4.0.3
Student Notebook
Uempty
3 part name
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
1 part name
local name
remote name
SYSTABLES
table/view
alias
local name
remote name
process locally
CV8317.0
Notes:
At BIND time, DB2 resolves your 1-, 2-, or 3-part table name.
The logic followed is outlined below:
cl
1. The SQL statement specifies a 1-part name, for example SELECT ... FROM X.
Ex
X can be a synonym, table, view, or alias. First it adds the prefix (CURRENT SQLID for
dynamic SQL, BIND QUALFIER for static SQL) and looks up the catalog table
SYSIBM.SYSSYNONYMS. The relevant columns in this table are:
- NAME, synonym for the table or view
pr
a. If a row is present for the matching values in NAME and CREATOR columns, then it
is a synonym.
2-119
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Be aware that, if an alias is used to denote the table or view, the name of that table
or view, not the alias, is recorded in the catalog as the definition of the synonym.
That severs the connection between the synonym and alias, and even if the alias is
dropped and redefined, the synonym is still in effect and names the original table or
view.
If the matching row has T in column TYPE, the synonym is defined for a table and
the resolution is complete. It uses the values in columns NAME and CREATOR to
retrieve the data.
If the matching row has V in column TYPE, the synonym is defined for a view.
Using the values in columns NAME and CREATOR, it looks up for a matching row in
SYSIBM.SYSVIEWDEP on columns DNAME and DCREATOR. In addition to
SYSVIEWDEP, an internal table SYSIBM.SYSVTREE may also be looked into.
The relevant columns in SYSIBM.SYSVIEWDEP table are:
BNAME, the name of the table or view on which the view is dependent
BCREATOR, authorization ID of the owner of BNAME
cl
If BTYPE = T, the resolution is complete. It uses the values in columns BNAME and
BCREATOR to retrieve the data.
Ex
If BTYPE = V, the resolution is still not complete because the view is based on
another view. The cycle is repeated until the table can be reached!
b. If a row is not present for the matching values in the NAME and CREATOR columns,
then it is not a synonym. It is a table, a view, or an alias, and looks up the
SYSIBM.SYSTABLES catalog table. The relevant columns in this table are:
pr
V5.4.0.3
Student Notebook
Uempty
LOCATION, location name of the object of an alias. Blank for a table, a view, or
an alias that was not defined with a three-part object name.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
If the row has T in column TYPE, the resolution is complete. It uses the values in
columns NAME and CREATOR to retrieve the data.
If the row has V in column TYPE, using the values in columns NAME and
CREATOR, it looks up for a matching row in SYSIBM.SYSVIEWDEP on columns
DNAME and DCREATOR. In addition to SYSVIEWDEP, an internal table
SYSIBM.SYSVTREE may also be looked into.
The relevant columns in the SYSIBM.SYSVIEWDEP table are:
BNAME, the name of the table or view on which the view is dependent
BCREATOR, authorization ID of the owner of BNAME
If the row has A in column TYPE, using the values in columns TBNAME and
TBCREATOR, it looks up for a matching row in SYSIBM.SYSTABLES on columns
NAME and OWNER. The following possibilities exist:
A row is not present. That is, the table or view on which the alias is defined does
not exist. This results in SQLCODE -204 (undefined name).
- The row has T in column TYPE. That is, the alias is defined on a table. The
resolution is complete. It uses the values in columns NAME and OWNER to
retrieve the data.
- The row has V in column TYPE. That is, the alias is defined on a view. Using
the values in columns TBNAME and TBCREATOR, it looks up for a matching
row in SYSIBM.SYSVIEWDEP on columns DNAME and DOWNER. The
relevant columns in this table are:
pr
Ex
cl
BNAME, the name of the table or view on which the view is dependent
BCREATOR, authorization ID of the owner of BNAME
BTYPE, type of object (T is table and V is view)
2-121
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
If BTYPE = V, the resolution is still not complete because the view is based
on another view. The cycle is repeated until the table can be reached!
A row is present and column LOCATION is not blank. That is, the alias is defined
with a three-part object name, and the request is sent to the location named in
the column LOCATION.
2. The SQL statement specifies a 2-part name, for example SELECT ... FROM RAVI.X.
pr
Ex
cl
The assumption is that the communication database is properly set up at the local
location, and there is an entry for location SANJOSE. The request is simply sent to the
remote location for processing.
V5.4.0.3
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
2.7. Indexes
pr
Ex
cl
Uempty
2-123
Student Notebook
What is an index?
IX2EMPL Index
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
EMPLOYEE table
CV8317.0
Notes:
If you have no indexes on a table then every row would have to be read even when you are
looking for only 1 or 2 rows amongst, say, many thousands of rows.
cl
Also the order of rows in a table would not be maintained as rows are inserted, updated,
and deleted.
pr
Ex
An index is a DB2 object that can be used efficiently to locate a row or rows that meet
particular search conditions. It is an ordered list of values with one or more pointers called
RIDs that point to where these values can be found within the table. Conceptually, a RID is
comprised of a page number and a row locator within the page.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Access paths
Index only access
Avoid sorts
Cluster data
Foreign keys
Integrity
Uniqueness
But:
Performance impact
Delete
Insert
Update (Delete / Insert)
Disk space
Utility run times
CV8317.0
Notes:
Performance improvement
cl
There are several ways indexes can benefit your SQL transactions. These are discussed
below. Also, in some cases, indexes can improve DB2 utility run times, for example,
CHECK DATA.
Ex
Access paths
DB2 may choose to use an index in one of several different ways - matching, non-matching
or screening. DB2 can process an index either forwards or backwards. In some cases DB2
may not even need to access the table.
pr
Index-only access
If DB2 finds it can meet the requirements of an SQL query by processing only the index
then it does so because avoiding access to the table reduces I/O and CPU costs. You can
increase the likelihood of index-only access by creating a composite or fat index by
including up to 64 columns per key.
2-125
Student Notebook
Avoid sorts
A sort in an access path forces DB2 to materialize the whole result set, perhaps thousands
of rows, at OPEN CURSOR time. DB2 then knows which row to deliver to the program first
and in the right sequence. But this could mean a lot of unnecessary DB2 processing
especially if the program subsequently decides to FETCH, say, only the first 20 rows in the
result set, build only one screen of output and then terminate.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
An ORDER BY, for example, does not cause a sort if DB2 uses an index in which the
matching index rows are in the requested order. In this case, DB2 materializes the result
set FETCH by FETCH and not at OPEN CURSOR.
All sorts should be investigated in every EXPLAIN review. You should check whether DB2
really needs to do the sort.
Cluster data
A clustering index indicates that data rows should be clustered on pages in the table space
(or partition) in the same sequence as the sequence of the index. DB2 maintains that
clustering sequence as much as possible during SQL INSERT activity and during a
REORG. An application that could benefit from a clustering index, for example, is one that
frequently SELECTs rows over a range of values using BETWEEN, <, > or LIKE.
Foreign keys
These are covered in the next unit when we shall see that it is strongly recommended that
you build an index on each foreign key for performance.
Uniqueness
The UNIQUE option prevents the table from containing two or more rows with the same
value of the index key. The constraint is enforced when rows of the table are updated or
new rows are inserted or loaded.
Primary keys and unique keys
cl
These are covered in the next unit when we shall see that primary keys and unique keys
must have an index built on them.
Performance impact
Ex
Every time a row is DELETEd from or INSERTed into a table then each of the indexes
needs to be maintained. The number of indexes you build on a table are probably limited by
the required performance of INSERTs and DELETEs. You must also consider the cost of
UPDATEs to the columns in affected indexes.
pr
Indexes occupy disk space and disk space costs money. However, if you drop a column
from a composite index to save disk space then it could mean response times go up due to,
for example, the loss of index-only access. This may not always be a good tradeoff.
Some utilities, for example, REORG TABLESPACE may run longer with more indexes.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
LAST_NAME
DEPTNO
E_MAIL_ADDR
000010
000020
000030
000050
000060
000070
000090
000100
000110
000120
000130
000140
000150
000160
000170
HAAS
THOMPSON
KWAN
GEYER
STERN
PULASKI
HENDERSON
SPENSER
LUCCHESI
O'CONNELL
QUINTANA
NICHOLLS
ADAMSON
PIANKA
YOSHIMURA
A00
B01
C01
E01
D11
D21
E11
E21
A00
A00
C01
C01
D11
D11
D11
c.haas@us.ibm.com
s.kwan@us.ibm.com
j.geyer@us.ibm.com
t.spenser@us.ibm.com
m.yoshimura@us.ibm.com
CHRISTINE
MICHAEL
SALLY
JOHN
IRVING
EVA
EILEEN
THEODORE
VINCENZO
SEAN
DOLORES
HEATHER
BRUCE
ELIZABETH
MASATOSHI
CV8317.0
Notes:
cl
A unique index allows only one pointer (RID) per index value. For example, a unique index
on LAST_NAME, FIRST_NAME would prevent a row from being added to the table if its
LAST_NAME, FIRST_NAME values matched those of an existing row. On the other hand,
a non-unique index allows duplicate values.
Ex
The key of an index is the ordered set of columns (or expressions) on which the index is
defined, in the example on the visual LAST_NAME,FIRST_NAME. The individual
combination of values in these columns are called key values or just keys.
ASC (default) or DESC
pr
DB2 has the capability for backward index scans, and it is not necessary to create an
ascending and descending index on the same table columns.
UNIQUE
This specifies a unique index. DB2 does not allow you to insert or update a value that
would lead to a duplicate key. One null entry is allowed.
UNIQUE WHERE NOT NULL
Copyright IBM Corp. 1993, 2011
2-127
Student Notebook
This is like UNIQUE except that DB2 allows any number of null entries.
If the value of any column of the key is null, then the value of the whole key is considered to
be null and DB2 allows multiple occurrences of such cases.
Note
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
If you do not specify UNIQUE or UNIQUE WHERE NOT NULL then you have no control
over duplicates.
The sum of the length attributes of the columns must not be greater than the following
limits, where n is the number of columns that can contain null values, and m is the number
of varying-length columns in the key:
pr
Ex
cl
V5.4.0.3
Student Notebook
"
Uempty
Index on expression
Example
INTEGER
VARCHAR(20)
VARCHAR(20)
DEC(15,2)
FLOAT );
NOT
NOT
NOT
NOT
NULL,
NULL,
NULL,
NULL,
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
cl
You can also build an index on expression. For this, your SQL must reference an
expression equivalent to the one used in the index key.
DB2 stores the results of the expression in the index.
Ex
Any uniqueness constraint is enforced against the values that are stored in the index and
not against the original values of the columns.
pr
Restrictions:
2-129
Student Notebook
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Primary key and foreign keys with expressions are not supported.
V5.4.0.3
Student Notebook
Uempty
Clustering
EMPLOYEE
table
Non-Clustering
Index
IX2EMPL
(LAST_NAME,FIRST_NAME)
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Clustering
Index
IX1EMPL
(EMPNO)
DB2 tries to keep the data rows in the sequence of the clustering index
Copyright IBM Corporation 2011
CV8317.0
Notes:
cl
We mentioned earlier that a clustering index indicates that data rows should be clustered
on pages in the table space (or partition) in the same sequence as the sequence of the
index.
Ex
The clustering index itself is essentially no different from any other index, except that it
affects the physical order of rows in the table.
DB2 maintains that clustering sequence as much as possible during SQL INSERT activity
and during a REORG.
SQL INSERT
pr
When a row is inserted into the table, DB2 tries to place the new row in the home page
defined by the clustering index. If there is not enough room, then DB2 tries the pages close
to the home page.
CREATE TABLE has an APPEND. The default value for APPEND is NO and SQL INSERT
observes the clustering index.
2-131
Student Notebook
If the value for APPEND is YES, SQL INSERT disregards the clustering index and append
the row at the end of the table (or appropriate partition)
For a Universal table space (partition-by-range), the partition is determined by the key. For
a Universal table space (partition-by-growth), any partition with space at the end is used.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
This option applies to all user tables (except global temp tables) and is independent of
table space type.
This can be useful when queries are probes rather than range scans.
REORG
When you REORG a table space, DB2 restores the rows in the sequence defined by the
clustering index.
An application that could benefit from a clustering index, for example, is one that frequently
SELECTs rows over a range of values using BETWEEN, <, > or LIKE.
On the CREATE INDEX you can specify CLUSTER and this designates the index as the
clustering index.
You can have only one clustering index per table.
You should define a clustering index for each table. If there is no explicitly created
clustering index, DB2 uses the first index created in the chronological order as the implicit
clustering index. If there is no index on the table, DB2 uses the physical sequence.
pr
Ex
cl
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Creates:
13
45
Indexspace
86
CI_1
...4
19
13
...8
. . 13
. . 19
33
. . 33
45
62
. . 45
. . 62
75
. . 75
CI_ CI_9
CI_2
CI_A
5
CI_
CI_3
CI_B
6
CI_
CI_4
CI_C
7
CI_
8
VSAM Data Set
86
. . 86
Index
CV8317.0
Notes:
cl
An index space is the set of VSAM Linear Data Sets that hold the index data. It belongs to
the same database as the table and its table space. It can only be used for storing one
index.
Ex
The CREATE INDEX creates the index, index space and VSAM data set.
There is no explicit CREATE INDEXSPACE statement.
pr
If the index is defined with the DEFINE NO clause then this defers the physical creation of
the data set until data is first inserted into the index.
There is also an option that allows you to create the VSAM data set yourself separately.
creator.IX1EMPL and dbname.IX1EMPL must be unique.
Index name has a maximum of 128 alphanumeric characters and index space name has a
maximum of 8 alphanumeric characters.
2-133
Student Notebook
"
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
cl
The components of the USING STOGROUP clause are discussed below, first for
nonpartitioned indexes and then for partitioned indexes. If you omit USING STOGROUP,
DB2 defines the data sets using the default storage group of the database and the defaults
for PRIQTY and SECQTY.
Ex
For nonpartitioned indexes, USING STOGROUP indicates the data set for the index is
defined by DB2. This clause also gives space allocation parameters.
pr
PRIQTY
PRIQTY integer specifies the minimum primary space allocation for a DB2-managed data
set.
1. If you specify PRIQTY with a value other than -1, the primary space allocation is at least
n kilobytes, where n is the value of integer with the following exceptions:
- For 4KB page sizes, if integer is less than 12, n is 12.
2-134 DB2 10 for z/OS DB Admin Workshop Part 1
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
If the DB2 subsystem is under DFP 1.5, the maximum value allowed for PRIQTY is
64GB (67108864 kilobytes). Otherwise, the maximum value of 4GB (4194304 kilobytes)
applies.
- if the IXQTY subsystem parameter value is specified and is greater than 0, actual
primary quantity is at least the value of IXQTY.
SECQTY
SECQTY integer specifies the minimum secondary space allocation for a DB2-managed
data set.
1. If you do not specify SECQTY, the following formulas determine actual secondary
quantity:
- If the maximum size of a data set in the index is < 32 GB, the formula is:
- If the maximum size of a data set in the index is >= 32 GB, the formula is:
Ex
cl
The calculated extent in cylinders is arrived at by DB2 using a sliding scale. A sliding scale
means that the first secondary extent allocations are smaller than later secondary
allocations. For example, for the sliding scale of secondary extent allocations that DB2
uses for a 64-GB data set, the size of each secondary extent is larger for each secondary
extent that is allocated up to the 127th extent. For the 127th secondary extent and any
subsequent extents, the secondary size allocation is 559 cylinders.
2. If you specify SECQTY 0, the actual secondary quantity is 0.
pr
3. If you specify SECQTY and the value is not 0 or -1, the following applies;
This is the only rule that depends on the value of subsystem parameter MGEXTSZ
(field OPTIMIZE EXTENT on installation panel DSNTIP7).
If MGEXTSZ is YES:
- If SECQTY is specified and specified secondary quantity is not equal to -1 or 0, the
following formulas determine actual secondary quantity:
2-135
Student Notebook
If the maximum size of a data set in the index < 32 GB, the formula is:
actual secondary quantity in cylinders=
MAX(MIN(calculated extent in cylinders,127),specified secondary quantity in cylinders)
If the maximum size of a data set in the index is >= 32 GB, the formula is:
actual secondary quantity in cylinders=
MAX(MIN(calculated extent in cylinders,559),specified secondary quantity in cylinders)
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
If MGEXTSZ is NO:
Executing the CREATE INDEX statement causes only one data set to be created.
However, you might have more data than this one data set can hold. DB2 automatically
defines more data sets when they are needed. Regardless of the value in PRIQTY, when a
data set reaches its maximum size, DB2 creates a new one. To enable a data set to reach
its maximum size without running out of extents, it is recommended that you allow DB2 to
automatically choose the value of the secondary space allocations for extents.
If the index is partitioned, there is a PARTITION clause for each partition. Within a
PARTITION clause, a USING clause is optional. If a USING clause is present, it applies to
that partition in the same way that a USING clause for a secondary index applies to the
entire index.
Ex
cl
When a USING specification is absent from a PARTITION clause, the USING clause
parameters for the partition depend on whether a USING clause is specified before the
PARTITION clauses.
If the USING clause is specified, it applies to every PARTITION clause that does not
include a USING clause.
If the USING clause is not specified, the following defaults apply to the partition:
pr
V5.4.0.3
Student Notebook
Uempty
Preformatting
Two cylinders or tracks are preformatted at a time for indexes when they are created.
Preformatting means writing X'00' on the pages. Further preformatting can occur when an
application is issuing, for example, SQL INSERTs, and this can be resource-intensive. You
can minimize this by having the LOAD or REORG utilities preformat allocated space by
specifying the PREFORMAT parameter.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
PADDED
This specifies that varying-length string columns within the index are always padded with
the default pad character to their maximum length. When the index contains at least one
varying-length column, the default depends on an install option.
Only NOT PADDED indexes support index-only access.
BUFFERPOOL
A FREEPAGE value of, say, 15 means that a free page is left for every 15 pages populated
with data and this is good with sequential prefetch.
FREEPAGE is useful if you cannot prevent leaf page splits: the other half of the leaf page
does not go to the end of the index. It is generally better, however, to minimize the number
of leaf page splits by distributing all the free space you can afford to the leaf pages.
PCTFREE 20% may be reasonable if you cannot reorganize your indexes frequently.
Values of 0 are best for PCTFREE and FREEPAGE in cases where there is no insert or
update processing, or where you are inserting ever-ascending keys.
PCTFREE
PCTFREE is the percentage of free space left at the bottom of each index page by utilities
that build indexes such as LOAD, REORG, and REBUILD INDEX.
cl
The default is 10 percent. You should consider a higher value if the index key is longer than
80 bytes. To minimize leaf page splits with random inserts, at least four or five should fit in
the free space per leaf page.
Ex
The value you specify should be sufficient to accommodate any additional index entries
between one REORG and the next. REORG reestablishes free space.
pr
Because of random variation, you should plan an index reorganization when half of the
PCTFREE is used, assuming random inserts and deletes. Thus, with 10% PCTFREE, an
index reorganization is probably needed when the index has grown by 5%. Hot spots cause
leaf page splits much before this point.
Changing PCTFREE and FREEPAGE requires the index to be rebuilt in order for the new
values to take effect.
2-137
Student Notebook
RENAME INDEX
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
You can use the extended RENAME statement to rename existing indexes. As for any
other object, an index is unambiguously identified within a database by its OBID. Renaming
the index does not change this number. Plans and packages identify indexes used in their
access paths by those IDs. As a result, your plans and packages are not invalidated when
you rename an index.
V5.4.0.3
Student Notebook
Uempty
15
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
........WHERE EMPNO = 15
13
19
13
...8
...4
. . 13
. . 19
45
33
. . 33
86
Root
45
. . 45
62
. . 62
75
Non-Leaf
Pages
86
. . 75
T
A
B
L
E
. . 86
Leaf
Pages
Row
Data Page
Data Page
Data Page
Data Page
CV8317.0
Notes:
cl
When an index is built, DB2 scans the table and collect the key value and address (RID or
record ID) from each table row. Each key value and its appended RID become an index
entry.
Ex
DB2 then sorts the keys and RIDs into key sequence, and builds a set of LEAF PAGES
containing the index entries in key sequence.
Depending on your CREATE INDEX specification, DB2 may leave a certain percentage of
free space in each leaf page, and every nth leaf page may be left completely empty.
The index page size can be 4K, 8K, 16K, or 32K.
pr
The size of index pages is usually 4K, so typically 100 to 200 index entries fit on one page.
For, say, a 1,000,000 row table, the number of leaf pages might be approximately 5000.
When the leaf pages are complete, DB2 creates nonleaf pages, which enable it to find the
first index entry with a given key value very quickly, even when the table may have billions
of rows. Each nonleaf page points to a set of pages, typically 100 to 300 on the next lower
level. The nonleaf page at the top of the index tree structure is called the root page.
Copyright IBM Corp. 1993, 2011
2-139
Student Notebook
Catalog tables
SYSIBM.SYSINDEXES
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
---------+---------+---------+---------+---------+---------+---------+----NAME
INDEXSPACE
TBNAME
CLUSTERING
CLUSTERRATIOF
---------+---------+---------+---------+---------+---------+---------+----IX1EMPL
IX1EMPL
EMPLOYEE Y
+0.1000000000000000E+01
IX2EMPL
IX2EMPL
EMPLOYEE N
+0.7777777777777777E+00
IX3EMPL
IX3EMPL
EMPLOYEE N
+0.7777777777777777E+00
IX4EMPL
IX4EMPL
EMPLOYEE N
+0.7777777777777777E+00
---------+---------+---------+---------+---------+--------+-----PGSIZE BPOOL
NLEAF NLEVELS UNIQUERULE PADDED
AVGKEYLEN
---------+---------+---------+---------+---------+--------+-----4096
BP2
1
2 U
6
4096
BP2
1
2 D
N
28
4096
BP2
1
2 N
51
4096
BP3
1
2 D
N
12
SYSIBM.SYSINDEXPART
---------+---------+---------+---------+---------+ ---------+---------+----IXNAME
PQTY
SQTY PCTFREE FREEPAGE
SPACEF
---------+---------+---------+---------+---------+ ---------+---------+----IX1EMPL
-1
-1
10
0 +0.7200000000000000E+03
IX2EMPL
-1
-1
10
0 +0.7200000000000000E+03
IX3EMPL
-1
-1
10
0 +0.7200000000000000E+03
IX4EMPL
3600
360
25
15 +0.1440000000000000E+05
SYSIBM.SYSKEYS
---------+---------+---------+---------+-IXNAME
COLNAME
COLSEQ ORDERING
---------+---------+---------+---------+-IX1EMPL
EMPNO
1 A
IX2EMPL
LAST_NAME
1 A
IX2EMPL
FIRST_NAME
2 A
IX3EMPL
E_MAIL_ADDR
1 A
IX4EMPL
LAST_NAME
1 A
IX4EMPL
DEPTNO
2 D
CV8317.0
Notes:
SYSIBM.SYSINDEXES
cl
One row is inserted into the SYSIBM.SYSINDEXES catalog table when a CREATE INDEX
statement is executed.
Ex
CLUSTERING
pr
When multiplied by 100, the value of the column is the percentage of rows that are in
clustering order. For example, a value of +0.1000000000000000E+01 indicates 100%.
NLEAF
Number of active leaf pages in the index. The value is -1 if statistics have not been
gathered.
V5.4.0.3
Student Notebook
Uempty
NLEVELS
Number of levels in the index tree. The value is -1 if statistics have not been gathered.
UNIQUERULE
This indicates whether the index is unique:
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
AVGKEYLEN
Average length of keys within the index. The value is -1 if statistics have not been gathered.
SYSIBM.SYSINDEXPART
Contains one row for each nonpartitioned secondary index (NPSI) and one row for each
partition of a partitioning index or a data-partitioned secondary index (DPSI).
PQTY
The value is the primary space allocation in units of 4 KB storage blocks or -1.
For user-specified values of PRIQTY other than -1, the value is set to the primary space
allocation only if RUNSTATS INDEX with UPDATE(ALL) or UPDATE(SPACE) is executed;
otherwise, the value is zero.
PQTY is based on a value of PRIQTY in the appropriate CREATE or ALTER INDEX
statement. Unlike PQTY, however, PRIQTY asks for space in 1KB units.
A value of -1 indicates that either of the following cases is true:
PRIQTY was not specified for a CREATE INDEX statement or for any subsequent
ALTER INDEX statements.
-1 was the most recently specified value for PRIQTY, either on the CREATE INDEX
statement or a subsequent ALTER INDEX statement.
SQTY
The value is the secondary space allocation in units of 4 KB storage blocks or -1.
Ex
cl
For user-specified values of SECQTY other than -1, the value is set to the secondary
space allocation only if RUNSTATS INDEX with UPDATE(ALL) or UPDATE(SPACE) is
executed; otherwise, the value is zero.
SQTY is based on a value of SECQTY in the appropriate CREATE or ALTER INDEX
statement. Unlike SQTY, however, SECQTY asks for space in 1KB units.
A value of -1 indicates that either of the following cases is true:
pr
SECQTY was not specified for a CREATE INDEX statement or for any subsequent
ALTER INDEX statements.
-1 was the most recently specified value for SECQTY, either on the CREATE INDEX
statement or a subsequent ALTER INDEX statement.
If the value does not fit into the column, the value of the column is 0.
2-141
Student Notebook
SPACEF
Number of kilobytes of DASD storage allocated
SYSIBM.SYSKEYS
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SYSIBM.SYSKEYTARGETS
pr
Ex
cl
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Index Entry
Page-ID
Slot
Key Field(s)
Data Page
Page 8
ROW
Free Space
ID
Map
CV8317.0
Notes:
An index entry on a leaf page contains a key value and a RID. A RID is 4 bytes in length for
non-large partitioned and nonpartitioned table spaces.
cl
The first 3 bytes of a RID refer to a data page, and the fourth byte of the RID points to a slot
in the ID map array that contains the offset of the row on the page.
pr
Ex
In the case of a large partitioned table space, the RIDs in indexes on the table are 5 bytes.
The first 4 bytes of the RID refer to a data page, and the fifth byte of the RID points to a slot
in the ID map array.
2-143
Student Notebook
Overflow record
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
RID
Key Field(s)
8
Page-ID
5
Slot
Free Space
ID Map
Data Page
Page 8
Overflow pointer
100
RID
Free Space
ID Map
CV8317.0
Notes:
Ex
cl
If necessary, DB2 compacts available free space in an attempt to keep the row on its
original page.
pr
If there is insufficient free space, DB2 may have to move the expanded row to a new page
with sufficient free space leaving behind a mail forwarding address, that is, a pointer (a
RID) in the original location pointing to the new location.
If the row is increased in length again with insufficient free space on the page, DB2 moves
the row again, and the original pointer is updated to point to the new location. This avoids
the build-up over time of a chain of pointers that must be followed to locate a moved row.
2-144 DB2 10 for z/OS DB Admin Workshop Part 1
V5.4.0.3
Student Notebook
Uempty
The use of the mail forwarding address avoids having to update each index referencing
the table to point to the new location of the row. Instead, each index continues to point to
the original location of the row.
I/O wait time can be adversely affected by moved rows. If there is a mail forwarding
address to a moved row outside the range of prefetched pages, a synchronous I/O is
issued to locate the moved row.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
REORG TABLESPACE (see later) eliminates these mail forwarding addresses and
reestablishes free space.
2-145
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Key
Flag
byte
4 or 5
bytes
Non-Unique Keys
Key
RID
RID
Number
of
RIDs
(2 bytes)
RID
Flag
bytes
CV8317.0
Notes:
cl
If an index is on a column with unique values, then each index key value has one RID
appended to it. If an index is on a column with duplicate values, then each index key has
one or more RIDs appended to it. The RIDs on a RID chain are maintained in data page
number sequence to assist delete processing performance. The flag byte contains a
pseudo-delete bit which indicates whether the entry is deleted or not.
Ex
Variable length keys can be padded with blanks (to maximum length) to create fixed-length
keys. The decision to pad with blanks or not is determined by an installation option or the
PADDED / NOT PADDED option on CREATE INDEX statement
pr
If the fields can be null, a one-byte null indicator is stored with the key. This null indicator is
before the column value so all null and all not-null values are grouped together.
NOT NULL WITH DEFAULT keys are stored as NOT NULL keys, for example, no null
indicator bytes.
UNIQUE WHERE NOT NULL indexes are stored like non-unique indexes.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Non-leaf pages
3
4
Gi
Root Page 2
5
6
7
Cow
El
8
9
10
Nonleaf Page 3
Antelope 201
Bear
202
Cod
203
Cow
Dog
Eel
Leaf Page 5
204
205
206
Leaf Page 6
J
Lione
Nonleaf Page 4
Elephant 207
Frog
208
Gazelle 209
Leaf Page 7
Giraffe
Horse
Iguana
301
302
303
Jaguar 304
Leopard 305
Lion
306
Leaf Page 8
Leaf Page 9
Lioness 307
Marmot 308
Osprey 309
Leaf Page 10
CV8317.0
Notes:
cl
A leaf page has a high-key value. This high-key value is, in fact, the first key on the next
leaf page. All keys on a leaf page are less than its high-key value. For example, the
high-key for leaf page 9 is Lioness.
Ex
Nonleaf pages also contain the high-key values. But in the nonleaf pages unique high-key
values are truncated. This is to save space and allow more keys to be stored per page.
Only the low order positions are dropped. The remaining fragment is just enough to allow
DB2 to determine the range of keys for a given leaf page.
pr
For nonunique keys with many duplicates, the index entry has a long RID chain. This may
span several leaf pages. The nonleaf page contains the high RID as well as the key. By
using the RID as well as the key, DB2 can more quickly find the correct leaf page.
2-147
Student Notebook
"
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
UPDATE
DELETE
Pseudo delete
Physical delete
CV8317.0
Notes:
cl
When you INSERT a row and the free space on an index leaf page is used up, a page split
occurs.
Ex
a. if a random insert pattern is detected on the index DB2 splits the index entries 50/50.
Half the entries remain on the original page, and half of them are placed on a new
page.
DB2 updates the nonleaf page above the split page to point to the new lower-level
leaf page. This process can continue up to the root page.
pr
If there is no free space in the root page to account for a new nonleaf page, the root
page is split and a new root page is created. This results in an extra level being
added to the index structure.
V5.4.0.3
Student Notebook
Uempty
page, thereby leaving more space on the new index page for future inserts and
preventing pages that are 50% empty from being left in the index.
When the last leaf page becomes full, DB2 simply adds a new leaf page rather than
splitting the full one.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
When the last leaf page becomes full, DB2 simply adds a new leaf page rather than
splitting the full one.
You should consider reorganizing an index when leaf page splits start to appear, because
leaf page splits make sequential processing slower. This is significant for SELECTs which
process a significant slice of the index, say, thousands of index entries. However, leaf page
splits may be unavoidable if many inserts go to a hot spot which is not the end of the index.
Index page management - UPDATE
Update in the index consists of deletion of the entry on one page followed by insertion of a
new entry on the same page or a different page.
Updating a column that is a low-order field in a composite index is less likely to cause the
entry to move to another page than updating a high-order field in a composite index.
Index page management - DELETE
For page or row level locking (see later), DB2 simply switches on the pseudo-delete bit in
the one-byte flag preceding each RID to indicate that the RID has been deleted or updated.
This process is a logical delete.
A logical delete is a much faster process than the physical removal of the index entry.
It might increase the required DASD space, as both the old and new values exist for an
index update.
The space is eligible for reuse after a commit.
cl
Ex
Garbage collection is triggered by either an SQL INSERT when the page is a certain
percentage full, or an SQL FETCH when a threshold number of pseudo-deleted RIDs is
reached.
pr
For a task running with Uncommitted Read (UR) (see later), a RID with the pseudo-delete
bit turned on is considered as not being present even if the delete is not committed. Other
tasks attempting to access a RID with the pseudo-delete bit turned on are suspended until
the delete is committed.
If the table or table space is X-locked (see later), physical delete is always used.
2-149
Student Notebook
Index-controlled partitioning
Partitioning index has high-key definitions and has to be the clustering index
Clustering
Nonpartitioning
index (NPI) SI_0
(STATE) Table Space Definition
CREATE TABLESPACE USTS7
AL
IN USDB1
CT
NUMPARTS 4
DE
SEGSIZE 0;
Table Definition
FL
CREATE TABLE CUSTOMER (
IA
ACCOUNT_NUM INTEGER
IL
NOT NULL,
CUST_LAST_NAME CHAR(30),
IN
LAST_ACTIVITY_DATE DATE,
KY
STATE CHAR(2) )
LA
IN USDB1.USTS7;
MA
Index Definition
CREATE INDEX PART_IX
MD
ON CUSTOMER
MI
(ACCOUNT_NUM ASC)
MN
CLUSTER
(PARTITION 1 ENDING AT (199),
MO
PARTITION 2 ENDING AT (299),
MS
PARTITION 3 ENDING AT (399),
NC
PARTITION 4 ENDING AT (499))
Index Definition
NH
CREATE INDEX SI_0
NJ
ON CUSTOMER
NY
(STATE ASC)
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
101
102
103
104
105
106
299
201
202
203
204
205
206
201
202
203
204
205
206
399
301
302
303
304
305
306
301
302
303
304
305
306
401
402
403
404
405
406
401
402
403
404
405
406
199
999
CV8317.0
Notes:
Index-controlled partitioning
cl
Index-controlled partitioning is one of the two types of partitioning in DB2 for z/OS. The
other one is table-controlled partitioning. Table-controlled partitioning is the preferred type
as it provides more capabilities.
Ex
Note
pr
Index-controlled partitioning is possible only with a classic partitioned table space. Notice
SEGSIZE 0 in the CREATE TABLESPACE statement. In DB2 10 this ensures a classic
partitioned table space is created.
V5.4.0.3
Student Notebook
Uempty
AT
AT
AT
AT
(199),
(299),
(399),
(499) )
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
This example shows an outline of the syntax. DB2 also accepts the syntax PART n
VALUES (value) in place of PARTITION n ENDING AT (value).
Partitioning index
You have to create a partitioning index to designate which rows go into which partitions.
The partitioning index is also partitioned into the same number of VSAM data sets as is
used for the table space partitions.
You have to specify a high key value for the columns in the partitioning index. Each high
key value is the maximum value that can be placed in a partition. The high key values are
used by DB2 to determine into which partition to insert a row.
The highest key for the last partition depends on how you define the table space.
1. For table spaces you create without LARGE or DSSIZE, the constant you specify after
ENDING AT is not enforced.
2. For table spaces you create with LARGE or DSSIZE, the constant you specify after
ENDING AT is enforced.
cl
You can ALTER the high key values for one or more partitions as requirements change
over time. The altered partitions then have to be reorganized.
The partitioning index has to be the clustering index.
Ex
pr
Any index that does not specify the PART n VALUES keywords and is defined on a classic
partitioned table, is a non-partitioning index (NPI) and cannot be physically partitioned; that
is, it cannot have multiple physical partitions. However, pieces of an NPI can be allocated
across multiple volumes to reduce I/O contention.
2-151
Student Notebook
Logical partitions
In a non-partitioning index (NPI), the index entries that point to one particular data partition
constitute an index logical partition. Some utilities, for example, REBUILD INDEX can run
at the logical partition level. But NPIs can cause, for example, contention problems.
Converting index-controlled partitioning to table-controlled partitioning
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
With table-controlled partitioning, the term partitioning index has a different meaning as we
shall see in a later visual.
V5.4.0.3
Student Notebook
Uempty
Partitioning Index
PART_IX2
(ACCOUNT_NUM ASC,STATE ASC)
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Partitioning Index
PART_IX1
(ACCOUNT_NUM ASC)
Table Definition
101
102
103
104
105
106
101
102
103
104
105
106
101
102
103
104
105
106
DE
MO
MI
IL
CT
KY
201
202
203
204
205
206
201
202
203
204
205
206
201
202
203
204
205
206
NJ
FL
IA
MD
AL
NC
301
302
303
304
305
306
301
302
303
304
305
306
301
302
303
304
305
306
LA
MD
MN
MS
CT
NH
401
402
403
404
405
406
401
402
403
404
405
406
401
402
403
404
405
406
NY
IA
FL
IN
MA
NH
CV8317.0
Notes:
Table-controlled partitioning is possible either with a classic partitioned table space or with
a UTS PBR.
cl
Partitioning index
Ex
An index is regarded as a partitioning index if the columns in the index are the same as
(that is, are in the same order and have the same collating sequence), or start with the
same columns as, those specified in the PARTITION BY clause of the CREATE TABLE
statement for table-controlled partitioned tables.
pr
A partitioning index can have a superset of the partitioning columns, that is, it can contain
all the partitioning columns plus additional columns.
A partitioning index can be either partitioned or non-partitioned.
Index PART_IX1
This is a partitioning index, because its key has the same left-most columns, in the same
order, and using the same collating sequence as the columns (in this case only one
column, ACCOUNT_NUM ASC) which partitions the table.
Copyright IBM Corp. 1993, 2011
2-153
Student Notebook
The partitioning index PART_IX1 is also partitioned because the keyword PARTITIONED is
specified in the CREATE INDEX statement.
Index PART_IX2
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
This is also a partitioning index, because its key also has the same left-most columns, in
the same order, and using the same collating sequence as the columns (in this case only
one column, ACCOUNT_NUM ASC) which partition the table. In fact, it has a superset of
the partitioning columns, because column STATE is included.
pr
Ex
cl
The partitioning index PART_IX2 is not partitioned because the keyword PARTITIONED is
not specified in the CREATE INDEX statement.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Table-controlled
partitioning
Non-partitioned
Secondary Index
(NPSI) SI_1
LAST_ACTIVITY_DATE
105
101
104
106
103
102
APR
FEB
NOV
JAN
DEC
MAR
CT
DE
IL
KY
MI
MO
205
202
203
206
201
204
JAN
AUG
OCT
FEB
JUL
DEC
AL
FL
IA
NC
NJ
MD
305
301
302
306
303
304
SEP
NOV
JUL
JUN
MAY
APR
CT
LA
MD
NH
MN
MS
JUL
403
402
404
405
406
401
MAR
NOV
FEB
JUN
OCT
SEP
FL
IA
IN
MA
NH
NY
OCT
JAN
FEB
MAR
APR
MAY
JUN
AUG
SEP
Table Definition
CREATE TABLE CUSTOMER (
ACCOUNT_NUM INTEGER NOT NULL,
CUST_LAST_NAME CHAR(30),
LAST_ACTIVITY_DATE DATE,
STATE CHAR(2) )
PARTITION BY (ACCOUNT_NUM ASC)
(PARTITION 1 ENDING AT (199),
PARTITION 2 ENDING AT (299),
PARTITION 3 ENDING AT (399),
PARTITION 4 ENDING AT (499))
IN USDB1.USTS8;
Index Definition (NPSI)
CREATE INDEX SI_1 ON CUSTOMER
(LAST_ACTIVITY_DATE ASC);
NOV
DEC
CV8317.0
Notes:
cl
A non-partitioning (or secondary) index is any index where the columns do not coincide
with the partitioning columns of the table. That is, a non-partitioning (or secondary) index is
any index which is not a partitioning index.
There are two types of non-partitioning (or secondary) index:
Ex
pr
Index SI_1 is a secondary index, because its key (LAST_ACTIVITY_DATE ASC) does not
have the same left-most columns as those which partition the table (in this case,
ACCOUNT_NUM ASC).
Also index SI_1 lacks the option PARTITIONED so the index is non-partitioned. It is made
up of a single data set.
Such an index is called a Non-Partitioned Secondary Index (NPSI).
Copyright IBM Corp. 1993, 2011
2-155
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Table-controlled
partitioning
Data Partitioned
Secondary Index
(DPSI) SI_2
LAST_ACTIVITY_DATE
105
101
104
106
103
102
APR
FEB
NOV
JAN
DEC
MAR
CT
DE
IL
KY
MI
MO
205
202
203
206
201
204
JAN
AUG
OCT
FEB
JUL
DEC
AL
FL
IA
NC
NJ
MD
JAN
FEB
JUL
AUG
OCT
DEC
305
301
302
306
303
304
SEP
NOV
JUL
JUN
MAY
APR
CT
LA
MD
NH
MN
MS
APR
MAY
JUN
JUL
SEP
NOV
403
402
404
405
406
401
MAR
NOV
FEB
JUN
OCT
SEP
FL
IA
IN
MA
NH
NY
FEB
MAR
JUN
SEP
OCT
NOV
Table Definition
CREATE TABLE CUSTOMER (
ACCOUNT_NUM INTEGER NOT NULL,
CUST_LAST_NAME CHAR(30),
LAST_ACTIVITY_DATE DATE,
STATE CHAR(2) )
PARTITION BY (ACCOUNT_NUM ASC)
(PARTITION 1 ENDING AT (199),
PARTITION 2 ENDING AT (299),
PARTITION 3 ENDING AT (399),
PARTITION 4 ENDING AT (499))
IN USDB1.USTS8;
Index Definition (DPSI)
CREATE INDEX SI_2 ON CUSTOMER
(LAST_ACTIVITY_DATE ASC)
PARTITIONED;
JAN
FEB
MAR
APR
NOV
DEC
CV8317.0
Notes:
cl
Ex
pr
The DPSI SI_2 is made up of multiple physical index partitions. The number of these index
partitions equals the number of table space partitions. All RIDs of any one index partition
point to the data rows of the corresponding table space partition.
Each partition of the DPSI SI_2 potentially has values for all dates JAN, .....DEC.
If the keyword UNIQUE is used when creating a DPSI, the uniqueness is enforced within
each partition, not across partitions.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
For example, if you run multiple LOAD jobs concurrently to load the partitions of a
partitioned table space and if all indexes on the table are partitioned then index page
contention is eliminated because there are no shared pages between the partitions on
which contention can occur.
Another example is that online REORG of individual partitions can benefit from DPSIs.
DPSIs and query performance
Queries with predicates that solely reference columns of the DPSI are likely to experience
performance degradation due to the need to probe each partition of the index for values
that satisfy the predicate.
Queries with predicates that reference the DPSI and which also reference columns of the
partitioning index can be restricted to specific partitions and therefore can benefit from the
DPSI organization.
pr
Ex
cl
The DB2 optimizer is aware of the nature of DPSIs and takes this into account when
determining the best access path.
2-157
Student Notebook
Table-controlled partitioning
partition one way and cluster another way
Clustering
Sequence
Non-Partitioning,
Partitioned and
Table-controlled
Clustering
partitioning
Index IX2
Table Definition
CREATE TABLE CUSTOMER (
ACCOUNT_NUM INTEGER NOT NULL,
CUST_LAST_NAME CHAR(30),
LAST_ACTIVITY_DATE DATE,
STATE CHAR(2) )
PARTITION BY (ACCOUNT_NUM ASC)
(PARTITION 1 ENDING AT (199),
PARTITION 2 ENDING AT (299),
PARTITION 3 ENDING AT (399),
PARTITION 4 ENDING AT (499))
IN USDB1.USTS8;
Index Definition
CREATE INDEX PART_IX1
ON CUSTOMER
(ACCOUNT_NUM ASC)
PARTITIONED
Index Definition
CREATE INDEX IX2
ON CUSTOMER
(LAST_ACTIVITY_DATE ASC)
PARTITIONED
CLUSTER
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Partitioning,
Partitioned but
Non-Clustering
Index PART_IX1
101
102
103
104
105
106
106
101
102
105
104
103
JAN
FEB
MAR
APR
NOV
DEC
KY
DE
MO
CT
IL
MI
JAN
FEB
MAR
APR
NOV
DEC
201
202
203
204
205
206
205
206
201
202
203
204
JAN
FEB
JUL
AUG
OCT
DEC
AL
NC
NJ
FL
IA
MD
JAN
FEB
JUL
AUG
OCT
DEC
301
302
303
304
305
306
304
303
306
302
305
301
APR
MAY
JUN
JUL
SEP
NOV
MS
MN
NH
MD
CT
LA
APR
MAY
JUN
JUL
SEP
NOV
401
402
403
404
405
406
404
403
405
401
406
402
FEB
MAR
JUN
SEP
OCT
NOV
IN
FL
MA
NY
NH
IA
FEB
MAR
JUN
SEP
OCT
NOV
Figure 2-91. Table-controlled partitioning - partition one way and cluster another way
CV8317.0
Notes:
cl
With table-controlled partitioning the clustering index can be any one of the indexes on the
table whether partitioning, nonpartitioning, partitioned or non-partitioned.
No explicit CLUSTER index
Ex
If the clustering index is not defined, then the oldest index created on the table is used as
the clustering index.
No index at all
pr
If there are no indexes defined on the partitioned table, then clustering is done using the
partitioning columns specified in the PARTITION BY clause.
V5.4.0.3
Student Notebook
Uempty
Index PART_IX1
This has the following attributes:
Partitioning, because its key has the same left-most columns, in the same order, and
using the same collating sequence as the columns (in this case, only one column,
ACCOUNT_NUM ASC) which partitions the table.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Non-partitioning, because its key is different from the column (ACCOUNT_NUM ASC)
which partitions the table.
Partitioned, because the keyword PARTITIONED is specified on the CREATE INDEX
statement.
With table-controlled partitioning, the partitioning columns determine which partition the
rows go in and the clustering index controls the location within the partition. This example
illustrates that partitioning and clustering can be done different ways.
cl
You also have the capability to change the clustering index with the ALTER INDEX
statement. Since only one index can be explicitly defined as a clustering index, you must
follow these steps to change the clustering:
Ex
1. ALTER INDEX <index name> NOT CLUSTER on the current clustering index.
Clustering continues to be done according to this index until a new clustering index is
explicitly defined.
pr
2. ALTER INDEX <index name> CLUSTER on any index that should become the
clustering index. Rows added to the table after these two ALTERs are clustered
according to the new clustering index, but the existing rows remain in their current
location.
REORG the table space to rearrange the rows in the new clustering index order. All
existing rows are rearranged in the new clustering sequence. Any new rows are inserted in
the new clustering sequence.
"
2-159
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
---------+---------+---------+-----NAME
CLUSTERING INDEXTYPE
---------+---------+---------+-----TAB3_IX1
N
P
TAB3_IX2
N
D
TAB3_IX3
N
D
TAB3_IX4
N
P
TAB3_IX5
Y
D
SYSIBM.SYSCOLUMNS
---------+---------+---------+---------+------NAME
PARTKEY_COLSEQ PARTKEY_ORDERING
---------+---------+---------+---------+------EMPNO
1 A
FIRST_NAME
0
E_MAIL_ADDR
0
LAST_NAME
0
SYSIBM.SYSTABLEPART
SYSIBM.SYSTABLES
---------+---------+--------NAME
PARTKEYCOLNUM
---------+---------+--------EMPLOYEE
1
CV8317.0
Notes:
cl
SYSIBM.SYSINDEXES
Ex
INDEXTYPE
pr
SYSIBM.SYSCOLUMNS
PARTKEY_COLSEQ
V5.4.0.3
Student Notebook
Uempty
PARTKEY_ORDERING
Order of the column in the partitioning key:
A Ascending
D Descending
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SYSIBM.SYSTABLEPART
LIMITKEY
The highest value of the limit key of the partition in an internal format. Note that
HEX_LIMITKEY_INTERNAL shows the value in hex. This is not a column in the catalog
table.
SYSIBM.SYSTABLES
PARTKEYCOLNUM
pr
Ex
cl
2-161
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Student Notebook
V5.4.0.3
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
Uempty
2-163
Student Notebook
Index compression
Software compression is done on a block of data
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
After a page is read from disk, a decompress routine is used to expand the
page from 4 KB to either 8 KB, 16KB, or 32 KB
Prior to writing a page to disk, a compress routine is used to shrink an 8 KB,
16 KB, or 32 KB page to a 4 KB page
CV8317.0
Notes:
cl
In data warehouse type applications, it is common to have many very large indexes defined
on large tables. It is also possible to have index spaces that are much larger than the tables
they are based upon. In order to lower the cost of ownership and to improve scalability
(more index entries in a single physical index data set), in DB2 10, you can choose to
compress your indexes and also define indexes with page sizes larger than 4 KB.
pr
Ex
V5.4.0.3
Student Notebook
Uempty
DB2 only compresses leaf pages, which represent the vast majority of the index space.
However, even the non-leaf pages are read into the I/O work area and then copied (instead
of being expanded, since they are not compressed) into the buffer pool.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Unlike data compression, log records for index keys are not compressed and image copies
for indexes are not compressed. However, BSAM compression can be used to compress
index image copies on DASD, and tape controllers are capable of compressing all data
sets on tape.
Index compression does not use Ziv-Lempel and does not use a compression dictionary.
Rather index compression takes advantage of the fact that indexes contain sorted keys.
Some access methods such as VSAM in z/OS have long employed the concept of prefix
compression for indexes. That is what DB2 is now doing for indexes.
DB2 compresses the index keys as well as RID (record identifier) Lists. At the end of each
page is a key map which consists of two bytes for each distinct key value. This key map is
not written to disk for a compressed index since the key map can be reconstructed when a
compressed page is read from disk. Normally the key map does not constitute a large
percentage of the space, but an exception would be a unique index with small keys, in
which case DB2 can fit a lot of distinct key values on one page. For example, if there are
400 distinct key values on a page, then the key map consumes 800 bytes. As the
non-uniqueness increases, the space for the key map decreases and the space consumed
by RID Lists increases. When a RID List is compressed, we can expect to save one byte
for every three bytes in the RID list.
The chief technique used by DB2 to compress the indexes is called prefix compression.
Prefix compression is made possible by the fact that the keys on each index page are
stored in sorted order from the left byte to right, either in ascending or descending order.
The left-most bytes that are common from one key to the next constitute the prefix. Of
course, the prefix length varies throughout the index, but generally the longer the prefix is,
the better the compression is. Table 2-1 illustrates the concept of prefixes.
Ex
cl
This sample index contains three CHAR(20) columns, the last name followed by a birth
date, followed by the first name. Since the last name is defined as CHAR(20), we can
expect to see some common prefixes of at least 20 bytes. In this case, since the first three
characters of the second column tend to be repeated, they would be included in the prefix.
Therefore the prefix for the 5 instances of Smith would be 23 bytes, and DB2 saves 4 of the
occurrences of this prefix (minus the cost of control information). For Smithers and
Smithson, 5 bytes are saved for the first 5 characters that are common to the previous key.
pr
BIRTHDATE DATE
1980-02-05
1980-04-22
1982-10-11
1984-05-31
FIRSTNAME CHAR(20)
John
Andrea
Bill
Tommie
2-165
Student Notebook
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DSN1PRNT, DSN1COPY
DSN1COMP
CV8317.0
Notes:
cl
The CREATE INDEX and ALTER INDEX have the keyword COMPRESS YES/NO. If you
ALTER INDEX COMPRESS YES/NO, the index is placed in Rebuild Pending.
Compression takes place for the whole index, cannot be activated at the partition level.
Ex
Note
pr
IMAGE COPY of a compressed index creates an uncompressed format output file. This
likely causes the size of the space required to store the image copy data set to exceed
the size of the source index.
Utility DSN1PRNT is changed to print the contents of compressed VSAM index data sets. If
you specify PARM='PRINT', unformatted compressed 4 KB pages are printed. If you
specify PARM='PRINT,EXPAND', unformatted uncompressed 8 KB, 16 KB or 32 KB pages
are printed. If you specify PARM='PRINT,FORMAT,EXPAND', formatted uncompressed 8
KB, 16 KB or 32 KB pages are printed. If you specify PARM='PRINT,FORMAT', formatted
Copyright IBM Corp. 1993, 2011
2-167
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
As with table compression, you can use DSN1COMP to estimate the effectiveness of
compressing an index. This helps you decide which indexes benefit from compression.
Only one parameter is required for index estimates, PARM='LEAFLIM(x)', where 'x' is the
number of leaf pages to be scanned. If you wish to scan all index pages, do not specify the
LEAFLIM parameter. The estimate provides you with an evaluation of compression using
different index page sizes. It is very important that you do not choose a larger index page
size than what is recommended. You can end up wasting valuable buffer pool space if you
do. A good rule of thumb is to stop below 50% unused space when choosing the
compressed index page size.
Note
pr
Ex
cl
Index compression always compresses down, whatever index page size you choose, to
a 4 KB page on disk. As inserts and deletes are processed, DB2 keeps track of the
available space remaining for compressed data (keys/RIDs) as it would fit on disk (4 KB).
Once DB2 determines there is no more available space in the compressed version of the
page, it does not allow additional inserts against the non-compressed index page. This
can cause you to waste space in the uncompressed index page and your buffer pool
pages. It is best to choose a buffer/page size that reduces unused buffer space. One size
does not fit all!
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Data
Level of compression
Row
Page (1)
CPU overhead
Accounting
Accounting and/or
DBM1 SRB
Compression in DASD
Yes
Yes
Yes
No
Compression dictionary
Yes
No (2)
10 to 90%
25 to 75% (3)
CV8317.0
Notes:
Use DSN1COMP utility to simulate compression ratio without real index compression.
cl
Ex
pr
2-169
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Student Notebook
V5.4.0.3
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
Uempty
2-171
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
Ex
cl
If you do not specify the IN clause in CREATE TABLE statement, DB2 creates an implicit
database. The table space that is implicitly created is a partition-by-growth universal table
space if the CREATE TABLE statement does not specify PARTITION BY clause, or a
partition-by range universal table space if the CREATE TABLE statement specifies
PARTITION BY clause. An implicit database can have multiple such table spaces, one for
each of the tables stored in that database.
pr
The name of the database is DSNnnnnn where nnnnn ranges from 00001 to 10000 and
creator is SYSIBM. You cannot explicitly create a database with the same naming
convention used for the implicit databases, nor can you specify the name of the implicit
database in the IN clause of CREATE TABLE or CREATE TABLESPACE statement.
The characteristics of the implicit database depend on the DSNZPARM settings. You can
use the ALTER DATABASE statement to change the characteristics.
The implicit partition-by-growth universal table space is created with DSSIZE 4G,
SEGSIZE 32, MAXPARTITIONS 256, LOCKSIZE ROW, and LOCKMAX SYSTEM.
V5.4.0.3
Student Notebook
Uempty
The partition-by-range universal table space is implicitly created with DSSIZE 4G,
SEGSIZE 32, MAXPARTITIONS 0, LOCKSIZE ROW, and LOCKMAX SYSTEM.
System required objects, for example, indexes to enforce primary or unique keys are
automatically created for both implicit database and DSNDB04 if the table space is implicit.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Note
You can simplify your database implementation by letting DB2 implicitly create certain
objects for you. On a CREATE TABLE statement, if you do not specify a database name,
DB2 uses an existing implicitly created database. If an implicitly created database does not
exist, DB2 creates one using the naming convention of DSNxxxxx (from DSN00001 to
DSNnnnnn, where nnnnn is the maximum value of the sequence
SYSIBM.DSNSEQ_IMPLICITDB, with a default of 10000).
If you wish to lower the 10000 limit, the maximum value for the sequence
SYSIBM.DSNSEQ_IMPLICITDB can be altered with the installation SYSADM authority
using the ALTER SEQUENCE statement with the MAXVALUE option. For example, to
lower the limit to 500, you would use the following statement:
pr
Ex
cl
2-173
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
cl
The visual shows you exactly how DB2 reuses those implicit databases. In V8, you were
allowed to use DSN followed by a 5-digit number as the database name. Due to that, it is
possible that after migration to V9, you already have databases defined in your subsystem
that use names that are now reserved for implicit databases.
Ex
We assume that databases DSN00004 and DSN00005 are explicit databases that have
been migrated from V8 to V9.
1. Create table TB1 without using the IN clause on the CREATE TABLE statement. As a
result, DB2 creates database DSN00001 implicitly for this table.
pr
2. Create table TB2 without using the IN clause on the CREATE TABLE statement.
DSN00001 is not used to store a second table now. Instead, the database name is
incremented by 1 and DB2 implicitly creates database DSN00002.
3. Again and for all following CREATE TABLE statements, the IN clause is not used. The
database name is incremented by 1 again and database DSN00003 is implicitly
generated.
2-174 DB2 10 for z/OS DB Admin Workshop Part 1
V5.4.0.3
Student Notebook
Uempty
4. Starting with the creation of table TB4, the numbering of the table and the database
diverges. Since DSN00004 already exists, DB2 continues to check whether DSN00005
is available for being created. Since this one also exists, DB2 must increment the
number one more time, and finally implicitly creates database DSN00006.
5. You drop table TB2 now. Database DSN00002 that hosted this table is not implicitly
dropped and continues to exist as an empty database.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
6. If you create a new table TB5 subsequently, DB2 does not reuse database DSN00002,
which has just become empty. Instead, it continues incrementing the database names
by 1 and creates the next database in row, which is DSN00007.
7. If you continuously create new tables without using the IN clause, DB2 also
continuously creates new implicit databases until it finally reaches DSN10000. Once
this number is reached, DB2 starts wrapping around.
8. The next CREATE TABLE statement, which could be, for example, for table TB10001,
again starts using the implicit databases with the counter reset to 1. Following this, DB2
creates table TB10001 in DSN00001, which has been used in the past for hosting TB1.
DSN00001 now contains two tables.
pr
Ex
cl
The whole process continues over and over again, incrementing the number or database
that is to be used by 1 after every CREATE TABLE without an IN clause in it.
2-175
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SYSIBM.SYSTABLES
SELECT NAME,DBNAME,TSNAME,OWNER
FROM
SYSIBM.SYSTABLES
WHERE NAME = TT' AND
CREATOR = USER;
---------+-----+---------+---------+----NAME
DBNAME
TSNAME
OWNER
---------+-----+---------+---------+----TT
DSN00050
TT
TSOUD01
SYSIBM.SYSINDEXES
SELECT NAME,OWNER,TBNAME,TBCREATOR
FROM
SYSIBM.SYSINDEXES
WHERE TBNAME = TT';
---------+---------+---------+---------+---------+----NAME
OWNER
TBNAME
TBCREATOR
---------+---------+---------+---------+---------+----TTABCDEF_#_Y6P TSOUD01
TT
TSOUD01
TTABCDEF_#_VZP TSOUD01
TT
TSOUD01
Copyright IBM Corporation 2011
CV8317.0
pr
Ex
cl
Notes:
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
vcat.DSNDBx.db.ps.m000y.Lnnn
vcat
db
ps
Lnnn
CV8317.0
Notes:
A naming convention is used for the names of the VSAM data sets associated with table
spaces and index spaces.
cl
VSAM data sets for table spaces are named like this example:
VCAT.DSNDBD.DB1.TS1.I0001.A001
Ex
VSAM data sets for index spaces are named like this example:
VCAT.DSNDBD.DB1.IX1.I0001.A001
pr
2-177
Student Notebook
Object dependencies
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DATABASE
TABLESPACE
TABLE
CONSTRAINT
INDEX
VIEW
SYNONYM
SYNONYM
CV8317.0
Notes:
pr
Ex
cl
Any dependent objects are dropped such as indexes, views, synonyms, and triggers
(see CV841)
Any referential constraints are dropped
Entries for PROJECT are removed from SYSTABLES and SYSCOLUMNS
Security privileges for PROJECT are removed from SYSTABAUTH
Any dependent application plans or packages are invalidated
Cached dynamic SQL statements that use PROJECT are removed
Access path statistics and space statistics for the table are deleted
DB2 reclaims the space if PROJECT is in a segmented table space
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
RESTRICT ON DROP
You must alter tables that have been created with RESTRICT ON DROP to remove the
restriction before you can drop them.
Database hierarchy
pr
Ex
cl
DB2 maintains control information in the DB2 Directory (DSNDB01) which describes this
hierarchy for each database. This control information is called a Database Descriptor DBD.
2-179
Student Notebook
Checkpoint quiz
1 CREATE TABLE TEMPL (C INTEGER) PARTITION BY (C)
(PARTITION 1 ENDING AT(100),
PARTITION 2 ENDING AT(200))
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
STOGROUP DSN8G200
BUFFERPOOL BP1
IN DATABASE DSN8D2AP
STOGROUP DSN8G200
BUFFERPOOL BP1
TABLE
IN DSN8D2AP
USING STOGROUP DSN8G200
1.
TEMPL
2.
MYEMP
3.
YREMP
4.
TDEPT
TS
DB
SG
IN DSN8SEMP
IN DSN8D2AP
IN DSN8D2AP.DSN8SDEP
CV8317.0
Notes:
This quiz helps to recap which table space, database and storage group would be used
when you create a table.
cl
Determine the:
1. Table space
Ex
2. Database and
3. Storage group
that would be used in each of the examples in the visual and complete the grid.
pr
Can you also infer the type of table space, SEGSIZE, and MAXPARTITIONS in each case?
Each example should be treated independently of the others.
V5.4.0.3
Student Notebook
Uempty
Unit summary
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Having
Havingcompleted
completedthis
thisunit,
unit, you
youshould
shouldbe
beable
ableto:
to:
Describe
Describethe
theDB2
DB2objects
objectsthat
thatmake
makeup
upaaDB2
DB2database
database
Select
Selectthe
themost
mostappropriate
appropriateparameters
parameters for
forthese
theseobjects
objectsso
so
that
thatthey
theyare
areimplemented
implementedwith
withthe
themost
mostappropriate
appropriate
attributes
attributes
Create
Createstorage
storagegroups,
groups,databases,
databases,table
tablespaces,
spaces,tables,
tables,
views,
views,synonyms,
synonyms,aliases,
aliases,materialized
materialized query
query tables,
tables, and
and
indexes
indexes
Alter
Alter the
the attributes
attributesof
ofDB2
DB2database
databaseobjects
objectsas
asrequirements
requirements
change
change over
over time
time
Describe
Describehow
howdata
dataisisstored
storedin
inaaDB2
DB2database
database
CV8317.0
pr
Ex
cl
Notes:
2-181
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Student Notebook
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
cl
Accountability:
pr
Ex
References
SC19-2968
SC19-2972
SC19-2983
3-1
Student Notebook
SC18-9844
SC18-9854
SC18-7413
SC18-7416
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SC18-9840
pr
Ex
cl
SC18-7426
3-2
V5.4.0.3
Student Notebook
Uempty
Unit objectives
After
Aftercompleting
completingthis
thisunit,
unit,you
youshould
shouldbe
beable
ableto:
to:
Describe
the
concepts
of:
Describe the concepts of:
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
PRIMARY
PRIMARYKEY
KEY
UNIQUE
UNIQUEKEY
KEYand
and
FOREIGN
KEY
FOREIGN KEY
Implement
Implementthese
thesekeys
keysininDB2
DB2
Describe
Describethe
theconcepts
conceptsof:
of:
REFERENTIAL
REFERENTIALCONSTRAINT
CONSTRAINTand
and
REFERENTIAL
INTEGRITY
REFERENTIAL INTEGRITY
Implement
ImplementREFERENTIAL
REFERENTIALCONSTRAINTS
CONSTRAINTSon
on
New
Newtables
tables
Existing
Existingtables
tables
With
Withthe
theappropriate
appropriateDELETE
DELETErules
rules
Drop
DropREFERENTIAL
REFERENTIALCONSTRAINTS
CONSTRAINTSand
andtheir
theirenforcing
enforcingindexes
indexes
Query
the
appropriate
catalog
tables
for
RI
details
Query the appropriate catalog tables for RI details
Describe
Describethe
therelationships
relationshipsthat
thatrequire
requirespecial
specialhandling
handling
Describe
what
informational
constraints
are
and
Describe what informational constraints are andtheir
theiruse
use
Discuss
identity
columns
and
sequence
objects
Discuss identity columns and sequence objects
Copyright IBM Corporation 2011
CV8317.0
pr
Ex
cl
Notes:
3-3
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Student Notebook
3-4
V5.4.0.3
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
Uempty
3-5
Student Notebook
Primary key
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DEPT
DEPTNO
E01 Support Services
E11 Operations
E21 Software Support
EMPNO
EMP
000050
000280
NI_NUMBER WORKDEPT
John
Ethel
B
R
GEYER
SCHNEIDER
US01234
US05678
E01
E11
Primary
Key
Primary Key
Must be UNIQUE
Cannot be NULL
Only ONE per table
CV8317.0
Notes:
Primary key
cl
A primary key is a column (or set of columns) that identifies a row and distinguishes it from
every other occurrence of a row.
Ex
A primary key must be UNIQUE. You can have more columns than are necessary to
achieve uniqueness, although this is generally not recommended.
pr
NOT NULL WITH DEFAULT is generally not recommended for primary key columns unless
they have, say, the TIMESTAMP attribute.
Entity integrity
Entity integrity exists when the uniqueness of each occurrence of a row is guaranteed.
3-6
V5.4.0.3
Student Notebook
Uempty
Primary index
A unique index known as the primary index must be defined on the columns of the primary
key. The sequence of the columns in this index must be the same as that of the primary key
definition.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Choose a primary key that is not updateable. It is good practice to have unique identifiers
that remain the same.
As we shall see later, the columns of a primary key can be referenced by a foreign key.
pr
Ex
cl
You can still define a primary key even if no other table references the primary key table.
The definition of a primary key will force the creation of a primary index, and once a primary
index has been created, DB2 won't allow it to be inadvertently dropped. You will need to
take into account any possible index maintenance overhead in this case.
3-7
Student Notebook
Unique key
DEPTNO
E01 Support Services
E11 Operations
E21 Software Support
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DEPT
EMPNO
EMP
000050
000280
John
Ethel
B
R
NI_NUMBER WORKDEPT
GEYER
US01234
E01
SCHNEIDER
US05678
E11
Unique
Key
DB2 forces you to create a unique index on the columns that constitute each
unique key
DB2 enforces the unique constraints whenever you add or modify data
DB2 does not allow you to drop an enforcing index
Copyright IBM Corporation 2011
CV8317.0
Notes:
Unique keys
cl
You can also define any number of unique keys to ensure that no two values are equal.
DB2 enforces the unique constraints whenever you add or modify data.
Ex
A unique key may be composed of one or more columns. Each column must be defined as
NOT NULL.
You can define a unique key by using the UNIQUE clause of the CREATE TABLE or
ALTER TABLE statement.
pr
DB2 forces you to create a unique index on the columns that constitute a unique key.
As we shall see later, the columns of a unique key can be referenced by a foreign key.
3-8
V5.4.0.3
Student Notebook
Uempty
Foreign key
DEPTNO
E01 Support Services
E11 Operations
E21 Software Support
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DEPT
(Parent
table)
EMPNO
EMP
000050
(Dependent 000280
table)
John
Ethel
B
R
NI_NUMBER WORKDEPT
GEYER
US01234
E01
SCHNEIDER
US05678
E11
REFERENTIAL CONSTRAINT
Foreign Key
Foreign
Key
CV8317.0
Notes:
Foreign key
cl
A foreign key is a column (or set of columns) associated with a primary or unique key.
Ex
A foreign key value can be NULL. A multi-column foreign key is considered NULL if at least
one of the columns is NULL.
You can define any number of foreign keys.
You can define a foreign key in a CREATE TABLE or ALTER TABLE statement.
pr
Referential constraint
3-9
Student Notebook
A parent table is a table containing a parent key that is referenced by one or more foreign
keys.
A dependent table is a table containing a foreign key. It can contain foreign keys
referencing one or more parent tables. A dependent table can also be a parent table.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Referential integrity
If you define an index on the foreign key columns, the index columns may be ASC or
DESC, which may be different to the ASC or DESC attribute of the corresponding parent
key index.
pr
Ex
cl
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DEPT
DEPTNO
E01 Support Services
E11 Operations
E21 Software Support
Parent
Key
EMPNO
EMP
000050
000280
NI_NUMBER WORKDEPT
John
Ethel
B
R
GEYER
SCHNEIDER
US01234
US05678
E01
E11
REFERENTIAL CONSTRAINT
Foreign
Key
CV8317.0
Notes:
Dependent Table
--------------...
...
...
...
Col5
...
...
Col8
pr
Ex
cl
Parent Table
-----------Col1
...
Col3
...
...
...
...
...
Col5 and Col8 need not be adjacent, and Col8 may precede Col5 in the foreign key.
Col8 must have the same data type and size as Col1, and Col5 must have the same data
type and size as Col3.
Copyright IBM Corp. 1993, 2011
3-11
Student Notebook
FOREIGN KEY
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
You can add a row to a parent table You can add a row to a dependent
provided the value of its PK is
table if the value of its FK is NULL (if
UNIQUE and NOT NULL. That is, the permitted) or equal to a value of its
PK obeys the entity integrity rule.
PK.
INSERT
UPDATE
DELETE
CV8317.0
Notes:
cl
INSERTs
UPDATEs
DELETEs
Ex
Certain rules define the referential constraints to be met between parent tables and
dependent tables.
pr
In most cases, the rule is implicit. In one case (the DELETE rule for PKs), you choose the
rule explicitly.
V5.4.0.3
Student Notebook
Uempty
DELETE rules
The DELETE rule for PKs states the requirements to be met when you delete a row in a
parent table. The options are:
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
1. RESTRICT
- A row in a parent table cannot be deleted if there are rows in the dependent tables
with FK values equal to the PK value of this row.
- Default if CURRENT RULES = DB2
2. CASCADE
- If a row in a parent table is deleted using SQL DELETE, then all rows in the
dependent tables with FK values equal to the PK value of this row will also be
deleted.
3. SET NULL
- If a row in a parent table is deleted using SQL DELETE, then the FK of all rows in
the dependent tables with FK values equal to the PK value of this row will be
changed to NULL.
4. NO ACTION
- Same as for RESTRICT except for self-referencing tables.
- Default if CURRENT RULES = STD
3-13
Student Notebook
On existing tables
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
If there is no unique index on the specified primary key or unique key, then DB2 considers
the table definition to be incomplete.
cl
The STATUS column in SYSIBM.SYSTABLES equals 'I', and the TABLESTATUS column
gives the reason for the table being incomplete.
Ex
DB2 will not allow SQL DML access or utility access to an incomplete table.
pr
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
PRIMARY KEY
cl
The first step in setting up a new table's RI is to specify the PRIMARY KEY in the CREATE
TABLE statement.
Ex
The CONSTRAINT clause for defining a PRIMARY KEY provides consistency with the way
other DB2 constraints can be defined.
pr
You can give a PRIMARY KEY constraint a NAME of up to 128 characters. This name must
be unique for all constraints defined for a given table. If you omit a constraint name, then
DB2 will provide one for you by using the name of the first column referred to in the
constraint definition.
3-15
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
ON EMP
(WORKDEPT);
--
ON EMP
(NI_NUMBER);
CV8317.0
Notes:
UNIQUE KEYs
cl
You specify a new table's UNIQUE keys in the CREATE TABLE statement.
Ex
The CONSTRAINT clause for defining a UNIQUE key provides consistency with the way
other DB2 constraints can be defined.
pr
You can give a UNIQUE key constraint a NAME of up to128 characters. This name must be
unique for all constraints defined for a given table. If you omit a constraint name, then DB2
will provide one for you by using the name of the first column referred to in the constraint
definition.
FOREIGN KEYs
You specify a new table's FOREIGN KEYs in the CREATE TABLE statement.
A FOREIGN KEY can reference a PRIMARY KEY or a UNIQUE key.
V5.4.0.3
Student Notebook
Uempty
The CONSTRAINT clause for defining a FOREIGN KEY provides consistency with the way
other DB2 constraints can be defined.
You can give a FOREIGN KEY constraint a NAME. DB2 uses this name in error messages
and also returns it in the SQLCA to identify a violated constraint.
This name must be unique for all constraints defined for a given table.
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
If you omit a constraint name, then DB2 will provide one for you by using the name of the
first column referred to in the constraint definition.
3-17
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
(WORKDEPT)
CV8317.0
Notes:
cl
An informational referential constraint is a referential constraint that DB2 does not enforce
during normal operations. Use these constraints only when referential integrity can be
enforced by another means, such as when retrieving data from other sources. These
constraints might improve performance by enabling the query to qualify for automatic query
rewrite.
Ex
DB2 ignores informational referential constraints during insert, update, and delete
operations. Some utilities ignore these constraints; other utilities recognize them.
pr
You should use this type of referential constraint only when an application process verifies
the data in a referential integrity relationship. For example, when inserting a row in a
dependent table, the application should verify that a foreign key exists as a primary or
unique key in the parent table. To define an informational referential constraint, use the
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Typically, data in a data warehouse has been extracted and cleansed from other
sources. Referential integrity might already be guaranteed. In this situation,
enforcement by DB2 is unnecessary.
pr
Ex
cl
Automatic query rewrite is a process that examines a submitted query that references
source tables and, if appropriate, rewrites the query so that it executes against a
materialized query table that has been derived from those source tables. This process
uses informational referential constraints to determine whether the query can use a
materialized query table. Automatic query rewrite results in a significant reduction in
query run time, especially for decision-support queries that operate over huge amounts
of data.
3-19
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
pr
Ex
cl
Notes:
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
NULL,
NULL,
NULL WITH DEFAULT,
NULL,
NULL,
--
CV8317.0
pr
Ex
cl
Notes:
3-21
Student Notebook
Dropping constraints
Dropping a Primary Key Constraint
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Example
CV8317.0
Notes:
The visual shows how the DROP CONSTRAINT clause of the ALTER TABLE statement
can be used to drop:
Ex
cl
pr
ALTER TABLE....
DROP UNIQUE UKNI_NUMBER;
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Example
DEPTNO
DEPT
Primary
Key
E01
E11
E21
Support Services
Operations
Software Support
EMP
Constraint
Name
PKDEPT
EMPNO
000050
000280
John
Ethel
B
GEYER
R SCHNEIDER
NI_NUMBER WORKDEPT
US01234
E01
US05678
E11
REFERENTIAL CONSTRAINT
Foreign
Key
Example
CV8317.0
Notes:
cl
Once an index has been defined to enforce a primary key constraint or unique key
constraint, it cannot be dropped until the constraint itself is removed. Any attempt to
remove such an index will result in an error.
Ex
This protects you from inadvertently dropping an index which is fundamental to the
enforcement of data integrity.
pr
To remove an index that supports a primary key or unique key constraint, you must first
remove the constraint itself, using the ALTER TABLE statement. Any foreign key
constraints that refer to the primary key or unique key constraints will be dropped
automatically.
3-23
Student Notebook
Catalog information
Primary Key and Unique Key constraints are recorded in two catalog tables:
1 SYSIBM.SYSTABCONST
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
---------+---------+---------+---------+---------+---------+-------CONSTNAME
TBCREATOR TBNAME
TYPE IXNAME
COLCOUNT
---------+---------+---------+---------+---------+---------+-------PKDEPT
KIDDJA
DEPT
P
XDEPT
1
UKNI_NUMBER
KIDDJA
EMP
U
XNI_NUMBER
1
2 SYSIBM.SYSKEYCOLUSE
---------+---------+---------+---------+---------+---------+---CONSTNAME
TBCREATOR TBNAME
COLNAME
COLNO COLSEQ
---------+---------+---------+---------+---------+---------+---PKDEPT
KIDDJA
DEPT
DEPTNO
1
1
UKNI_NUMBER
KIDDJA
EMP
NI_NUMBER
5
1
---------+---------+---------+---------+---------+---------+-----TBNAME
CREATOR
RELNAME
REFTBNAME COLCOUNT DELETERULE
---------+---------+---------+---------+---------+---------+-----EMP
KIDDJA
RED
DEPT
1 N
2 SYSIBM.SYSFOREIGNKEYS
---------+---------+---------+---------+---------+---------+-TBNAME
CREATOR
RELNAME
COLNAME
COLNO COLSEQ
---------+---------+---------+---------+---------+---------+-EMP
KIDDJA
RED
WORKDEPT
6
1
CV8317.0
Notes.
cl
SYSIBM.SYSTABCONST contains one row for each primary key or unique key constraint.
Ex
SYSIBM.SYSKEYCOLUSE contains one row for every column in a primary key or unique
key constraint from the SYSIBM.SYSTABCONST table.
pr
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Set to X when the table has a unique constraint (primary key or unique key) and the
table definition is complete.
Set to I when the table definition is incomplete.
TABLESTATUS
Set to P if the definition is incomplete because the table lacks a primary index.
Set to U if the definition is incomplete because the table lacks a required index on a
unique key.
Set to PU if the definition is incomplete because the table lacks a primary index AND a
required index on a unique key.
SYSIBM.SYSCOLUMNS contains:
KEYSEQ
SYSIBM.SYSINDEXES contains:
UNIQUERULE
SYSIBM.SYSFOREIGNKEYS contains:
pr
Ex
cl
A row for every column of every foreign key. It includes the name of the constraint for
which the column is part of the foreign key.
3-25
Student Notebook
Referential structures
Tables linked by Referential Constraints
Types of Referential Structure:
SELF-REFERENCING
CYCLE
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
HIERARCHY
CYCLES
Customer
Employee
MULTIPATH
EMPLOYEE
CASCADE
CASCADE
Order
Department
RESTRICT
CUSTOMER
Employee
T1
RESTRICT
ORDER
Orderline
T2
T3
Terminology:
Dependent is immediately below Parent
Descendent is somewhere below a Parent
Copyright IBM Corporation 2011
CV8317.0
Notes:
cl
In order to meet the design objectives of the DB2 implementation of RI a number of DDL
restrictions apply to ensure access path independence requirements. Any violations are
detected at DDL time.
Cycles
Ex
A table within a cycle cannot be delete-connected to itself. This means in a cycle involving
more than two tables, two or more delete rules must not be CASCADE.
On the next page is an example of the anomaly that might occur if this restriction were not
enforced.
pr
Self-referencing cycle
In the visual, ORDER correctly has identical delete rules of RESTRICT on its relationships.
Identical delete rules are a requirement and they must not be SET NULL.
3-26 DB2 10 for z/OS DB Admin Workshop Part 1
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Table T1
_______________
| PKT1 | FKT3 |
|______|______|
| T1A | ---- |
<------<---- | T1B | T3A | <-----<-----|
| T1C | T3B |
|
CASCADE
| T1D | T3C |
CASCADE
|
|
|
|
|
V
|
Table T2
Table T3
_______________
_______________
| PKT2 | FKT1 |
| PKT3 | FKT2 |
|______|______|
|_______|_____|
| T2A | T1A |
| T3A
| --- |
| T2B | T1B |
| T3B
| T2B |
| T2C | T1C |
| T3C
| T2C |
| T2D | T1D |
| T3D
| T2D |
|
|
|
|
|
|
|
|
V
SET NULL
|
---------------->------------------>----------------->
If DB2 accesses table T3 in the sequence of rows T3D, T3C, T3B and T3A, only row
T3A would qualify for the delete.
pr
Ex
cl
If DB2 accesses table T3 in the sequence of rows T3A, T3B, T3C and T3D, all four rows
T3A, T3B, T3C and T3D would qualify for the delete.
3-27
Student Notebook
Additional considerations
What if DB2 does not support your requirements?
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
pr
Ex
cl
If you write your own code to implement special requirements, it is safe to put this code into
a dedicated module that takes care of the required processing. Due to the operational
impact, DB2 makes sure your data is consistent. Thus, it is better that your RI structures be
of acceptable size. Avoid connecting applications through code tables. DB2 doesn't know
the nature of these tables, and therefore requires a coordinated backup and recovery
procedure. If a table has an RI problem, the entire table space is put into problem state
(CHECK PENDING ... see later), even the tables that are not part of the RI structure. So,
once again, one table per table space are the words of wisdom. At least one NULLable
foreign key in a cycle allows you to insert data after the definition of the constraints.
V5.4.0.3
Student Notebook
Uempty
TABLESPACESET concept
Table space TS1
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
A table space set is the set of table spaces containing a set of RI-connected tables so that
no RI constraint leaves or enters the table space set.
cl
The table space set is a DB2 concept and not a DB2 object. It is not explicitly created.
pr
Ex
The visual serves as an illustration of the definitions rather than a recommendation of the
way to implement referential structures.
3-29
Student Notebook
REPORT TABLESPACESET
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
1DSNU000I
DSNUGUTC - OUTPUT START FOR UTILITY, UTILID = USCV831O
DSNU1044I
DSNUGTIS - PROCESSING SYSIN AS EBCDIC
0DSNU050I
DSNUGUTC - REPORT TABLESPACESET USV831OD.TSLOCN
DSNU587I -DBP8 DSNUPSET - REPORT TABLESPACE SET WITH TABLESPACE USV831OD.TSLOCN
TABLESPACE SET REPORT:
TABLESPACE
: USV831OD.TSDEPT
TABLE
INDEXSPACE
INDEX
INDEXSPACE
INDEX
INDEXSPACE
INDEX
DEP TABLE
TABLESPACE
USCV831O.DEPARTMENT
USV831OD.IXDEPT1
USCV831O.IXDEPT1
USV831OD.IXDEPT2
USCV831O.IXDEPT2
USV831OD.IXDEPT3
USCV831O.IXDEPT3
USCV831O.DEPARTMENT
USCV831O.EMPLOYEE
USCV831O.PROJECT
: USV831OD.TSEMPL
TABLE
INDEXSPACE
INDEX
INDEXSPACE
INDEX
INDEXSPACE
INDEX
DEP TABLE
DSNU580I
DSNU010I
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
:
USCV831O.EMPLOYEE
USV831OD.IXEMPL1
USCV831O.IXEMPL1
USV831OD.IXEMPL2
USCV831O.IXEMPL2
USV831OD.IXEMPL3
USCV831O.IXEMPL3
USCV831O.DEPARTMENT
USCV831O.ON_PROJECT
USCV831O.PROJECT
.
.
CV8317.0
Notes:
pr
Ex
cl
The REPORT TABLESPACESET utility allows you to see the table spaces that make up a
table space set. This utility will be primarily used for setting up backup and recovery
procedures for RI-related tables.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
cl
When a table space is put in the CHECK PENDING state, it means that DB2 isn't sure that
no RI or table check integrity violations exist in all the tables in the table space. In that case,
since DB2 only allows access to data when it is 100% consistent, no access will be allowed
to the data, except for certain utilities.
pr
Ex
A typical example is the case where a table with data is altered to add a constraint. DB2 will
not do inflight RI verifications, but will force the user to run the CHECK DATA utility to make
sure that no RI violations exist, and if they do exist, to do the necessary cleanup.
3-31
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Student Notebook
V5.4.0.3
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
Uempty
3-33
Student Notebook
Identity columns
A way for DB2 to automatically generate unique, sequential, and
recoverable values
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
VALUES IDENTITY_VAL_LOCAL()
INTO :IVAR;
CV8317.0
Notes:
An identity column is defined using the AS IDENTITY clause of the CREATE TABLE
statement.
cl
pr
Ex
GENERATED specifies that DB2 generates values for the column. GENERATED must be
specified for an identity column.
3-34 DB2 10 for z/OS DB Admin Workshop Part 1
V5.4.0.3
Student Notebook
Uempty
GENERATED ALWAYS specifies that DB2 always generates values for the column when a
row is inserted into the table. DB2 guarantees that all column values are unique. If an
INSERT or UPDATE statement is issued in an attempt to provide an explicit value, DB2
returns an error and the statement fails.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
GENERATED BY DEFAULT specifies that DB2 generates a value for the column when a
row is inserted into the table except when a value is provided on the INSERT statement.
Values can be changed using an UPDATE statement. DB2 does not guarantee the
uniqueness of a generated value among all the column values, but only among the set of
values it previously generated. GENERATED BY DEFAULT is intended to be used only for
data propagation, where the intent is to copy the identity column values from one table to
another.
AS IDENTITY is an attribute of the GENERATED keyword on the CREATE TABLE SQL
statement.
START WITH, if used, specifies the first, or starting, value for the identity column. This
value has a numeric data type, and can be any positive or negative value with a zero scale.
If not specified, this keyword defaults to 1.
INCREMENT BY defines the interval between subsequent sequentially generated values.
This keyword allows any positive or negative numeric value with a zero scale within the
range of a large integer. If not specified, this keyword defaults to 1.
You have two options when dealing with caching. The first turns caching on by specifying
CACHE followed by the number of values that should be cached. Any value greater than 2
within the range of an integer can be used, and if not specified, the default is 20. The other
option is to turn caching completely off with NO CACHE. Caching sequence values in
memory is a performance and tuning option that promotes faster access to the sequence
values when the application can handle the behavior. NO CACHE turns off the caching
mechanism.
cl
CYCLE allows an identity to wrap around to a new beginning when the minimum or
maximum value is reached. Cycling through values a second time can create duplicate
values. NO CYCLE causes the identity value to stop being generated when the minimum or
maximum is reached. NO CYCLE is the default.
Ex
MAXVALUE specifies the maximum value that can be generated for this identity column.
MINVALUE specifies the minimum value that can be generated for this identity column.
pr
NO MINVALUE specifies that no minimum end point of the range has been set. The
minimum value for an ascending sequence becomes the START WITH value, or 1 if a
START WITH value is not specified. For a descending sequence, the default is the lowest
value, the largest negative value, for the data type of the column to which the identity is
assigned.
3-35
Student Notebook
NO MAXVALUE specifies that no maximum end point of the range has been set. The
maximum value for an ascending sequence is the greatest value allowed by the data type
for that column. For a descending sequence, it is the START WITH value if specified, or 1
if no START WITH was specified.
For MINVALUE and MAXVALUE, any negative or positive value including zero can be
specified. In addition, MINVALUE can be equal to MAXVALUE.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
ORDER and NO ORDER specify whether or not the identity values must be generated in
the order of the request. The default is NO ORDER. NO ORDER specifies that the values
do not need to be generated in order of request, while ORDER specifies that the values are
generated in order of request.
IDENTITY_VAL_LOCAL
The built-in scalar function, IDENTITY_VAL_LOCAL, returns a value with a data type of
DECIMAL(31,0) regardless of the actual data type of the identity column to which the
returned value corresponds.
pr
Ex
cl
The value returned by the function is the value that was assigned to the identity column of
the table identified in the most recent single row INSERT statement with a VALUES clause
for a table containing an identity column, where the INSERT statement was issued.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
Ex
cl
When a table is being created like another table that contains an identity column, the
new table inherits only the data type of the identity column and none of the other column
attributes. If INCLUDING IDENTITY COLUMN ATTRIBUTES is specified, the new table
inherits all of the identity column attributes. If the identified object of LIKE is a view,
INCLUDING IDENTITY COLUMN ATTRIBUTES cannot be specified.
pr
If the table is not defined with an identity column, you can use the ALTER TABLE ...
ADD COLUMN ... statement to add a new column as an identity column. An existing
column cannot be altered to an identity column.
Assume that tables T1 and T2 have the same definition but different identity column
attributes. How can you copy the contents of T1 to T2 using an INSERT statement, but
ask DB2 to generate new identity column values for T2?
- Ensure that the identity column for T2 is defined as GENERATED ALWAYS
- Execute the INSERT statement shown in the visual
3-37
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DROP SEQUENCE
COMMENT ON SEQUENCE
GRANT / REVOKE ON SEQUENCE
NEXT VALUE FOR and PREVIOUS VALUE FOR
CV8317.0
Notes:
There are six SQL statements and two expressions in support of sequence objects.
CREATE SEQUENCE
Ex
cl
The CREATE SEQUENCE SQL statement creates a sequence object at the application
server. The sequence object is a user-defined object that generates sequential numeric
values. The CREATE is also used to describe the sequence values specifications.
pr
The example in the visual shows the CREATE SEQUENCE statement with all of the
keywords specified. In this example, we have specified INTEGER, even though it is the
default. The sequence starts at 1, and increments by 1 to a maximum value of 5. When the
maximum is reached, the sequence cycles back to the minimum value of 0 (zero). Five
values are kept in the cache to assist with performance. On the first pass, that would be all
five values generated. However, on subsequent cycles, six values will be generated.
ALTER SEQUENCE
The ALTER SEQUENCE SQL statement changes the attributes of the sequence object,
such as INCREMENT BY, MINVALUE, MAXVALUE, CACHE, CYCLE, ORDER, and the
Copyright IBM Corp. 1993, 2011
3-39
Student Notebook
point at which the sequence should be restarted. Only future values are affected, and then
only after the ALTER has been committed. Whenever a sequence is ALTERed, there is
always the risk of creating duplicate values, so care should be taken. If uniqueness is
critical to the application, an unique index can be defined for the column using the
sequence value.
Example
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
In the ALTER SEQUENCE in the visual, the sequence just created will be modified. It was
decided that the original maximum was a little low, so it is being increased to 1,000,000.
The sequence will also no longer be incremented by 1, but rather by 100. When the
sequence is restarted, it will restart at 100 rather than the minimum. The minimum, cache
size, and cycle values will all remain the same as the values with which the sequence was
created.
The ALTER does not take affect until it has been committed.
DROP SEQUENCE
The DROP SEQUENCE statement drops the sequence object and removes a sequence
object description from the DB2 catalog.
COMMENT ON SEQUENCE
Allows a user to supply a comment or description for a sequence in the REMARKS column
of the DB2 catalog table SYSIBM.SYSSEQUENCES.
GRANT/REVOKE
Grant or revoke the ALTER or USAGE privilege for a user-defined sequence or list of
user-defined sequences from an authorization identifier (auth ID, or list of auth IDs, or from
PUBLIC).
NEXT VALUE FOR and PREVIOUS VALUE FOR
pr
Ex
cl
NEXT and PREVIOUS VALUE FOR are used to reference the sequence values by
specifying the named sequence.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Example 1
Example 2
Example 3
CV8317.0
Notes:
Example 1
cl
Ex
The last VALUES, performing a PREVIOUS VALUE FOR, still returns the last value
generated by the previous VALUES.
Example 2
pr
This uses the sequence number generated as part of the SET clause of an UPDATE. This
is valid for both searched and positioned updates.
Example 3
In this example, we use the syntax for a select from insert. This syntax allows us to
examine the sequence value generated without having to perform a separate SELECT.
3-41
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
The first cycle always starts with the START WITH value, but all subsequent cycles start
with MINVALUE (ascending sequence) or MAXVALUE (descending sequence).
If NO CYCLE is in effect, sequence value generation will stop when the sequence
reaches the end of a logical range of values.
Generating constants example
Ex
cl
If START WITH were equal to MINVALUE and MINVALUE were equal to MAXVALUE,
and CYCLE were specified, the START WITH value would be generated repeatedly.
However, if NO CYCLE were specified, one sequence value would be generated equal
to the START WITH value, and a subsequent attempt to generated a sequence would
receive an error. This is true even if INCREMENT BY is a non-zero value.
Duplicate sequences
pr
DB2 will make every attempt to ensure that all sequences generated are unique.
However, this is not always possible. If CYCLE is specified for the sequence, the
sequence is restarted and the same sequence values are generated. If the previous set
of values are still in use, the new set will be duplicates of the previous set.
The same can be true if the RESTART WITH keyword is specified and the sequence is
restarted with a value that is already in use, or the direction the sequence is generated
is reversed by altering the INCREMENT BY keyword from positive to negative or
negative to positive. Duplicate values can be avoided if a unique index is created on the
V5.4.0.3
Student Notebook
Uempty
column that will contain the sequence value. Prevention is accomplished by generating
a error and having the insert process fail for that sequence value.
Caching
- CACHE option allows DB2 to cache preallocated values in memory for fast access
Reduces synchronous I/O to the catalog table SYSIBM.SYSSEQUENCES
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
- Recommended value is 20
Assigning a higher value gives no benefit and increases the size of gap should a
failure occur
Gaps
cl
A gap is any unintentional break in a sequence. Unintentional here means that the gap
was not the result of using the INCREMENT BY keyword of the CREATE or ALTER
SQL statement. If INCREMENT BY specifies a value greater than 1 or less than -1, the
generated sequences, although sequential, will not be without gaps. The gaps that are
of concern to us are the unintentional gaps caused by anything other than the
INCREMENT BY keyword. There are a number of ways in which gaps might be caused
in a sequence. If a transaction is rolled back after the sequence has been generated,
that generated sequence is lost and a gap will exist. The SQL statement using the
sequence is all that is rolled back; the sequence itself continues to increment forward.
Similarly, if an SQL statement fails after the sequence for that SQL statement has
already been generated, the generated sequence is once again lost and a gap will
exist. Generated sequences can also be lost if something happens to the cache. If the
cache is lost, all sequences not used in the cache are also lost, causing a gap. The
cache can be lost by stopping SYSIBM.SYSSEQ table space, if the DB2 subsystem is
stopped, or if the DB2 subsystem crashes. DDL can also negatively affect a sequence.
If the SEQUENCE is DROPped or ALTERed and the DROP or ALTER is rolled back,
gaps could be left in the sequence.
Ex
Unintentional gaps could also result if multiple transactions are processing using the
same sequence name.
Catalog information
pr
Columns:
- PRECISION: Precision of sequence object's decimal or other numeric data
type
Copyright IBM Corp. 1993, 2011
3-43
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
- BSCHEMA / BNAME
- GRANTOR / GRANTEE
- SCHEMA / NAME
Sequences can also be used to spread inserted rows across multiple partitions if used to
supply the partitioning value.
By manipulating the INCREMENT BY and RESTART WITH values and specifying CYCLE,
a sequence could be generated that would force each new inserted row to go to a different
partition of a partition table space.
pr
Ex
cl
V5.4.0.3
Student Notebook
Uempty
Sequences
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Can only be used to supply a value for Values can be used by application for
that column
whatever purpose it chooses
Copyright IBM Corporation 2011
CV8317.0
Notes:
Ex
cl
Sequence objects build on the concepts introduced with the identity columns, with some
significant enhancements. Sequences are standalone objects that generate sequence
values when requested by an application. These values can be used by that application for
whatever purpose the application chooses. However, identity columns are associated with
a specific column in a specific table, and can be used only to supply a value for that
column.
What is similar and what is different?
pr
A sequence has nothing to do with a specific table. There is not a one-to-one relationship
between a sequence object and any table. A sequence object simply gives the application
the ability to get the next unique sequential value. The application can then do with that
value anything it chooses. In fact, a single sequence value could be used multiple times in
the same SQL statement to supply a value for multiple columns, or multiple sequence
objects could be used. Sequences use the SQL expressions NEXT VALUE FOR and
PREVIOUS VALUE FOR to retrieve the next generated or previously generated values
from the sequence. These expressions are not allowed against identity columns.
Copyright IBM Corp. 1993, 2011
3-45
Student Notebook
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
The ALTER SEQUENCE statement is used to modify the sequence attributes, whereas the
identity column characteristics can be altered using the ALTER TABLE ... ALTER COLUMN
statement.
V5.4.0.3
Student Notebook
Uempty
Unit summary
Having
Havingcompleted
completedthis
thisunit,
unit,you
youshould
shouldbe
beable
ableto:
to:
Describe
the
concepts
of:
Describe the concepts of:
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
PRIMARY
PRIMARYKEY
KEY
UNIQUE
UNIQUEKEY
KEYand
and
FOREIGN
KEY
FOREIGN KEY
Implement
Implementthese
thesekeys
keysininDB2
DB2
Describe
the
concepts
of:
Describe the concepts of:
REFERENTIAL
REFERENTIALCONSTRAINT
CONSTRAINTand
and
REFERENTIAL
INTEGRITY
REFERENTIAL INTEGRITY
Implement
ImplementREFERENTIAL
REFERENTIALCONSTRAINTS
CONSTRAINTSon
on
New
Newtables
tables
Existing
Existingtables
tables
With
Withthe
theappropriate
appropriateDELETE
DELETErules
rules
Drop
DropREFERENTIAL
REFERENTIALCONSTRAINTS
CONSTRAINTSand
andtheir
theirenforcing
enforcingindexes
indexes
Query
Querythe
theappropriate
appropriatecatalog
catalogtables
tablesfor
forRI
RIdetails
details
Describe
the
relationships
that
require
special
Describe the relationships that require specialhandling
handling
Describe
what
informational
constraints
are
and
Describe what informational constraints are andtheir
theiruse
use
Discuss
identity
columns
and
sequence
objects
Discuss identity columns and sequence objects
Copyright IBM Corporation 2011
CV8317.0
pr
Ex
cl
Notes:
3-47
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Student Notebook
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
This unit describes how to get your data into and out of DB2.
SC19-2983
SC19-2984
SC19-2972
SC18-9854
SC18-9855
SC18-9844
SC18-7426
SC18-7427
SC18-7416
pr
Ex
cl
References
4-1
Student Notebook
Unit objectives
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
After
Aftercompleting
completingthis
thisunit,
unit,you
youshould
shouldbe
beable
ableto:
to:
Describe
Describehow
howDB2
DB2utilities
utilitiesmay
maybe
beexecuted,
executed,monitored,
monitored,
terminated,
terminated,and
andrestarted
restarted
Run
Runthe
theDB2
DB2LOAD
LOADutility,
utility,and
andmake
makefull
fulluse
useof
ofits
itsmany
many
features
features
Run
Runthe
theCHECK
CHECKDATA
DATAutility
utilityto
tocheck
checkDB2
DB2table
tablespace
spacedata
data
for
forany
anyviolations
violationsof
ofreferential
referentialintegrity
integrityconstraints
constraintsor
ortable
table
check
checkconstraints
constraints
Run
Runthe
theDB2
DB2UNLOAD
UNLOADutility,
utility,and
andmake
makefull
fulluse
useof
ofits
its many
many
features
features
CV8317.0
pr
Ex
cl
Notes:
4-2
V5.4.0.3
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
Uempty
4-3
Student Notebook
******
000001
000002
000003
000004
000005
000006
000007
000008
1
2
3
4
5
6
7
8
D
X
SPUFI
DCLGEN
PROGRAM PREPARATION
PRECOMPILE
BIND/REBIND/FREE
RUN
DB2 COMMANDS
UTILITIES
DB2I DEFAULTS
EXIT
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
***********************
LOAD DATA....
...
LOAD DATA...
//
EXEC DSNUPROC
//SYSIN DD *
Control Statements
Utility JCL
CV8317.0
Notes:
Step 1: Create a sequential data set (or PDS member) with control statements.
cl
Ex
Utility control statements define the function that the utility job performs.
Create the utility statements with the ISPF/PDF edit function.
After the utility control statements are created, save them in a sequential or partitioned data
set.
pr
DB2 typically reads utility control statements from the SYSIN data set.
4-4
V5.4.0.3
Student Notebook
Uempty
SSID: DSN1
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
===>
Select from the following:
1 FUNCTION ===> EDITJCL
2 JOB ID
===> TSOUD01
3 UTILITY ===> LOAD
===> NO
7 lib == >
* The data set names panel will be displayed when required by a utility.
F1=HELP
F7=UP
F2=SPLIT
F8=DOWN
F3=END
F9=SWAP
F4=RETURN
F10=LEFT
F5=RFIND
F11=RIGHT
F6=RCHANGE
F12=RETRIEVE
CV8317.0
Notes:
pr
Ex
cl
In STATEMENT DATA SET, you must enter the name of the data set (or PDS member)
which contains the control statements for the utility.
4-5
Student Notebook
SSID:
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
===>
Enter data set name for LOAD, REORG TABLESPACE, or UNLOAD:
1 RECDSN
===> TSDEPT.DATA
Enter data set name for LOAD or REORG TABLESPACE:
2 DISCDSN ===> DISCARD
Enter output data sets for local/current site for COPY, MERGECOPY,
LOAD, or REORG TABLESPACE:
3 COPYDSN ===> COPY1
4 COPYDSN2 ===> COPY2
Enter output data sets for recovery site for COPY, LOAD, or REORG
TABLESPACE:
5 RCPYDSN1 ===> COPY3
6 RCPYDSN2 ===> COPY4
Enter output data sets for REORG or UNLOAD
7 PUNCHDSN ===>
F1=HELP
F7=UP
F2=SPLIT
F8=DOWN
F3=END
F9=SWAP
F4=RETURN
F10=LEFT
F5=RFIND
F11=RIGHT
F6=RCHANGE
F12=RETRIEVE
CV8317.0
Notes:
pr
Ex
cl
4-6
V5.4.0.3
Student Notebook
Uempty
DSNUxxx.CNTL
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
//UTIL EXEC
DSNUPROC,SYSTEM=DSN1,UID='TSOUD01',UTPROC=''
//*
//**********************************************
//*
//* GENERATING JCL FOR THE LOAD UTILITY
//* DATE: 08/15/11
TIME: 14:03:50
//*
//**********************************************
//*
//DSNUPROC.SORTWK01 DD DSN=TSOUD01.SORTWK01,
//
DISP=(MOD,DELETE,CATLG),
//
SPACE=(16384,(20,20),,,ROUND),
//
UNIT=SYSDA
*
*
//DSNUPROC.SYSIN
DD *
LOAD DATA INDDN SYSREC
LOG NO RESUME YES
EBCDIC CCSID(00000,00000,00000)
INTO TABLE "TSOUD01 "."DEPARTMENT
"
WHEN(00004:00005 = X'000A')
( "DEPTNO
" POSITION(00007:00009) CHAR(003)
, "SUPERIOR_DEPTNO
" POSITION(00011:00013) CHAR(003)
NULLIF(00010)=X'FF'
, "MGRNO
" POSITION(00015 :00020)CHAR(006)
NULLIF(00014)=X'FF'
, "NAME
" POSITION(00021)
VARCHAR
)
//
Copyright IBM Corporation 2011
CV8317.0
Notes:
This is an example of the JCL DB2I will generate for the LOAD utility.
cl
The procedure invoked is DSNUPROC (see next visual). Note the SYSIN DD card with the
control statements from the edited member.
Ex
The generated JCL is saved in a data set with the name DSNUxxx.CNTL where xxx
depends on the utility being invoked. Table 4-2 lists the data set name for each utility:
pr
Table 4-2: Data set name containing the generated JCL for the utility
Utility name
Data set name with generated JCL
CHECK INDEX
DSNUCHI.CNTL
CHECK DATA
DSNUCHD.CNTL
COPY
DSNUCOP.CNTL
LOAD
DSNULOA.CNTL
MERGECOPY
DSNUMER.CNTL
MODIFY
DSNUMOD.CNTL
4-7
Student Notebook
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Table 4-2: Data set name containing the generated JCL for the utility
Utility name
Data set name with generated JCL
QUIESCE
DSNUQIE.CNTL
REBUILD INDEX
DSNUREB.CNTL
RECOVER
DSNUREC.CNTL
REORG INDEX
DSNURGI.CNTL
REORG TABLESPACE
DSNURGT.CNTL
REPORT
DSNURPT.CNTL
RUNSTATS
DSNURUN.CNTL
STOSPACE
DSNUSTO.CNTL
UNLOAD
DSNUUNL.CNTL
4-8
V5.4.0.3
Student Notebook
Uempty
DSNUPROC
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
cl
You can see that the program is DSNUTILB. It will read the control statements via the
SYSIN DD card. DSNUTILB is called the batch utility driver.
pr
Ex
4-9
Student Notebook
SYSIBM.SYSUTILX
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
UTILID
UTILITY
NAME
CHECKPOINT
INFORMATION...
Unique Identifier
CV8317.0
Notes:
SYSIBM.SYSUTILX contains status information about your utility, and this information is
updated as the utility progresses.
pr
Ex
cl
You can monitor the progress of your utility with the DB2 -DISPLAY UTILITY command.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
-DIS UTIL(CF8CHKD)
DSNU105I
DSN9022I
-TERM UTIL(CF8CHKD)
DSNU166I
DSN9022I
CV8317.0
Notes:
When working with utilities, it is handy to be able to find out whether your utility is running
("active") or has encountered some kind of a problem ("stopped").
cl
Ex
A utility that is in a stopped state will remain "known" to DB2 as long as it is not "terminated"
by a DB2 -TERM command or restarted.
Terminating an active utility may leave restrictive states on the accessed objects.
pr
Warning
4-11
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Utility #1
z/OS
JOB
(2.1)
Utility stopped
(REORG UNLOAD PAUSE, error
condition, operator cancel, and so on)
z/OS
JOB
(1)
Utility restarted
(key = utilid)
z/OS
JOB
(2.2)
Utility terminated
* Row deleted from
SYSUTILX
CV8317.0
Notes:
The vertical line on the left of the visual represents a utility z/OS JOB (1) which starts
and runs to completion in one execution.
Ex
cl
The vertical lines on the right represent a utility which starts and for some reason does not
complete. The utility is restarted and then completes. This represents one utility, but
requires two z/OS jobs.
pr
When a utility is restarted, the utility control cards must be the same as in the original job
(that is, DO NOT change the utility control cards before restarting a utility).
V5.4.0.3
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
Uempty
4-13
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
0 . . . . 1 . . . . 2 . . . . 3 . . . . 4 . . .
E21
SOFTWARE SUPPORT
TABLE DEPTNO
DEPT
E21
031435
DEPTNAME
SOFTWARE SUPPORT
LOAD DATA
INTO TABLE DEPT
(DEPTNO
POSITION(7)
DEPTNAME
POSITION(10)
MGRNO
POSITION(36)
ADMRDEPT
POSITION(42)
E01
MGRNO ADMRDEPT
031435
E01
CHAR(3),
CHAR(26),
CHAR(6),
CHAR(3))
CV8317.0
Notes:
The LOAD utility provides a great deal of flexibility in loading data into a table space from a
sequential data set. Some of its features are:
cl
You can selectively load some fields from records in the input data set and ignore
others.
Ex
You can specify conditions on the LOAD control statement such that only records
meeting certain conditions are loaded into the table space.
Records that do not meet your conditions or are in error can be written to a discard data
set.
pr
Multiple tables in a table space can be loaded from a single input data set.
Selected partitions of a partitioned table space can be loaded.
LOAD can convert many data types and field lengths to match the data types and
lengths of the table columns.
V5.4.0.3
Student Notebook
Uempty
EBCDIC, ASCII, or Unicode data can be loaded into a table space. The LOAD utility
can convert as necessary.
LOAD can replace existing data or add to existing data.
LOAD can validate data to ensure no RI or table check constraint violations.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
The visual shows a simple, first example of the LOAD utility's control statements needed to
load data into a single table.
TRUNCATE and STRIP
You can load certain fields that are longer than the length of target column by truncating the
data. DB2 truncates the data only when you explicitly specify the TRUNCATE option. You
can specify TRUNCATE with the CHAR, VARCHAR, GRAPHIC, VARGRAPHIC, BINARY,
and VARBINARY data type options. LOAD first applies any CCSID conversion, and then
truncates the data. The TRUNCATE option of the LOAD utility truncates string data, and it
has a different purpose than the SQL TRUNCATE scalar function.
You can also remove a specified character from the beginning, end, or both ends of the
data by specifying the STRIP option. This option is valid only with the CHAR, VARCHAR,
GRAPHIC, VARGRAPHIC, BINARY, and VARBINARY data type options. If you specify
both the TRUNCATE and STRIP options, LOAD performs the strip operation first.
Note
pr
Ex
cl
The workshop DB2 for z/OS: How to Deal with Unicode and Multiple CCSID Statements
(CV280) provide you in-depth knowledge about how DB2 for z/OS supports the usage of
Unicode data.
4-15
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
0 . . . . 1 . . . . 2 . . . . 3 . . . . 4 . . .
E21
SOFTWARE SUPPORT
031435
E01
TABLE DEPT
DEPTNO
E21
DEPTNAME
SOFTWARE SUPPORT
LOAD DATA
INTO TABLE DEPT
(DEPTNO
POSITION(7)
DEPTNAME
POSITION(10)
MGRNO
POSITION(36)
ADMRDEPT
POSITION(42)
MGRNO ADMRDEPT
031435
E01
CHAR(3),
CHAR(26),
INTEGER EXTERNAL(6),
CHAR(3))
CV8317.0
Notes:
In some cases, the data types of the input record field and the target column do not match.
cl
In the example, the INTEGER EXTERNAL keyword tells DB2 that the record field contains
numeric data formatted as characters.
pr
Ex
V5.4.0.3
Student Notebook
Uempty
Data conversion
Numeric Data Conversion
SMALLINT
INTEGER
BIGINT
DECIMAL
FLOAT
DECFLOAT
INTEGER
Y
D
Y
Y
Y
Y
BIGINT
Y
Y
D
Y
Y
Y
DECIMAL
Y
Y
Y
D
Y
Y
FLOAT
Y
Y
Y
Y
D
Y
DECFLOAT
Y
Y
Y
Y
Y
D
D=Defaults used
when you do not
specify the input
data type in a
field spec of the
INTO TABLE
statement
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Input
Data
Types
SMALLINT
D
Y
Y
Y
Y
Y
CHAR
VARCHAR
GRAPHIC
VARGRAPHIC
BINARY
VARBINARY
CHAR
D
Y
Y*
Y*
N
N
DATE EXTERNAL
TIME EXTERNAL
TIMESTAMP
EXTERNAL
DATE
D
N
Y
VARBINARY
Y
Y
N
N
Y
D
*=Conversion
applies when
either the input
data or the target
table is Unicode
TIMESTAMP
N
N
D
CV8317.0
Notes:
LOAD supports data type conversion between numeric data types and between character
data types.
Ex
cl
If a column is defined as DATE, TIME, or TIMESTAMP, then the data on the sequential
input data sets must be in character format with punctuation. For example, with ISO
(International Standards Organization) format:
2002-02-06 is a valid date
19.15.30 is a valid time
2002-02-06-19.15.30.123456 is a valid timestamp
pr
The input fields must be defined to the LOAD utility as DATE EXTERNAL, TIME
EXTERNAL, and TIMESTAMP EXTERNAL.
If NOT NULL WITH DEFAULT is specified, and the date or time is not provided in the input
data set, then the date or time at the start of the LOAD utility is used for all rows loaded
without a date or time. This may or may not be what you want. If it is not, then the input
data set must have a valid date or time.
Copyright IBM Corp. 1993, 2011
4-17
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
0 . . . . 1 . . . . 2 . . . . 3 . . . . 4 . . .
E21
SOFTWARE SUPPORT
TABLE DEPT
DEPTNO
E21
??????
DEPTNAME
SOFTWARE SUPPORT
E01
MGRNO ADMRDEPT
-----E01
LOAD DATA
INTO TABLE DEPT
(DEPTNO
POSITION(7:9)
CHAR,
DEPTNAME
POSITION(10:35) CHAR,
MGRNO
POSITION(36:41) CHAR
NULLIF (36:41) = '??????',
ADMRDEPT
POSITION(42:44) CHAR)
CV8317.0
Notes:
Ex
cl
In the visual, '??????' in the input data is used to indicate that a value is unknown. The
NULLIF (36:41) = '??????' parameter of the MGRNO column causes X'FF' to be loaded in
the one-byte null flag prefixing the MGRNO column. This indicates in DB2 that the value is
unknown.
pr
V5.4.0.3
Student Notebook
Uempty
the DEFAULTIF parameter can be used with the LOAD statement as shown below:
LOAD DATA INTO TABLE TAB1
(COL1
POSITION (1:4) INTEGER
DEFAULTIF (1:4) = -1,
................................
................................
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
The LOAD utility will load zero in COL1 if positions 1-4 in the input data set contain -1 in
binary.
4-19
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
'),
'),
'))
CCV831 DB2 10 FOR Z/OS DATABASE ADMINISTRATION WORKSHOP PART 1 THIS CO URSE PRX
CCEFFF03CCF4FF4CDD4E6DE4CCECCCEC4CCDCDCEEDCECDD4EDDDECDD4DCDE4F04ECCE4CD EDEC4DDE
335831074220100669091620413121250144959239139650669228670719301013892036 49250797
OCV83104018.10.201122.10.2011201
DCEFFFFFFFF4FF4FFFFFF4FF4FFFFFFF
63583104018B10B201122B10B2011201
Copyright IBM Corporation 2011
CV8317.0
Notes:
cl
The WHEN clause tells the utility which records in the input data set are to be loaded. Input
records that satisfy the WHEN condition for a particular table are loaded into that table.
Ex
Input records that do not satisfy any WHEN clause of any INTO TABLE statement are
written to the discard data set, if one exists.
pr
In the visual, the input data for SHTITLE begins in position 7 of the input record. It contains
a 2-byte length field containing X'0037', and this is immediately followed by 55 input
characters for SHTITLE.
SHTITLE Is loaded with no trailing blanks.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
BINARY fields are assumed to be of fixed maximum length. VARBINARY fields must
contain a valid 2-byte binary length field preceding the data.
NULLIF
CONTINUEIF
In the visual, two input records are concatenated into one row for COURSE.
RESUME NO
pr
Ex
cl
4-21
Student Notebook
INTO-TABLE spec
REPLACE
REUSE
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
RESUME YES
RESUME NO
Initial load:
Empty table space
This is the
default
LOAD DATA
RESUME NO
INTO TABLE EMP
Start of
LOAD
RESUME YES
Additional load:
Non-empty table space
To load another
table in the same
table space
To add rows to a
non-empty table
LOAD DATA
RESUME YES
INTO TABLE EMP
Table space
Start of
LOAD
Table space
Existing
Data
REPLACE
Replace old data:
Resets a table space
and related indexes to
empty before loading
Easy way to refresh
data
LOAD DATA
REPLACE
INTO TABLE EMP
Start of
LOAD
Table space
Existing
Data
CV8317.0
Notes:
When you specify RESUME or REPLACE as part of the LOAD spec as shown in the visual,
LOAD will serialize at the table space level.
cl
RESUME NO
This causes the LOAD utility to write into an empty table space.
Ex
If any data is present in the table space, the utility does not load any rows.
RESUME YES
pr
This causes the LOAD utility to add new rows at the end of the table space.
This is a good choice for adding rows compared to using SQL INSERT. In general, the
LOAD utility has performance advantages.
V5.4.0.3
Student Notebook
Uempty
REPLACE
DB2 will reset to empty the table space and all its indexes.
For DB2-managed data sets, the data sets are deleted and redefined.
The data in all tables is lost when you LOAD REPLACE a single table in a multi-table
table space.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
The first time you run a LOAD REPLACE, DB2 automatically converts the table space from
the old basic format to the new reordered format. With reordered row format, DB2 no
longer stores the length of varying length columns, and the data can look significantly
different within the row/page.
LOAD will reset and reuse all existing DB2-managed data sets. That is, the data sets are
not deleted and redefined.
RESUME YES with SHRLEVEL CHANGE
It causes the LOAD utility to add new rows using processing similar to SQL INSERT.
It inserts rows according to the clustering index, provided that sufficient free space is
available.
pr
Ex
cl
4-23
Student Notebook
PART integer
REPLACE
REUSE
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
RESUME YES
Example
WHEN(1)='0'
(EMPNO POSITION(1:6) CHAR(6),
FIRSTNME POSITION(7:18) CHAR(12),
.
.
.
)
WHEN(1)='1'
(EMPNO POSITION(1:6) CHAR(6),
FIRSTNME POSITION(7:18) CHAR(12),
.
.
Copyright
CopyrightIBM
IBMCorporation
Corporation2011
2007
)
CV8317.0
Notes:
RESUME, REPLACE and REUSE can be used when loading individual partitions. In this
case, these specifications must come after the INTO TABLE clause
cl
LOAD will serialize at the partition level. That is, other partitions can be processed
concurrently.
pr
Ex
REPLACE with REUSE resets and reuses existing DB2-managed data sets for the
partitions. That is, no delete / redefine occurs.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
ENFORCE CONSTRAINTS
cl
Ex
ENFORCE NO
pr
The use of ENFORCE NO always causes CHKP to be set on, because the LOAD utility
does not check for any constraint violations, and some may exist.
It is necessary to clear CHKP in order to SELECT or UPDATE rows. One way to do this is
with the CHECK DATA utility covered in the next topic.
ENFORCE NO is one solution to the problem of loading tables in a referential cycle. You
can load all the tables in the referential structure with ENFORCE NO. This will result in all
the table spaces being put into CHKP, which you can clear with CHECK DATA...DELETE
YES.
Copyright IBM Corp. 1993, 2011
4-25
Student Notebook
LOG YES
This is the default.
LOG NO
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Following the LOAD take a full image copy with SHRLEVEL REFERENCE.
REPAIR....NOCOPYPEND
When adding rows to a table space, use LOAD....RESUME YES...LOG NO, and follow it
with a full image copy SHRLEVEL REFERENCE.
When replacing rows in a table space, use LOAD....REPLACE...LOG NO, and take an
inline image copy.
DISCARDS n
Records that cannot be loaded are written to the discard data set.
The DISCARDS parameter sets a limit for the number of records to be discarded.
Use the DISCARDS parameter to avoid writing all input records to the discard data set
because of, say, a typing error on the LOAD statement.
pr
Ex
cl
Discarding all records can take several times longer than loading the data.
V5.4.0.3
Student Notebook
Uempty
LOAD phases
UTILINIT
Sysin
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Sequential
Data Set
RELOAD
Table
Space
SORT
Sysut1
Sortout
BUILD
Index
Violations Deleted
Unique IX
Load
Errors
Errors
Row
Map
INDEXVAL
Syserr
Sysmap
ENFORCE
Discarded Rows
DISCARD
Error
Summary
Sysdisc
REPORT
CV8317.0
Notes:
LOAD RESUME NO
cl
Message DSNU345I reports keys which duplicate another previously loaded, showing
the input record number and RID of both.
All rows with the duplicating key values are removed from the target table.
Ex
A summary report shows the input records rejected, their error type, and their position in
the input data set.
pr
DSNU344I reports rows with a key duplicating an existing key of a row in the table. The
invalid input row is then deleted with a message, but NOT THE ROW ALREADY
EXISTING IN THE TABLE.
4-27
Student Notebook
DISCARD file
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
WHEN condition
not satisfied
Conversion
error
Table check
constraint violation
DISCARD
FILE
Duplicate key
in unique index
Referential
constraint violation
CV8317.0
Notes:
cl
Ex
- Conversion errors
pr
V5.4.0.3
Student Notebook
Uempty
INDEX
ENTRIES
A
B
C
D
B
OPTIONAL
DISCARD
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
INPUT
KEYS
C
D
C
D
USER CORRECTION
CV8317.0
pr
Ex
cl
Notes:
4-29
Student Notebook
LOAD considerations
Pre-process input to remove errors
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Duplicate keys
Check constraint violations
Referential constraint violations
Unless you have self-referencing tables and you do not load the whole
data at once, or you have cycles with non-null foreign keys
CV8317.0
Notes:
cl
The LOAD utility will LOAD data coming from a sequential data set into one or more tables
in the same table space. Since the data is coming from a non-DB2 source, all integrity
checks have to be performed:
Ex
Entity integrity
Referential integrity
Check integrity
Check integrity is accomplished during the initial load of the data; other integrity checks are
done after the data is loaded.
pr
By the time LOAD gets to the ENFORCE phase, all the data has been loaded (and the
indexes built).
In case LOAD is interrupted, the data and indexes will be left in "restrictive states", such as
Check Pending and Recover Pending (see later).
LOAD errors include conversion errors and table check constraint errors. If LOAD errors
occur, the utility abends unless a DISCARD data set is provided.
4-30 DB2 10 for z/OS DB Admin Workshop Part 1
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
LOAD
utility
DB2 family
Oracle
Sybase
Informix
IMS
VSAM
SQL Server
NCR Teradata
SELECT
Local DB2,
DRDA, or
Information
Integrator
Data
conversion
EXEC SQL
DECLARE C1 CURSOR FOR
SELECT *
FROM HONOLULU.SCHEMA1.EMP
WHERE SALARY > 10000
ENDEXEC
LOAD DATA INCURSOR C1
REPLACE
INTO TABLE MYEMP
CV8317.0
Notes:
You can load the output of any SQL SELECT statement directly into a DB2 for z/OS table
using the LOAD utility.
Ex
cl
Since the SELECT can access any DRDA server, the data source can be any member of
the DB2 family, Information Integrator, or any other vendor that supports DRDA server
capabilities.
This function of the LOAD utility is referred to as the DB2 Family Cross Loader Function.
pr
EXEC SQL
DECLARE C1 CURSOR FOR
SELECT *
FROM HONOLULU.SCHEMA1.EMP
WHERE SALARY > 10000
ENDEXEC
LOAD DATA INCURSOR C1
REPLACE
INTO TABLE MYEMP
4-31
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
LOAD DATA
FORMAT DELIMITED COLDEL , CHARDEL " DECPT .
INTO TABLE DELT
(FILENO
CHAR,
NAME
VARCHAR,
DATE1
DATE EXTERNAL,
TIME1
TIME EXTERNAL)
"001", KUMAR,2000-02-16, 00.00.00
"002", KASCHTA,2001-04-17, 06.30.00
"003", HUTH,2002-06-18, 12.30.59
"004", WHITLARK,1991-08-19, 18.59.30
"005", BAUMGARTNER,2000-12-20
CV8317.0
Notes:
cl
The control statement specifies that data in delimited format is to be loaded into the
specified columns (FILENO, DATE1, and TIME1) in table DELT. The FORMAT DELIMITED
option indicates that the data is in delimited format.
Ex
The COLDEL option indicates that the column delimiter is a comma (,). The CHARDEL
option indicates that the character string delimiter is a double quotation mark ("). The
DECPT option indicates that the decimal point character is a period (.). You are not
required to explicitly specify these particular characters, because they are all defaults.
pr
Note
All numeric fields are in EXTERNAL format for LOAD. Field specifications of INTEGER or
SMALLINT are treated as if they are INTEGER EXTERNAL; specifications of DECIMAL,
DECIMAL PACKED, or DECIMAL ZONED are treated as DECIMAL EXTERNAL; and
specifications of FLOAT, REAL, or DOUBLE are treated as FLOAT EXTERNAL.
V5.4.0.3
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
Uempty
4-33
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
cl
CHECK PENDING is a DB2 restrictive state that is set on if there is a potential violation of
a constraint.
Ex
pr
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
4-35
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
cl
Ex
Name of table
Constraint name
pr
FOR EXCEPTION
DELETE NO
- All dependent (not descendent) tables must be specified with the IN keyword.
4-36 DB2 10 for z/OS DB Admin Workshop Part 1
V5.4.0.3
Student Notebook
Uempty
- All dependent (not descendent) tables must be specified with the USE keyword.
Note
If CHECK DATA specifies DELETE NO and SHRLEVEL REFERENCE, and constraint
violations are detected, what is done by the utility depends on the DB2 version you use.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CHECK DATA utility places the table space in the CHECK-pending status.
DB2 10:
If CHECK_SETCHKP is set to NO (which is the default), then CHECK DATA utility does
not place the table space in check-pending status.
If CHECK_SETCHKP is set to YES, then CHECK DATA utility places the table space in
check-pending status.
IN
IN
IN
IN
UID1.TBL1
UID1.TBL2
UID1.TBL3
UID1.TBL4
USE
USE
USE
USE
UID1.E_TBL1
UID1.E_TBL2
UID1.E_TBL3
UID1.E_TBL4
DELETE YES
- Invalid rows can optionally be deleted from the dependent table after they have
been copied to the exception table.
- Option is invoked by DELETE YES.
cl
Ex
pr
SCOPE PENDING causes only rows in dependent table spaces added by the LOAD utility
since the last invocation of CHECK DATA to be checked. This is the default.
4-37
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
An exception table is a user-created table that mimics the definition of a table being
checked.
cl
The first n columns of the exception table must have exactly the same name and
description as the corresponding column of the table being checked.
Ex
The nth + 1 column is optional. If you define it, CHECK DATA provides the RIDs of the
invalid rows of the table being checked. You can use the RIDs to match the utility output
with rows in the exception table. The RID column must be defined as CHAR(5) for
LARGE table spaces, but may be either CHAR(4) or (5) for other table spaces,
pr
The nth + 2 column is optional. If it exists it must be defined as a TIMESTAMP, and the
starting time of the CHECK utility will be placed in it. It can be used to identify different
invocations of the CHECK utility.
V5.4.0.3
Student Notebook
Uempty
Any other columns of the exception table that are not in the dependent table will be
ignored, but must be nullable or NOT NULL WITH DEFAULT.
An exception table can reside in any table space, but should not have a unique index or
any other constraint that could cause errors at insert.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
An exception table can be easily defined using the CREATE TABLE statement with the
LIKE operand.
The ALTER TABLE ADD statement can be used to add the additional columns.
pr
Ex
cl
The CHECK utility will NOT ensure that the exception tables are empty at the beginning
of the processing.
4-39
Student Notebook
UTILINIT
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SYSIN
DB2
SCANTAB
TABLE
Repeat for
each table
SYSUT1
SORT
SORTOUT
DB2
Parent
PK
CHECKDAT
OR
DB2
Dependent
FK IX
IX
SYSERR
DB2
DB2
Descendent
TABLES
REPORTCK
EXCEPTION
TABLES
Summary
Report
CV8317.0
Notes:
The CHECK DATA utility checks table spaces for referential integrity or table check
constraint violations.
cl
When a table space is in the CHECK PENDING restrictive state, the CHECK DATA utility
should be run to reset the status and do the necessary cleanup.
pr
Ex
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
CHECK DATA uses index-only access when an index is available and no sort is required.
cl
However, if there is no index available on a FK, or the index contains additional columns
that are not part of the FK, the FK key values are extracted and sorted before checking the
constraints.
Ex
pr
4-41
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Student Notebook
V5.4.0.3
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
Uempty
4-43
Student Notebook
Incremental
Image Copy
Partitioned
Table space
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Table space
DB2 UNLOAD
Data Conversion
Row Sampling
UNLOAD Data
LOAD Utility
Statement
CV8317.0
Notes:
Ex
cl
1.
2.
3.
4.
5.
6.
7.
pr
UNLOAD has a sampling capability so that you can reduce the number of rows to be
unloaded to fill, say, a test table. You can also limit the number of rows to be unloaded.
UNLOAD has a number of conversion options so that you can convert character data:
One CCSID to another.
EBCDIC, ASCII, Unicode
4-44 DB2 10 for z/OS DB Admin Workshop Part 1
V5.4.0.3
Student Notebook
Uempty
You can:
Reorder columns compared to the order in the table
Specify the position of columns in the unload data set
You can unload from copies created by:
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
COPY
- Full image copy
- Incremental image copy
- Copy of a partition
- Copy of a data set of a nonpartitioned table space.
COPYTOCOPY
MERGECOPY
Inline full image copies from LOAD or REORG
DSN1COPY
pr
Ex
cl
4-45
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
cl
The table spaces from which the data is to be unloaded can be selected by specifying a
table space name for the TABLESPACE keyword.
Ex
If you specify the name of a partitioned table space, then you can also specify an individual
partition or a range of partitions.
For a selected table space, rows from the entire table space, selected partitions, or
selected tables within the table space can be unloaded. Specific columns can also be
selected.
pr
Specific tables can be selected for unloading using one or more FROM TABLE
specification:
If no FROM TABLE specification is given, the rows from all the tables in the table space will
be unloaded.
If one or more FROM TABLE specification is given, only the rows belonging to the specified
tables are unloaded.
4-46 DB2 10 for z/OS DB Admin Workshop Part 1
V5.4.0.3
Student Notebook
Within a FROM TABLE specification, you can also specify which columns and rows should
be unloaded. When a column (field specification) list is given, only the listed columns are
unloaded, and the columns are unloaded in the listed order. If the column list is omitted, all
the columns in the row are unloaded, and the columns are unloaded in the order in which
the columns are defined in the table. The rows to be unloaded from a table can be qualified
using the WHEN clause. The format of the WHEN clause is similar to the format of the
WHERE clause in an SQL statement.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Uempty
In addition to row selection using the WHEN clause, the UNLOAD utility also provides a
row sampling capability. The following sampling criteria can be specified:
The percentage of rows to be unloaded from a table (the SAMPLE option). The sampling is
applied to the rows that qualify the WHEN conditions, if specified.
The maximum number of rows to be unloaded from a table (the LIMIT option). If a limit is
specified, then as soon as this limit is reached, the UNLOAD utility stops unloading any
further rows from the table.
SAMPLE and LIMIT are independent options. If both options are specified, the limit is
applied to the sampled rows. For example, let's say you specify SAMPLE 50 LIMIT 4000. If
there are approximately 10,000 rows in the table, and approximately 5,000 rows qualify the
WHEN conditions specified, then the UNLOAD utility unloads approximately 2,500 rows
from the table, but no more than 4,000 rows.
pr
Ex
cl
The HEADER option is used to specify a header field at the beginning of the output records
for a table. This field can then be used to associate an output record with the table from
which it was unloaded. By default, the UNLOAD utility prefixes each output record with a
2-byte binary field which contains the table OBID. HEADER NONE specifies that a header
field should not be created.
4-47
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
EMP000060@@SHEARER# 32250.00
EMP000150@@BECKHAM# 25280.00
EMP000200@@SCHOLES# 27740.00
EMP000220@@OWEN# 29840.00
EMP200220@@HALL# 29840.00
record header
character string
CV8317.0
Notes:
pr
Ex
cl
The visual shows an example of the output records unloaded by the UNLOAD utility control
statement shown on the previous visual.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Note: You can also specify "field-name CONSTANT 'string' " for a table to
request creation of a fixed-length output field which contains the specified
string. In this case, "field-name" cannot be the same as one of the table's
column names.
CV8317.0
Notes:
cl
For each selected column you can use the POSITION option to specify where the column's
output field should start in the output record. Of course, if explicit (byte) start positions are
specified for certain columns, then the values specified must be in ascending order as they
appear in the column selection list.
pr
Ex
An output field can have a different data type from the one defined for the column as long
as the data types are compatible. For example, if the column data type is CHAR, then the
output field data type can be CHAR, VARCHAR, or CLOB. The output field data type is
reflected in the generated LOAD statement.
4-49
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
#001#;#KUMAR#;02/16/2000;00:00 AM
No trailing blanks for VARCHAR
#002#;#KASCHTA#;04/17/2001;06:30 AM
#003#;#HUTH#;06/18/2002;12:30 PM
#004#;#WHITLARK#;08/19/1991;06:59 PM
#005#;#BAUMGARTNER#;12/20/2000;12:00 AM
Note that field lists are optional for LOAD / UNLOAD, and are primarily used
for selecting a subset of columns or selecting columns in a different order
Copyright IBM Corporation 2011
CV8317.0
Notes:
UNLOAD utility can produce a delimited file when unloading the data and simplifies the
process of moving or migrating data out of DB2 for z/OS.
Ex
cl
When delimited is specified for UNLOAD, the NOPAD option is in effect for variable length
column output, even if NOPAD is not specified. Therefore, trailing padded blanks are not
used for variable length columns. For example, if a VARCHAR (100) field contains the data
ABC in it, UNLOAD DELIMITED unloads it as "ABC".
pr
For fixed length columns, the normal padding rules applies. For example, a CHAR(100)
column containing ABC, UNLOAD DELIMITED unloads as "ABC <97 blanks>" (with 97
blanks actually following ABC).
The UNLOAD utility generates the delimited LOAD utility control statement in the data set
specified in PUNCHDDN.
V5.4.0.3
Student Notebook
Uempty
Error handling
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
cl
The MAXERR keyword specifies the number of records in error at which the unloading
process should terminate. When the error count reaches the specified number, the
UNLOAD utility issues message DSNU1219 and terminates with return code 8.
Ex
The default is MAXERR 1, which means that the UNLOAD utility terminates when it
encounters the first error. If zero or a negative number is specified, then the check on the
error count will not be performed (utility execution continues regardless of the number of
records in error).
pr
If multiple table spaces are being processed, the number of records in error is counted
separately for each table space.
If the input data contains rows belonging to dropped tables, then these rows will be ignored
by the UNLOAD utility and the error count will not be incremented.
If less than MAXERR errors are encountered, then the final return code will be 4.
4-51
Student Notebook
Considerations
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
The UNLOAD utility does not support unloading data from index spaces or concurrent
image copy data sets.
pr
Ex
cl
The UNLOAD utility does not support the specification of a view name in the FROM TABLE
clause. To overcome this restriction, you can use the sample program DSNTIAUL. This is
an SQL program. You might choose to use it, for example, when you want to unload data
with the use of some SQL functions or when you want to unload only a few rows specified
with a WHERE condition and where the access path is supported by an index.
V5.4.0.3
Student Notebook
Uempty
Checkpoint quiz (1 of 3)
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
This is an optional quiz. Use the student notes and the Utility Guide to research your
answers. Write your answers here:
cl
1.
Ex
2.
pr
3.
4.
4-53
Student Notebook
Checkpoint quiz (2 of 3)
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Partition level
CV8317.0
Notes:
6.
7.
pr
Ex
cl
5.
V5.4.0.3
Student Notebook
Uempty
Checkpoint quiz (3 of 3)
8.
T3
T2
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
T1
CV8317.0
Notes:
pr
Ex
cl
8.
4-55
Student Notebook
Unit summary
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Having
Havingcompleted
completedthis
thisunit,
unit, you
youshould
shouldbe
beable
ableto:
to:
Describe
Describehow
howDB2
DB2utilities
utilitiesmay
maybe
beexecuted,
executed,monitored,
monitored,
terminated,
terminated,and
andrestarted
restarted
Run
Runthe
theDB2
DB2LOAD
LOADutility,
utility,and
andmake
makefull
fulluse
useof
ofits
itsmany
many
features
features
Run
Runthe
theCHECK
CHECKDATA
DATAutility
utilityto
tocheck
checkDB2
DB2table
tablespace
spacedata
data
for
forany
anyviolations
violationsof
ofreferential
referentialintegrity
integrityconstraints
constraintsor
ortable
table
check
checkconstraints
constraints
Run
Runthe
theDB2
DB2UNLOAD
UNLOADutility,
utility,and
andmake
makefull
fulluse
useof
ofits
its many
many
features
features
CV8317.0
pr
Ex
cl
Notes:
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Machine exercises
SC19-2968
SC19-2978
SC19-2983
SC19-2984
SC18-9840
SC18-9851
SC18-9854
SC18-9855
SC18-7413
SC18-7426
SC18-7427
pr
Ex
cl
References
5-1
Student Notebook
Unit objectives
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
After
Aftercompleting
completingthis
thisunit,
unit,you
youshould
shouldbe
beable
ableto:
to:
Describe
Describeand
andinterpret
interpretthe
thestatistics
statisticscollected
collectedby
byRUNSTATS
RUNSTATS
and
andreal-time
real-timestatistics
statistics
Run
RunRUNSTATS
RUNSTATSwith
withan
anappropriately
appropriatelychosen
chosenset
setof
ofoptions
options
Describe
Describehow
howDB2
DB2collects
collectsstatistics
statistics in
in real
realtime
time
Describe
Describewhy
whyititisisnecessary
necessaryto
toperiodically
periodicallyREORG
REORGyour
your
table
tablespaces
spacesand
andindex
indexspaces
spaces
Run
RunREORG
REORGwith
withan
anappropriately
appropriatelychosen
chosenset
setof
ofoptions
options
CV8317.0
pr
Ex
cl
Notes:
5-2
V5.4.0.3
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
Uempty
5-3
Student Notebook
RUNSTATS
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Data
Index
Table Space
RUNSTATS
SYSPRINT
REPORT YES
Report
Catalog
Access
path
selection
statistics
OPTIMIZER
Space-related
statistics
DBA
Reorganize?
...
CV8317.0
Notes:
The optimizer, to choose a good access path (access path selection statistics).
cl
The DBA, for space management, to determine the frequency of reorganizations and
for current documentation (space-related statistics).
Ex
RUNSTATS is also used to invalidate all cached dynamic statements using the objects
referred to by RUNSTATS. If you want only to invalidate cached dynamic statements
without collecting statistics, use UPDATE NONE REPORT NO.
pr
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
RUNSTATS ...
Almost real-time
Catalog
Catalog
Space-related
statistics
Space-related
statistics
and more
CV8317.0
Notes:
cl
For the space-related statistics, there is an alternative, called real-time statistics, which
collects the same information as RUNSTATS and some other statistics, such as the
number of rows inserted, updated, or deleted since the last execution of REORG, LOAD
REPLACE, REBUILD INDEX, RUNSTATS, or the number of changed pages since the last
execution of COPY.
pr
Ex
The collection of real-time statistics is almost real time. DB2 writes or updates these
statistics to the mentioned catalog tables every n minutes, n being a system parameter
(DSNZPARM STATSINT).
5-5
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SYSIBM.SYSINDEXSPACESTATS
CV8317.0
Notes:
pr
Ex
cl
Refer to DB2 catalog tables section in the Appendix of DB2 10 for z/OS SQL Reference for
a complete list of columns for real-time statistics contained in the catalog tables
SYSIBM.SYSTABLESPACESTATS and SYSIBM.SYSINDEXSPACESTATS.
5-6
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
pr
Ex
cl
Notes:
5-7
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
OFFPOSLIMIT integer
cl
You may recall from unit 2 that an SQL INSERT causes DB2 to search for the best page to
hold the row so that it can maintain the rows in clustering sequence. If there is insufficient
free space on the best page then DB2 has to choose a less favorable page.
Ex
Occurrences of records that have been inserted into pages other than their best pages are
reported by the RUNSTATS utility in NEAROFFPOSF and FAROFFPOSF in
SYSIBM.SYSINDEXPART
pr
When you run the REORG utility you can specify OFFPOSLIMIT integer. This means that
DB2 does a calculation involving NEAROFFPOSF and FAROFFPOSF and compare the
result with your OFFPPOSLIMIT integer. If this integer is exceeded then REORG is
performed or recommended.
5-8
V5.4.0.3
Student Notebook
Uempty
INDREFLIMIT integer
You may recall from unit 2 that an SQL UPDATE can increase the length of a variable
length row and if there is insufficient free space on the page to hold the expanded row then
DB2 may have to place it on a neighboring, less favorable page.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Occurrences of records that have overflowed to pages other than their best pages are
reported by the RUNSTATS utility in NEARINDREF and FARINDREF in
SYSIBM.SYSTABLEPART.
When you run the REORG utility you can specify INDREFLIMIT integer. This means that
DB2 does a calculation involving NEARINDREF and FARINDREF and compare the result
with your INDREFLIMIT integer. If this integer is exceeded then REORG is performed or
recommended.
LEAFDISTLIMIT integer
You may recall from unit 2 that when you SQL INSERT a row and the free space on an
index leaf page has been used up, a page split occurs. Occurrences of page splits are
reflected by the LEAFDIST value in SYSINDEXPART.
You should consider reorganizing an index when leaf page splits start to appear, because
leaf page splits make sequential processing slower.
pr
Ex
cl
When you run the REORG INDEX utility you can specify LEAFDISTLIMIT integer. This
means DB2 compares the LEAFDIST value in SYSINDEXPART for your index with your
LEAFDISTLIMIT integer. If this integer is exceeded then REORG INDEX is performed or
recommended.
5-9
Student Notebook
TABLE
or
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
TABLE, COLUMN,
INDEX
not specified
TABLE (ALL)
TABLE (...)
COLUMN (...)
INDEX (...)
CV8317.0
pr
Ex
cl
Notes:
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
HISTORY
PART
CV8317.0
Notes:
History statistics are for your own documentation. They are not used by DB2.
Ex
cl
Partition statistics (collected using the PART keyword) are not used by the optimizer for
access path selection. The optimizer uses only statistics at the table space / index level.
For this reason, the RUNSTATS utility also updates statistics at the table space / index
level, even when the PART keyword is used, if one or both of the following applies:
Statistics already exist for ALL other partitions in the table space / index.
pr
The option FORCEROLLUP YES is used in the RUNSTATS utility. If statistics are
missing for one or more partitions, DB2 extrapolates the statistics from the partitions
having statistics. The DSNZPARM STATROLL sets the default for FORCEROLLUP.
Other options (KEYCARD, FREQVAL, ...) are related to access path selection statistics
and are covered in detail in course CV96.
5-11
Student Notebook
Inline statistics
The inline statistics function is supported by:
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
pr
Ex
cl
Notes:
V5.4.0.3
Student Notebook
Uempty
Recommended RUNSTATS
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
TABLESPACE DB.TS specifies the table space (and the database to which it belongs) on
which table space and table statistics are to be gathered.
cl
SHRLEVEL CHANGE allows other programs to change the table space or index. With
SHRLEVEL CHANGE, RUNSTATS might collect statistics on uncommitted data.
Ex
TABLE (ALL) specifies that column statistics are to be gathered on all columns of all tables
in the table space.
INDEX (ALL) specifies that column statistics are to be gathered for all indexes that are
defined on tables that are contained in the table space.
pr
KEYCARD collects all of the distinct values in all of the 1 to n intermediate key column
combinations for the specified indexes. n is the number of columns in the index. For
example, suppose that you have an index defined on three columns: A, B, and C. If you
specify KEYCARD, RUNSTATS collects cardinality statistics for the intermediate column
set A and B.
5-13
Student Notebook
In DB2 10, the KEYCARD option is deprecated in the RUNSTATS TABLESPACE (and
RUNSTATS INDEX) control statement and no longer needs to be specified to collect
statistics on the values in the key columns of an index if INDEX is specified.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
The RUNSTATS utility automatically collects all of the distinct values in all of the 1 to n
intermediate key column combinations for the specified indexes, where n is the number of
columns in the index. For example, suppose that you have an index defined on three
columns: A, B, and C. RUNSTATS collects cardinality statistics for column A, column set A
and B, and column set A, B, and C. With the deprecation of KEYCARD, this functionality
cannot be disabled.
The RUNSTATS utility tolerates the specification of the KEYCARD option. The utility does
not issue any messages if the control statement includes or excludes the KEYCARD option
when INDEX is specified.
Histogram statistics
This is a way of summarizing data distribution. This technique divides up the range of
possible values into intervals (quantiles), such that each interval contains approximately
the same percentage of the rows. A set of statistics is collected for each interval. This gives
the optimizer the potential to select a better access path.
The number of intervals is specified by the value of NUMQUANTILES when you use the
HISTOGRAM option of RUNSTATS. For example:
RUNSTATS TABLESPACE DB.TS
HISTOGRAM NUMCOLS 2 NUMQUANTILES 10
RUNSTATS will produce an equal-depth histogram, that is, each interval (range) will have
about the same number of rows. As a general recommendation, specify NUMQUANTILES
100 or, for example, if the query ranges are always done on boundaries like 0-10%,
10-20%, 20-30%... then NUMQUANTILES 10 might be a better choice.
cl
The better filter factor derived from histogram statistics will benefit RANGE, LIKE and
BETWEEN predicates. Furthermore, the optimizer will evaluate the predicate filter factor
more accurately if the searching range matches the boundary of any one quantile or any
group of consecutive quantiles.
Ex
In the example on the visual, NUMCOLS 1 and NUMQUANTILES 100 are used by default.
Important
pr
Running RUNSTATS in this way will ensure that all important statistics are collected. The
overhead of collecting lots of statistics is by far outweighed by the better access path
chosen by the optimizer due to the availability of complete statistics. One way to reduce
RUNSTATS costs is to run RUNSTATS only if needed, meaning that 20% of the data has
changed since the last RUNSTATS.
V5.4.0.3
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
5.2. REORG
pr
Ex
cl
Uempty
5-15
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Eliminate overallocations
CV8317.0
Notes:
cl
For non-partitioned table spaces, all indexes related to all tables in the table space are
"reorganized" when the table space is reorganized, since they are rebuilt as part of the
process.
Ex
For partitioned table spaces, if the whole table space is reorganized, all indexes related to
the table in the table space are "reorganized" when the table space is reorganized, since
they are rebuilt as part of the process.
pr
For partitioned table spaces, if one or more partitions are reorganized, the corresponding
partitions of all partitioned indexes related to the table in the table space are
"reorganized", since they are rebuilt as part of the process. The nonpartitioned indexes are
not rebuilt, and therefore are not reorganized. The RIDs of the corresponding logical
partitions in the nonpartitioned indexes are corrected in place.
V5.4.0.3
Student Notebook
Uempty
Note
One more reason to reorganize table spaces is to reset the REORP, AREO*, and AREOR
states.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
The REORG-pending (REORP) restrictive status indicates that a table space partition
definition has changed and the affected partitions must be reorganized before the data is
accessible.
The REORG-pending (AREO*) advisory status indicates that a table space, or partition
needs to be reorganized for optimal performance.
pr
Ex
cl
The advisory REORG-pending (AREOR) status indicates that a table space needs to be
reorganized for optimal performance and to apply pending definition changes. This state is
applicable only in DB2 10.
5-17
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Eliminate overallocations
CV8317.0
Notes:
Note
cl
One more reason to reorganize indexes is to reset the AREO* and AREOR states.
Ex
The REORG-pending (AREO*) advisory status indicates that an index, or partition needs to
be reorganized for optimal performance.
pr
V5.4.0.3
Student Notebook
Uempty
REORG TABLESPACE
UTILINIT
PHASE
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
TABLE
SPACE
UNLOAD
PHASE
RELOAD
PHASE
SORT
PHASE
BUILD
PHASE
TABLE
SPACE
SYSIBM.
SYSCOPY
INDEX
SPACE(S)
CV8317.0
Notes:
cl
The SHRLEVEL parameter determines the accessibility of the data during reorganization
and the destination of the reloaded data.
SHRLEVEL NONE
Ex
pr
Data can be read only during the unload of the data, and no SELECT, INSERT, UPDATE or
DELETE statements are allowed while the data is being reloaded.
LOG specifies whether records are logged during the RELOAD phase of REORG. If the
records are not logged, the table space is recoverable only if an image copy has been
taken before REORG. For SHRLEVEL NONE, the default is LOG YES.
SHRLEVEL NONE always reloads data back in the original data sets. The REUSE
parameter avoids to DELETE/DEFINE these data sets.
5-19
Student Notebook
SHRLEVEL REFERENCE
The data can only be read during most of the reorganization processing. The data is
reloaded into shadow (new) data sets.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
REORG SHRLEVEL REFERENCE is outside the scope of this course and is covered in
detail in course CV870.
SHRLEVEL CHANGE
SELECT, INSERT, UPDATE, and DELETE processing can be executed on the original data
during most of the reorganization processing. The data is reloaded into shadow (new) data
sets.
REORG SHRLEVEL CHANGE is outside the scope of this course and is covered in detail
in course CV870.
STATISTICS
pr
Ex
cl
The first time you run a REORG, DB2 automatically converts the table space from the old
basic format to the new reordered format. With reordered row format, DB2 no longer stores
the length of varying length columns, and the data can look significantly different within the
row/page.
V5.4.0.3
Student Notebook
Uempty
REORG INDEX
SHRLEVEL NONE / REFERENCE / CHANGE
REUSE
STATISTICS
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
UTILINIT
PHASE
INDEX
UNLOAD
PHASE
SYSUT1
BUILD
PHASE
INDEX
CV8317.0
Notes:
The options for REORG INDEX are a subset of those for REORG TABLESPACE.
pr
Ex
cl
5-21
Student Notebook
REORG examples
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
cl
In the second example above, PART 3 says reorganize PART 3 of the table space, PART 3
of all partitioned indexes, and update the index entries for PART 3 in all nonpartitioned
indexes.
Ex
In the third example above, PART 3:6 says reorganize PART 3 through PART 6 of the table
space, PART 3 through PART 6 of all partitioned indexes, and update the index entries for
PART 3 through PART 6 in all nonpartitioned indexes.
Note
pr
Reorganizing partitions of a table space having NPIs results in the reorganization of the
whole NPI.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
pr
Ex
cl
Notes:
5-23
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
REORG INDEX
REPORTONLY keyword
Checks limits and sets return code without reorganizing the data
Return codes
CV8317.0
Notes:
cl
REPORTONLY checks the limits, set the return code, and produce a report.
pr
Ex
Return codes are set to indicate the action taken or not taken.
V5.4.0.3
Student Notebook
Uempty
Unit summary
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Having
Havingcompleted
completedthis
thisunit,
unit, you
youshould
shouldbe
beable
ableto:
to:
Describe
Describeand
andinterpret
interpretthe
thestatistics
statisticscollected
collectedby
byRUNSTATS
RUNSTATS
and
andreal-time
real-timestatistics
statistics
Run
RunRUNSTATS
RUNSTATSwith
withan
anappropriately
appropriatelychosen
chosenset
setof
ofoptions
options
Describe
Describehow
howDB2
DB2collects
collectsstatistics
statistics in
in real
realtime
time
Describe
Describewhy
whyititisisnecessary
necessaryto
toperiodically
periodicallyREORG
REORGyour
your
table
tablespaces
spacesand
andindex
indexspaces
spaces
Run
RunREORG
REORGwith
withan
anappropriately
appropriatelychosen
chosenset
setof
ofoptions
options
CV8317.0
Notes:
pr
Ex
cl
5-25
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Student Notebook
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
This unit describes what you need to do to make DB2 user data
available again following media failures.
Machine exercises
SC19-2968
SC19-2983
SC19-2984
SC19-2972
SC18-9840
SC18-9854
SC18-9855
SC18-9844
SC18-7413
SC18-7426
SC18-7427
pr
Ex
cl
References
SC18-7416
6-1
Student Notebook
Unit objectives
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
After
Aftercompleting
completingthis
thisunit,
unit,you
youshould
shouldbe
beable
ableto:
to:
Make
MakeDB2
DB2user
userdata
dataavailable
availableagain
againfollowing
followingmedia
mediafailures
failures
CV8317.0
pr
Ex
cl
Notes:
6-2
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Create TS TS1
A updates
Recover
Tablespace TS1
(to current)
updates
updates D
updates
Time
Log
FIC of TS1
IIC
TS1
RESTORE
SYSIBM.SYSCOPY
ICTYPE
C
F
I
I
TSNAME
TS1
TS1
TS1
TS1
START_RBA
A
B
C
D
IIC
TS1
LOG APPLY
BSDS
Directory of
ARCHIVE LOG /
ACTIVE LOG
Archlog
Active L.
SYSIBM.SYSLGRNX
TS1
TS1
TS1
TS1
A1 A8
B1 B7
C1 C7
D1 D7
CV8317.0
Notes:
pr
Ex
cl
Before we discuss the individual details, the overall big picture for application data recovery
is presented in this visual.
6-3
Student Notebook
DB2 logging
Changes
Written to
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DB2 Log
Active Log
Data Set 1
Active Log
Data Set 2
Active Log
Data Set 1
Wrap-around
Offloading
Archive
Log Data
Set n
Active Log
Data Set 3
Archive
Log Data
Set n+1
Archive
Log Data
Set n+2
CV8317.0
Notes:
cl
As you make changes to your tables, DB2 writes appropriate records to the DB2 log,
allowing DB2 to back out the changes if a unit of recovery fails, or to reapply these changes
during a recovery.
Ex
The DB2 log is mapped onto data sets. Each DB2 system has a predefined fixed set of
active log data sets on disk. Log records are first written by DB2 into a log buffer. They are
subsequently written from there onto the active log data sets. As an active log data set fills
up, DB2 moves onto the next active log data set.
pr
When all active log data sets have been filled, DB2 wraps around to the first active log data
set and uses it again. In order not to lose log records that may be required for a backout or
recovery, the active log data sets are automatically offloaded as they fill up.
They are offloaded by DB2 to archive log data sets, which may be on disk but which
frequently reside on tapes. In contrast to the limited set of active log data sets, there is
effectively an endless set of archive log data sets. Only those archive logs that are still
needed for backout or recovery operations need to be kept.
6-4
V5.4.0.3
Student Notebook
Uempty
The individual log data sets, both active and archive, are recorded in the Bootstrap Data
Set (BSDS).
Note
In general, DB2 logs undo and redo records. The following are the exceptions:
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Use the LOG NO option with caution because its use limits your ability to recover data
by using the log. For example, if you issue the CHECK DATA DELETE YES LOG NO
utility control statement at particular log RBA, you can recover data that exists on the
log before that point in time or after the point on the log at which the utility execution
completes.
You should establish image copies after the successful completion of LOAD and
REORG, or take inline image copies as part of LOAD and REORG execution.
Nothing is logged for a LOB table space defined with LOG YES or LOG NO. Control
information is logged for a LOB table space defined with LOG NO even if LOG YES is
specified with the LOAD or REORG utility.
Nothing is logged for a XML table space defined with LOG YES or LOG NO. Nothing is
logged for a XML table space defined with LOG NO even if LOG YES is specified with
the LOAD or REORG utility.
Specifies that changes made to data in this table space are not to be recorded on the
log. The NOT LOGGED attribute suppresses the logging of undo and redo information.
pr
Ex
cl
You should use the NOT LOGGED attribute only for situations where the data is in
effect being duplicated. If the data is corrupted, you can recreate it from its original
source, rather than from an image copy and the log.
6-5
Student Notebook
SYSIBM.SYSLGRNX
1st update
after open
1st update
after open
Last update
before close
Last update
before close
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Table
Space 1
Table
Space 2
1st update
after open
Last update
before close
DB2 Log
DBID OBID
DBID OBID
DBID OBID
Start RBA
End RBA
DB2 Directory
SYSIBM.SYSLGRNX
Copyright IBM Corporation 2011
CV8317.0
Notes:
cl
To be able to determine log records for a specific table space more easily, DB2 uses table
SYSIBM.SYSLGRNX of the DB2 Directory to record periods during which this table space
was updated.
Ex
Each row for a table space identifies a time interval during which there was update activity
for the table space.
pr
The information in SYSIBM.SYSLGRNX allows DB2 to determine the log data sets or even
the parts of the log data sets that DB2 needs to process for a recovery of the table space.
6-6
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Table Space
C
COPY TABLESPACE
FULL YES
DB2 Catalog
COPY TABLESPACE
FULL NO
SYSIBM.SYSCOPY
C = Changed
CV8317.0
Notes:
cl
The DB2 recovery concept is based on the assumption that you regularly back up your
table spaces by creating image copies using the COPY utility. By means of the COPY utility
you can create FULL IMAGE COPIES (FICs) or INCREMENTAL IMAGE COPIES (IICs).
Ex
A full image copy consists of all pages of the table space, whether changed or not.
You specify FULL YES on the COPY utility control statement.
pr
6-7
Student Notebook
SYSIBM.SYSCOPY
Image copies are sequential data sets. The fact that an image copy exists is recorded in
the table SYSIBM.SYSCOPY in the DB2 Catalog.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Each image copy has a row in this table. Column ICTYPE specifies the type of image copy
taken:
pr
Ex
cl
6-8
V5.4.0.3
Student Notebook
Uempty
Backing up partitions
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
PART
1
Image Copy
PART
2
COPY . . .
DSNUM 2
PART
2
PART
3
CV8317.0
Notes:
You can create image copies for units smaller than the entire table space.
Partitioned table spaces
cl
You can create image copies for individual partitions by specifying the partition number via
the DSNUM keyword of the COPY utility control statement.
Ex
DSNUM integer identifies the partition within the partitioned table space to be copied.
Non-partitioned table spaces
pr
You can create image copies for individual data sets by specifying the data set number via
the DSNUM keyword of the COPY utility control statement.
DSNUM integer identifies the data set number within the non-partitioned table space to be
copied.
6-9
Student Notebook
Since image copies are physical copies, you cannot establish image copies for logical
partitions of non-partitioned indexes. You also cannot take image copies of the physical
data sets of non-partitioned indexes.
The DSNUM column of SYSIBM.SYSCOPY row for an image copy identifies the partition
or data set copied.
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
If you establish the image copies for partitions or data sets, then you cannot recover the
table space as a unit in this case. You must recover the table space partition by partition, or
data set by data set.
V5.4.0.3
Student Notebook
Uempty
Local Backup
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Table Space or
Index Space
CV8317.0
Notes:
cl
If an image copy is not available because it has been destroyed, then DB2 uses the second
copy. DB2 can also fall back and use earlier image copies. This avoids lengthy recoveries
from the log.
Ex
By means of the COPYDDN parameter of the COPY utility control statement, you can
specify the DD names for two copies, the regular image copy, referred to as the PRIMARY
copy, and a second copy, referred to as the BACKUP copy.
pr
For each copy produced, SYSIBM.SYSCOPY contains a row. Column DSNAME specifies
the data set name for the copy, and column ICBACKUP specifies whether it is a PRIMARY
copy, or a BACKUP copy. Most of the other columns are identical, even the TIMESTAMP
column containing the date and time when the row was inserted.
For the copies, you should:
Consider using generation data groups (GDGs) to prevent duplicate data set names.
Specify DISP=(MOD,CATLG) if you want to allow for the restart of the COPY utility.
Copyright IBM Corp. 1993, 2011
6-11
Student Notebook
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Large format data sets are automatically supported when they are input data sets to
utilities. Large format data sets are greater than 65,535 tracks per disk volume. They are
supported as output for utilities if the DD card specifies table spaces that are created with
DSNTYPE=LARGE.
V5.4.0.3
Student Notebook
Uempty
COPY - examples
Example 1
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
FULL
FULL
FULL
FULL
FULL
YES
YES
YES
YES
YES
Example 2
CV8317.0
Notes:
Example 1 shows the COPY utility control statements to take a PRIMARY and a BACKUP
full image copy of each of six table spaces.
cl
Example 2 shows an alternative way of taking the same copies using LISTDEF and
TEMPLATE statements.
Ex
LISTDEF and TEMPLATE statements can be used with most utilities. They ease the
management of utilities, because they minimize the need to change utility control
statements, as a result of:
pr
6-13
Student Notebook
COPYTOCOPY utility
establishing copies of image copies
DB2 Catalog
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SYSIBM.
SYSCOPY
Primary
Backup
CV8915.0
Notes:
cl
Using COPY utility you can establish a primary copy and a backup copy. Of course, if you
establish both primary and backup copies, the longer the COPY, REORG TABLESPACE,
or LOAD utility takes and the longer the object is unavailable.
Ex
You can establish only the primary copy during the utility execution and create the missing
backup copy later by means of the COPYTOCOPY utility. From an existing image copy for
a table space, an index space, a table space partition, or an index partition, the
COPYTOCOPY utility establishes the backup copies and records them in
SYSIBM.SYSCOPY.
pr
The copies established are the corresponding backup, and must not exist yet. You specify
via the COPYDDN parameter.
The SYSIBM.SYSCOPY rows for the copies established by COPYTOCOPY receive the
same TIMESTAMP, ICDATE, ICTIME, and START_RBA values as the input copy. Of
course, the data set names (DSNAME column) must be different.
V5.4.0.3
Student Notebook
Uempty
You have multiple options for selecting the input copy, that is, the copy data set to be
copied. You can select a specific copy data set (FROMCOPY parameter) by specifying its
data set name (and volume serial number and file sequence number if the data set is not
cataloged). For generation data sets, you must provide the fully qualified data set names,
including absolute generation and version numbers.
You can also select the input copy data set in a more generic manner:
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
If you specify FROMLASTCOPY, the most recent image copy for the object is copied
independent of the type of image copy (full image copy or incremental image copy).
If you specify FROMLASTFULLCOPY, the most recent full image copy for the object is
copied.
If you specify FROMLASTINCRCOPY, the most recent incremental image copy for the
object is copied.
If you specify the input copy generically, you do not specify the type of input copy data set
(primary copy, or backup copy) either. From the existing copy data sets, the
COPYTOCOPY utility determines the one it is going to copy.
There are also rules for the order in which missing copies can be established by different
COPYTOCOPY utility jobs: A missing primary copy must be established before a missing
backup copy. If the primary copy does not exist when a backup copy is to be established,
message DSNU1401I is issued and the COPYTOCOPY utility fails.
The example on the visual establishes a backup copy which must not exist yet. Since the
backup copy is established, the primary copy must already exist.
pr
Ex
cl
An advantage of the COPYTOCOPY utility over the COPY utility is that it does not restrict
the access to the table space, index space, or partition. It need not restrict the access since
it copies a copy and not the "real" object. It only needs to restrict the access to
SYSIBM.SYSCOPY. Therefore, it might be your strategy to create only the primary copy by
means of the COPY utility and to establish the backup copy data set using COPYTOCOPY.
6-15
Student Notebook
5
9
13
17
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Table Space
55
10
11
12
14
15
16
18
19
20
Incremental
Image Copy 1
3
21
25
29
10
11
24
Incremental
Image Copy 2
4
11
10
10
11
12
12
13
14
15
16
17
18
19
20
20
22
23
24
21
22
23
24
26
27
28
25
26
27
28
30
31
32
29
30
31
32
RESTORE
LOGAPPLY
10
12 20
DB2 Log
BSDS
DB2 Directory
SYSIBM.SYSLGRNX
Log Data
Sets
CV8317.0
Notes:
cl
Table spaces are recovered by means of the RECOVER utility. When recovering a table
space, the RECOVER utility performs two major phases, the RESTORE phase and the
LOGAPPLY phase.
Ex
By means of the rows in SYSIBM.SYSCOPY, the RECOVER utility determines the full
image copy it needs as a basis for the requested recovery.
RESTORE phase
pr
During the RESTORE phase, it merges the full image copy with its incremental image
copies to form the basis for the restored table space. Pages in an incremental image copy
replace the appropriate pages in the full image copy or in earlier incremental image copies.
The restored version of the table space is far from complete, because there were most
likely changes to the table space after the latest image copy was taken. These changes are
recorded only on the DB2 log.
V5.4.0.3
Student Notebook
Uempty
LOGAPPLY phase
During the LOGAPPLY phase, the appropriate log records are read from the DB2 log and
applied to the restored table space.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
This is where SYSIBM.SYSLGRNX comes in handy, because it identifies the ranges on the
DB2 log pertaining to the table space being recovered. DB2 needs to read only these log
ranges.
Point-in-time recovery
Point-in-time recovery, also known as partial recovery, is the process of resetting part or all
of your data to an earlier point in time. The reasons for point-in-time recoveries can be
manifold. In contrast to a recovery to current which is generally used to correct physical
problems, point-in-time recoveries are mostly used to undo logical errors such as the
execution of an erroneous program as illustrated above:
Imagine that, after having stopped the online service and having taken image copies of
your table spaces and COPY enabled index spaces, you run a program overnight. When
returning to the office the next morning, you discover that the program did not work as
expected and has corrupted your data. As a consequence, you may not have another
chance than resetting the data to a point in time before you started the erroneous program,
most likely to the point in time when the image copies were taken.
DB2 supports point-in-time recovery. However, it does not allow you to back out erroneous
programs or transactions as such. It only allows you to reset your data consistently to a
prior point in time.
cl
Point-in-time recoveries must be handled with extreme care. In many cases, you cannot
really perform point-in-time recoveries. Unless you have a detailed transaction log allowing
you to reapply the transactions correctly, you cannot reset your data if there were updating
online transactions in the meantime. Many transactions involve human interactions that are
not automatically undone by resetting the data. For example, money may have been paid
to a customer who has left in the meantime. The customer does not come back just
because you are resetting your data. Similarly, customers who have deposited money in
bank accounts would not be very happy finding out that their money has disappeared.
Ex
pr
6-17
Student Notebook
Recovery - examples
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Example 1
RECOVER TABLESPACE DB01.TS01
Example 2
RECOVER TABLESPACE
TABLESPACE
TABLESPACE
TABLESPACE
DB01.TS01
DB01.TS01
DB01.TS01
DB01.TS01
DSNUM
DSNUM
DSNUM
DSNUM
1
2
3
4
Example 4
CV8317.0
Notes:
You can use the RECOVER utility to recover an entire table space, a list of table spaces,
individual partitions, or data sets.
cl
If you make image copies by table space, you can recover the entire table space or a data
set or partition from the table space.
pr
Ex
If you make image copies separately by partition or data set, you must recover the
partitions or data sets by separate RECOVER steps.
V5.4.0.3
Student Notebook
Uempty
DB2 catalog
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SYSIBM.SYSCOPY
DB2 Directory
SYSIBM.SYSLGRNX
BSDS
CV8317.0
Notes:
With the REPORT RECOVERY utility, you can determine which image copies and archive
log data sets (if any) you need for the recovery of a table space.
cl
Ex
pr
By specifying CURRENT, you can restrict the information to entries since the most recent
recovery base. This is the point at which the most recent full image copy was taken, or
when LOAD REPLACE LOG YES or REORG LOG YES was last run.
By specifying SUMMARY, you can limit the information to the volume serial numbers for the
image copies and for the archive log data sets.
6-19
Student Notebook
9
13
17
21
25
29
10
11
12
5
9
13
17
21
25
29
2
6
3
7
4
8
14
15
16
18
19
20
22
23
26
30
10
11
12
13
14
15
16
17
18
19
20
24
21
22
23
24
27
28
25
26
27
28
31
32
29
30
31
32
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
10
11
12
14
15
16
18
19
20
22
23
24
26
27
28
30
31
32
Incremental
Image Copy 1
3
10
11
Incremental
Image Copy 2
24
11
MERGECOPY . . . NEWCOPY NO
Incremental
Image Copy 1
3
10
11
Incremental
Image Copy 2
24
10
11
24
11
CV8317.0
Notes:
The MERGECOPY utility allows you to merge image copies of a table space in advance to
reduce the time required when a recovery is necessary.
cl
Ex
You can merge the latest full image copy and the incremental image copies based on it
to form a new full image copy (NEWCOPY YES).
You can merge the incremental image copies of the latest full image copy to form a new
single incremental image copy (NEWCOPY NO).
pr
V5.4.0.3
Student Notebook
Uempty
Table
Space
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
UNLOAD
A partition
A logical partition
Key/RID pairs
for all rows
and indexes
SORT
SORTBLD
BUILD
Index 1
Index 2
CV8317.0
Notes:
The REBUILD INDEX utility allows you to rebuild an index from scratch using the data in
the table on which the index is based.
cl
If the table space is defective as well, then you must recover the table space before you
can rebuild the index.
Ex
You can rebuild the entire index, a partition of a partitioned index, or a logical partition of a
nonpartitioned index. A logical partition always corresponds to a physical partition of the
associated partitioned table space. It contains all index entries whose target rows are
contained in that physical partition.
pr
With a single REBUILD INDEX statement you can rebuild multiple indexes, index partitions
or logical partitions. All indexes or index partitions rebuilt must be for tables of the same
table space.
REBUILD INDEX allows you to rebuild all indexes for all tables of a table space by
specifying the table space.
6-21
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
pr
Ex
cl
The visual shows some examples of REBUILD INDEX and REBUILD INDEXSPACE.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
pr
Ex
cl
Notes:
6-23
Student Notebook
Non-Partitioned
Index
PSRBD
RBDP*
RBDP
RECP
RECP
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
RBDP
RBDP
Data Partitions
CUSTNO
Index
Logical
Partition
Logical
Partition
Logical
Partition
Logical
Partition
CUSTNAME
Index
CV8317.0
Notes:
RECP
cl
Ex
A table space or table space partition is broken and must be recovered by running the
RECOVER utility on the affected object.
RBDP
pr
V5.4.0.3
Student Notebook
Uempty
RBDP*
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
PSRBD
Set, for example, by LOADing data partition and abending in BUILD phase for the NPI
Can be set for:
pr
Ex
cl
6-25
Student Notebook
MODIFY RECOVERY
integer
(*)
integer
LAST
(*)
(integer)
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DELETE
RETAIN
Queries SYSCOPY
Queries BSDS
Queries GDG
LOGLIMIT
GDGLIMIT
GDGLIMIT
GDGLIMIT
LAST
(integer)
LOGLIMIT
CV8317.0
Notes:
cl
Since SYSIBM.SYSLGRNX can become very large, run the MODIFY RECOVERY utility
regularly to delete obsolete SYSIBM.SYSCOPY and related rows (log ranges) from
SYSIBM.SYSLGRNX.
Ex
If you specify the AGE parameter, the rows older than the specified number of days are
deleted. In particular, AGE(0) means that the rows older than zero days are deleted, that is,
the rows created yesterday or before. If you specify the DATE parameter, the rows created
before the specified date are removed. For AGE(*) or DATE(*), all rows for the specified
object are deleted regardless of their age or the date on which they were created.
pr
You can specify an entire table space or, by means of the DSNUM parameter, a specific
partition or data set. As usual, an entire table space is specified by not providing the
DSNUM parameter or specifying DSNUM ALL.
If you specify an entire table space, those SYSIBM.SYSCOPY rows for the entire table
space and for its partitions or data sets are deleted that meet the specified age or date
criterion. In addition, all SYSIBM.SYSLGRNX rows for the table space or its partitions are
removed meeting the age or date criterion.
6-26 DB2 10 for z/OS DB Admin Workshop Part 1
V5.4.0.3
Student Notebook
Uempty
If you specify a specific partition or data set, SYSIBM.SYSCOPY rows for the specified
partition or data set, meeting the specified age or date criterion, are deleted. However,
SYSIBM.SYSCOPY rows having a START_RBA greater than that of the most recent
recoverable point for the entire table space are not deleted and message DSNU577I is
issued. The most recent recoverable point may have been established by a full image copy,
an inline copy, a LOAD LOG YES, or a REORG TABLESPACE LOG YES.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
If a partition copy is deleted, the SYSIBM.SYSLGRNX rows for the partition copy, meeting
the age or date criterion, are deleted as well.
When performing a MODIFY RECOVERY for a table space, the SYSIBM.SYSCOPY and
SYSIBM.SYSLGRNX rows for related COPY enabled indexes, meeting the age and date
criterion, are automatically deleted. You cannot specify an index or index space for the
MODIFY RECOVERY utility.
Alternatively, in DB2 V9 you can use the keyword RETAIN to indicate that records are to be
retained and older records are to be deleted.
The RETAIN keyword has five possible settings. Note for all these settings DB2 establishes
a date before which entries can be deleted. To satisfy the retention criteria, RETAIN works
internally with a date, not a complete timestamp. As a result, more copies might be kept
than are specified by RETAIN. For example, if the most recent five copies have been taken
on the same day, and RETAIN LAST (2) is specified, then all five copies remain in
SYSCOPY. When the criteria is related to the number of copies, then DB2 only considers
primary full copies (ICTYPE=F and ICBACKUP=blank, in SYSCOPY).
LAST (integer) specifies the number of recent records to retain in SYSIBM.SYSCOPY if
the most recent record in SYSIBM.SYSCOPY refers to a non-GDS.
LOGLIMIT queries the BSDS to determine the oldest archive log timestamp and deletes
records older than this timestamp, if the most recent record in SYSIBM.SYSCOPY
refers to a non-GDS.
cl
GDGLIMIT retrieves the GDG limit if the most recent record in SYSIBM.SYSCOPY
refers to a GDS (Generation data set). As many recent records (referring to the same
GDG) as specified in the GDG limit is retained.
Ex
GDGLIMIT LAST (integer) is a combination of the GDGLIMIT and LAST options. DB2
uses the GDG base limit, if the last primary copy is a GDG, if not it uses the integer
specified. This is useful when both GDGs and non-GDGs are used and when using
LISTDEF.
pr
Use the AGE or DATE options when you want to delete SYSLGRNX rows and there are no
SYSCOPY rows that meet the deletion criteria. The SYSLGRNX rows are deleted based
on the AGE or DATE specified.
The example control statement specifies that MODIFY RECOVERY is to retain as much
recent records in SYSIBM.SYSCOPY as defined in the GDG limit.
6-27
Student Notebook
Unit summary
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Having
Havingcompleted
completedthis
thisunit,
unit, you
youshould
shouldbe
beable
ableto:
to:
Make
MakeDB2
DB2user
userdata
dataavailable
availableagain
againfollowing
following media
media failures
failures
CV8317.0
Notes:
pr
Ex
cl
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
This unit covers the main steps needed to prepare a program with
embedded static SQL to access DB2.
SC19-2968
SC19-2969
SC19-2972
SC18-9840
SC18-9841
SC18-9844
SC18-7413
SC18-7415
SC18-7416
pr
Ex
cl
References
7-1
Student Notebook
Unit objectives
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
After
Aftercompleting
completingthis
thisunit,
unit,you
youshould
shouldbe
beable
ableto:
to:
Describe
Describethe
themain
mainsteps
stepsneeded
neededto
toprepare
prepareaaprogram
programwith
with
embedded
embeddedstatic
staticSQL
SQLto
toaccess
accessDB2
DB2
CV8317.0
pr
Ex
cl
Notes:
7-2
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
cl
Traditionally, source programs that access DB2 contain ordinary SQL statements; such
statements are called "embedded SQL statements".
Ex
If these SQL statements are (almost) complete, they are called "embedded static SQL
statements". If these SQL statements are provided by the user at run time, they are called
"embedded dynamic SQL statements".
pr
Why precompile?
In the source code, these SQL statements are delimited by:
PLI: "EXEC SQL" and ";"
COBOL: "EXEC SQL" and "END-EXEC", and so on.
Copyright IBM Corp. 1993, 2011
7-3
Student Notebook
Therefore, a precompiler must eliminate or modify such statements which the language
compiler cannot process.
DB2 precompiler
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
The DB2 precompiler processes the program source code and comments out all of the
SQL statements and replaces them with calls to the language interface (CALL DSNHLI). It
creates two outputs, the first output being the modified source code that is input to the
program compiler. The second output is the DBRM that is the input into the BIND process.
You process the SQL statements in your source program by using the DB2 precompiler.
The output is a load module, possibly one or more packages, and an application plan.
Creating a load module involves compiling the modified source code that is produced by
the precompiler into an object program, and link-editing the object program to create a load
module. Creating a package or an application plan involves binding one or more DBRMs,
which are created by the DB2 precompiler, using the BIND PACKAGE or BIND PLAN
commands.The BIND PLAN can be done asynchronously with an n:m relationship
implemented only via the package list.
You must include the correct interface module (DSNELI, DSNCLI, DSNRLI, DSNALI)
during the link-edit step with options that support the precompiler.
It determines access path to the data for each SQL statement. For example, whether to
access the data directly or via an index.
It stores the access paths and the executable form of the statements in a package in a
collection in DB2.
Ex
cl
BIND PLAN builds an application plan from the specified collections containing packages.
All local DB2 programs require an application plan to allocate DB2 resources and support
SQL requests made at run time.
pr
The load module must be associated with the corresponding plan. For example, in a TSO
environment, via the DSN subcommand RUN.
When a CALL DSNHLI statement in the load module is executed, the appropriate access
path in the package is retrieved and executed.
7-4
V5.4.0.3
Student Notebook
Uempty
Sub1 Sub2
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DBRM
Mod.Source
Main
Precompile
Sub1 Sub2
Main
Compile
BIND PACKAGE
Object
Main
Sub1 Sub2
DB2
Sub1 Sub2
Col2
Col1
Main
BIND
PACKAGE(Col1)
MEMBER(Main)
BIND
PACKAGE(Col1)
MEMBER(Sub1)
BIND
PACKAGE(Col2)
MEMBER(Sub2)
Sub2
Sub1
BIND PLAN
Link/Edit
Plan1
Load
Main
Sub1 Sub2
Run Time:
RUN
PROGRAM (Main)
PLAN (Plan1)
Main
Sub1 Sub2
CV8317.0
Notes:
Ex
cl
When dealing with a more complex application, for example one consisting of a MAIN
program and two external sub programs SUB1 and SUB2, modularization in DB2 is
achieved with packages.
Each external program is subject to a precompiler run of its own, so we get three DBRMs
and three modified sources.
pr
You bind each DBRM separately into a package which is a DB2 object. Then, you
associate these three packages with an application plan.
The advantage of using packages is that, if you change one source program module, you
have to precompile only that one, and bind the corresponding package.
The access paths are determined at BIND PACKAGE and stored with each package.
Packages are qualified with a collection, hence the statement has two parameters:
7-5
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
With packages the CALL DSNHLI specifies the dbrm-member as one parameter, and
hence DB2 knows which package to search for the corresponding access path.
7-6
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
PROG1
DBRM
PROG1
Precompile
USER1 issues:
BIND PACKAGE(C)
MEMBER(PROG1)
OWNER(TEST)
BIND PLAN PLAN2
PKLIST(C.PROG1)
BIND PACKAGE(C)
MEMBER(PROG1)
BIND PLAN PLAN2
PKLIST(C.PROG1)
Load
PROG1
USER1.V1
BIND PACKAGE(C)
MEMBER(PROG1)
OWNER(PROD)
QUALIFIER(ADMIN)
BIND PLAN PLAN2
PKLIST(C.PROG1)
TEST.V1
ADMIN.V1
DB2
CV8317.0
Notes:
cl
Assume that your test and production environments run on the same DB2 subsystem.
When you want to promote your application from the tables with the TEST qualifier to
tables with PROD qualifier, you must ensure that your programs access the right tables.
Ex
As you do not want to change all your source programs, you can omit the table qualifiers in
your source programs and specify these qualifiers at package bind time.
When promoting from TEST to PROD, you have to bind only your DBRMs again.
pr
When you specify neither the package owner (OWNER option) nor the table qualifier
(QUALIFIER option), then the binder userid (USER1) becomes the qualifier of all
non-qualified tables names in your program.
When you specify the package owner, but do not use the QUALIFIER option, the package
owner is also used as qualifier for all non-qualified table names.
When you specify the table qualifier (QUALIFIER option), this parameter becomes the
qualifier for all unqualified table names in your program.
7-7
Student Notebook
Unit summary
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Having
Havingcompleted
completedthis
thisunit,
unit, you
youshould
shouldbe
beable
ableto:
to:
Describe
Describethe
themain
mainsteps
stepsneeded
neededto
toprepare
prepareaaprogram
programwith
with
embedded
embeddedstatic
staticSQL
SQLto
toaccess
accessDB2
DB2
CV8317.0
pr
Ex
cl
Notes:
7-8
V5.4
Student Notebook
Uempty
Unit 8. Security
What this unit is about
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Machine exercises
SC19-2968
SC18-2983
SC18-9840
SC18-9854
SC18-7413
SC18-7426
pr
Ex
cl
References
Unit 8. Security
8-1
Student Notebook
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
.
CV8317.0
pr
Ex
cl
Notes:
8-2
V5.4
Student Notebook
Uempty
DB2 authorization
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CICS
IMS
BATCH
DB2
AUTHORIZATION
TSO
Distributed
DB2
SQL
COMMANDS
UTILITIES
DB2
CATALOG
CV8317.0
Notes:
RACF security can be used to authenticate address spaces, such as IMS, CICS, TSO, and
DB2 address spaces, when they are initialized.
cl
RACF security can be used to control which address spaces can connect to DB2.
Ex
When a user signs on to, say, CICS or IMS, then they provide an ID and password which
have to be checked.
RACF security can control which user can use a transaction in CICS or IMS.
pr
The ENABLE / DISABLE BIND options define which environments are valid or invalid for
particular plans or packages. For example, you could BIND a package for execution only in
MPP regions of a specific IMS system.
The Id issuing an SQL statement, a DB2 command, or executing a DB2 utility can be
checked from the information stored in DB2 catalog tables to verify the ID is authorized.
RACF can protect DB2 data sets, including the times when these data sets are not
allocated to DB2.
Unit 8. Security
8-3
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Userid
composite list
USER
PRIMARY
AUTHID
AUTHORIZATION
EXIT
SECONDARY
CURRENT
SQLID
ROUTINE
AUTHID
LIST
(0 - 1012)
CV8317.0
Notes:
Authorization IDs
cl
Every process that connects to or signs on to DB2 is represented by a set of one or more
DB2 identifiers called authorization IDs.
Ex
Every process is assigned one mandatory ID called the primary authorization ID.
pr
8-4
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
By separating personal IDs from functional IDs means granted privileges will be more
stable and the need for granting and revoking them will be reduced.
DB2 learns what a primary authorization IDs secondary list is going to be from an
authorization exit. The exit learns it from RACF, though exit program could build it (not
recommended).
User exits provide secondary authorization ID values:
pr
Ex
cl
- Except that SYSADM can set CURRENT SQLID to any value (with
SEPARATE_SECURITY set to NO -this DSNZPARM is introduced later in this unit)
Unit 8. Security
8-5
Student Notebook
DB2
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Primary Authid
LOGON/
SIGNON
ID
LOGON/
SIGNON
ID
Exit
PAYROLL
RACF
Control
Block
LOGON/SIGNON
ACCNTG
LOGON
ID
MANUF
PAYROLL
Secondary Authids
RACF
Data Set
CV8317.0
Notes:
How authorization IDs are established depends on the environment of the requesting
process.
Ex
cl
Requests from, for example, TSO go through CONNECTION PROCESSING, which drives
a CONNECTION EXIT ROUTINE. This routine establishes the PRIMARY AUTHID, the
initial value of the CURRENT SQLID, and can provide up to 1012 SECONDARY AUTHIDs.
Similarly, requests from CICS or IMS transactions go through SIGNON PROCESSING,
which drives a SIGNON EXIT ROUTINE.
pr
The visual shows how the exit routine fetches information from RACF to assign the
authorization IDs. By default, it obtains the RACF GROUPS you are connected to via the
RACF CONNECT command, and assigns these RACF GROUP names as SECONDARY
AUTHIDs.
The sample exit routine provided by DB2 can be tailored to suit your particular
requirements.
This mechanism works with other security products as well as RACF.
8-6
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CURRENT SQLID
CV8317.0
Notes:
cl
Cannot be changed.
Ex
This is set to either your primary authid or one of your secondary authids. (Except for
SYSADM / SYSCTRL - see later)
For dynamic SQL, your CURRENT SQLID special register has 3 purposes:
pr
Unit 8. Security
8-7
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
There is no standard way within DB2 for you to retrieve a list of all your SECONDARY
AUTHIDs.
Setting CURRENT SQLID
You can change the value with the SET CURRENT SQLID SQL statement.
The default CURRENT SQLID is the PRIMARY AUTHORIZATION ID. It is set initially by
the exit routine invoked at connection to DB2.
The CURRENT SQLID can be set to your PRIMARY AUTHID or to any SECONDARY
AUTHID provided by the exit routine.
If an invalid ID is specified for SET CURRENT SQLID, a -553 return code results and the
SQLSTATE is set to 42503.
pr
Ex
cl
8-8
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Explicit
Collection (CREATEIN)
Database (CREATETAB, CREATETS, LOAD, DROP, STARTDB, )
Package (BIND, COPY, )
Plan (BIND, EXECUTE)
Routine (EXECUTE on function, EXECUTE on procedure)
Schema (CREATEIN, ALTERIN, DROPIN)
System (ARCHIVE, BINDADD, BSDS, CREATEALIAS,
CREATEDBA, )
Table and view (SELECT, INSERT, UPDATE, DELETE, INDEX, )
Usage (USAGE ON SEQUENCE, USAGE ON JAR, )
Use (USE OF BUFFERPOOL, USE OF TABLESPACE,
USE OF STOGROUP, )
CV8317.0
Notes:
pr
Ex
cl
In DB2 for z/OS we talk about privileges and Authorities. Refer to the security section of the
DB2 10 for z/OS Administration Guide for a complete description of these privileges and
administrative authorities. An administrative authority is simply a collection of individual
privileges.
Unit 8. Security
8-9
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Installation authorities
Install SYSADM
Install SYSOPR
SECADM
Grantable authorities
SYSADM
SYSCTRL
SYSOPR
DBADM
DBCTRL
DBMAINT
PACKADM
System DBADM
ACCESSCTRL
DATAACCESS
SQLADM
CV8317.0
Notes:
pr
Ex
cl
This falls into two categories. One category is installation authorities controlled by
DSNZPARM settings. Another category is Grantable authorities controlled by use of
GRANT and REVOKE statements.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SYSADM
SYSCTRL
SECADM
Install SYSOPR
DBADM
DBCTRL
System DBADM
ACCESSCTRL
SYSOPR
DBMAINT
SQLADM
DATAACCESS
PACKADM
CV8317.0
Notes:
pr
Ex
cl
Unit 8. Security
8-11
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Suggestion:
CV8317.0
Notes:
cl
The key point here is that you want to avoid GRANTing SYSADM. Get that authority from a
secondary ID. Otherwise, if a GRANTed SYSADM user leaves your company, you are apt
to find them owning many DB2 objects. There is no way to modify ownership except by
means of DROP/CREATE which can be extraordinarily painful.
Ex
SYSADM System administrator, which includes SYSCTRL, plus access to all data.
SYSADM can:
pr
Use EXECUTE and BIND on any plan or package, COPY on any package
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Install SYSADM One or two IDs are assigned this authority when DB2 is installed. They
have all the privileges of SYSADM, plus:
Authority is not recorded in the DB2 catalog. The catalog need not be available to check
installation SYSADM authority. (The authority outside the catalog is crucial: If the
catalog table space SYSDBAUT is stopped, for example, DB2 cannot check the
authority to start it again. Only an installation SYSADM can start it.)
No ID can revoke this authority; it can be removed only by changing the module that
contains the subsystem initialization parameters (typically DSNZPARM). Those IDs can
also:
- Access DB2 when the subsystem is started with ACCESS(MAINT)
pr
Ex
cl
- Start databases DSNDB01 and DSNDB06 when those are stopped or in restricted
status
Unit 8. Security
8-13
Student Notebook
Separation of duties
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DSNZPARM SEPARATE_SECURITY
NO
YES
SYSADM
SECADM
SYSADM
SYSCTRL
SYSCTRL
System DBADM
SECADM
ACCESSCTRL
ACCESSCTRL
DATAACCESS
System DBADM
DATAACCESS
CV8317.0
Notes:
cl
Ex
pr
Users with SYSCTRL authority implicitly have ACCESSCTRL authority and can continue to
perform security-related tasks with no authority to access user data. Additionally,
SYSCTRL can manage roles and can set the BIND OWNER to any value, provided the
specified owner qualifies for the data access privilege that is required by the SQL DML
statements contained in the package.
In the above visual on the left hand side, SECADM and ACCESSCTRL are not separated
from SYSADM, including those of the DBADM and DATAACCESS authorities.
8-14 DB2 10 for z/OS DB Admin Workshop Part 1
V5.4
Student Notebook
After the SEPARATE_SECURITY system parameter is set to YES, the SYSADM authority
cannot manage security objects, or grant and revoke privileges. The SYSCTRL authority
does not have the ability to manage roles either. From this point, the SECADM
authorization grants and revokes privileges and manages these objects instead. SYSADM
or SYSCTRL cannot perform grants to revoke privileges granted by others. Existing
privileges granted by SYSADM and SYSCTRL are left intact. A user with ACCESSCTRL or
SECADM authority is required to revoke any of these privileges.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Uempty
pr
Ex
cl
In the above visual on the right hand side, SECADM and ACCESSCTRL separated from
SYSADM, illustrates the separation of security administrative duties from the SYSADM
authorities when DB2 is configured with DSNZPARM SEPARATE_SECURITY set to YES.
In this scenario, the security administration authority SECADM is separated from
SYSADM. The SYSADM authority loses the power to execute the privileges held by the
SECADM and ACCESSCTRL authorities.
Unit 8. Security
8-15
Student Notebook
Columnname
Remarks
SYSADMAUTH
SYSCTRLAUTH
SYSOPERAUTH
BINDADDAUTH
BINDAGENTAUTH
SYSDBAUTH
Database privileges
DBADMAUTH
DBCTRLAUTH
DBMAINTAUTH
SYSRESAUTH
Resource privileges
OBTYPE
SYSSEQUENCEAUTH
Sequence privileges
SYSTABAUTH
Table privileges
USEAUTH
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SYSUSERAUTH
System privileges
DELETEAUTH
INSERTAUTH
SELECTAUTH
UPDATEAUTH
SYSCOLAUTH
COLNAME
SYSPLANAUTH
Plan privileges
BINDAUTH
EXECUTEAUTH
SYSPACKAUTH
Package privileges
BINDAUTH
EXECUTEAUTH
COPYAUTH
SYSSCHEMAAUTH
Schema privileges
CREATEINAUTH
ALTERINAUTH
DROPINAUTH
SYSROUTINEAUTH
Routine privileges
ROUTINETYPE
EXECUTEAUTH
CV8317.0
Notes:
cl
We recommend that you become thoroughly familiar with these catalog tables. Refer to the
catalog appendix in the SQL Reference. Not all of the columns are listed in the visual
above. This is just an abbreviation of the authorization information kept in the DB2 catalog.
Ex
It is important to minimize the use of the WITH GRANT OPTION on GRANT statements.
Evidence for its use is easily found in the catalog by looking for "G" in the appropriate
columns.
pr
All DB2 security information is maintained in the catalog, with the major exception of the
Install SYSADM, SECADM, and Install SYSOPR IDs (which reside in DSNZPARM).
V5.4
Student Notebook
Uempty
Which ID is checked?
Primary authid:
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
pr
Ex
cl
Sometimes we use the primary ID to verify authorization, sometimes one (or more) IDs in
the composite set, and sometimes the current SQLID. The ID we use depends on the
operation we were doing.
Unit 8. Security
8-17
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Owning an object
CV8317.0
Notes:
pr
Ex
cl
In DB2, you cannot do anything without authorization, and that authorization has to come
from somewhere. Initially, it all comes from Install SYSADMs and Install SYSOPRs. A
SYSADM might GRANT authorization to a very few secondary IDs that will be used to
spread other authorization. Other secondary IDs will be production secondaries that
actually do the work.
V5.4
Student Notebook
Uempty
Granting privileges
Primary Authorization ID
GRANT SELECT
ON TABLE TABLE1
TO TSOUDxx
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
TSOUDxx
PAYROLL
GRANT BIND
ON PACKAGE COLL1.PKG1
TO PAYROLL
ACCNTG
GRANT SELECT
ON TABLE ACCNTG1
TO ACCNTG
MANUF
CV8317.0
Notes:
cl
A DB2 GRANT statement gives a specific privilege to an authorization ID. A process which
is connected to DB2 and has several authorization IDs will be able to use the privileges
granted to each one of those.
Depending on the type of activity, DB2 will look at either:
Ex
pr
Unit 8. Security
8-19
Student Notebook
Privileges example
DBADM1 Issues:
DB11
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
GRANT CREATETAB
ON DATABASE DB11 TO TSOUDxx
GRANT USE OF TABLESPACE
DB11.TS0001
TO TSOUDxx
TS0001
TSOUDxx Issues:
T_ORDER
Database Privileges
Resource Privileges
SYSIBM.SYSDBAUTH
GRANTOR
GRANTEE
NAME
CREATETABAUTH
DBADM1
TSOUDxx
DB11
...
SYSIBM.SYSRESAUTH
GRANTOR
GRANTEE
NAME
OBTYPE
QUALIFIER
DBADM1
TSOUDxx
TS0001
DB11
...
CV8317.0
Notes:
pr
Ex
cl
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SYSADM, SECADM,
SYSCTRL, or
ACCESSCTRL revokes
Grantor REVOKEs
CV8317.0
Notes:
cl
With secondary IDs, individuals have no GRANTs done to their primary authorization ID,
which is exactly what we recommend. The user can be disconnected from the group when
that particular authorization is no longer required.
If you GRANT something, then you can also take it away.
Ex
pr
Be careful of this one: If you omit RETAIN on BIND PLAN ... REPLACE then all GRANTs to
the plan go away. That is the default.
Unit 8. Security
8-21
Student Notebook
Revoking privileges
REVOKE privileges ON resource FROM authid (BY authid)
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SYSADM / SYSCTRL /
SECADM / ACCESSCTRL
only
GRANT SELECT TO B
WITH GRANT OPTION
GRANT SELECT TO C
WITH GRANT OPTION
SELECT
REVOKE SELECT
FROM B
SELECT
SELECT
CV8317.0
Notes:
When revoking privileges, beware of the cascade effect, especially if the ID involved has
granted a lot of privileges with the WITH GRANT OPTION.
cl
pr
Ex
V5.4
Student Notebook
Uempty
CREATE privileges
DBADM
DBCTRL
DBMAINT
and other privileges
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CREATEDBA
CREATEDBC
CREATESG
CREATETS
CREATETAB
BINDADD
Versus
CV8317.0
Notes:
cl
Some authorities apply to objects that already exist, others to objects that do not exist. If
the authority applies to an existing object, and the object is DROPped, then the authority is
lost.
Ex
CREATEDBA Grants the privilege to issue the CREATE DATABASE statement and
acquire DBADM authority over those databases.
CREATEDBC Grants the privilege to issue the CREATE DATABASE statement and
acquire DBCTRL authority over those databases.
CREATESG Grants the privilege to create new storage groups.
pr
Unit 8. Security
8-23
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Alter / drop:
Table or any of its indexes
CV8317.0
Notes:
cl
You create DB2 objects, except for plans and packages, by issuing SQL CREATE
statements in which you name the object. When you create the object, you establish its
ownership and the owner implicitly holds certain privileges over it.
pr
Ex
The visual lists, by way of example, the privileges held implicitly by the owner of a table.
V5.4
Student Notebook
Uempty
Establishing ownership
CREATE TABLE
DYNAMIC SQL
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Becomes OWNER
Must have privileges needed to create table
Recorded in OWNER column in SYSTABLES
CREATOR column in SYSTABLES has the SCHEMA of the table
SCHEMA is same as CURRENT SQLID, unless SET SCHEMA is issued
specifying a different value
Primary Authid:
Primary Authid:
CV8317.0
Notes:
UNQUALIFIED
Ex
cl
If you issue a CREATE statement dynamically, and if the name of the table, view, index,
alias, or synonym you are creating is unqualified, then the owner of the created object is
your CURRENT SQLID. This CURRENT SQLID must have the privileges needed to create
the object. CURRENT SCHEMA content is used to qualify unqualified object references (it
has the schema name).
QUALIFIED
pr
If you issue a CREATE statement dynamically, and if the name of the table, view, index, or
alias you are creating is qualified, then the owner of the created object is the qualifier you
specify.
If you have no administrative authority, this qualifier must be your primary authid or one of
your secondary authids.
If you have no administrative authority, this qualifier must have the CREATETAB privilege
implicitly or explicitly for the database specified by the IN clause.
Copyright IBM Corp. 1993, 2011
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Unit 8. Security
8-25
Student Notebook
If your CURRENT SQLID has at least DBCTRL authority, you can use any qualifier for a
table or an index.
To specify any qualifier when creating a view or a MQT, you need SYSADM or DBADM,
depending on the setting of DSNZPARM DBACRVW.
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
To specify any qualifier when creating an alias, you need SYSCTRL or DBCTRL,
depending on the setting of DSNZPARM DBACRVW.
V5.4
Student Notebook
Uempty
Ownership examples
1. LOGON(USER01)...(CENSUS)
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Auth IDs
established by
EXIT ROUTINE
2.
3.
4.
5.
6.
7.
PRIMARY ID SECONDARY ID
SET CURRENT SQLID=CENSUS;--has CREATEDBA
SET CURRENT SCHEMA=RAVI
CREATE DATABASE DB01;
CREATE TABLESPACE TBSP01 IN DB01;
CREATE TABLE TAB01 IN DB01.TBSP01;
CREATE TABLE USER03.TABLEX IN DB01.TBSP01;
--Note different qualifier
RESULTS IN CATALOG TABLE ENTRIES:
SYSDATABASE
SYSTABLESPACE
SYSTABLES
CREATOR
CREATEDBY
SYSDBAUTH
SYSTABAUTH
GRANTOR
GRANTEE
4,5
CENSUS
CENSUS
6
7
CENSUS
CENSUS
USER03
USER03
OWNER
CENSUS USER01
N/A
RAVI
USER01
CENSUS
USER03 USER01
USER03
CV8317.0
Notes:
cl
Ex
pr
In catalog table SYSTABLES, column CREATOR contains the schema of the table, column
CREATEDBY contains the primary authorization ID of the user who created the table, and
column OWNER contains the authorization ID of the owner of the table.
Unit 8. Security
8-27
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
GRANT / REVOKE
Current SQLID is checked for required privileges
CREATE
Current SQLID is checked for required privileges
Other SQL
Composite Privileges checked for required privileges
Primary Authorization ID
GRANT SELECT
ON TABLE
A.TABLE1
TO TSOUDxx
TSOUDxx
SELECT * FROM
A.TABLE1,A.TABLE2
SELECT * FROM
A.TABLE1,PAYROLL.TABLE3
GRANT ALL
ON TABLE
A.TABLE2
TO PAYROLL
PAYROLL
ACCNTG
MANUF
Secondary
Authorization IDs
CV8317.0
Notes:
cl
Ex
pr
V5.4
Student Notebook
Uempty
Composite privileges
An SQL statement can name more than one object. For example, a SELECT can join two
or more tables. An INSERT can use a subquery. These operations require privileges on all
the tables.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
With DYNAMICRULES (RUN), you can issue such a statement dynamically, even if one of
your authids alone does not have all the required privileges.
pr
Ex
cl
The statement is validated if your primary authid and all your secondary authids have all
the necessary privileges among them.
Unit 8. Security
8-29
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
cl
We have already seen some of the considerations for controlling dynamic DDL, now we
examine DML (SELECT, INSERT, UPDATE, DELETE). As always, SYSADMs can do
anything that can be done. Ownership has privileges. And then again, specific table
privileges are GRANTable.
Ex
Ordinarily, current SQLID is used to check authorization and to act as the qualifier on table
names for DDL and as GRANTEE on GRANT and REVOKE.
pr
DYNAMICRULES (BIND) is a BIND option that causes DB2 to check the authorization of
the PLAN OWNER, rather than the user for dynamic SQL. That is, dynamic SQL
authorization checking behaves like that for static SQL. With this option, dynamic DDL and
GRANT/REVOKE are not allowed. Nor is SET CURRENT SQLID.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
RACF can prevent execution of a load module, which is similar to not GRANTing
EXECUTE authority to a plan.
pr
Ex
cl
Many shops GRANT access to a plan to PUBLIC and then handle the real security
checking from a transaction manager. But do not leave the back door open and allow such
a transaction to run (illicitly) in, perhaps, batch: BIND with the ENABLE or DISABLE
keyword.
Unit 8. Security
8-31
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
PLAN
PKLIST (...)
CREATE THREAD/SIGNON
MEMBER(dbrm)
Plan Owner
has
EXECUTE on
local/remote
Packages
DBRMLIB
PGMC ...
CV8317.0
Notes:
cl
Causing a BIND to succeed is not a trivial thing. Many authorizations are required. The
distinction between BIND and BINDADD is important. BINDADD lets you do anything BIND
permits, but it also lets you add new plans and packages.
Ex
Note that there is an install parameter set on panel DSNTIPP called BIND NEW PACKAGE
that controls whether BIND or BINDADD authority is required to BIND a new version of an
existing package. The default, BINDADD, only allows users with BINDADD to create a new
package. BIND allows users with only BIND authority to create new versions of packages.
This also affects users with PACKADM authority.
pr
With BINDAGENT and PACKADM you can separate owners of plans (say production
control) from owners of packages (perhaps programmers).
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
cl
CREATE VIEW ... WITH CHECK OPTION and table check constraints are another type of
security, or more properly, ways of guaranteeing data integrity by forcing data to stay within
certain bounds.
pr
Ex
Then again, by only allowing people to see some of the data because of a limiting view, you
prevent them from seeing sensitive fields.
Unit 8. Security
8-33
Student Notebook
Unit summary
Having completed this unit, you should be able to:
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
pr
Ex
cl
Notes:
V5.4.0.3
Student Notebook
Uempty
Unit 9. Serialization
What this unit is about
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Describe concurrency
Explain the aspects of transaction locks
Tell when locks are acquired and released
Describe S and X locks
Explain isolation levels
Explain LOCKSIZE
Machine exercises
References
SC18-9851
SC18-7413
pr
Ex
cl
SC19-2978
Unit 9. Serialization
9-1
Student Notebook
Unit objectives
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
After
Aftercompleting
completingthis
thisunit,
unit,you
youshould
shouldbe
beable
ableto:
to:
Describe
Describeconcurrency
concurrency
Explain
Explain the
theaspects
aspectsof
oftransaction
transactionlocks
locks
Tell
Tellwhen
whenlocks
locksare
areacquired
acquiredand
andreleased
released
Describe
DescribeSSand
andXXlocks
locks
Explain
Explainisolation
isolationlevels
levels
Explain
ExplainLOCKSIZE
LOCKSIZE
CV8317.0
pr
Ex
cl
Notes:
9-2
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Prog-1
Acct
Read Acct
Prog-2
Change Acct
110
120
Acct
Read Acct
Acct =
Acct + 300
Acct =
Acct + 20
Change Acct
200
Rollback
-100
Change Acct
DB2 provides
Write Integrity
Prog-2
-100
Read Acct
100
Acct =
Acct + 10
Prog-1
Read Acct
IF Acct > 0
Then ....
DB2 provides
Read Integrity
CV8317.0
Notes:
cl
Without these serialization mechanisms, data integrity would be lost as illustrated by these
examples.
Ex
Lost updates
pr
Two applications, Prog-1 and Prog-2, might both read the same row from a table and both
calculate new values for one of its columns based on the data these applications read. If
Prog-1 updates the row with its new value and Prog-2 then also updates the row, the
update performed by Prog-1 is lost.
Access to uncommitted data
Prog-1 might update a value in a table and Prog-2 might read that value before it is
committed. Then, if the value of Prog-1 is rolled back the calculations performed by Prog-2
are based on uncommitted and, presumably, invalid data.
Unit 9. Serialization
9-3
Student Notebook
There are other possible problems (not illustrated) that could occur without serialization
mechanisms.
Non-repeatable reads
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Application A reads a row from a table and then goes on to process other SQL. In the
meantime, application B either modifies or deletes the same row and commits its change. If
application A then attempts to read the original row again it will see the modified row or
discover that the original row has been deleted.
9-4
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Transaction locking
Applies to all SQL statements, wherever they are
(batch, distributed applications, etc.)
Restricted states
CV8317.0
Notes:
cl
DB2 has a number of different serialization mechanisms which can be used to:
Prevent loss of data integrity and
Ex
They are:
pr
1. Transaction locking
2. Restricted states
For example, STOP, START UT, CHKP, UTUT, UTRO, UTRW...
Restricted states can be set on DB2 objects by a DB2 utility or a DB2 command.
Copyright IBM Corp. 1993, 2011
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Unit 9. Serialization
9-5
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
The structure of a page may be inconsistent while changes (inserts, updates) are being
made. During this time a page latch is used to prevent access to the page.
Concurrency
pr
Ex
cl
This access is controlled by DB2s serialization mechanisms which prevent the undesirable
effects discussed above.
9-6
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Catalog / directory
Note:
CV8317.0
Notes:
cl
User data
Ex
The data you target with SQL gets locked (although there are exceptions). You have the
most control over these locks.
pr
Additionally, the data in related tables may get locked. For example, if you delete a row
from a parent table DB2 might delete rows from a dependent table as well. In this case,
DB2 locks data in the dependent table as well as in the parent table.
Note
Unit 9. Serialization
9-7
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
9-8
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Mode
Does the lock allow others to read and/or update the locked object?
Duration
CV8317.0
Notes:
cl
Ex
Knowing the characteristics helps you understand why a process suspends or times out or
why two processes deadlock.
pr
Unit 9. Serialization
9-9
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Table lock
Row lock
Page lock
Row lock
Page lock
CV8317.0
Notes:
Ex
cl
The same piece of data can be controlled by locks of different sizes. A table space lock (the
largest size) controls the most data, all the data in an entire table space. A page or row lock
controls only the data in a single page or row.
CREATE TABLESPACE and ALTER TABLESPACE have the LOCKSIZE option. The
choices are ANY (default), ROW, PAGE, TABLE, or TABLESPACE.
ANY
pr
ANY permits DB2 to make the final choice and DB2 favours page locking as a good
compromise between high concurrency and high CPU consumption.
DB2 has the option to use table or table-space level or even row locking as an alternative
depending on the SQL.
V5.4.0.3
Student Notebook
Uempty
LOCK TABLE
The explicit SQL statement LOCK TABLE is another option.
The SHARE and EXCLUSIVE options make the table read-only or give exclusive
ownership of the table to a single process respectively.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Lock hierarchy
In a segmented table space, for example, if DB2 decides to lock a data row, then according
to the locking hierarchy, DB2 locks the table space and the table before locking the row.
pr
Ex
cl
In a simple table space, if DB2 decides to lock a data row, then according to the locking
hierarchy, DB2 locks the table space before locking the row.
Unit 9. Serialization
9-11
Student Notebook
Contention problems
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CPU consumption
Low
Page
Row
Locksize
CV8317.0
Notes:
cl
Your choice of lock size represents a trade-off between concurrency and CPU overhead.
pr
Ex
For example, row locking may increase the use of available CPU resources. No I/O
operations are done, but each lock request requires two-way communication between DB2
and the internal resource lock manager (IRLM).
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Table
Page(s)
or
Row(s)
OR
CV8317.0
Notes:
Strategy 1
cl
In this case, all rows in the table will be affected to the same degree.
Ex
Strategy 2
pr
Only for segmented table spaces does table locking occur. For simple table spaces, locks
are taken at the table space level and possibly pages / rows. For partitioned and universal
table spaces, locks are taken at the partition level and possibly pages / rows.
Unit 9. Serialization
9-13
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Share (S)
Lock owner and others can read but not update
Update (U)
Reduces deadlocks
Exclusive (X)
CV8317.0
Notes:
cl
Share (S)
Ex
The lock owner and any concurrent process can read, but not change, the locked page or
row.
Concurrent processes may acquire S- or U-lock on a page or row, or might read the data
without acquiring a page or row lock.
pr
Update (U)
The lock owner can read, but not change, the locked page or row. However, the owner can
promote the U-lock to an X-lock and then change the page or row. Promotion to X-lock may
cause a suspension if concurrent processes hold S-locks.
U-locks reduce the chance of deadlocks when the lock owner is reading data to determine
whether to change it.
V5.4.0.3
Student Notebook
Uempty
Exclusive (X)
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Only the lock owner can read or change the locked page or row. A concurrent process can
access the data only if the process runs with uncommitted read isolation.
Unit 9. Serialization
9-15
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Held
Lock
Requested Lock
S
Yes
Yes
No
Yes
No
No
No
No
No
CV8317.0
Notes:
The visual shows whether page locks of any two modes, or row locks of any two modes are
compatible.
Ex
cl
For example, USERA is holding an S lock on a page and USERB requests an X lock on the
same page. The lock mode of USERA does not permit the request from USERB because
the two lock modes S and X are not compatible.
Note
pr
Within a table space you cannot use both page and row locks at the same time.
V5.4.0.3
Student Notebook
Uempty
Example: SELECT . . .
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Example:
LOCK TABLE . . .
IN EXCLUSIVE MODE
IS
Page
or
row
Page
or
row
Page
or
row
Page
or
row
Page
or
row
CV8317.0
Notes:
At the table, table space or partition level, you can expect to see IS, IX, SIX, S, U or X
locks. We shall discuss each one of these in a moment.
cl
For now, we shall observe that these locks fall into two categories:
Ex
pr
In the visual, User 1 has an IS intent lock at the table level and additionally S locks at the
page or row level.
The intent lock is like a warning light that shows other concurrent transactions what type
of access the underlying pages or rows are undergoing.
The Intent lock also regulates the type of locks taken on the underlying pages or rows.
S, U or X (Gross lock mode)
In the visual, User 2 attempts to take an X gross lock at the table level.
Copyright IBM Corp. 1993, 2011
Course materials may not be reproduced in whole or in part
without the prior written permission of IBM.
Unit 9. Serialization
9-17
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Update (U)
Exclusive (X)
Figure 9-12. More about table, table space and partition locks
CV8317.0
Notes:
cl
The lock owner can read data in the table, table space or partition but not change it.
Ex
pr
The lock owner and concurrent processes can read and change data in the table, table
space or partition.
The lock owner acquires X locks on pages or rows changed. The lock owner is also
eligible to read pages or rows and acquires S locks on pages or rows read. The lock
owner is also eligible to read pages or rows with update intent and acquires U locks on
these pages or rows. When a page or row is changed then the U lock is promoted to an
X lock.
V5.4.0.3
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Concurrent processes can read data in the table, table space or partition but not change
it.
Only when the lock owner changes data, is the X lock acquired on a page or row.
The lock owner and concurrent processes can read, but not change, data in the table or
table space.
There is no need for S-lock on a page or row.
Update (U)
The lock owner can read, but not change, the locked data. However, the owner can
promote the U-lock to an X-lock and then change the data.
Processes concurrent with the U-lock can acquire S-locks and read the data, but no
concurrent process can acquire a U-lock.
The lock owner does not need page or row locks.
U-locks reduce the chance of deadlocks when the lock owner is reading data to
determine whether to change it.
Exclusive (X)
The lock owner may read and change data in the table space or table.
pr
Ex
cl
Only the concurrent processes using uncommitted read isolation (see later) may access
the table space or table.
Unit 9. Serialization
9-19
Student Notebook
Held
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Requested Lock
Lock
IS
IX
SIX
IS
Yes
Yes
Yes
Yes
Yes
No
IX
Yes
Yes
No
No
No
No
Yes
No
Yes
Yes
No
No
Yes
No
Yes
No
No
No
SIX
Yes
No
No
No
No
No
No
No
No
No
No
No
CV8317.0
Notes:
pr
Ex
cl
The visual shows whether or not table / table space / partition locks of any two modes are
compatible.
V5.4.0.3
Student Notebook
Uempty
Isolation levels
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
I don't want
EMPLOYEE to change
during processing the
inner and outer query
User 2
I just need an
approximate
average salary as
quickly as possible
User 3
Use ISOLATION CS
SELECT
FROM EMPLOYEE
Use ISOLATION UR
Use ISOLATION RR
CV8317.0
Notes:
You have control over the extent to which your SELECT is isolated from the possible
effects of other concurrent processes.
cl
You do this by specifying an ISOLATION LEVEL for your SELECT. This is coded on the
individual SELECT statement or specified as a BIND option for the entire package.
pr
Ex
You have a choice of 4 options: Cursor Stability (CS), Repeatable Read (RR), Read
Stability (RS) or Uncommitted Read (UR).
Unit 9. Serialization
9-21
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
OPEN C1
As user 1 issues each FETCH, If the row or page he wants is already X locked by someone
else then DB2 will wait to take an S lock. This S lock is subsequently released as soon as
subsequent FETCHes cause DB2 to move off this page or row onto another one.
As user 1 issues each FETCH, If the row or page wanted is not already X locked by
someone else then DB2 will avoid taking an S lock. This avoids some locking overhead
and reduces the impact user 1 might have had on follow-on updaters. But if user 1 attempts
to re-read data in the same unit of work he may find he does not get the same result set.
If the above DECLARE had specified FOR UPDATE OF then DB2 would have taken U
locks instead of S locks. U locks are also released as soon as subsequent FETCHes cause
DB2 to move off the page or row onto another one.
cl
Note
Ex
pr
A row or page S lock is held for all accessed rows, qualifying or not, at least until the next
commit point.
For the non-correlated subquery shown, the inner query executes first and the result feeds
into the outer query.
User 2 does not want EMPLOYEE to change between processing the inner query and the
outer query. This can be achieved with repeatable read.
V5.4.0.3
Student Notebook
Uempty
Note
DB2 never practises lock avoidance with repeatable read.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
RS can offer more concurrency than RR because, although other applications cannot
change any of the qualifying rows returned to the original application, they can insert new
rows or update rows that did not satisfy the original applications search criteria.
For example, suppose your application reads a set of rows. Then another application
inserts some additional rows that satisfy your applications query and commits. Your
application then repeats the original query within the same unit of work and the additional
rows are returned as part of the result set.
Uncommitted Read (UR)
Less overhead
Considerations:
pr
Ex
cl
Unit 9. Serialization
9-23
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CV8317.0
Notes:
DB2 locking allows inter-process concurrency and avoids data integrity problems
cl
Ex
The locking mechanism worsens application performance where the user only wants to
see available and committed data
pr
SKIP LOCKED DATA is designed to allow a transaction to silently skip rows locked by other
transactions. When processing a SELECT, UPDATE or DELETE request, if a row is
incompatibly locked by another transaction, then the attempt to lock that row is given up
and the row is skipped. No error is returned. Instead, the transaction is resumed so that it
can retrieve the next row. With this capability, applications performance can improve
significantly. Unlike isolation UR, application can only see unlocked committed rows.
V5.4.0.3
Student Notebook
Uempty
To use the SKIP LOCKED DATA option, specify the clause in a SELECT, SELECT INTO,
PREPARE, searched UPDATE, or searched DELETE statement. You can also use the
SKIP LOCKED DATA option with the UNLOAD utility.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
You can use the SKIP LOCKED DATA option with cursor stability (CS) isolation and read
stability (RS) isolation. However, you cannot use SKIP LOCKED DATA with uncommitted
read (UR) or repeatable read (RR) isolation levels. For UR and RR, DB2 ignores the SKIP
LOCKED DATA clause.
The SKIP LOCKED DATA option works only with row locks and page locks. If you specify
SKIP LOCKED DATA for a transaction with row level locking, incompatibly locked rows are
skipped. If you specify SKIP LOCKED DATA for a transaction with page level locking, all
rows on pages with incompatible locks are skipped.
In general, the SKIP LOCKED DATA clause does not apply to table, partition, or table
space locks. When LOCKSIZE TABLE or LOCKSIZE TABLESPACE is specified for a table
space or when a lock is escalated to a gross table, partition, or table space lock, DB2
ignores the SKIP LOCKED DATA clause.
Lock mode compatibility for transactions that use the SKIP LOCKED DATA option is the
same as lock mode compatibility for other page- and row-level locks. However, when
incompatible locks are held, a transaction that uses the SKIP LOCKED DATA option does
not wait for the locks to be released and skips the locked data instead.
Example: Suppose that transaction A holds an s lock on a row that transaction B also
wants to access. The query in transaction B specifies SKIP LOCKED DATA. The outcome
of transaction B depends on the mode of lock that it acquires. If transaction B requires a
compatible s or u lock, transaction B can access the row on which transaction A holds an s
lock. If transaction B requires an incompatible x lock, transaction B cannot access the
locked row. Because the SKIP LOCKED DATA option is specified, that row is skipped and
the results of transaction B are returned without that row.
With some queries that use the SKIP LOCKED DATA clause, you can receive unexpected
or inconsistent results as shown in the example in the above visual.
Ex
cl
Example: Suppose that a table T exists in a table space with row-level locking, or in a table
space with page-level locking and the rows of the table are distributed across several
pages. Assume transaction A issues the update statement.
pr
If transaction B issues the SELECT statement before transaction A commits., and if you do
not have an index defined on C2, DB2 returns a count of 2 because DB2 skips the two rows
that are locked by the uncommitted UPDATE statement.
Unit 9. Serialization
9-25
Student Notebook
Scenario 2
Repeatable Read and Read Stability
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Isolation level:
Scenario 1
Cursor Stability
Program start
Plan allocation
FETCH Page 1
FETCH Page 2
Pg Pg Pg Pg
1 2 3 4
Pg Pg Pg Pg
1 2 3 4
(S)
(S)
UPDATE Page 3
FETCH Page 4
(S)
COMMIT
CV8317.0
Notes:
The lock duration shown in the visual above is for page locks. If the LOCKSIZE=ROW, then
the duration will be for row locks.
Ex
cl
Scenario 1 shows that with CURSOR STABILITY (CS), a page S lock, such as page 1, 2,
and 4, is held only long enough to allow the cursor to move to another page. The same is
true for U locks. Any X locks caused by an update, such as page 3, are always held until
commit. In this situation, any page read can disappear and a new page can appear upon
re-read. The user will not get uncommitted data and the users changed data will not be
seen by others (non UR) until the change is committed. This satisfies the CS requirement.
pr
Scenario 2 shows that with REPEATABLE READ (RR) as well as READ STABILITY (RS)
index access, a page S lock is held for all accessed rows, qualifying or not, such as page 1,
2, and 4, until the next commit point. Any X locks, such as page 3, caused by an update,
are always held until commit. The index access protects against any new rows to appear
and the duration of the S lock (until commit time) protects against a read row from
disappearing on re-read. If no index is used to access the data, a gross lock would have to
be required to satisfy the semantics of RR.
9-26 DB2 10 for z/OS DB Admin Workshop Part 1
V5.4.0.3
Student Notebook
Uempty
Use/
Commit
TS TS TS
A B C
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Acquire:
Release:
Program start
Plan allocation
SELECT
TSA
UPDATE
TSB
COMMIT
SELECT
TSC
COMMIT
Plan deallocation
CV8317.0
Notes:
cl
ACQUIRE(USE) option
Ex
Since ACQUIRE(ALLOCATE) is no longer supported, this option causes the DB2 locks on
table, table space or partition to be acquired only when that DB2 object is first accessed.
This option does not affect page or row locks.
pr
Generally, these options release the lock when the application ends or at the next commit.
The partition locks on any locked partitions of a partitioned table space are all held for the
same duration. That is to say, if one package has been bound with RELEASE(COMMIT)
and another package has been bound with RELEASE(DEALLOCATE) then the locks on
the locked partitions will be released according to RELEASE(DEALLOCATE).
Unit 9. Serialization
9-27
Student Notebook
Lock avoidance
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
page header
TABLE
PAGE X
CV8317.0
Notes:
Lock avoidance is used for read-only cursors (no FOR UPDATE) defined with isolation
level CS if the plan or package containing the cursor is bound with CURRENTDATA(NO).
cl
Lock avoidance applies also to singleton selects (with isolation level CS and
CURRENTDATA(NO)).
Ex
Lock avoidance does not mean 100% lock avoidance. If the timestamp of the last update
from the page header is not older than the start time of all units of recovery updating the
table space containing the page, lock avoidance fails and DB2 asks for an S lock as it
would have done without lock avoidance.
pr
Lock avoidance failures are normally less than 1% on average under normal conditions.
V5.4.0.3
Student Notebook
Uempty
Lock escalation
Segmented table space
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Table lock
Row lock
Page lock
Row lock
Page lock
CV8317.0
Notes:
pr
Ex
cl
DB2 provides the application developer with the option to dynamically switch from low level
locking to high level locking based on the number of low level locks held by the application
process. This option is called lock escalation and is controlled by the parameter LOCKMAX
of CREATE TABLESPACE and ALTER TABLESPACE. The purpose of lock escalation is to
reduce the impact of low level locks, both in terms of CPU and storage, at the possible
expense of concurrency. When the number of low level locks exceeds a specified
threshold, DB2 attempts to promote the high level intent locks, either IS or IX, to S or X,
respectively. If this succeeds, then the low level locks are all released and no more low
level locks are requested. The lock promotion request may itself fail if other processes hold
incompatible locks, either low or high level, for longer than the timeout period. If the higher
level lock cannot be acquired before the timeout value has been reached, then the process
times out, all updates are rolled back, and all locks are released.
The flow chart in the above visual describes the lock escalation scenario and how DB2
uses NUMLKTS and LOCKMAX values. By the same token, you can develop an
application specific mechanism to control lock escalation by using the NUMLKTS
parameter at the system level and LOCKMAX parameter at the table space level.
Unit 9. Serialization
9-29
Student Notebook
LOCKMAX
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
The LOCKMAX parameter specifies the maximum number of locks that can be taken on
this table space by a single process before lock escalation occurs. LOCKMAX SYSTEM
means that the value specified in the NUMLKTS DSNZPARM should dictate the maximum
number of locks before escalation occurs for this table space. LOCKMAX 0 specifies that
lock escalation should not occur for this table space.
When lock escalation occurs, DB2 requests a single lock at the next higher level in the lock
hierarchy of the table space. The lock requested is equal to the most restrictive lock held by
the process on the table space. If this lock is acquired, then all of the lower level locks that
were held by the process are released. If the higher level lock cannot be acquired before
the timeout value has been reached, then the process times out, all updates are rolled back
and all locks are released.
There is no locking hierarchy at the table space level for a partitioned table space. The
highest level of locking is at the partition level. Therefore, when the LOCKMAX threshold is
reached, all partitions on which your process currently holds locks are escalated to a gross
lock. Any unlocked partitions that are subsequently accessed will also be locked with a
gross partition level lock.
NUMLKTS
the DSNZPARM NUMLKTS corresponds to the field LOCKS PER TABLE (SPACE) on the
DB2 installation panel INSTALL DB2 - IRLM PANEL 2 (DSNTIPJ). The acceptable values
are 0 to 104857600 and the default is 2000. The value that you specify for this field must be
less than the value specified for LOCKS PER USER (NUMLKUS), except when
LOCKS PER USER is set to 0. This value becomes the default value (SYSTEM) for the
LOCKMAX clause of the SQL statements CREATE TABLESPACE and
ALTER TABLESPACE. A value of 0 indicates that there is no limit to the number of page
and row locks that a program can acquire.
cl
NUMLKUS
Ex
The DSNZPARM NUMLKUS corresponds to field LOCKS PER USER on the DB2
installation panel INSTALL DB2 - IRLM PANEL 2 (DSNTIPJ). It specifies the maximum
number of page or row locks that a single application process can hold concurrently on all
table spaces. Once that limit is reached, the program that accumulated these locks will
terminate with SQLCODE -904.
pr
The maximum includes locks on data pages or rows that the process acquires when it
accesses table spaces. The limit applies to all table spaces defined with the LOCKSIZE
PAGE, LOCKSIZE ROW, or LOCKSIZE ANY options. The acceptable values are 0 to
104857600 with a default value of 10,000.
A value of 0 means that there is no limit to the number of page and row locks a program
can acquire.
9-30 DB2 10 for z/OS DB Admin Workshop Part 1
V5.4.0.3
Student Notebook
Uempty
IS
S
IS
IS,IS
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
IS,IS
S
USER 1
USER 2
USER 1
USER 2 USER 1
USER 2
IS
After Escalation
S,IS
S,IS
USER 1
USER 2
USER 1
USER 2 USER 1
USER 2
CV8317.0
Notes:
pr
Ex
cl
See the above visual for an illustration of the consequences of lock escalation for a
partitioned table space. Partitions 1, 4, and 7 contain low level locks acquired by USER 1.
Lock escalation is then triggered by USER 1. Note that escalation is tracked at the
table space level, and not the partition level, so if the total number of locks on the entire
table space exceeds the LOCKMAX value, lock escalation is triggered for all partitions that
have been accessed by User 1. After the lock escalation, USER 1 now holds gross S locks
on partitions 1, 4, and 7. The low level locks previously held by USER 1 are released.
Unit 9. Serialization
9-31
Student Notebook
COMMIT interval
Table
User 2
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
User 1
UPDATE
EXCLUSIVE
LOCK
Maximum 5 seconds
STOP
SELECT
( No UR and
No SKIP .LOCKED DATA )
WAIT
.
WAIT
.
WAIT
.
COMMIT
CV8317.0
Notes:
cl
An application process is suspended when it requests a lock that is already held by another
application process and cannot be shared. The suspended process temporarily stops
running.
pr
Ex
The suspended process resumes running when all processes that hold the conflicting lock
release it, ideally within 5 seconds.
V5.4.0.3
Student Notebook
Uempty
Timeouts
Table
User 2
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
User 1
UPDATE
EXCLUSIVE
LOCK
STOP
SELECT
( No UR and
No SKIP .LOCKED DATA )
11
12
WAIT
.
WAIT
.
WAIT
. -911 / -913
10
TIMEOUT
CV8317.0
Notes:
cl
An application process is suspended when it requests a lock that is already held by another
application process and cannot be shared. The suspended process temporarily stops
running.
The suspended process resumes running when:
Ex
The requesting process times out after a preset interval and the process resumes to
deal with an error condition. DB2 issues two messages to the console and returns
SQLCODE -911 or -913 to the process.
pr
Unit 9. Serialization
9-33
Student Notebook
Deadlocks
Table N
User 1
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Page B
3.. SELECT
000300
SUSPENDED
1. UPDATE
OK
2. UPDATE
OK
Table M
User 2
Page A
4.. SELECT
000100
SUSPENDED
CV8317.0
Notes:
1. User 1 updates table M, and acquires an exclusive lock for page A, which contains
record 000100.
cl
2. User 2 updates table N, and acquires an exclusive lock for page B, which contains
record 000030.
Ex
3. User 1 then attempts to SELECT from page B of table N, while still holding the lock on
page A of table M. The job is suspended, because user 2 is holding an exclusive lock
on page B.
pr
4. User 2 then attempts to SELECT from page A of table, M while still holding the lock on
page B of table N. The job is suspended, because user 1 is holding an exclusive lock on
page A.
The situation is a deadlock. After a preset time interval, DB2 can roll back the current unit
of work for one of the processes or request a process to terminate. That frees the locks and
allows the remaining processes to continue.
V5.4.0.3
Student Notebook
Uempty
Reducing deadlocks
One way to reduce the likelihood of deadlocks is to access data in a consistent order.
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
In the above example, both users should access the tables in the same order if possible,
that is, table M followed by table N. The first user to access table M might delay the second
user but the two users cannot deadlock.
Unit 9. Serialization
9-35
Student Notebook
SQL statements
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
2. DDL
LOCKSIZE
LOCKMAX
MAXROWS
3. BIND
Lock avoidance
(ISOLATION - CS / RS / RR)
4. Programming
5. DSNZPARM thresholds
CV8317.0
Notes:
pr
Ex
cl
V5.4.0.3
Student Notebook
Uempty
Unit summary
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Having
Havingcompleted
completedthis
thisunit,
unit, you
youshould
shouldbe
beable
ableto:
to:
Describe
Describeconcurrency
concurrency
Explain
Explain the
theaspects
aspectsof
oftransaction
transactionlocks
locks
Tell
Tellwhen
whenlocks
locksare
areacquired
acquiredand
andreleased
released
Describe
DescribeSSand
andXXlocks
locks
Explain
Explainisolation
isolationlevels
levels
Explain
ExplainLOCKSIZE
LOCKSIZE
CV8317.0
pr
Ex
cl
Notes:
Unit 9. Serialization
9-37
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Student Notebook
V5.4.0.3
Student Notebook
Bibliography
DB2 10 for z/OS system reference library
Administration Guide
SC19-2969
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SC19-2968
GC19-2971
Codes
SC19-2972
Command Reference
SC19-2973
GC19-2974
SC19-2975
Internationalization Guide
SC19-2976
GC19-2977
SC19-2978
Managing Performance
GC19-2979
Messages
SC19-2980
SC19-2981
pureXML Guide
SC19-2982
SC19-2983
SQL Reference
SC19-2984
GC19-2985
Whats New?
GC19-2986
GC19-2987
GC19-2666
IRLM Messages and Codes for IMS and DB2 for z/OS
LY37-3220
GI10-8829
GI10-8830
GI10-8840
GI10-8842
cl
SC19-2970
pr
Ex
bibl
Bibliography
X-1
Student Notebook
SC18-9841
SC18-9842
GC18-9843
Codes
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SC18-9840
Command Reference
SC18-9845
LY37-3218
LY37-3219
GC18-9846
Installation Guide
SC19-1161
Internationalization Guide
SC18-9847
Introduction to DB2
GC18-9848
GC18-9849
Messages
SC18-9850
SC18-9851
GI10-8737
Program Directory
SC18-9852
SC18-9853
SX26-3854
Reference Summary
SC18-9854
SQL Reference
SC18-9855
GC18-9856
Whats New?
SC18- 9857
SC18-9858
XML Guide
pr
Ex
cl
SC18-9844
X-2
V5.4.0.3
Student Notebook
SC18-7415
SC18-7414
GC18-9603
Codes
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SC18-7413
Command Reference
SC18-7417
LY37-3201
GC18-9602
Installation Guide
GC18-7422
Messages
SC18-7423
SC18-7433
SC18-7424
SX26-3853
Reference Summary
SC18-7425
SC18-7426
SQL Reference
SC18-7427
GC18-7428
What's New?
SC18-7431
cl
SC18-7416
pr
Ex
bibl
Bibliography
X-3
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Student Notebook
X-4
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
backpg
pr
cl
Ex
V5.4.0.3
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