You are on page 1of 474

Business

Intelligence

Data
Centre

Cloud

Mobility

Enterprise Computing Solutions

Student Manual

Direccin General de Formacin


CONSEJERA DE EMPLEO,
TURISMO Y CULTURA

Comunidad de Madrid

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

EDUCATION
S

V5.4.0.3

cover

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

Front cover

DB2 10 for z/OS


Database Administration
Workshop Part 1
(Course code CV831)

Student Notebook

pr

Ex

cl

ERC 7.0

IBM certified course material

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

October 2011 edition

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

Copyright International Business Machines Corporation 1993, 2011.


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

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

Unit 1. Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-1


Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2
What is DB2 for z/OS? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3
The users view (dynamic) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-5
Another kind of user (static) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-6
A program's responsibility - a logical unit of work . . . . . . . . . . . . . . . . . . . . . . . . . . 1-8
The systems view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9
Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-11
DB2s responsibilities - SECURITY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-12
Data integrity and concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-13
DB2 system log data set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-15
COPY/RECOVER utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-16
DB2s responsibilities - PERFORMANCE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-17
DB2s responsibilities - METADATA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-18
DB2 for z/OS data sharing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-20
Roles and interfaces in DB2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-21
Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-23

cl

Unit 2. Setting up a DB2 database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-1


Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
2.1. Overview of setting up a DB2 database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3
Objects used by DB2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-4
Defining DB2 objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-6
Owner concept - example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7
Owners privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-8
DB2 naming rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-9
2.2. Storage groups, databases, and table spaces . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-11
DB2 Storage Group . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-12
DB2 database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-15
DB2 buffer pools (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-17
DB2 buffer pools (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-18
EBCDIC, ASCII, and Unicode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-21
Administration at the database level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-22
DB2 table spaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-23
Types of table spaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-24
Simple table space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-26
Segmented table space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-27
Single and multiple-table table spaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-29

pr

Ex

TOC

Copyright IBM Corp. 1993, 2011


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

Contents

iii

Student Notebook

pr

Ex

cl

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

Segmented table space - sample definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-30


Segmented table space - maximum size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-31
Classic partitioned table space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-32
Create a classic partitioned table space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-33
Advantages of partitioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-34
Classic partitioned table space - Maximum size for NUMPARTS < 255 . . . . . . . .2-35
Classic partitioned table space - Maximum number of partitions . . . . . . . . . . . . . .2-36
Classic partitioned table space - Maximum size for NUMPARTS > 254 . . . . . . . .2-37
Partition-by-range universal table space (PBR) . . . . . . . . . . . . . . . . . . . . . . . . . . .2-38
Partition-by-growth universal table space (PBG) . . . . . . . . . . . . . . . . . . . . . . . . . .2-40
Partition-by-growth UTS - pros and cons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-41
Create a partition-by-growth universal table space . . . . . . . . . . . . . . . . . . . . . . . .2-42
Default type of a table space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-44
SYSTABLESPACE catalog information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-45
Space allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-47
Space map page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-50
Data page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-52
Free space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-53
2.3. Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-55
CREATE TABLE - column attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-56
Row format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-59
Basic row format and reordered row format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-61
DEFAULT attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-64
Table check constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-66
SYSTABLES and SYSCOLUMNS catalog information . . . . . . . . . . . . . . . . . . . . .2-69
Check constraints catalog information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-70
Enable table-controlled partitioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-71
ALTER TABLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-72
Examples for adding / removing check constraints . . . . . . . . . . . . . . . . . . . . . . . .2-73
Adding able check constraints - considerations . . . . . . . . . . . . . . . . . . . . . . . . . . .2-74
The -DISPLAY DATABASE command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-75
2.4. Data compression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-77
Data compression - overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-78
Data compression - data management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-79
Data compression - building the dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-80
Compress on INSERT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-84
How Compress on INSERT works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-85
Location of compression dictionary pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-87
Miscellaneous on Compress on Insert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-89
2.5. Views and Materialized Query Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-91
DB2 views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-92
CREATE VIEW - examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-94
Views on views on tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-96
DELETE from a view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-98
SELECT, UPDATE, INSERT, DELETE with VIEWS . . . . . . . . . . . . . . . . . . . . . . .2-99
The disappearing row . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-101
WITH CHECK OPTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-102
Effect of cascaded CHECK OPTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-104

iv

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

Views - catalog information (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-106


Views - catalog information (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-107
Materialized query tables (MQTs) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-108
Populating and refreshing MQTs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-111
Special registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-112
Synonyms and Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-113
SYNONYM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-114
SYNONYM - catalog information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-115
ALIAS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-116
ALIAS - catalog information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-118
Object name translation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-119
Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-123
What is an index? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-124
Why have indexes? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-125
Creating unique and non-unique indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-127
Index on expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-129
Clustering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-131
Index and index space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-133
Index and index space attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-134
DB2 index tree structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-139
Catalog tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-140
DB2 addressing scheme . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-143
DB2 addressing scheme - overflow pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-144
Leaf page - index entry format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-146
Non-leaf pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-147
Index page management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-148
Index-controlled partitioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-150
Table-controlled partitioning - partitioning index(partitioned and non-partitioned) 2-153
Table-controlled partitioning - non-partitioning non-partitioned secondary index (NPSI)
2-155
Table-controlled partitioning - non-partitioning partitioned secondary index (DPSI) . . .
2-156
Table-controlled partitioning - partition one way and cluster another way . . . . . 2-158
Table-controlled partitioning - catalog entries . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-160
Index compression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-163
Index compression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-164
DDL, catalog and utility changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-167
Comparison: Index and data compression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-169
Additional information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-171
Automatic creation of objects (1 of 3) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-172
Automatic creation of objects (2 of 3) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-174
Automatic creation of objects (3 of 3) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-176
DB2 naming conventions for data sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-177
Object dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-178
Checkpoint quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-180
Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-181

cl

2.7.

Ex

2.8.

pr

2.9.

Copyright IBM Corp. 1993, 2011


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

Contents

Student Notebook

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

Unit 3. Implementing unique and referential requirements . . . . . . . . . . . . . . . . . . . .3-1


Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-3
3.1. Referential integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-5
Primary key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-6
Unique key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-8
Foreign key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-9
Foreign key - column considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-11
Referential integrity - DB2 implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-12
Creating a referential constraint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-14
Creating the primary key and primary index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-15
Creating unique keys and foreign keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-16
Creating informational referential constraint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-18
Adding primary key constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-20
Adding unique and foreign key constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-21
Dropping constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-22
Restrictions on dropping indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-23
Catalog information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-24
Referential structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-26
Additional considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-28
TABLESPACESET concept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-29
REPORT TABLESPACESET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-30
CHECK PENDING restrictive state . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-31
3.2. Generating unique values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-33
Identity columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-34
Identity column considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-37
DB2 sequence object (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-39
DB2 sequence object (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-41
Identity columns versus sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-45
Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3-47

pr

Ex

cl

Unit 4. Getting data into and out of DB2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-1


Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-2
4.1. Introduction to DB2 utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-3
Generating utility JCL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-4
DB2 utility panel (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-5
DB2 utility panel (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-6
DSNUxxx.CNTL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-7
DSNUPROC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-9
SYSIBM.SYSUTILX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-10
DB2 utility commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-11
DB2 utility flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-12
4.2. The LOAD utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-13
Loading data into one table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-14
Data type conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-16
Data conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-17
Loading NULL values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-18
Loading multiple tables in a table space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-20
LOAD RESUME, REPLACE, and REUSE at TS level . . . . . . . . . . . . . . . . . . . . . .4-22
vi

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

LOAD RESUME, REPLACE, and REUSE at partition level . . . . . . . . . . . . . . . . .


Other LOAD options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
LOAD phases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
DISCARD file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Unique index violations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
LOAD considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
DB2 family cross-loader function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
LOAD - delimited input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.3. Ensuring referential and check constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Check pending (CHKP) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the CHECK DATA utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
CHECK DATA - exception tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
CHECK DATA - phases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
CHECK DATA - example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.4. The UNLOAD utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
DB2 UNLOAD utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Table space input specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Example of the unloaded records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Output field specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
UNLOAD - delimited output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Error handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Checkpoint quiz (1 of 3) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Checkpoint quiz (2 of 3) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Checkpoint quiz (3 of 3) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

cl

Unit 5. Keeping your DB2 data in good shape. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-1


Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2
5.1. RUNSTATS and real-time statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-3
RUNSTATS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-4
RUNSTATS versus real-time statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-5
Enabling real-time statistics (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-6
Enabling real-time statistics (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-7
When are space-related statistics still needed? . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-8
Important RUNSTATS options (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-10
Important RUNSTATS options (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-11
Inline statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-12
Recommended RUNSTATS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-13
5.2. REORG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-15
Reasons for reorganizing table spaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-16
Reasons for reorganizing indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-18
REORG TABLESPACE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-19
REORG INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-21
REORG examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-22
REORG TABLESPACE ... DISCARD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-23
Determining the need for REORG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-24
Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-25

pr

Ex

TOC

Copyright IBM Corp. 1993, 2011


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

Contents

vii

Student Notebook

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

Unit 6. Application data recovery basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-1


Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-2
Application data recovery - overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-3
DB2 logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-4
SYSIBM.SYSLGRNX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-6
Backing up table spaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-7
Backing up partitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-9
Copy data sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-11
COPY - examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-13
COPYTOCOPY utility - establishing copies of image copies . . . . . . . . . . . . . . . . .6-14
Table space recovery concept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-16
Recovery - examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-18
Preparing for a recovery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-19
Combining image copies for table spaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-20
REBUILD INDEX utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-21
REBUILD INDEX examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-22
REBUILD INDEX considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-23
Recovery / rebuild pending . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-24
MODIFY RECOVERY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-26
Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-28
Unit 7. Program preparation / bind. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-1
Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-2
Program preparation flow for DB2 precompiler . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-3
Program preparation flow for multiple modules . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-5
Let BIND determine object to be accessed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-7
Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-8

pr

Ex

cl

Unit 8. Security. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-1


Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-2
DB2 authorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-3
Who is the user? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-4
Establishing authorization IDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-6
USER and CURRENT SQLID special registers . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-7
What can be authorized? (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-9
What can be authorized? (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-10
What do authorities contain? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-11
Install SYSADM and install SYSOPR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-12
Separation of duties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-14
Where is authorization information kept? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-16
Which ID is checked? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-17
How do you acquire authority? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-18
Granting privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-19
Privileges example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-20
How do you control it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-21
Revoking privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-22
CREATE privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-23
Example - ownership of a table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8-24

viii

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Unit 9. Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-1


Unit objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-2
Why does DB2 have serialization mechanisms? . . . . . . . . . . . . . . . . . . . . . . . . . . 9-3
DB2 serialization mechanisms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-5
What gets locked? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-7
Main characteristics of transaction locking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-9
Size of transaction locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-10
Concurrency versus CPU overhead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-12
Two locking strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-13
Page or row locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-14
Lock mode compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-16
Table, table space and partition locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-17
More about table, table space and partition locks . . . . . . . . . . . . . . . . . . . . . . . . . 9-18
Lock mode compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-20
Isolation levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-21
SKIP LOCKED DATA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-24
Duration of page and row locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-26
Duration of table, table space and partition locks . . . . . . . . . . . . . . . . . . . . . . . . . 9-27
Lock avoidance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-28
Lock escalation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-29
Lock escalation example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-31
COMMIT interval . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-32
Timeouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-33
Deadlocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-34
Some factors which affect locking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-36
Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-37
Bibliography. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . X-1

pr

Ex

TOC

Copyright IBM Corp. 1993, 2011


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

Contents

ix

pr

Ex

cl

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

Student Notebook

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Copyright IBM Corp. 1993, 2011


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

Trademarks

xi

pr

Ex

cl

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

Student Notebook

xii

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

This course provides you with instruction on how to physically


implement a logical database design in DB2. The course includes
instruction on DB2 data management, DB2 catalog tables, the bind
process, database utilities such as LOAD and REORG, and security
considerations.
Note: This course material is at the DB2 10 for z/OS level.

Audience

Future DB2 for z/OS database administrators who need to acquire the
basic skills required to administer a DB2 database.

Prerequisites

Before taking this course, you should be able to:


Design a relational table
Describe the SQL data manipulation statements to access and
change the contents of DB2 tables

You should also take DB2 Family Fundamentals (CE03) and SQL
Workshop (CE12), or have equivalent experience.

cl

Objectives

After completing this course, you should be able to:

Implement a DB2 database design


Use database utilities to load and reorganize data
Define and implement a DB2 database recovery strategy
Control access to database using DB2 authorization facilities

pr

Ex

pref

Contents

This course covers the following major topics:


Setting up a DB2 database
Referential integrity

Copyright IBM Corp. 1993, 2011


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

Course description

xiii

Student Notebook

Getting data into and out of DB2


Keeping your DB2 data in good shape
Application data recovery basics
Program preparation and Bind

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

Security
Serialization

The course includes extensive machine exercises.

Curriculum relationship

DB2 9 for z/OS Database Administration Workshop


Part 2

CV851

DB2 10 for z/OS System Administration

CV870

DB2 9 for z/OS Utilities for Database Administrators

CV891

DB2 9 for z/OS Application Data Recovery

CV940

DB2 9 for z/OS Index Modeling and Design

CV950

DB2 9 for z/OS System Performance Analysis and


Tuning

CV960

DB2 9 for z/OS Application Performance for Tuning

pr

Ex

cl

CV841

xiv

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

DB2 9 for z/OS Database Administration Workshop Part 2 (CV841 - 3 days)


This course is a continuation of CV83 and is designed to teach you how to perform
additional database administration tasks. The main topics covered by the course are as
follows:
Program Preparation and use of Packages
Online Schema Changes
Clone Tables
Partition Management
UDTs and UDFs
Stored Procedures
Triggers
Large Objects

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

The course includes extensive machine exercises.

DB2 10 for z/OS System Administration (CV851 - 5 days)

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

Implement DB2 and Resource Access Control Facility (RACF) security


Describe DB2 program flow for all environments

Display normal and problem threads and database status

Ex

pref

See how the SQL Processor Using File Input (SPUFI) AUTOCOMMIT option defers the
COMMIT/ROLLBACK decision

pr

Identify and cancel particular threads

Describe available DB2 utilities to manage system and user page sets

Copyright IBM Corp. 1993, 2011


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

Course description

xv

Student Notebook

DB2 9 for z/OS Utilities for Database Administrators (CV870 - 3 days)


This course is designed to teach you advanced topics about DB2 for z/OS utilities. It is
assumed that you attended course CV831 so that you already have basic skills about the
main utilities. Recovery-oriented topics are not part of this course, so such utility functions
are not presented. The main topics covered by this course are as follows:

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

CV831 utilities review


More on utilities and their functions (RUNSTATS, DSN1COPY, REPAIR, DIAGNOSE)
LOAD and REBUILD INDEX performance and availability
Online CHECK DATA

REORG performance and availability

UNLOAD performance and availability

Generic utility jobs (LISTDEF and TEMPLATE)

The course includes extensive machine exercises.

DB2 9 for z/OS Application Data Recovery (CV891 - 3 days)

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

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

DB2 9 for z/OS Index Modeling and Design (CV940 - 3 days)


This course is designed to teach the students how to design the right indexes for improving
query performance, both for single table access and for multiple table access.

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

Different index formats and distinct reasons for using indexes


Indexing enhancements in DB2 V9
Indexes for join

Index versioning

Less-known index features

Operational utilities for indexes


Index cost

Indexes for Star Schema

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

Operating system and hardware

Service time, queuing time and transaction analysis


The DB2 attachments

Ex

pref

In-depth application of the DB2 system


Disk subsystems and I/Os

pr

Capacity analysis and massive Batch

Copyright IBM Corp. 1993, 2011


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

Course description

xvii

Student Notebook

DB2 9 for z/OS Application Performance and Tuning (CV960 - 5 days)


This course is designed to teach you how to prevent application performance problems and
how to improve the performance of existing applications. The main topics covered by the
course are as follows:
Index design
Table implementation considerations related to performance (denormalization,...)
SQL writing
Optimizer pitfalls
Avoiding long lock waits
Detecting and analyzing performance problems with accounting traces

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

pr

Ex

cl

The course includes paper and machine exercises.

xviii DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Copyright IBM Corporation 2011

Figure 1. Course objectives

CV8317.0

pr

Ex

cl

Notes:

Copyright IBM Corp. 1993, 2011


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

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

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Unit 2 (topic 5) - Views, synonyms, and aliases


Exercise 3 and review
Unit 2 (topic 6) - Materialized Query Tables
Unit 2 (topic 7) - Indexes
Exercise 4 and review
Unit 2 (topic 8) - Index compression
Unit 2 (topic 9) - Additional information

Day 3

cl

Unit 3 (topic 1) - Referential integrity


Exercise 5 and review
Unit 3 (topic 2) - Identity columns and sequences
Unit 4 - Getting data into and out of DB2
Exercise 6 and review

Day 4

Unit 5 - Keeping your DB2 data in good shape


Exercise 7 and review
Unit 6 - Application data recovery basics
Exercise 8 and review

pr

Ex

pref

Copyright IBM Corp. 1993, 2011


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

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
.

Unit 7 - Program preparation


Unit 8 - Security
Exercise 9 and review
Unit 9 - Serialization
Exercise 10 and review

xxii DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

This unit is an introduction to the different components of a DB2


environment. Starting with a high-level overview of DB2 for z/OS, the
user, the responsibilities of the application programs and of the DB2
system itself will be discussed, as well as the major roles people will
play in a DB2 shop.

What you should be able to do

After completing this unit, you should be able to:

Describe the main components of a DB2 environment and the roles


they play:
- Users
- Data

- SQL statements (static, dynamic)


- System

How you will check your progress


Accountability:

Classroom discussion

Student Notebook

pr

Ex

cl

References

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-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

Copyright IBM Corporation 2011

Figure 1-1. Unit objectives

CV8317.0

pr

Ex

cl

Notes:

1-2

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

What is DB2 for z/OS?


Relational Data Base Management System (RDBMS)
Data base consists of tables

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

Simple concepts
Dynamic relationships

Structured query language (SQL)


High level

Data Manipulation Language (DML)


Data Definition Language (DDL)
Data Control Language (DCL)

User specifies WHAT not HOW

RDBMS facilities

Integrity
Dynamic definition of DB2 objects
Active catalog
Recovery / restart
Continuous operations
Security
Interactive tools

Copyright IBM Corporation 2011

Figure 1-2. What is DB2 for z/OS?

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

DB2 for z/OS is a Relational Database Management System (RDBMS).


Data is presented as a set of tables, that is, rows and columns.

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

Data Manipulation Language (DML) comprises SELECT, INSERT, UPDATE, DELETE,


and MERGE statements.
Data Definition Language (DDL) comprises CREATE, ALTER, and DROP statements
Data Control Language (DCL) comprises GRANT and REVOKE statements

There are many other statements in the language.

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-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

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

The users view (dynamic)


Chris

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

Result Data: Geyer

SELECT LASTNAME
FROM EMP
WHERE EMPNO = 000050

QMF / SPUFI

A00
B01
C01
D11
...
D01

Spiffy Computer Ser. Div.


Planning
Information Center
Manufacturing System
Development Center

000010
000020
000030
000060

...

Copyright IBM Corporation 2011

Figure 1-3. The users view (dynamic)

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.

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-5

Student Notebook

Another kind of user (static)


Chris

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

Spiffy Computer Ser. Div.

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

Copyright IBM Corporation 2011

Figure 1-4. Another kind of user (static)

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

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

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-7

Student Notebook

A program's responsibility - a logical unit of work


Logical Unit of Work
UPDATE B

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

A program transfers $1000 from account A to account B


Copyright IBM Corporation 2011

Figure 1-5. A program's responsibility - a logical unit of work

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

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

The systems view


Distributed
Applications

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

DB2 receives MANY requests


from MANY user programs to
access potentially the SAME data
Besides USER PROGRAMS, DB2
also receives requests from
UTILITIES

Copyright IBM Corporation 2011

Figure 1-6. The systems view

CV8317.0

Notes:

There are many different ways that DB2 data can be accessed concurrently such as:
User-written applications

cl

SQL statements that users key in dynamically

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

Distributed Data Facility (DDF) or DIST (optional)


This address space is required in a distributed environment.
One or more WLM-established Stored Procedure address spaces (optional), also used
to execute User Defined Functions (UDF).
WLM = Workload Manager (component of z/OS)

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

A distributed environment enables applications to access data on different DB2 systems


located at, say, different sites in a network.

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.

1-10 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Work at the data set level

Run in BATCH

Compete with user programs for resources

Specify your requirements using CONTROL


STATEMENTS mostly
Copyright IBM Corporation 2011

Figure 1-7. Utilities

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

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-11

Student Notebook

DB2s responsibilities - SECURITY


DB2 must verify whether
a request is LEGITIMATE

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

Copyright IBM Corporation 2011

Figure 1-8. DB2s responsibilities - SECURITY

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

1-12 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Data integrity and concurrency


EMPLOYEE Table
Prog2

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

Prog1

UPDATE

EXCLUSIVE
LOCK

Row

STOP

... WAIT ...

..
.

RELEASE
LOCK

SELECT

OK

SHARED
LOCK

COMMIT

DB2 must maintain data integrity and also


provide maximum concurrency
Copyright IBM Corporation 2011

Figure 1-9. Data integrity and concurrency

CV8317.0

Notes:
Step 1

Program 1 is updating an employee row.

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

Programs that are denied locks will have to wait.


Step 2
Program 2 attempts to read the employee record that is currently being updated by
program 1.
Program 2 is made to wait until the LUW finishes.

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

Step 3

Program 1 completes the update and issues a COMMIT.

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.

1-14 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

DB2 system log data set


DB2

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

Copyright IBM Corporation 2011

Figure 1-10. DB2 system log data set

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.

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-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

Copyright IBM Corporation 2011

Figure 1-11. COPY/RECOVER utilities

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.

1-16 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

DB2s responsibilities - PERFORMANCE


Program

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

DB2 must translate a users or programs


SQL request into ACCESS STRATEGIES
Copyright IBM Corporation 2011

Figure 1-12. DB2s responsibilities - PERFORMANCE

CV8317.0

Notes:

The program formulates WHAT it wants by coding an SQL request.

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.

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-17

Student Notebook

DB2s responsibilities - METADATA


DB2 uses a centralized CATALOG and DIRECTORY
Database
Administrator

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

Copyright IBM Corporation 2011

Figure 1-13. DB2s responsibilities - METADATA

CV8317.0

Notes:

Another responsibility of the DBMS is to manage its objects, programs, program-to-object


dependencies, security, and so on.

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

Security definitions of privileges / authorizations granted to users. By querying the


catalog with SQL you can find out who can access what
Recovery data has, for example, details of backups taken and this information is used
for recovery

1-18 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

The DB2 catalog is in database DSNDB06.


The Directory

The directory contains further metadata used mostly by DB2.

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

The DB2 directory is placed in database DSNDB01.

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-19

Student Notebook

DB2 for z/OS data sharing


z/OS

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

Incremental processing growth


Configuration flexibility
Dynamic workload balancing

Copyright IBM Corporation 2011

Figure 1-14. DB2 for z/OS data sharing

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.

1-20 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Roles and interfaces in DB2

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

Copyright IBM Corporation 2011

Figure 1-15. Roles and interfaces in DB2

CV8317.0

Notes:

This course is targeted to an audience of DB2 database administrators and system


administrators. Database administrators will:

cl

In development:

Support the application/development population in the more technical domains


Be involved in the database design
Create DB2 objects
Participate in application design reviews
Provide information to the SYSADM regarding DASD requirements, number of
users, types of transactions, logging requirements, data loading, and archiving
requirements
- Design the methods to create test data
- Be responsible for adhering to the system authorization procedures and guidelines
established by the SYSADM

pr

Ex

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

1-22 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Copyright IBM Corporation 2011

Figure 1-16. Unit summary

CV8317.0

pr

Ex

cl

Notes:

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-23

pr

Ex

cl

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

Student Notebook

1-24 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Unit 2. Setting up a DB2 database


What this unit is about

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

This unit describes how to implement a DB2 database.

What you should be able to do

After completing this unit, you should be able to:

Describe the DB2 objects that make up a DB2 database


Select parameters for these DB2 database objects so that they are
implemented with the most appropriate attributes
Create storage groups, databases, table spaces, tables, views,
synonyms, aliases, materialized query tables, and indexes
Alter the attributes of DB2 database objects as requirements
change over time
Describe how data is stored in a DB2 database

How you will check your progress


Accountability:

Machine exercises

GC19-2985

DB2 10 for z/OS Whats New?

SC19-2968

DB2 10 for z/OS Administration Guide

SC19-2983

DB2 10 for z/OS SQL Reference

GC18-9856

DB2 Version 9.1 for z/OS Whats New?

SC18-9840

DB2 Version 9.1 for z/OS Administration Guide

SC18-9854

DB2 Version 9.1 for z/OS SQL Reference

GC18-7428

DB2 UDB for z/OS Version 8 Whats New?

SC18-7413

DB2 UDB for z/OS Version 8 Administration Guide

SC18-7426

DB2 UDB for z/OS Version 8 SQL Reference

pr

Ex

cl

References

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-1

Student Notebook

Unit objectives

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

After
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

Copyright IBM Corporation 2011

Figure 2-1. Unit objectives

CV8317.0

pr

Ex

cl

Notes:

2-2

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

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

2.1. Overview of setting up a DB2 database

pr

Ex

cl

Uempty

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-3

Student Notebook

Objects used by DB2


DB2 Storage Group

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

Synonyms and Aliases

Table

SELECT * FROM T1

Index
Space

Table

Index

V_LONG_TABLE_NAME

Copyright IBM Corporation 2011

Figure 2-2. Objects used by DB2

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

- A collection of table spaces and index spaces grouped together to facilitate


operations and security

Table space

- An area of external storage used to store the records of one or more tables

pr

Index space

- An area of external storage used to store the entries of one index

Index

- An ordered set of key values and pointers to the rows containing those keys

2-4

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Materialized query table

pr

Ex

cl

- A materialized query table is a table that contains pre-computed data from other
tables / views

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-5

Student Notebook

Defining DB2 objects


DB2 Storage Group
Hi-level
qualifier

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

Copyright IBM Corporation 2011

Figure 2-3. Defining DB2 objects

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

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Owner concept example

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

CREATE TABLE EMP


(EMPNO
CHAR(6) NOT NULL,
FIRSTNME
VARCHAR(12),
. . .
IN DBX.TSX

COMPLETE TABLE NAME:


OWNER_name.TABLE_name

BART

Copyright IBM Corporation 2011

BART.EMP

Figure 2-4. Owner concept - example

CV8317.0

Notes:

A DB2 object has an owner.

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

In this course we consider local objects.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Copyright IBM Corporation 2011

Figure 2-5. Owners privileges

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

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

DB2 naming rules

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

STORAGE GROUP and DATABASE no Qualifier


Qualifies

DATABASE

TABLESPACE

Qualifies

SCHEMA

TABLE

Qualifies

COLUMN

VIEW

SYNONYM
ALIAS

INDEX

Qualifies

DATABASE

INDEXSPACE

Copyright IBM Corporation 2011

Figure 2-6. DB2 naming rules

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-9

pr

Ex

cl

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

Student Notebook

2-10 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

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

2.2. Storage groups, databases, and table spaces

pr

Ex

cl

Uempty

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-11

Student Notebook

DB2 Storage Group

DB2
storage
group:
hlq1

VSAM data set:


hlq1 . xxxxxxxxxx

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
-

if SMS not used

Tablespace
Table

Syntax:

CREATE STOGROUP SG1 VOLUMES(*) VCAT hlq1;

SMS

nonSMS

SG1: SMS-managed datasets

-- also possible:
CREATE STOGROUP SG1 VOLUMES(*) VCAT hlq1
DATACLAS(dc1) MGMTCLAS(mc1) STORCLAS(sc1);

...

CREATE STOGROUP SG2


VOLUMES(VOL001,VOL002...) VCAT hlq1;

SG2: DB2-managed datasets

ALTER STOGROUP SG2


ADD VOLUMES (VOL007, VOL008)
REMOVE VOLUMES(VOL001);

VOL001

VOL002

...

Default: SYSDEFLT

Copyright IBM Corporation 2007

Figure 2-7. DB2 Storage Group

CV8317.0

Notes:

A DB2 storage group is a named set of disk volumes, in which DB2:


Allocates storage for table spaces and indexes

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.

2-12 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

CREATE STOGROUP SG2


VOLUMES('*')
VCAT VCATID;

If your installation uses SMS the following applies:

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-13

Student Notebook

2. DB2-managed DB2 data sets


CREATE STOGROUP SG1
VOLUMES(VOL001,VOL002,....)
VCAT VCATID;
VOLUMES

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

Typically, the systems administrator creates storage groups.

2-14 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

CREATE DATABASE USDB1 BUFFERPOOL BP1 INDEXBP BP2


Copyright IBM Corporation 2011

Figure 2-8. DB2 database

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

CREATE DATABASE DB1


STOGROUP SG1
BUFFERPOOL BP1
INDEXBP BP2
CCSID EBCDIC;

pr

The STOGROUP, BUFFERPOOL, and INDEXBP parameters establish default storage


groups and buffer pools for objects created in the database.
CCSID specifies the default encoding scheme for data stored in the database.

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

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

The database name can be a maximum of 8 alphanumeric characters. It must be unique


within the DB2 subsystem. It has no prefix.

DSNDB04

DSNDB04 is used when you issue a CREATE TABLESPACE without IN clause or a


CREATE TABLE ... IN table-space-name.

pr

Ex

cl

It is recommended you always explicitly specify a database name.

2-16 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

DB2 buffer pools (1 of 2)


DB2 DBM1 Address Space

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

CREATE TABLESPACE TS1


IN DB1
USING STOGROUP SG1

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

VSAM Data Set 1

CI_2

CI_4

CI_5
CI_5

CREATE TABLESPACE TS2


IN DB1
USING STOGROUP SG2

CI_6

BUFFERPOOL BP8K2

VSAM Data Set 2

Copyright IBM Corporation 2011

Figure 2-9. DB2 buffer pools (1 of 2)

CV8317.0

Notes:

DB2 buffer pools

Buffer pools are areas of virtual storage that temporarily store pages of:

cl

Table spaces and / or

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-17

Student Notebook

DB2 buffer pools (2 of 2)


Numbers and sizes of DB2 Buffer Pools

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

BP0, BP1, BP2,........, BP49 which contain 4 KB buffers


BP8K0, ..... , BP8K9 which contain 8 KB buffers

BP16K0, ...... , BP16K9 which contain 16 KB buffers

BP32K, BP32K1, .......... , BP32K9 which contain 32 KB buffers


Do not confuse BP32 with BP32K

Catalog uses BP0, BP8K0, BP16K0, and BP32K

Size of a TS data page and index page determined by


buffer pool
VSAM CI sizes of 4 KB, 8 KB, 16 KB, or 32 KB
Only for table spaces

Can improve query performance

Copyright IBM Corporation 2011

Figure 2-10. DB2 buffer pools (2 of 2)

CV8317.0

Notes:

Numbers and sizes of DB2 buffer pools


DB2 allows you to use:

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

Up to 10 buffer pools called BP32K, BP32K1, .......... , BP32K9 which contain 32 KB


buffers.
Note

Do not confuse BP32 which contains 4 KB pages with BP32K which contains 32 KB pages.

2-18 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

You assign a table space or an index to a particular buffer pool with:


CREATE / ALTER TABLESPACE BUFFERPOOL BPn
CREATE / ALTER INDEX BUFFERPOOL BPn

CREATE / ALTER DATABASE BUFFERPOOL BPn

- 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

Data page or index page size

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-19

Student Notebook

VSAM control intervals


A VSAM control interval (CI) is an area on disk in which VSAM stores records. It is the unit
of I/O between the disk subsystem and the DB2 buffer pool.
DB2 can define data sets with VSAM CI sizes of 4 KB, 8 KB, 16 KB or 32 KB.

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.

2-20 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

EBCDIC, ASCII, and Unicode


EBCDIC
Mainframe data sets in most cases

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

ASCII

PCs and workstations store


data using ASCII

UNICODE

All countries, languages, platforms,


technical characters, punctuation marks,
and so forth
Single, unique definition (code point)
For every character in the world

Specify at table space level

Copyright IBM Corporation 2011

Figure 2-11. EBCDIC, ASCII, and Unicode

CV8317.0

Notes:

Unicode is an encoding scheme that supports all:

cl

Countries, languages, platforms, technical characters, punctuation marks,


all accessible by people anywhere

Unicode meets the needs of:

Ex

Multinational corporations
e-commerce

Goal is to provide:

pr

Single, unique definition (code point) for every character in the world

The Unicode standard is published by the Unicode Consortium, http://www.unicode.org

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-21

Student Notebook

Administration at the database level


Database
Table Space

GRANTs at the database level

Table Space

Commands at the database level


DISPLAY DATABASE(...........)
SPACENAM(..........)
STOP DATABASE(...........)
SPACENAM(..............)
START DATABASE(...........)
SPACENAM(..............)

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

Copyright IBM Corporation 2011

Figure 2-12. Administration at the database level

CV8317.0

Notes:

Database administration (DBADM) authority can be granted at the database level.

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.

2-22 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

DB2 table spaces

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

VSAM linear data set

Table space
Table

What is a table space?

DB2 storage structure

Contains the data rows for one or more tables

Resides in a page set of one or more VSAM linear data sets


Page size is 4 (usually), 8, 16, or 32 K

Created in a database using SQL

Copyright IBM Corporation 2007

Figure 2-13. DB2 table spaces

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

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-23

Student Notebook

Types of table spaces

Simple table space

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

Segmented table space

Partitioned table space (classic partitioned table space)


Universal table space (UTS) partition-by-growth (PBG)
Universal table space (UTS) partition-by-range (PBR)
LOB table space

XML table space

See other courses

Copyright IBM Corporation 2007

Figure 2-14. Types of table spaces

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.

Simple table space

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.

2-24 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Segmented table space


This can hold one or more tables. The table space is divided into SEGMENTS of from 4 to
64 pages. Each segment is dedicated to a single table, but a table can occupy many
segments. This internal structure offers processing advantages over a simple table space.

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

Classic partitioned table space


This can hold only one table. The table space is divided into parts (partitions). Each
partition is stored in a separate VSAM linear data set.
Advantages:

1. A classic partitioned table space can hold more data


2. Individual partitions can be processed

3. Partitions can be processed in parallel.

Universal table space partition-by-growth (UTS PBG)

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.

Universal table space partition-by-range (UTS PBR)

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

LOB table space, XML 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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-25

Student Notebook

Simple table space


Simple Table Space

One or more tables


No limit
Rows from multiple tables can be
interleaved

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

Copyright IBM Corporation 2011

Figure 2-15. Simple table space

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.

2-26 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Segmented table space

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

Segmented Table Space One or more tables


No limit
Dept Table
Divided into segments
Each segment
dedicated to a table
A table can occupy
multiple segments
Employee Table
SEGSIZE must be a
multiple of 4 between
4 and 64 (inclusive)

Segment 3

Segment 2

Data Page

Space Map

Segment 1

Header

=DEPT Row

=EMPLOYEE Row

Copyright IBM Corporation 2011

Figure 2-16. Segmented table space

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

A REORG is not necessary to reclaim space occupied by a dropped table. However, a


REORG is necessary to reclaim space from a mass delete.

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

2-28 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Single and multiple-table table spaces


Single

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

Each table can have different attributes


Space allocations
Buffer pool assignments

Can schedule utilities at table level

Pending states (see later) limit availability of only one table

Multiple

Need to run fewer utilities

Easier to keep related tables in step


Especially backup and recovery

Good for small reference tables

Avoids header / space map for each table


Copyright IBM Corporation 2011

Figure 2-17. Single and multiple-table table spaces

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-29

Student Notebook

Segmented table space sample definition

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

CREATE TABLESPACE USTS1


IN USDB1 ;
CREATE TABLESPACE USTS2
IN USDB1
USING STOGROUP USCV831S
PRIQTY 14400
SECQTY 720
ERASE NO
SEGSIZE 64
BUFFERPOOL BP2
FREEPAGE 4
PCTFREE 20
MAXROWS 255
LOCKSIZE ANY
CCSID EBCDIC;
Copyright IBM Corporation 2007

Figure 2-18. Segmented table space - sample definition

CV8317.0

Notes:

The IN clause of the CREATE TABLESPACE statement specifies the database.

cl

The USING STOGROUP clause specifies the storage group that is used for space
allocation of the table space.

Ex

Most parameters you specify on CREATE TABLESPACE can be changed with


ALTER TABLESPACE. But there are exceptions.
Note

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.

2-30 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

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

Segmented table space maximum size

Segmented table space (single data set max. 2 GB):

2 GB

2 GB

2 GB

max. 32 data sets 2


....................................

GB

64 GB

Copyright IBM Corporation 2007

Figure 2-19. Segmented table space - maximum size

CV8317.0

Notes:

The data set of a segmented table space can grow up to 2 GB.

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-31

Student Notebook

Classic partitioned table space


Table-Controlled Partitioning
Partitioned Table Space

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

EMPLOYEE Table
(Partitioned by EMPNO)
Data Partition 1

Max EMPNO 099

Data Page

Space Map

Header

Data Partition 2

4th Data Partition


(Data set)

Max EMPNO 199

Data Partition 3

Data Page

Max EMPNO 299

Space Map

Header

Data Partition 4

2nd Data Partition


(Data set)

Max EMPNO 999

One table only


Divided into parts (partitions)
Each partition is a VSAM data set
CREATE TABLE specifies:
The partitioning key and
The partition boundary values

Data Page

Space Map

Header

1st Data Partition


(Data set)

Copyright IBM Corporation 2011

Figure 2-20. Classic partitioned table space

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.

2-32 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Create a classic partitioned table space

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

CREATE TABLESPACE USTS3


IN USDB1
USING STOGROUP USCV831S
PRIQTY 14400
SECQTY
720
ERASE NO
PCTFREE 20
FREEPAGE 4
SEGSIZE 0
NUMPARTS 4

(PARTITION 1 USING STOGROUP USCV832S


PRIQTY 7200
SECQTY 720
ERASE YES
PCTFREE 15
FREEPAGE 8
COMPRESS YES,
PARTITION 2 USING STOGROUP USCV833S
PRIQTY 7200
SECQTY 720
ERASE YES
PCTFREE 25
FREEPAGE 2
COMPRESS YES)
BUFFERPOOL BP2;

Copyright IBM Corporation 2011

Figure 2-21. Create a classic partitioned table space

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

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

As a rule-of-thumb, at least 10,000,000 rows or 0.5GB data

Table space is in smaller, more manageable pieces

Can run utilities, commands or SQL independently at partition level

Individual partitions can have different space allocations


Parallelism

Partition scan

Copyright IBM Corporation 2011

Figure 2-22. Advantages of partitioning

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.

2-34 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Classic partitioned table space


Maximum size for NUMPARTS < 255
Partitioned TS, without specifying DSSIZE:

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

4 GB almost 1 TB (1016 GB)

If you specify DSSIZE nG

[n=1,2,4,8,16,32, or 64],

then the maxiumum size is almost 16 TB.


(for NUMPARTS < 255)

Copyright IBM Corporation 2007

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-35

Student Notebook

Classic partitioned table space


Maximum number of partitions

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

If you specify NUMPARTS > 254 and do not specify DSSIZE,


DB2 chooses a default DSSIZE (governed by page size) .

Depending on page size and DSSIZE, NUMPARTS can be up to:


DSSIZE

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

Copyright IBM Corporation 2007

Figure 2-24. Classic partitioned table space - Maximum number of partitions

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.

2-36 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Classic partitioned table space


Maximum size for NUMPARTS > 254
Maximal table space size depends on page size and DSSIZE:
DSSIZE
Max # of partitions
1 GB
4096
4 GB
4096
8 GB
2048
16 GB
1024
32 GB
512
64 GB
256
1 GB
4096
8 GB
4096
16 GB
2048
16 GB
4096
32 GB
2048
32 GB
4096
64 GB
2048

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

Copyright IBM Corporation 2007

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-37

Student Notebook

Partition-by-range universal table space (PBR)

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

Perfect mixture of classic partitioned and segmented


table spaces
To create, specify both, NUMPARTS and SEGSIZE on
CREATE TABLESPACE
One table per table space

Better space management when using varying-length rows


Better performance in mass delete operations
Example:

CREATE TABLESPACE USTS5 IN USDB1


USING STOGROUP USCV831S
NUMPARTS 10 SEGSIZE 64 ;

Copyright IBM Corporation 2011

Figure 2-26. Partition-by-range universal table space (PBR)

CV8317.0

Notes:

Ex

cl

In contrast to a classic partitioned table space, each partition of a partition-by-range


universal table space has the structure of a segmented table space. You easily create this
type of table space when you specify both keywords, NUMPARTS and SEGSIZE in the
CREATE TABLESPACE statement. Once you have created a partition-by-range universal
table space, you can take any action that is allowed on classic partitioned or segmented
table spaces. However, as for classic partitioned table spaces, only one table is allowed.
Some benefits of partition-by-range universal table spaces are:

pr

Better space management in conjunction with varying-length rows. This is because a


segmented space map page has more information on free space than a classic
partitioned space map page.

Improved mass delete performance. Mass delete in a segmented table space


organization tends to be faster than in other types of table space organizations such as
classic partitioned table spaces.

2-38 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-39

Student Notebook

Partition-by-growth universal table space (PBG)

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

What is it?

Similar to segmented table space, but allows for additional partitions

Starts as 1-partition table space, gets more partitions if data is added

Each partition can be up to a maximum of SEGSIZE

Created using MAXPARTITIONS clause on CREATE TABLESPACE

Maximum accepted number determined by page size and DSSIZE:

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

Copyright IBM Corporation 2007

Figure 2-27. Partition-by-growth universal table space (PBG)

CV8317.0

Notes:

The maximum size of a segmented table space is 64 GB.

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.

2-40 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Partition-by-growth UTS pros and cons


Useful for table spaces that:
Do not have a suitable partitioning key (*)
Are expected to exceed the 64 GB limit

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

Can grow up to 128 TB


Advantageous data structure of a segmented table space
Begins its life as single-partition table space
Additional partitions automatically created when more data is added
You can increase MAXPARTITIONS
You can limit the number of partitions to prevent run-away applications
Many capabilities of partition-level operations (*)
Once created partitions cannot be deleted if not needed anymore (*)
You cannot decrease MAXPARTITIONS
Must be storage-group controlled
Only NPIs allowed (*)

(*) see subsequent units

Copyright IBM Corporation 2007

Figure 2-28. Partition-by-growth UTS - pros and cons

CV8317.0

Notes:

cl

The data structure of a partition-by-growth universal table space is that of a segmented


table space. This structure is preferable over a simple table space or a classic partitioned
table space because segmented table spaces have better space management and mass
delete capabilities.

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

Indexes on PBG UTS cannot be partitioned. The explanation of a partitioned index


follows later.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-41

Student Notebook

Create a partition-by-growth universal table space

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

CREATE TABLE TB2


(C1 SMALLINT,
C2 DECIMAL(9,2))
PARTITION BY SIZE EVERY 1G
IN DATABASE USDB1;

Copyright IBM Corporation 2007

Figure 2-29. Create a partition-by-growth universal table space

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

2-42 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-43

Student Notebook

Default type of a table space


Default for explicitly created TS via CREATE TABLESPACE:
Type of table space

Explicitly specified

MAXPARTITIONS
clause
Explicitly specified

Explicitly specified

Not specified

Segmented table space

Not specified

Explicitly specified

Not specified

Not specified

PBG table space with


SEGSIZE set by
DSNZPARM DPSEGSZ
Segmented table space
with SEGSIZE 4

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

SEGSIZE clause

PBG table space

Default for implicitly created TS via CREATE TABLE:


PBG table space with:
SEGSIZE = 4

DSSIZE = 4 GB

can be overridden by:


PARTITION BY SIZE EVERY integer

can only be increased


MAXPARTITIONS = 256
Copyright IBM Corporation 2007

Figure 2-30. Default type of a table space

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.

The default DSSIZE of 4 GB can be overridden by PARTITION BY SIZE EVERY integerG


in the CREATE TABLE statement.

2-44 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

SYSTABLESPACE catalog information

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

---------+---------+---------+---------+---------+-----SEGSIZE PGSIZE TYPE STATUS IMPLICIT NTABLES


DSSIZE
---------+---------+---------+---------+---------+-----4
64
0
64
4
64

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

Copyright IBM Corporation 2011

Figure 2-31. SYSTABLESPACE catalog information

CV8317.0

Notes:

cl

Looking at the entries in the SYSIBM.SYSTABLESPACE catalog table, how do you


determine whether a table space is simple, segmented, classic partitioned, universal
(either partition-by growth or partition-by-range)?. You have to look at columns
PARTITIONS, MAXPARTITIONS, SEGSIZE, and TYPE.

Ex

In this example, we have information about these table spaces as follows:

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

USTS3 is a classic partitioned table space, because column PARTITIONS contains a


non-zero value, and MAXPARTITIONS and SEGSIZE contain zero values, and TYPE is
blank.

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

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

Notice the value in the STATUS column.

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.

2-46 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

CREATE TABLESPACE INVS0001


IN INVDB11
USING STOGROUP INVDG112
PRIQTY p (in kilo bytes)
SECQTY s (in kilo bytes)
BUFFERPOOL ...

INSERT, LOAD

Copyright IBM Corporation 2011

Figure 2-32. Space allocation

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

USING STOGROUP clause for nonpartitioned table spaces:

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-47

Student Notebook

- For 8KB page sizes, if integer is less than 24, n is 24.


- For 16KB page sizes, if integer is less than 48, n is 48.
- For 32KB page sizes, if integer is less than 96, n is 96.
- For any page size, if integer is greater than 67108864, n is 67108864.

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

2. If you specify PRIQTY -1 or do not specify PRIQTY, actual primary quantity is


determined as follows:

- If the TSQTY subsystem parameter value is specified and is greater than 0, actual
primary quantity is at least the value of TSQTY.

- If the TSQTY subsystem parameter is not specified or is 0, actual primary quantity is


one cylinder.
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 table space is < 32 GB, the formula is:

actual secondary quantity in cylinders =


MAX(0.1 * actual primary quantity in cylinders, MIN(calculated extent in cylinders,127))

- If the maximum size of a data set in the table space is >= 32 GB, the formula is:

actual secondary quantity in cylinders =


MAX(0.1 * actual primary quantity in cylinders, MIN(calculated extent in cylinders,559))

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.

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:

- If SECQTY is specified and specified secondary quantity is not equal to -1 or 0, the


following formulas determine actual secondary quantity:
If the maximum size of a data set in the table space < 32 GB, the formula is:
actual secondary quantity in cylinders =
MAX(MIN(calculated extent in cylinders,127),specified secondary quantity in cylinders)

2-48 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

actual secondary quantity in cylinders =


MAX(MIN(calculated extent in cylinders,559),specified secondary quantity in cylinders)

If MGEXTSZ is NO:

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

- For a table space, if SECQTY is n, the secondary space allocation is at least n


kilobytes, with the following exceptions:
If SECQTY is greater than 4194304, n is 4194304 kilobytes.
Note

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.

USING STOGROUP clause for partitioned table spaces:

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

A USING clause that is not in any PARTITION clause

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-49

Student Notebook

Space map page


Data
Page
Data
Page
Data

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

Page
Data
Page
Space Map

Header

Simple and Classic Partitioned Table Space

Page Header 26 bytes

Page Header 28 bytes

One 2-bit
entry
for each page

xx .. .. ..

xx
00
01
10
11

space
> max
< max
< avg
< min

Segmented and Universal Table Space

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

And 4-bit entry


for each page
in the segment

Free Space Status

Free Space Status

One 7-byte entry


for each segment

One 1-bit
entry
for each page

One 1-bit entry


for each page

x :
0
not modified
1
modified

Page Status

Page Status

1-byte trailer

1-byte trailer

Copyright IBM Corporation 2011

Figure 2-33. Space map page

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

Space map for simple and classic partitioned table spaces

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.

2-50 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Space map for segmented and universal table spaces


DB2 uses four bits to determine if there is space available on a particular page to insert or
update a row.
The meaning of the bits is as follows:
Page is empty and unformatted
Page is emptied by mass delete
Page is empty and formatted
Page has space for longest row
Values used for placing variable length rows
For future use
Page is full

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Page Header 20 bytes

6 bytes

Header ...

Row Data

Flags
Length
and so forth
ID Map
2 bytes/row
max 255

2-byte trailer

Copyright IBM Corporation 2011

Figure 2-34. Data page

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

2-52 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Total Free Space

ts

Large
hole

Header

LL

Space

Small
hole

Next
Large
hole

Free Space

ID Map

Copyright IBM Corporation 2011

Figure 2-35. Free space

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

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

2-54 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-55

Student Notebook

CREATE TABLE column attributes


Datatype

Null Attribute

Default Attribute

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

CREATE TABLE EMP


(EMPNO
CHAR(6)
NOT NULL
,
FIRSTNME
VARCHAR(30) NOT NULL
,
MIDINIT
CHAR(1)
,
LASTNAME
VARCHAR(30) NOT NULL
,
WORKDEPT
CHAR(3)
,
PHONENO
CHAR(4)
,
HIREDATE
DATE
,
JOB
CHAR(8)
WITH DEFAULT NEW HIRE ,
EDLEVEL
SMALLINT
,
SEX
CHAR(1)
NOT NULL
,
BIRTHDATE
DATE
NOT NULL
,
SALARY
DECIMAL(9,2)
,
RESUME
CLOB(10M)
,
INFO
XML) ;
[ or IN DBX.TSX ;]
[ or IN DATABASE DBX ;]
[ or IN TSX ;]
Copyright IBM Corporation 2011

Figure 2-36. CREATE TABLE - column attributes

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

EBCDIC, ASCII or UNICODE


EBCDIC, ASCII or UNICODE
NOT ASSOCIATED WITH ANY CCSID
NOT ASSOCIATED WITH ANY CCSID
DBCS
DBCS

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

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

2-58 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

Fixed Length Row (no VL fields)

fld1

00

fld2

FF

fld3

fld3 is null
fld2 is present
fld1 cannot be null

Variable Length Row (VL fields)

fld1

00

fld2

03

00

fld3

Length of fld3 (VL)


Fixed length fields

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

Figure 2-37. Row format

CV8317.0

Notes:

Nullable fields

cl

Nulls are used when actual values are unknown.

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-59

Student Notebook

Variable length fields


The VARCHAR data type can provide disk space savings. Space is allocated according to
the actual length of each data item, and trailing blanks are not included.

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

The use of VARCHAR columns means the row is variable in length.

2-60 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Basic row format and reordered row format


INTEGER
NOT NULL,
VARCHAR(10) NOT NULL,
CHAR(10)
NOT NULL,
VARCHAR(20)

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

CREATE TABLE TB1 (C1


C2
C3
C4
)
Basic Row Format
C1

C2
8000000A 0006 WILSON

C3
ANDREW

C4
0008 SAN JOSE

2-byte length

Reordered Row Format


C1
C3
8000000A ANDREW

O2

O4

12

offset to C2

C2
18 WILSON

C4
SAN JOSE

offset to C4

The offset is a hexadecimal value, indicating the offset for the


column from the start of the data in the row.
Note: More log volume may occur for rows stored in RRF..
Copyright IBM Corporation 2011

Figure 2-38. Basic row format and reordered row format

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

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

2-62 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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)

CURRENT SQLID (special register)


NULL

System defaults

Examples:
WITH
WITH
WITH
WITH
WITH

DEFAULT
DEFAULT
DEFAULT
DEFAULT
DEFAULT

MY OWN VALUE
USER
CURRENT SQLID
NULL

Copyright IBM Corporation 2011

Figure 2-39. DEFAULT attributes

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

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-65

Student Notebook

Table check constraints

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

CREATE TABLE EMPLOYEE


(EMPNO
INTEGER NOT NULL,
NAME
CHAR(10) NOT NULL,
DEPT
INTEGER CHECK (DEPT BETWEEN 1 AND 100),
JOB
CHAR(10),
YEARS
INTEGER,
SALARY
DECIMAL(10,2),
INVESTP
INTEGER NOT NULL WITH DEFAULT,
MAXINVEST INTEGER NOT NULL WITH DEFAULT,
CHECK
(JOB IN ('MANAGER','SALES','TECHNICAL')),
CONSTRAINT NOINVEST CHECK ((YEARS < 5 AND INVESTP = 0)
OR (YEARS >= 5)),
CHECK
(INVESTP <= MAXINVEST))
WITH RESTRICT ON DROP
IN USDB1.USTS2;

Copyright IBM Corporation 2011

Figure 2-40. Table check constraints

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

2-66 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

- Built-in or user-defined functions


- Cast functions other than those created when the distinct type was created
- Host variables
- Parameter markers

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

- GX literals (hexadecimal graphic string constants)

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.

The same data type.

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

Restrict status is recorded in CLUSTERTYPE column of SYSIBM.SYSTABLES.

2-68 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

SYSTABLES and SYSCOLUMNS catalog information


SYSIBM.SYSTABLES

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

Figure 2-41. SYSTABLES and SYSCOLUMNS catalog information

CV8317.0

Notes:

All DB2 tables, including the catalog tables, are described in the DB2 catalog table
SYSIBM.SYSTABLES.

pr

Ex

cl

All DB2 table columns are described in SYSIBM.SYSCOLUMNS.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-69

Student Notebook

Check constraints catalog information


SYSIBM.SYSCHECKS

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

---------+---------+---------+---------+---------+---------+---------+-SELECT TBNAME,OBID,TBOWNER,CHECKNAME, CHECKCONDITION


FROM
SYSIBM.SYSCHECKS
WHERE TBOWNER
= TSOUD01' AND
TBNAME = 'EMPLOYEE'
---------+---------+---------+---------+---------+---------+---------+-------TBNAME
OBID TBOWNER CHECKNAME CHECKCONDITION
---------+---------+---------+---------+---------+---------+---------+-------EMPLOYEE
9 TSOUD01 DEPT
DEPT BETWEEN 1 AND 100
EMPLOYEE
12 TSOUD01 INVESTP
INVESTP <= MAXINVEST
EMPLOYEE
10 TSOUD01 JOB
JOB IN ('MANAGER','SALES','TECHNICAL')
EMPLOYEE
11 TSOUD01 NOINVEST (YEARS < 5 AND INVESTP = 0) OR (YEARS >= 5)

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

Figure 2-42. Check constraints catalog information

CV8317.0

Notes:

SYSIBM.SYSCHECKS describes the check constraints.

pr

Ex

cl

SYSIBM.SYSCHECKDEP has a cross-reference of the constraints and the columns they


refer to.

2-70 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Enable table-controlled partitioning

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

CREATE TABLE EMPLOYEE1


(EMPNO
INTEGER NOT NULL,
NAME
CHAR(10) NOT NULL,
DEPT
INTEGER,
JOB
CHAR(10),
YEARS
INTEGER,
SALARY
DECIMAL(10,2),
INVESTP
INTEGER NOT NULL WITH DEFAULT,
MAXINVEST INTEGER NOT NULL WITH DEFAULT)
PARTITION BY (EMPNO ASC)
(PARTITION 1 ENDING AT (099),
PARTITION 2 ENDING AT (199),
PARTITION 3 ENDING AT (299),
PARTITION 4 ENDING AT (999))
IN USDB1.USTS3;

Copyright IBM Corporation 2011

Figure 2-43. Enable table-controlled partitioning

CV8317.0

Notes:

Ex

cl

The table-controlled partitioning is initiated by specifying the PARTITION BY clause in the


CREATE TABLE statement. The PARTITION BY clause identifies the columns and values
used to define the partition boundaries. When the CREATE TABLE statement executes
successfully, the definition of the partitioned table space (either classic or
partition-by-range) is complete, and data can be inserted into the table. PARTITION n
(where n is the partition number) and ENDING AT keywords are used to specify the
partition boundaries.

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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)

But not DECIMAL(5,2) to DECIMAL (5,0)


Switch between fixed and variable types of same length such as:
CHAR(30) to VARCHAR(30) and vice versa
GRAPHIC(30) to VARGRAPHIC(30) and vice versa

You cannot:

Remove or Rearrange columns


Change NULL or default attribute

Copyright IBM Corporation 2011

Figure 2-44. ALTER TABLE

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

ALTER TABLE PROJ


ADD PROJCOST DECIMAL (7,0)
NOT NULL WITH DEFAULT

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.

2-72 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Examples for adding / removing check constraints

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

ALTER TABLE EMPLOYEE


ADD CONSTRAINT SALCHECK
CHECK(SALARY > 0);

-ALTER TABLE EMPLOYEE


DROP CONSTRAINT SALCHECK;
--

ALTER TABLE EMPLOYEE


ADD CONSTRAINT SALCHECK
CHECK(SALARY > 10000);
-ALTER TABLE EMPLOYEE
DROP CHECK SALCHECK;

Copyright IBM Corporation 2011

Figure 2-45. Examples for adding / removing check constraints

CV8317.0

pr

Ex

cl

Notes:

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-73

Student Notebook

Adding table check constraints - considerations

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

If table is empty, the check constraint is added


If table is populated, the action depends on:
CURRENT RULES special register (STD|DB2)
For STD

If no violating rows, then constraint is added


If violating rows, then ALTER fails

For DB2

Check Pending (CHKP) is set

Copyright IBM Corporation 2011

Figure 2-46. Adding able check constraints - considerations

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

A table in Check Pending status cannot be accessed through DML.

2-74 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

The -DISPLAY DATABASE command

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

Copyright IBM Corporation 2011

Figure 2-47. The -DISPLAY DATABASE command

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-75

pr

Ex

cl

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

Student Notebook

2-76 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

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

2.4. Data compression

pr

Ex

cl

Uempty

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-77

Student Notebook

Data compression overview


With
data compression

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

Copyright IBM Corporation 2011

Figure 2-48. Data compression - overview

CV8317.0

Notes:

Data compression means table space compression.

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

This discussion is applicable to all types of table spaces discussed earlier.

2-78 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Data compression data management

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

Copyright IBM Corporation 2011

Figure 2-49. Data compression - data management

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

Compression may be specified at the partition level.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-79

Student Notebook

Data compression building the dictionary


For table space or partition with COMPRESS YES

REORG TABLESPACE

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

1.

Builds the dictionary during UNLOAD

2.

When full, improves dictionary by sampling remaining data

3.

Compress at RELOAD

LOAD REPLACE or LOAD RESUME NO


1.

Inserts rows (uncompressed) until dictionary full

2.

Starts compressing

LOAD utility COPYDICTIONARY and


KEEPDICTIONARY options

REORG TABLESPACE utility KEEPDICTIONARY option


DSN1COMP stand-alone utility

Estimates space savings

Copyright IBM Corporation 2011

Figure 2-50. Data compression - building the dictionary

CV8317.0

Notes:

A compression dictionary can be built by the LOAD and REORG utilities.

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

LOAD COPYDICTIONARY option:

The LOAD COPYDICTIONARY option allows compression dictionaries to be copied from


one partition to another in a classic partitioned or partition-by-range universal table space.
The LOAD keyword, COPYDICTIONARY integer, at the table space level enables LOAD
PART REPLACE to copy an existing compression dictionary from the partition specified to
all partitions being PART REPLACEd.
2-80 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

1+1
=2

Example

How to copy a compression dictionary from physical partition 1 to partitions 3 and 5.

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

LOAD RESUME NO COPYDICTIONARY 1


INTO TABLE PART 3 REPLACE
INTO TABLE PART 5 REPLACE

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.

The COPYDICTIONARY keyword is only compatible on classic partitioned and


partition-by-range universal table spaces using the RESUME NO option at the table space
level with PART integer REPLACE statements, where the partition being copied is not the
same as the partitions being replaced.
LOAD PART integer RESUME is not supported with the COPYDICTIONARY keyword.
Neither is LOAD RESUME YES COPYDICTIONARY.

The COPYDICTIONARY keyword is incompatible with the KEEPDICTIONARY keyword at


the table space or partition level and with the REPLACE keyword at the table space level.

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

LOAD KEEPDICTIONARY option:

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

DB2 keeps the dictionary if a compression dictionary exists

You must use KEEPDICTIONARY to ensure that the compression dictionary is maintained.

REORG TABLESPACE KEEPDICTIONARY option:

Prevents REORG TABLESPACE from building a new compression dictionary when


unloading the rows. The efficiency of REORG increases with the KEEPDICTIONARY
option for the following reasons:
The processing cost of building the compression dictionary is eliminated.
Existing compressed rows do not need to be compressed again.

Existing compressed rows do not need to be expanded, unless indexes require it or


SORTDATA is used.
Possible reasons for not specifying KEEPDICTIONARY are:

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.

2-82 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

DSN1COMP Stand-alone utility:


The DSN1COMP stand-alone utility provides a close approximation to DB2 compression
results without altering the data. Input to the utility can be VSAM data sets containing DB2
table space or partition data, DB2 full image copy data sets, or sequential data sets
containing DB2 table space or partition data (for example, DSN1COPY output).

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Dictionary not built on a table space with COMPRESS YES attribute


until:
REORG or
LOAD utility was executed

For some customers, REORG or LOAD are not executed frequently

DB2 10 NFM allows for build of compression dictionary on:


INSERT
MERGE

LOAD SHRLEVEL CHANGE

No REORG or LOAD needed to get compressed data


Copyright IBM Corporation 2011

Figure 2-51. Compress on INSERT

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

2-84 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

How Compress on INSERT works

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

INSERT, MERGE and LOAD SHRLEVEL CHANGE trigger the


creation of a compression directory if:
The table space or partition is defined with COMPRESS YES

The table space or partition has no compression dictionary built


Inserted data reaches a threshold that allows the build of the
compression dictionary
Threshold is 1.2 MB (checked through RTS data)

If threshold is reached, dictionary build is done


asynchronously:

Data continues to be inserted uncompressed until dictionary is ready


Rows read with UR in order to build the dictionary

No way to turn off Auto Compress


Use COMPRESS NO if unwanted

Copyright IBM Corporation 2011

Figure 2-52. How Compress on INSERT works

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

DSNU241I -DB0B DSNUZLCR - DICTIONARY WITH 4096 755


ENTRIES HAS BEEN SUCCESSFULLY BUILT FROM 598 ROWS FOR TABLE SPACE
SABI6.TS6, PARTITION 1

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

2-86 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Location of compression dictionary pages

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

Copyright IBM Corporation 2011

Figure 2-53. Location of compression dictionary pages

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

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

2-88 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Miscellaneous on Compress on Insert


DSN1COMP can be used to estimate space savings

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

If REORG keyword is not specified:

Space saving calculated based on what LOAD would achieve


Compression ratio likely to be a little less effective

If COMPRESS YES:

Check for message DSNU241I or DSNU231I


Check, for example, RTS so see if data volume is large enough
(> 1.2 MB)
Remember that RTS only updated past the minutes you specified for
DSNZPARM STATSINT

If DATASIZE > 1,310,720 bytes, but compression not


activated:

Can happen if dictionary cannot be built due to nature of data


No error message
Copyright IBM Corporation 2011

Figure 2-54. Miscellaneous on Compress on Insert

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

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

2-90 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

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

2.5. Views and Materialized Query Tables

pr

Ex

cl

Uempty

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

View V2 (Subset of rows)

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

View V3 (Join of 2 tables)

Copyright IBM Corporation 2011

Figure 2-55. DB2 views

CV8317.0

Notes:

What are views?

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

A subset of rows of a real table

A JOIN of two or more real tables


The definition is recorded in the DB2 catalog and resolved into base table references when
SQL using the view is bound.
Users see views as if they were real tables.

2-92 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-93

Student Notebook

CREATE VIEW examples

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

CREATE VIEW MYTABLES


AS
SELECT *
FROM
SYSIBM.SYSTABLES
WHERE CREATOR = USER

CREATE VIEW VSALSTAT


CREATE VIEW VSALSTAT
(AVGSAL,
AS
SUMSAL,
SELECT AVG(SALARY) AS AVGSAL,
MINBIRTH) OR
SUM(SALARY)AS SUMSAL,
AS
MIN(BIRTHDATE)AS MINBIRTH
SELECT AVG(SALARY),
FROM
EMP
SUM(SALARY),
WHERE BIRTHDATE < 1980-01-01
MIN(BIRTHDATE)
FROM
EMP
WHERE BIRTHDATE < 1980-01-01
CREATE VIEW VFUTPROJ
AS
SELECT MGRNO,
DEPTNAME,
PROJNO,
RESPEMP
FROM
PROJ,DEPT
WHERE PROJ.DEPTNO = DEPT.DEPTNO
AND
PRENDATE < CURRENT DATE - 1 YEAR
Copyright IBM Corporation 2011

Figure 2-56. CREATE VIEW - examples

CV8317.0

Notes:

The visual has further examples of views.

cl

Virtual columns such as AVG(SALARY), SUM(SALARY) and MIN(BIRTHDATE) can be


given names on the CREATE VIEW as shown or on the SELECT using the AS clause as
shown.

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.

2-94 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-95

Student Notebook

Views on views on tables


SQL2

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

Copyright IBM Corporation 2011

Figure 2-57. Views on views on tables

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

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-97

Student Notebook

DELETE from a view


EMPLOYEE
NAME
SCHMIDT
ABELE
OBERHAUS
SCHMIDT
MUELLER

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

EMPNO, NAME, ROOM, TELEPHONE


EMPLOYEE
DEPT = 'E11';

EMPE11

EMPNO
290
300
310

NAME
OBERHAUS
SCHMIDT
MUELLER

ROOM
1012
1034
1022

TELEPHONE
4112
4234
4419

DELETE FROM EMPE11


WHERE NAME = 'SCHMIDT';

Two rows with name SCHMIDT in the table. What is the effect of DELETE?
Copyright IBM Corporation 2011

Figure 2-58. DELETE from a view

CV8317.0

pr

Ex

cl

Notes:

2-98 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

SELECT, UPDATE, INSERT, DELETE with VIEWS


VIEW
C
E

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

Figure 2-59. SELECT, UPDATE, INSERT, DELETE with VIEWS

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

There are some restrictions on updating views:


1. You cannot delete, update or insert into:

- Views that join tables (and no INSTEAD OF TRIGGER is defined).


- Views that use functions
- Views that use DISTINCT

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-99

Student Notebook

- Views that use GROUP BY


2. You cannot insert into:
- Views that contain derived data using arithmetic expressions
- Views that contain constants

pr

Ex

cl

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

- Views that eliminate columns without a default value

2-100 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

The disappearing row


EMPNO

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

CREATE VIEW SALARIES_2


CREATE VIEW SALARIES_1
AS
AS
SELECT EMPNO, LASTNAME, SALARY
SELECT EMPNO, LASTNAME, SALARY
FROM EMPLOYEE
FROM EMPLOYEE
WHERE SALARY < 40000
WHERE SALARY < 40000
WITH CHECK OPTION
UPDATE SALARIES_1
SET SALARY = 42000
WHERE EMPNO = '000030'

UPDATE SALARIES_2
SET SALARY = 42000
WHERE EMPNO = '000030'

WORKS!

FAILS!

Copyright IBM Corporation 2011

Figure 2-60. The disappearing row

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-101

Student Notebook

WITH CHECK OPTION


WITH CHECK OPTION

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

Either LOCAL or CASCADED

WITH LOCAL CHECK OPTION

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

WITH CASCADED CHECK OPTION

V2 with WHERE condition 2


!
Enforce your WHERE clause and
V1 with WHERE condition 1
Enforce WHERE clauses of underlying views
!
TB
Regardless of definitions of those views

Copyright IBM Corporation 2011

Figure 2-61. WITH CHECK OPTION

CV8317.0

Notes:

WITH CHECK OPTION has two forms: CASCADED and LOCAL.

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.

2-102 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-103

Student Notebook

Effect of cascaded CHECK OPTION


EMPLOYEE
NAME
LIEBHERR
ABELE
OBERHAUS
SCHMIDT
MUELLER

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

SET SALARY = SALARY + 200;

FAILS!

SET SALARY = SALARY + 200;

Copyright IBM Corporation 2011

FAILS!

Figure 2-62. Effect of cascaded CHECK OPTION

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.

2-104 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-105

Student Notebook

Views catalog information (1 of 2)


CREATE VIEW EMPLOYEEV AS SELECT EMPNO,DEPT,YEARS FROM EMPLOYEE;

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

Figure 2-63. Views - catalog information (1 of 2)

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.

2-106 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Views catalog information (2 of 2)


CREATE VIEW EMPLOYEEV AS SELECT EMPNO,DEPT,YEARS FROM EMPLOYEE;

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

Copyright IBM Corporation 2011

Figure 2-64. Views - catalog information (2 of 2)

CV8317.0

Notes:

In addition, the view definition is stored as well as "dependency" information.

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

SYSIBM.SYSVIEWDEP.table records the dependencies of views on tables, functions, and


other views. In this example, one row is inserted in this table to indicate the view
EMPLOYEEV is dependent on table EMPLOYEE.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-107

Student Notebook

Materialized query tables (MQTs)


MQTs are pre-computed result tables

1.

CREATE TABLE MQT1 AS


(SELECT D.DEPTNO, D.DEPTNAME,

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

FROM EMPLOYEE E, DEPARTMENT D

WHERE E.DEPTNO = D.DEPTNO

GROUP BY D.DEPTNO, D.DEPTNAME


ORDER BY D.DEPTNO)

MAINTAINED BY SYSTEM

ENABLE QUERY OPTIMIZATION


IN DBX.TSX;

4.

3. User query

SELECT
FROM EMPLOYEE E, DEPARTMENT D
WHERE E.DEPTNO = D.DEPTNO
AND .
GROUP BY D.DEPTNO, D.DEPTNAME

Copyright IBM Corporation 2011

Figure 2-65. Materialized query tables (MQTs)

CV8317.0

Notes:

The numbered steps above are as follows:

1. CREATE your MQT in terms of a Subselect

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

Decision support queries

Decision support queries typically:


Operate over huge amounts of data
Perform multiple joins and complex aggregation operations.
The problem
2-108 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

Materialized Query Tables (MQTs)

These precomputed results are known as materialized query tables (MQTs).

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

Can be chosen by the optimizer through automatic query rewrite

MQTs versus views

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

CREATE TABLE MQT1 AS


(SELECT D.DEPTNO, D.DEPTNAME,
SUM(E.SALARY) AS TOTAL_SALARY,
SUM(E.BONUS) AS TOTAL_BONUS,
SUM(E.COMMISSION) AS TOTAL_COMMISSION
FROM EMPLOYEE E, DEPARTMENT D
WHERE E.DEPTNO = D.DEPTNO
GROUP BY D.DEPTNO, D.DEPTNAME
ORDER BY D.DEPTNO)
MAINTAINED BY SYSTEM
ENABLE QUERY OPTIMIZATION
IN DBX.TSX;

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-109

Student Notebook

DATA INITIALLY DEFERRED


This means the data is not inserted into the materialized query table when it is created.
Use the REFRESH TABLE statement to populate the materialized query table, or use
INSERT, LOAD, or other means to insert data into a user-maintained materialized query
table.

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.

2-110 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Populating and refreshing MQTs


REFRESH TABLE SQL stmt

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

Can populate:

System-maintained MQT
User-maintained MQT

Performs 4 actions in a single commit scope


All MQT rows deleted

Logging
Mass delete if segmented table space

Subselect executed

Uses MQT isolation level


No other access to MQT

Data INSERTed into MQT


Logging

Catalog updated

Refresh timestamp
MQT cardinality

Copyright IBM Corporation 2011

Figure 2-66. Populating and refreshing MQTs

CV8317.0

Notes:

REFRESH TABLE SQL statement

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-111

Student Notebook

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

Special registers

CURRENT MAINTAINED TABLE TYPES FOR OPTIMIZATION

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

Copyright IBM Corporation 2011

Figure 2-67. Special registers

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

2. CURRENT MAINTAINED TABLE TYPES FOR OPTIMIZATION

2-112 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

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

2.6. Synonyms and Aliases

pr

Ex

cl

Uempty

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Copyright IBM Corporation 2011

Figure 2-68. SYNONYM

CV8317.0

Notes:

Synonym

An alternative name for a local table or local view.

cl

For example, you can create a synonym to refer to:

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.

Points to remember about synonyms

A synonym can only be defined on a local table or view.


The table or view must exist.
No authorization is required to define a synonym.
Dropping a table or view also drops its synonyms.
A synonym is unqualified and can only be used by the authid that created it.
A synonym can only be used on the local system.

pr

1.
2.
3.
4.
5.
6.

2-114 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

SYNONYM catalog information


CREATE SYNONYM SEMP1 FOR TSOUD01.EMPLOYEE;

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

CREATE SYNONYM SEMP2 FOR TSOUD01.EMPLOYEEV;

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

Copyright IBM Corporation 2011

Figure 2-69. SYNONYM - catalog information

CV8317.0

Notes:

pr

Ex

cl

A synonym can be considered as a "private" pointer to a table. It can only be referenced by


its owner. No qualifier can be used. This also explains why synonym definitions are not
stored in the SYSIBM.SYSTABLES catalog table as is the case for views.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Copyright IBM Corporation 2011

Figure 2-70. ALIAS

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

An alias can be defined on a local or remote table or view.


The table or view need not exist when the alias is defined.
Dropping a table or view has no effect on its aliases.
Need CREATEALIAS privilege, SYSADM or SYSCTRL authority, DBADM or DBCTRL
authority on the database that contains the table, or System DBADM authority to define
an alias.
5. An alias is a qualified name that can be used by any authid.
6. An alias defined at one DB2 subsystem can be used at another DB2 subsystem.
7. A fully qualified alias name (a three-part name) can refer to a remote alias.

pr

Ex

1.
2.
3.
4.

2-116 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-117

Student Notebook

ALIAS catalog information

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

CREATE ALIAS AEMP1 FOR TSOUD01.EMPLOYEE;

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

Copyright IBM Corporation 2011

Figure 2-71. ALIAS - catalog information

CV8317.0

Notes:

pr

Ex

cl

Its definition in the catalog is analogous to tables and views.

2-118 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Object name translation


Table Name used in SQL statement
2 part name

3 part name

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

1 part name

add prefix (authid)


SYSSYNONYMS

local name

remote name

SYSTABLES

table/view

alias

local name

remote name

process locally

Copyright IBM Corporation 2011

send to remote location

Figure 2-72. Object name translation

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

- CREATOR, authorization ID of the owner of the synonym

- TBNAME, name of the table or view referred to by the synonym

- TBCREATOR, authorization ID of the owner of the table or view

a. If a row is present for the matching values in NAME and CREATOR columns, then it
is a synonym.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-119

Student Notebook

Using the values in TBNAME and TBCREATOR columns, it looks up the


SYSIBM.SYSTABLES catalog table for a matching row on columns NAME and
CREATOR. The relevant columns in this table are:
NAME, name of the table, view
OWNER, authorization ID of the owner of the table or view

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

TYPE, Indicates if it is a table (T) or view (V)

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

BTYPE, type of object (T is table and V is view)


DNAME, name of the view

DOWNER, authorization ID of the owner of the view

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

NAME, name of the table, view, or alias

OWNER, authorization ID of the owner of the table, view, or alias


TYPE, indicates if it is a table (T), view (V), or alias (A)
TBNAME, Name of the table or view referred to, if TYPE is A, else blank

2-120 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

TBCREATOR, authorization ID of the owner of the referred to table or view, if


TYPE is A, else blank

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

BTYPE, type of object (T is table and V is view)


DNAME, name of the view

DOWNER, authorization ID of the owner of the view

If BTYPE = T, the resolution is complete. It uses the values in columns BNAME


and BCREATOR to retrieve the data.
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!

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

A row is present and column LOCATION is blank. This is treated as a local


request, and it proceeds as follows:

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)

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-121

Student Notebook

DNAME, name of the view


DOWNER, authorization ID of the owner of the view
If BTYPE = T, the resolution is complete. It uses the values in columns
BNAME and BCREATOR to retrieve the data.

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.

A qualified name is never interpreted as a synonym. Therefore, it directly looks up the


SYSIBM.SYSTABLES catalog table, using the values RAVI for column CREATOR and
X for column NAME, and follows the same logic as explained above.
3. The SQL statement specifies a 3-part name, for example SELECT ... FROM
SANJOSE.BART.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.

2-122 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

I need to call Larry Jones...


what is the best way for DB2 to
find his phone number among
10,000 entries in the
EMPLOYEE table?

I can create an index of course. It


gives DB2 direct access to the
table rows I want.

EMPLOYEE table

Copyright IBM Corporation 2011

Figure 2-73. What is an index?

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.

2-124 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Why have indexes?


Performance improvement

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

Access paths
Index only access
Avoid sorts

Order by / Group by / Distinct

Cluster data
Foreign keys

Integrity

Uniqueness

Primary keys / Unique keys

But:
Performance impact

Delete
Insert
Update (Delete / Insert)

Disk space
Utility run times

Copyright IBM Corporation 2011

Figure 2-74. Why have indexes?

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

2-126 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Creating unique and non-unique indexes

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

CREATE UNIQUE INDEX IX1EMPL


ON EMPLOYEE(EMPNO)
CLUSTER;

CREATE INDEX IX2EMPL


ON EMPLOYEE(LAST_NAME,FIRST_NAME);

CREATE UNIQUE WHERE NOT NULL INDEX IX3EMPL


ON EMPLOYEE(E_MAIL_ADDR);
EMPLOYEE table
EMPNO
FIRST_NAME

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

Copyright IBM Corporation 2011

Figure 2-75. Creating unique and non-unique indexes

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

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

- 2000 - n for a padded, nonpartitioning index


- 2000 - n - 2m for a non-padded, nonpartitioning index
- 255 - n for a partitioning index (padded or non-padded)

2-128 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

CREATE TABLE EMPLOYEE (


ID
LASTNAME
FIRSTNAME
SALARY
BONUS

CREATE INDEX INDEX1


ON EMPLOYEE (SALARY + BONUS);
SELECT SALARY + BONUS, LASTNAME, FIRSTNAME
FROM EMPLOYEE
WHERE SALARY + BONUS < 51000;

Copyright IBM Corporation 2011

Figure 2-76. Index on expression

CV8317.0

Notes:

You normally build an index on column values.

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:

Note the following restrictions:


Maximum number of expressions in an index key is 64.
An index on an expression cannot be a clustering index.
DESC / RANDOM cannot be specified for indexes on expressions.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-129

Student Notebook

You cannot use ALTER to add columns to an index on an expression.

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.

2-130 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Figure 2-77. Clustering

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

REORG is not influenced by the APPEND option.

2-132 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Index and index space


CREATE INDEX IX1EMPL ON EMPLOYEE......

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

Creates:

An index called creator.IX1EMPL and

An index space called dbname.IX1EMPL and

One or more VSAM data sets catname.DSNDBx.dbname.IX1EMPL.p0001.Innn

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

Copyright IBM Corporation 2011

Figure 2-78. Index and index space

CV8317.0

Notes:

What is an index space?

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-133

Student Notebook

"

Index and index space attributes

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

CREATE INDEX IX4EMPL


ON EMPLOYEE(LAST_NAME ASC,DEPTNO DESC)
USING STOGROUP USCV831S
PRIQTY 7200
SECQTY 7200
PADDED
FREEPAGE 5
PCTFREE 20
BUFFERPOOL BP2
ALTER INDEX IX4EMPL
PRIQTY 14400
SECQTY 1440
BUFFERPOOL BP3
FREEPAGE 15
PCTFREE 25
NOT PADDED
RENAME INDEX

Copyright IBM Corporation 2011

Figure 2-79. Index and index space attributes

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

USING STOGROUP clause for nonpartitioned indexes:

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

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

- For 8KB page sizes, if integer is less than 24, n is 24.


- For 16KB page sizes, if integer is less than 48, n is 48.
- For 32KB page sizes, if integer is less than 96, n is 96.
- For any page size, if integer is greater than 4194304, n is 4194304.

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.

2. If you specify PRIQTY -1 or do not specify PRIQTY, actual primary quantity is


determined as follows:

- if the IXQTY subsystem parameter value is specified and is greater than 0, actual
primary quantity is at least the value of IXQTY.

- If the IXQTY subsystem parameter is not specified or is 0, actual primary quantity is


one cylinder.

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:

actual secondary quantity in cylinders=


MAX(0.1 * actual primary quantity in cylinders, MIN(calculated extent in cylinders,127))

- If the maximum size of a data set in the index is >= 32 GB, the formula is:

actual secondary quantity in cylinders=


MAX(0.1 * actual primary quantity in cylinders, MIN(calculated extent in cylinders,559))

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:

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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:

- if SECQTY is n, the secondary space allocation is at least n kilobytes, where n is:


12 if n is less than 12 (for 4 KB page sizes)
n if n is between 12 and 4,194,304

4,194,304 if n is greater than 4,194,304


Note

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.

USING STOGROUP clause for partitioned indexes:

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

- Data sets are managed by DB2

- The default storage group for the database is used


- A value of 12 is used for PRIQTY and SECQTY

2-136 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

This identifies the buffer pool to be used for the index.


FREEPAGE

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

2-138 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

DB2 index tree structure

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

Copyright IBM Corporation 2011

Figure 2-80. DB2 index tree structure

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

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Copyright IBM Corporation 2011

Figure 2-81. Catalog tables

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

This indicates if the index is a clustering index or not


CLUSTERRATIOF

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.

2-140 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

Contains one row for each column of an index key.

SYSIBM.SYSKEYTARGETS

pr

Ex

cl

For indexes on expression, catalog table SYSKEYTARGETS is used instead of SYSKEYS.

2-142 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

DB2 addressing scheme

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

Index Leaf Page


RID

Index Entry

Page-ID

Slot

Key Field(s)

Data Page
Page 8

ROW

Free Space

ID

Map

Copyright IBM Corporation 2011

Figure 2-82. DB2 addressing scheme

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-143

Student Notebook

DB2 addressing scheme overflow pointer


Data Page
Page 100

Overflow record

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

Index Leaf Page


Index Entry

RID

Key Field(s)

8
Page-ID

5
Slot

Free Space

ID Map

Data Page
Page 8

Overflow pointer

100

RID

A "Mail forwarding address"

Free Space

After a row has


overflowed, all indexes
continue to point to the
original position

ID Map

Copyright IBM Corporation 2011

Figure 2-83. DB2 addressing scheme - overflow pointer

CV8317.0

Notes:

Variable length rows may occur for:

Ex

cl

1. Rows with VARCHAR columns


2. Tables which have had columns added or column lengths increased with the ALTER
statement
3. Compressed rows (see later)
An SQL UPDATE may increase the length of a variable length row.

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

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

It is therefore important to know about these mail forwarding addresses. Their


occurrences can be reported by the RUNSTATS utility (see later) and updated in
SYSIBM.SYSTABLEPART in columns NEARINDREF and FARINDREF.

pr

Ex

cl

REORG TABLESPACE (see later) eliminates these mail forwarding addresses and
reestablishes free space.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-145

Student Notebook

Leaf page index entry format


Unique Keys
RID

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

Copyright IBM Corporation 2011

Figure 2-84. Leaf page - index entry format

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.

2-146 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Copyright IBM Corporation 2011

Figure 2-85. Non-leaf pages

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-147

Student Notebook

"

Index page management


INSERT

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

Random insert pattern


50 / 50 page split

Ever-ascending or Ever-descending sequential insert pattern


Asymmetric page split

REORG to reestablish free space

UPDATE

Really DELETE then INSERT

Updating low-order field in composite index


Entry less likely to move to another page

DELETE

Pseudo delete

Physical delete

Copyright IBM Corporation 2011

Figure 2-86. Index page management

CV8317.0

Notes:

Index page management - INSERT

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.

b. If an ever-ascending sequential insert pattern is detected for an index, DB2 splits


index pages asymmetrically so that most existing keys stay on the original index

2-148 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

c. If an ever-descending sequential insert pattern is detected in an index, DB2 splits


index pages asymmetrically so that most existing keys are moved to the new index
page thereby making room on the original index page for future inserts.

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

Physical delete is performed at garbage collection time.

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

Partitioning Index Sequence


Index-controlled
PART_IX
partitioning
(ACCOUNT_NUM ASC)
101
102
103
104
105
106

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

Copyright IBM Corporation 2011

Figure 2-87. Index-controlled partitioning

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.

2-150 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Creating an index-controlled partitioned table space is a 3-step process.


Step 1: Create the partitioned table space

Step 3: Create the partitioning index

CREATE TABLESPACE USTS7


IN USDB1
NUMPARTS 4
SEGSIZE 0

AT
AT
AT
AT

(199),
(299),
(399),
(499) )

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

Step 2: Create the table

CREATE INDEX PART_IX


ON CUSTOMER
(ACCOUNT_NUM ASC)
CLUSTER
( PARTITION 1 ENDING
PARTITION 2 ENDING
PARTITION 3 ENDING
PARTITION 4 ENDING

CREATE TABLE CUSTOMER (


ACCOUNT_NUM INTEGER,
CUST_LAST_NAME CHAR(30),
LAST_ACTIVITY_DATE DATE,
STATE CHAR(2) )
IN USDB1.USTS7;

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

Non-partitioning index (NPI)

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

There are several ways to convert from index-controlled partitioning to table-controlled


partitioning.

A non-disruptive method is to run ALTER INDEX ixname NOT CLUSTER on the


partitioning index and this converts the table to table-controlled partitioning. The index
remains clustering until another index is explicitly defined with CLUSTER. If you wish, you
could then run ALTER INDEX ixname CLUSTER on the same partitioning index. The
partitioning remains as table-controlled partitioning.
Note

pr

Ex

cl

With table-controlled partitioning, the term partitioning index has a different meaning as we
shall see in a later visual.

2-152 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Table-controlled partitioning partitioning index


(partitioned and non-partitioned)
Has same leftmost columns as columns which partition table
Table-controlled
partitioning

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

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
Partitioning
(ACCOUNT_NUM ASC)
Column
PARTITIONED
Index Definition
CREATE INDEX PART_IX2
ON CUSTOMER
Partitioning
(ACCOUNT_NUM ASC,
Column
STATE ASC)

Copyright IBM Corporation 2011

Figure 2-88. Table-controlled partitioning - partitioning index(partitioned and non-partitioned)

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

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

2-154 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Table-controlled partitioning non-partitioning


non-partitioned secondary index (NPSI)
The index columns do not coincide with the partitioning columns.

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

Copyright IBM Corporation 2011

Figure 2-89. Table-controlled partitioning - non-partitioning non-partitioned secondary index (NPSI)

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

Non-partitioned secondary index and


Partitioned secondary index

Non-partitioned secondary index (NPSI)

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

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-155

Student Notebook

Table-controlled partitioning non-partitioning


partitioned secondary index (DPSI)
The index columns do not coincide with the partitioning columns.

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

Copyright IBM Corporation 2011

Figure 2-90. Table-controlled partitioning - non-partitioning partitioned secondary index (DPSI)

CV8317.0

Notes:

Data partitioned secondary index (DPSI)

cl

The partitioned secondary index SI_2 is generally referred to as a Data Partitioned


Secondary Index (DPSI):
1. Partitioned because of the attribute PARTITIONED

Ex

2. Secondary (or nonpartitioning) because the index contains a column


(LAST_ACTIVITY_DATE) different from the partitioning column (ACCOUNT_NUM). In
some cases, the index columns might be the same as the partitioning columns but
specified in a different order or collating sequence).

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.

2-156 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

DPSIs and utilities


DPSIs can promote efficient utility processing.

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Copyright IBM Corporation 2011

Figure 2-91. Table-controlled partitioning - partition one way and cluster another way

CV8317.0

Notes:

Only one index can be defined as the clustering index on a table.

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.

2-158 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

Partitioned, because the keyword PARTITIONED is specified on the CREATE INDEX


statement.

Partitioning columns determine which partition row goes in.


Clustering Index determines location within partition.
Index PART_IX2

This has the following attributes:

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.

Clustering, because the keyword CLUSTER is specified.


Note

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.

Changing the clustering index

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.
"

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-159

Student Notebook

Table-controlled partitioning catalog entries


SYSIBM.SYSINDEXES

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

-----+---------+---------+---------+---------+-----LIMITKEY LIMITKEY_INTERNAL HEX_LIMITKEY_INTERNAL


-----+---------+---------+---------+---------+-----20000
.+.....
80004E20
30000
......
80007530
40000
. ....
80009C40

SYSIBM.SYSTABLES
---------+---------+--------NAME
PARTKEYCOLNUM
---------+---------+--------EMPLOYEE
1

Copyright IBM Corporation 2011

Figure 2-92. Table-controlled partitioning - catalog entries

CV8317.0

Notes:

Catalog entries for table-controlled partitioning

cl

SYSIBM.SYSINDEXES

Ex

INDEXTYPE

D Data-partitioned secondary index

P Partitioning index on a table that uses table-controlled partitioning

pr

SYSIBM.SYSCOLUMNS
PARTKEY_COLSEQ

The columns numeric position within the tables partitioning key.


The value is 0 if it is not part of the partitioning key.

2-160 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

blank Column is not used as part of a partitioning key

SYSIBM.SYSTABLEPART
LIMITKEY

The high value of the partition in external format


LIMITKEY_INTERNAL

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

The number of columns in the partitioning key.

Catalog entries for index-controlled partitioning

LIMITKEY in external format is held in SYSIBM.SYSTABLEPART but LIMITKEY in internal


format is held in SYSIBM.SYSINDEXPART.

pr

Ex

cl

PARTKEYCOLNUM in SYSIBM.SYSTABLES and PARTKEY_SYSCOLSEQ and


PARTKEY_ORDERING in SYSIBM.SYSCOLUMNS are all blank for index-controlled
partitioning.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-161

pr

Ex

cl

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

Student Notebook

2-162 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

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

2.8. Index compression

pr

Ex

cl

Uempty

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

No dictionary for the compression is required, but a compression


algorithm is used:
Not compressed in buffer pool, logs, and image copies
Prefix compression and RID compression
See details in the notes taken from Redpaper (redp4345)

An index page on disk contains both compressed (leaf pages) and


non-compressed data (non-leaf pages)
The physical 4 KB page on disk is expanded to an
8 KB, 16 KB, or 32 KB page to reside in the buffer pool:

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

Index pages are only updated and logged while in uncompressed


format
Copyright IBM Corporation 2011

Figure 2-93. Index compression

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

Unlike table compression, the compression mechanism implemented for index


compression does not require a compression dictionary. Because DB2 performs
dictionary-less compression, newly created indexes might begin compressing their
contents immediately. The implementation of index compression only compresses the data
in the leaf pages. The goal is to reduce the cost of ownership, hence index pages are
stored on disk in their compressed format (physical 4 KB index page on disk) and are
expanded when read from disk into 8 KB, 16 KB or 32 KB pages. In order to turn on
compression for an index, it must be defined in an 8 KB, 16 KB or 32 KB buffer pool.

2-164 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Table 2-1: sample index


LASTNAME CHAR(20)
Smith
Smith
Smith
Smith

Copyright IBM Corp. 1993, 2011

BIRTHDATE DATE
1980-02-05
1980-04-22
1982-10-11
1984-05-31

FIRSTNAME CHAR(20)
John
Andrea
Bill
Tommie

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-165

Student Notebook

pr

Ex

cl

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

Table 2-1: sample index


LASTNAME CHAR(20)
BIRTHDATE DATE
FIRSTNAME CHAR(20)
Smith
1985-07-09
Susan
Smithers
1980-08-15
Fred
Smithson
1981-12-13
Eric
Stewart
1982-07-05
George
As this example illustrates, the length of the prefix is very often equal to the length of the
first column (if the first column is non-unique), but the prefix may be less than the first
column or it may extend into the second column.

2-166 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

DDL, catalog and utility changes


CREATE INDEX COMPRESS YES|NO:

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

This matches the keyword used for CREATE TABLESPACE


The default is COMPRESS NO

ALTER INDEX COMPRESS YES

Marks the index as REBUILD Pending

New column COMPRESS in SYSIBM.SYSINDEXES:


CHAR(1) NOT NULL WITH DEFAULT N'
Indicates whether or not index compression is active

DSN1PRNT, DSN1COPY

Print the uncompressed contents of an index page

DSN1COMP

Estimates space savings for indexes and recommends a page size of


8 KB, 16 KB, or 32 KB
Copyright IBM Corporation 2011

Figure 2-94. DDL, catalog and utility changes

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

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-167

Student Notebook

header portions of pages are printed. DSN1COPY can copy an 8 KB, 16 KB or 32 KB


IMAGE copy data set to a compressed 4 KB physical VSAM file.

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!

2-168 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Comparison: Index and data compression


Index

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

Compression in buffer pool and log

Yes

No

Compression dictionary

Yes

No (2)

Typical compression ratio

10 to 90%

25 to 75% (3)

Copyright IBM Corporation 2011

Figure 2-95. Comparison: Index and data compression

CV8317.0

Notes:

Use DSN1COMP utility to simulate compression ratio without real index compression.

cl

1. No compression or decompression in each insert or fetch. Instead at I/O time CPU


overhead is critically sensitive to index buffer pool hit ratio. Bigger index buffer pool is
strongly recommended for index compression.

Ex

2. LOAD or REORG is not required for compression.

pr

3. Higher for relatively unique indexes with long keys.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-169

pr

Ex

cl

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

Student Notebook

2-170 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

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

2.9. Additional information

pr

Ex

cl

Uempty

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-171

Student Notebook

Automatic creation of objects (1 of 3)


CREATE TABLE without IN clause

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

Database DSNnnnnn (where n is numeric) implicitly created


PBG UTS implicitly created if CREATE TABLE without PARTITION BY
is specified
PBR UTS implicitly created if CREATE TABLE with PARTITION BY
is specified

System required objects created automatically


(for example, index to enforce primary or unique key)

Only if the table space is created implicitly


For CREATE TABLE without IN or with IN DATABASE clause

CREATE TABLESPACE without IN clause


Database used is DSNDB04
Here there is no automatic creation of objects
Copyright IBM Corporation 2011

Figure 2-96. Automatic creation of objects (1 of 3)

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.

2-172 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

ALTER SEQUENCE SYSIBM.DSNSEQ_IMPLICITDB MAXVALUE 500;

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-173

Student Notebook

Automatic creation of objects (2 of 3)


CREATE TABLE TBx LIKE TBA

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

We assume databases DSN00004 and DSN00005 were explicitly


created in V8 and migrated to DB2 9
CREATE TABLE TB1 LIKETBA
CREATE TABLE TB2 LIKE TBA
CREATE TABLE TB3 LIKE TBA
CREATE TABLE TB4 LIKE TBA
DROP TABLE TB2
CREATE TABLE TB5 LIKE TBA
CREATE TABLE TB6 LIKE TBA
O
O
CREATE TABLE TB9998 LIKE TBA
CREATE TABLE TB9999 LIKE TBA
CREATE TABLE TB10000 LIKE TBA

DSN00001 implicitly created


DSN00002 implicitly created
DSN00003 implicitly created
DSN00006 implicitly created
DSN00002 stays as empty database
DSN00007 implicitly created
DSN00008 implicitly created
O
O
DSN10000 implicitly created
DSN00001 reused. Now has 2 tables
DSN00002 reused. Now has 1 table

Copyright IBM Corporation 2011

Figure 2-97. Automatic creation of objects (2 of 3)

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

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-175

Student Notebook

Automatic creation of objects (3 of 3)

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

CREATE TABLE TT(C1 INTEGER NOT NULL UNIQUE


C2 INTEGER NOT NULL UNIQUE);

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

Figure 2-98. Automatic creation of objects (3 of 3)

CV8317.0

pr

Ex

cl

Notes:

2-176 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

DB2 naming conventions for data sets


DB2 uses the following naming convention for table space and index space

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

vcat.DSNDBx.db.ps.m000y.Lnnn

vcat

High Level Qualifier (STOGROUP VCAT)

C if a cluster, D if a data object

db

Database name (8 characters)

ps

Table space name (8 characters) or first 8 characters of


index space name (the result of scrambling the index name to
ensure uniqueness within the database)

Normally I, can also be J if online REORG is used

Normally 1, can also be 2 if clone tables are used

Lnnn

For segmented table space A001-A032


For partitioned data sets (table space or index), the following:
A001-A999 for partitions 1 to 999
B000-B999 for partitions 1000 to 1999
C000-C999 for partitions 2000 to 2999
D000-D999 for partitions 3000 to 3999
E000-E096 for partitions
4000 to 4096
Copyright IBM Corporation 2011

Figure 2-99. DB2 naming conventions for data sets

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

The naming convention is of the form a.b.c.d.e000n.f where:

a = ICF catalog name or alias


b = DSNDBD for VSAM data and DSNDBC for VSAM cluster
c = Database name
d = Table space name or index space name
e = I or J, followed by a 4 digit number 000n and the last digit n can be 1 or 2
f = The data set number

pr

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

If you DROP a table:

Dependent objects, referential constraints, catalog definitions, statistics and cached


dynamic SQL are lost
Dependent plans / packages invalidated
DB2 reclaims space if table is in a segmented table space

Check for table dependencies in SYSVIEWDEP, SYSPLANDEP,


SYSPACKDEP, SYSINDEXES and SYSTABAUTH
Copyright IBM Corporation 2011

Figure 2-100. Object dependencies

CV8317.0

Notes:

Implications of dropping a table

As a result of, say, DROP TABLE PROJECT:

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

2-178 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Checking for dependencies


SYSVIEWDEP, SYSPLANDEP and SYSPACKDEP indicate what views, application plans
and packages are dependent on different DB2 objects. Also, SYSINDEXES tells you what
indexes currently exist on a table and SYSTABAUTH tells you which users are authorized
to use the table.

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.

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

2 CREATE DATABASE DSN8D2AP

STOGROUP DSN8G200
BUFFERPOOL BP1

CREATE TABLE MYEMP (C INTEGER)

IN DATABASE DSN8D2AP

3 CREATE DATABASE DSN8D2AP

STOGROUP DSN8G200
BUFFERPOOL BP1

CREATE TABLESPACE DSN8SEMP

TABLE

IN DSN8D2AP
USING STOGROUP DSN8G200

CREATE TABLE YREMP (C INTEGER)

1.

TEMPL

2.

MYEMP

3.

YREMP

4.

TDEPT

TS

DB

SG

IN DSN8SEMP

4 CREATE DATABASE DSN8D2AP

CREATE TABLESPACE DSN8SDEP

IN DSN8D2AP

CREATE TABLE TDEPT (C INTEGER)

IN DSN8D2AP.DSN8SDEP

Copyright IBM Corporation 2011

Figure 2-101. Checkpoint quiz

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.

2-180 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Copyright IBM Corporation 2011

Figure 2-102. Unit summary

CV8317.0

pr

Ex

cl

Notes:

Copyright IBM Corp. 1993, 2011

Unit 2. Setting up a DB2 database

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

2-181

pr

Ex

cl

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

Student Notebook

2-182 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Unit 3. Implementing unique and referential


requirements
What this unit is about

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

This unit describes referential integrity. Following this, there is a


discussion about identity columns and sequence objects.

What you should be able to do

After completing this unit, you should be able to:

cl

Describe the concepts of:


- PRIMARY KEY
- UNIQUE KEY and
- FOREIGN KEY
Implement these keys in DB2
Describe the concepts of:
- REFERENTIAL CONSTRAINT and
- REFERENTIAL INTEGRITY
Implement REFERENTIAL CONSTRAINTS on
New tables
Existing tables
- With the appropriate DELETE rules
Drop REFERENTIAL CONSTRAINTS and their enforcing indexes
Query the appropriate catalog tables for RI details
Describe the relationships that require special handling
Describe what informational constraints are and their use
Discuss identity columns and sequence objects

Accountability:

Machine exercises to implement referential constraints in the


CV831 case study.

pr

Ex

How you will check your progress

References

SC19-2968

DB2 10 for z/OS Administration Guide

SC19-2972

DB2 10 for z/OS Command Reference

SC19-2983

DB2 10 for z/OS SQL Reference

Copyright IBM Corp. 1993, 2011

Unit 3. Implementing unique and referential requirements

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-1

Student Notebook

DB2 Version 9.1 for z/OS Administration Guide

SC18-9844

DB2 Version 9.1 for z/OS Command Reference

SC18-9854

DB2 Version 9.1 for z/OS SQL Reference

SC18-7413

DB2 UDB for z/OS Version 8 Administration Guide

SC18-7416

DB2 UDB for z/OS Version 8 Command Reference

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

SC18-9840

DB2 UDB for z/OS Version 8 SQL Reference

pr

Ex

cl

SC18-7426

3-2

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Figure 3-1. Unit objectives

CV8317.0

pr

Ex

cl

Notes:

Copyright IBM Corp. 1993, 2011

Unit 3. Implementing unique and referential requirements

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

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

3.1. Referential integrity

pr

Ex

cl

Uempty

Copyright IBM Corp. 1993, 2011

Unit 3. Implementing unique and referential requirements

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Column (or set of columns)


Identifies a row
Distinguishes it from every other occurrence of a row

Must be UNIQUE
Cannot be NULL
Only ONE per table

Copyright IBM Corporation 2011

Figure 3-2. Primary key

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 table can have only ONE primary key.

A primary key must be UNIQUE. You can have more columns than are necessary to
achieve uniqueness, although this is generally not recommended.

pr

A primary key cannot be NULL.

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

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

Copyright IBM Corp. 1993, 2011

Unit 3. Implementing unique and referential requirements

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

You can also define any number of unique keys


Ensure no two values are equal

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

Figure 3-3. Unique key

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

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Column (or set of columns)


Associated with a primary or unique key
That is, a parent key

Foreign
Key

Association is called a REFERENTIAL CONSTRAINT


DB2 guarantees that non-null foreign key values always refer to
parent key values
Copyright IBM Corporation 2011

Figure 3-4. 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

This association is called a REFERENTIAL CONSTRAINT.


A primary key or unique key of a REFERENTIAL CONSTRAINT is also known as a parent
key.

Copyright IBM Corp. 1993, 2011

Unit 3. Implementing unique and referential requirements

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

Once a REFERENTIAL CONSTRAINT is defined, then DB2 guarantees that non-null


foreign key values always refer to parent key values.

Referential integrity

REFERENTIAL INTEGRITY is the automatic enforcement of all REFERENTIAL


CONSTRAINTS.

Foreign key index

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

A foreign key index is optional but strongly recommended.

3-10 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Foreign key - column considerations

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

A foreign key referencing a parent key consisting of


more than one column must be created with the same:
Number and
Sequence of columns

Foreign
Key

Each corresponding column must have the same:


Data type and
Size

Column names, default values, and null attributes may be different


Copyright IBM Corporation 2011

Figure 3-5. Foreign key - column considerations

CV8317.0

Notes:

More than one column

Dependent Table
--------------...
...
...
...
Col5
...
...
Col8

pr

Ex

cl

Parent Table
-----------Col1
...
Col3
...
...
...
...
...

Parent Key: (Col1,Col3)

Foreign Key: (Col8,Col5)

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

Unit 3. Implementing unique and referential requirements

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-11

Student Notebook

Referential integrity - DB2 implementation


PARENT KEY

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

You can update the PK value of a row


in a parent table provided that there
are no dependent rows.
The new value must obey the entity
integrity rule.
Updating PKs is not recommended.

You can update the FK of a row in a


dependent table to a value that is
NULL (if permitted) or to a value of its
PK.

You specify an explicit DELETE rule: OK


RESTRICT
CASCADE
SET NULL
NO ACTION
Copyright IBM Corporation 2011

Figure 3-6. Referential integrity - DB2 implementation

CV8317.0

Notes:

Referential constraints affect:

cl

INSERTs
UPDATEs
DELETEs

Ex

They do not affect SELECTs.

Certain rules define the referential constraints to be met between parent tables and
dependent tables.

pr

These rules are defined for each FK relationship.

In most cases, the rule is implicit. In one case (the DELETE rule for PKs), you choose the
rule explicitly.

3-12 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Copyright IBM Corp. 1993, 2011

Unit 3. Implementing unique and referential requirements

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-13

Student Notebook

Creating a referential constraint


On new tables

On existing tables

For the parent table

For the parent table

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

Create a unique index on the


columns that will be the:
Primary Key and / or
Unique Keys
Use ALTER TABLE to define the:
Primary Key constraint and / or
Unique Key constraints

Use CREATE TABLE to define the:


Primary Key constraint and / or
Unique Key constraints
Create a unique index on the columns
that will be the:
Primary Key and / or
Unique Keys

For the dependent table

For the dependent table

Use CREATE TABLE to define the:


Foreign Key constraint and
Delete Rule

Optional but recommended

Create an index on the columns that


will be the:
Foreign key

Use ALTER TABLE to define the:


Foreign Key constraint and
Delete rule

Optional but recommended

Create an index on the columns that


will be the:
Foreign key

Copyright IBM Corporation 2011

Figure 3-7. Creating a referential constraint

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

An incomplete table can be altered or dropped, or an index can be created.

3-14 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Creating the primary key and primary index

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

CREATE TABLE DEPT


(DEPTNO
CHAR(3)
NOT NULL,
DEPTNAME
VARCHAR(36) NOT NULL,
MGRNO
CHAR(96),
ADMRDEPT
CHAR(3)
NOT NULL,
LOCATION
CHAR(16),
CONSTRAINT PKDEPT PRIMARY KEY (DEPTNO))
IN EDUD001C.CV831DEV;
--

CREATE UNIQUE INDEX XDEPT ON DEPT (DEPTNO);

Copyright IBM Corporation 2011

Figure 3-8. Creating the primary key and primary index

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.

Copyright IBM Corp. 1993, 2011

Unit 3. Implementing unique and referential requirements

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-15

Student Notebook

Creating unique keys and foreign keys

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

CREATE TABLE EMP


(EMPNO
CHAR(6)
NOT NULL,
FIRSTNME
VARCHAR(12) NOT NULL,
MIDINIT
CHAR(1)
NOT NULL WITH DEFAULT,
LASTNAME
VARCHAR(15) NOT NULL,
NI_NUMBER CHAR(6)
NOT NULL,
WORKDEPT
CHAR(3),
PHONENO
CHAR(6),
HIREDATE
DATE,
JOB
CHAR(8),
EDLEVEL
SMALLINT,
SEX
CHAR(1),
BIRTHDATE DATE,
SALARY
DECIMAL(9,2),
BONUS
DECIMAL(9,2),
COMM
DECIMAL(9,2),
CONSTRAINT RED FOREIGN KEY (WORKDEPT)
REFERENCES DEPT
ON DELETE SET NULL,
CONSTRAINT UKNI_NUMBER UNIQUE (NI_NUMBER))
IN EDUD001C.CV831DEV;
--

CREATE INDEX XWORKDEPT

ON EMP

(WORKDEPT);

--

CREATE UNIQUE INDEX XNI_NUMBER

ON EMP

(NI_NUMBER);

Copyright IBM Corporation 2011

Figure 3-9. Creating unique keys and foreign keys

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.

3-16 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

Copyright IBM Corp. 1993, 2011

Unit 3. Implementing unique and referential requirements

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-17

Student Notebook

Creating informational referential constraint


NULL,
NULL,
NULL WITH DEFAULT,
NULL,
NULL,

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

CREATE TABLE EMP


(EMPNO
CHAR(6)
NOT
FIRSTNME
VARCHAR(12) NOT
MIDINIT
CHAR(1)
NOT
LASTNAME
VARCHAR(15) NOT
NI_NUMBER CHAR(6)
NOT
WORKDEPT
CHAR(3),
PHONENO
CHAR(6),
HIREDATE
DATE,
JOB
CHAR(8),
EDLEVEL
SMALLINT,
SEX
CHAR(1),
BIRTHDATE DATE,
SALARY
DECIMAL(9,2),
BONUS
DECIMAL(9,2),
COMM
DECIMAL(9,2),
CONSTRAINT RED FOREIGN KEY
REFERENCES DEPT
NOT ENFORCED;

(WORKDEPT)

Informational RI is not enforced by database manager and is ignored by most utilities


Informational RI is always used by the optimizer in query rewrite

Copyright IBM Corporation 2011

Figure 3-10. Creating informational referential constraint

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

For example, CHECK DATA and LOAD ignore these constraints.


QUIESCE TABLESPACESET recognizes these constraints by quiescing all table spaces
related to the specified table space.

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

3-18 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

NOT ENFORCED option of the referential constraint definition in a CREATE TABLE or


ALTER TABLE statement.
Informational referential constraints are often useful, especially in a data warehouse
environment, for several reasons:
To avoid the overhead of enforcement by DB2.

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.

To allow more queries to qualify for automatic query rewrite.

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.

Copyright IBM Corp. 1993, 2011

Unit 3. Implementing unique and referential requirements

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-19

Student Notebook

Adding primary key constraints


Use ALTER TABLE

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

A unique index must exist on the specified columns before a


Primary Key constraint can be added
Example

CREATE TABLE DEPT


(DEPTNO
CHAR(3)
NOT NULL,
DEPTNAME
VARCHAR(36) NOT NULL,
MGRNO
CHAR(96),
ADMRDEPT
CHAR(3)
NOT NULL,
LOCATION
CHAR(16))
IN EDUD001C.CV831DEV;
--

CREATE UNIQUE INDEX XDEPT ON DEPT (DEPTNO);


--

ALTER TABLE DEPT


ADD CONSTRAINT PKDEPT PRIMARY KEY (DEPTNO);

Copyright IBM Corporation 2011

Figure 3-11. Adding primary key constraints

CV8317.0

pr

Ex

cl

Notes:

3-20 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Adding unique and foreign key constraints


Use ALTER TABLE

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

A unique index must exist on the specified columns before a


Unique Key constraint can be added
Example

CREATE TABLE EMP


(EMPNO
CHAR(6)
NOT
FIRSTNME
VARCHAR(12) NOT
MIDINIT
CHAR(1)
NOT
LASTNAME
VARCHAR(15) NOT
NI_NUMBER CHAR(6)
NOT
WORKDEPT
CHAR(3),
PHONENO
CHAR(6),
HIREDATE
DATE,
JOB
CHAR(8),
EDLEVEL
SMALLINT,
SEX
CHAR(1),
BIRTHDATE DATE,
SALARY
DECIMAL(9,2),
BONUS
DECIMAL(9,2),
COMM
DECIMAL(9,2))
IN EDUD001C.CV831DEV;

NULL,
NULL,
NULL WITH DEFAULT,
NULL,
NULL,

--

CREATE UNIQUE INDEX XNI_NUMBER ON EMP (NI_NUMBER);


CREATE INDEX XWORKDEPT ON EMP (WORKDEPT);
--

ALTER TABLE EMP


ADD CONSTRAINT UKNI_NUMBER UNIQUE(NI_NUMBER);
ALTER TABLE EMP
ADD CONSTRAINT RED FOREIGN KEY(WORKDEPT)
REFERENCES DEPT
ON DELETE SET NULL;
Copyright IBM Corporation 2011

Figure 3-12. Adding unique and foreign key constraints

CV8317.0

pr

Ex

cl

Notes:

Copyright IBM Corp. 1993, 2011

Unit 3. Implementing unique and referential requirements

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

ALTER TABLE DEPT


DROP CONSTRAINT PKDEPT;

Dropping a Unique Key Constraint


Example

ALTER TABLE EMP


DROP CONSTRAINT UKNI_NUMBER;

Dropping a Foreign Key Constraint


Example

ALTER TABLE EMP


DROP CONSTRAINT RED;

Copyright IBM Corporation 2011

Figure 3-13. Dropping constraints

CV8317.0

Notes:

The visual shows how the DROP CONSTRAINT clause of the ALTER TABLE statement
can be used to drop:

Ex

cl

Primary key constraints


Unique key constraints
Referential constraints

Additionally, a unique key constraint can be dropped as follows:

pr

ALTER TABLE....
DROP UNIQUE UKNI_NUMBER;

3-22 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Restrictions on dropping indexes


Indexes which enforce either a Primary Key or a Unique Key cannot be dropped
until the constraint is dropped
Primary
Index
XDEPT

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

Example

DROP INDEX XDEPT;

---------+---------+---------+---------+---------+---------+---------+--DSNT408I SQLCODE = -669, ERROR: THE OBJECT CANNOT BE EXPLICITLY DROPPED.


REASON 0002
DSNT418I SQLSTATE
= 42917 SQLSTATE RETURN CODE
DSNT415I SQLERRP
= DSNXIDIX SQL PROCEDURE DETECTING ERROR
DSNT416I SQLERRD
= 500 0 0 -1 0 0 SQL DIAGNOSTIC INFORMATION
DSNT416I SQLERRD
= X'000001F4' X'00000000' X'00000000' X'FFFFFFFF'
X'00000000' X'00000000' SQL DIAGNOSTIC INFORMATION
---------+---------+---------+---------+---------+---------+---------+---

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

To remove an index that supports a


constraint, the constraint itself must
first be dropped

ALTER TABLE DEPT


DROP CONSTRAINT PKDEPT;
---------+---------+---------+---------+---------+---------+------DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0
---------+---------+---------+---------+---------+---------+------DROP INDEX XDEPT;
---------+---------+---------+---------+---------+---------+------DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0
---------+---------+---------+---------+---------+---------+-------

Copyright IBM Corporation 2011

Figure 3-14. Restrictions on dropping indexes

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.

Copyright IBM Corp. 1993, 2011

Unit 3. Implementing unique and referential requirements

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Foreign Key constraints are recorded in two catalog tables:


1 SYSIBM.SYSRELS

---------+---------+---------+---------+---------+---------+-----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

Other details of RI definitions are recorded in:


SYSIBM.SYSTABLES, SYSIBM.SYSCOLUMNS and SYSIBM.SYSINDEXES
Copyright IBM Corporation 2011

Figure 3-15. Catalog information

CV8317.0

Notes.

The visual shows where details of DB2 constraints are recorded.

cl

Primary key and unique key constraints

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

Foreign key constraints

SYSIBM.SYSRELS contains one row for every foreign key constraint.

3-24 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Other information recorded about RI definitions


SYSIBM.SYSTABLES contains:
STATUS

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

The column's numeric position within the table's primary key.

SYSIBM.SYSINDEXES contains:
UNIQUERULE

P means that the index is a primary index.

C means that the index is used to enforce a unique constraint.

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.

Copyright IBM Corp. 1993, 2011

Unit 3. Implementing unique and referential requirements

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Figure 3-16. Referential structures

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

The DELETE rule must be CASCADE.


Multipath

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

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Example: Potential problem with a table if it were allowed to be delete-connected to itself:

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
|
---------------->------------------>----------------->

Issue the following SQL:

DELETE FROM T3 WHERE FKT2 IS NULL;

The result depends on the sequence in which DB2 accesses table T3

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.

Copyright IBM Corp. 1993, 2011

Unit 3. Implementing unique and referential requirements

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

Write user code to provide it

How big should an RI structure be?

Limit it to the size of the business meaning


Do not connect applications via code tables
Do not mix structures in one table space

Create an Index on the Foreign Key


Allow at least one nullable Foreign Key in a cycle

Copyright IBM Corporation 2011

Figure 3-17. Additional considerations

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.

3-28 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

TABLESPACESET concept
Table space TS1

Table space TS2 Table space TS3


Dependent Table TAB3

Dependent Table TAB6

Dependent Table TAB2

Dependent Table TAB5

Parent Table TAB4

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

Parent Table TAB1

One Table space set


Two Referential Structures
Copyright IBM Corporation 2011

Figure 3-18. TABLESPACESET concept

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.

Copyright IBM Corp. 1993, 2011

Unit 3. Implementing unique and referential requirements

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

DSNUPORT - REPORT UTILITY COMPLETE - ELAPSED TIME=00:00:00


DSNUGBAC - UTILITY EXECUTION COMPLETE, HIGHEST RETURN CODE=0
Copyright IBM Corporation 2011

Figure 3-19. REPORT TABLESPACESET

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.

3-30 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

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

CHECK PENDING restrictive state

When DB2 suspects an RI violation,


it puts the table space in the
CHECK PENDING state.

No SQL DML access allowed

(57011/-904 + reason code 00C900A3)

The CHECK DATA utility can:

Verify RI and reset CHECK PENDING state if no RI violation


Verify RI, delete violating rows and reset CHECK PENDING state

Copyright IBM Corporation 2011

Figure 3-20. CHECK PENDING restrictive state

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.

Copyright IBM Corp. 1993, 2011

Unit 3. Implementing unique and referential requirements

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-31

pr

Ex

cl

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

Student Notebook

3-32 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

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

3.2. Generating unique values

pr

Ex

cl

Uempty

Copyright IBM Corp. 1993, 2011

Unit 3. Implementing unique and referential requirements

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

CREATE TABLE EMPLOYEE


(EMPNO INTEGER GENERATED ALWAYS
AS IDENTITY (START WITH 1,
INCREMENT BY 1,
CACHE 20),
NAME
CHAR(30),
SALARY DECIMAL(8,2),
DEPTNO SMALLINT);
INSERT INTO EMPLOYEE
(NAME, SALARY, DEPTNO)
VALUES (Kate, 100000.00, 50);

VALUES IDENTITY_VAL_LOCAL()
INTO :IVAR;

Can avoid concurrency and


performance problems
Useful for generating
unique primary key values

Copyright IBM Corporation 2011

Figure 3-21. Identity columns

CV8317.0

Notes:

An identity column is defined using the AS IDENTITY clause of the CREATE TABLE
statement.

cl

A table can have only one identity column.

pr

Ex

CREATE TABLE ... ... colname datatype ...


GENERATED ALWAYS / BY DEFAULT
AS IDENTITY
(START WITH n,
INCREMENT BY n,
CACHE n / NO CACHE,
CYCLE / NO CYCLE,
MINVALUE n / NO MINVALUE,
MAXVALUE n / NO MAXVALUE,
ORDER / NO ORDER)

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

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

NO MINVALUE and NO MAXVALUE are the defaults if either MINVALUE or MAXVALUE is


not specified.

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.

Copyright IBM Corp. 1993, 2011

Unit 3. Implementing unique and referential requirements

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

INCREMENT BY can be set to zero.

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.

3-36 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Identity column considerations


New LIKE clause option for CREATE TABLE

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

CREATE TABLE T2 LIKE T1 INCLUDING IDENTITY COLUMN


ATTRIBUTES

Adding an identity column to a table

ALTER TABLE T ADD <column> GENERATED ... AS IDENTITY

Overriding identity column values when using INSERT with


subselect
INSERT INTO T2
OVERRIDING USER VALUE
SELECT * FROM T1

Altering the characteristics of identity columns

ALTER TABLE <table> ALTER COLUMN <column>

Unload and reload a table which has identity column


Copyright IBM Corporation 2011

Figure 3-22. Identity column considerations

CV8317.0

Notes:

Identity column considerations

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

Copyright IBM Corp. 1993, 2011

Unit 3. Implementing unique and referential requirements

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-37

Student Notebook

You can alter the characteristics of an identity column by using the


ALTER TABLE ... ALTER COLUMN ... statement.
If you specify GENERATED ALWAYS and later have a requirement to unload or reload
your tables, you can:
ALTER TABLE ... ALTER COLUMN ... SET GENERATED BY DEFAULT

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

Unload the table


Reload the table

pr

Ex

cl

ALTER TABLE ... ALTER COLUMN ... SET GENERATED ALWAYS

3-38 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

DB2 sequence object (1 of 2)


A stored user-defined object that generates next ASC / DESC value
Values generated can be:
CREATE SEQUENCE SEQTEST1
AS INTEGER
START WITH 1
INCREMENT BY 1
MINVALUE 0
MAXVALUE 5
CYCLE
CACHE 5
NO ORDER;

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

SMALLINT, INTEGER, DECIMAL


(With 0 scale)

ALTER SEQUENCE SEQTEST1


RESTART WITH 100
INCREMENT BY 100
MAXVALUE 1000000;

DROP SEQUENCE
COMMENT ON SEQUENCE
GRANT / REVOKE ON SEQUENCE
NEXT VALUE FOR and PREVIOUS VALUE FOR

Copyright IBM Corporation 2011

Figure 3-23. DB2 sequence object (1 of 2)

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

Unit 3. Implementing unique and referential requirements

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

3-40 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

DB2 sequence object (2 of 2)


CREATE SEQUENCE MYSEQ START WITH 1 INCREMENT BY 1
Examples

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

Example 1

VALUES NEXT VALUE FOR MYSEQ INTO :HV .. Generates value of 1


VALUES NEXT VALUE FOR MYSEQ INTO :HV .. Generates value of 2
COMMIT;
VALUES PREVIOUS VALUE FOR MYSEQ INTO :HV .. Returns most recently generated
value of 2

Example 2

UPDATE T SET C1 = (SELECT PREVIOUS VALUE FOR MYSEQ FROM T);


UPDATE T SET C1 = NEXT VALUE FOR MYSEQ;

Example 3

SELECT * FROM FINAL TABLE


(INSERT INTO TESTTAB (KEYVALUE, TESTSEQ)
VALUES ( NEXT VALUE FOR MYSEQ,
NEXT VALUE FOR MYSEQ2 ))

Ranges and cycles


Generating constants
Duplicate sequences
Caching
Gaps
Catalog

Copyright IBM Corporation 2011

Figure 3-24. DB2 sequence object (2 of 2)

CV8317.0

Notes:

Example 1

The sequence is simply being incremented by 1.

cl

Each subsequent VALUES statement generates the next available sequence.

Ex

The COMMIT has no effect on the sequence generation.

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.

Copyright IBM Corp. 1993, 2011

Unit 3. Implementing unique and referential requirements

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-41

Student Notebook

Ranges and cycles


A sequence can be optionally defined to CYCLE after reaching its maximum value (or
minimum if descending).
- The sequence will wrap around to the other end of the range and start a new cycle
of values.

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

- The default is NOCYCLE.

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

CREATE SEQUENCE consequence AS INTEGER


START WITH 1
INCREMENT BY 0
MINVALUE 0
MAXVALUE 5
CYCLE
CACHE 5
NO ORDER;

A constant, non-changing sequence can be generated by specifying an INCREMENT


BY keyword set to 0 (zero). Every sequence generated would be the same as the
START WITH value, the first sequence generated. The MAXVALUE or high value of the
data type could never be reached, and a cycle would never be performed for this
sequence if the CYCLE keyword were specified. The START WITH value would have to
be within the MINVALUE and MAXVALUE keyword values.

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

3-42 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

I/O to table only required when cached values are exhausted

- Recommended value is 20

Assigning a higher value gives no benefit and increases the size of gap should a
failure occur

- Use NOCACHE if values must be assigned in strict numeric order

Each assignment of sequence value results in update of catalog

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

- SYSIBM.SYSSEQUENCES (in SYSIBM.SYSSEQ table space)


Records all data about SEQUENCE attributes
MAXASSIGNEDVAL column: last possible assigned value

Columns:
- PRECISION: Precision of sequence object's decimal or other numeric data
type
Copyright IBM Corp. 1993, 2011

Unit 3. Implementing unique and referential requirements

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-43

Student Notebook

- RESTARTWITH: Restart with numeric constant value specified during ALTER


of the sequence or NULL
- SYSIBM.SYSSEQUENCESDEP (in SYSIBM.SYSSEQ2 table space)
Columns:
- DTYPE / DSCHEMA

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

- BSCHEMA / BNAME

- SYSIBM.SYSSEQUENCEAUTH (in SYSIBM.SYSSEQ2 table space)


Columns:

- GRANTOR / GRANTEE

- SCHEMA / NAME

Spreading inserted rows across multiple partitions

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

As an alternative, and because a sequence is independent and not associated to any


particular column, all or part of the generated sequence value could be used as part of the
partition key in a attempt to spread the rows evenly across multiple partitions of a
partitioned table space.

3-44 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Identity columns versus sequences


Identity Columns

Sequences

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

Associated with one specific column in Stand-alone object


one specific table
One to one relationship between
Multiple sequences can be used in one
identity column and table
table
One sequence can be used in many
tables
One sequence can be used multiple
times in one SQL stmt to supply values
for multiple columns in one table
SELECT... IDENTITY_VAL_LOCAL
Within agents scope only
ALTER TABLE....ALTER COLUMN

SELECT....NEXT VALUE FOR /


PREVIOUS VALUE FOR
SELECT....(INSERT.......)
ALTER SEQUENCE

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

Figure 3-25. Identity columns versus sequences

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

Unit 3. Implementing unique and referential requirements

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-45

Student Notebook

As discussed earlier, to retrieve a value supplied by an identity, that column should be


SELECTed or retrieved with the IDENTITY_VAL_LOCAL function. The sequence object
used can also be displayed as it is used in an INSERT SQL statement using a SELECT...
FROM FINAL TABLE(INSERT ).

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.

3-46 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Figure 3-26. Unit summary

CV8317.0

pr

Ex

cl

Notes:

Copyright IBM Corp. 1993, 2011

Unit 3. Implementing unique and referential requirements

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

3-47

pr

Ex

cl

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

Student Notebook

3-48 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Unit 4. Getting data into and out of DB2


What this unit is about

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

This unit describes how to get your data into and out of DB2.

What you should be able to do

After completing this unit, you should be able to:

Describe how DB2 utilities may be executed, monitored,


terminated, and restarted
Run the DB2 LOAD utility and make full use of its many features
Run the CHECK DATA utility to check DB2 table space data for any
violations of referential integrity constraints or table check
constraints
Run the DB2 UNLOAD utility, and make full use of its many
features

How you will check your progress


Accountability:
Machine exercises

SC19-2983

DB2 10 for z/OS SQL Reference

SC19-2984

DB2 10 for z/OS Utility Guide and Reference

SC19-2972

DB2 10 for z/OS Command Reference

SC18-9854

DB2 Version 9.1 for z/OS SQL Reference

SC18-9855

DB2 Version 9.1 for z/OS Utility Guide and Reference

SC18-9844

DB2 Version 9.1 for z/OS Command Reference

SC18-7426

DB2 UDB for z/OS Version 8 SQL Reference

SC18-7427

DB2 UDB for z/OS Version 8 Utility Guide and


Reference

SC18-7416

DB2 UDB for z/OS Version 8 Command Reference

pr

Ex

cl

References

Copyright IBM Corp. 1993, 2011

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Copyright IBM Corporation 2011

Figure 4-1. Unit objectives

CV8317.0

pr

Ex

cl

Notes:

4-2

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

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

4.1. Introduction to DB2 utilities

pr

Ex

cl

Uempty

Copyright IBM Corp. 1993, 2011

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-3

Student Notebook

Generating utility JCL


EDIT userid.UTIL.SYSIN(LOAD)
Command ===> _________________

DB2I PRIMARY OPTION MENU

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

Copyright IBM Corporation 2011

Utility JCL

Figure 4-2. Generating utility JCL

CV8317.0

Notes:

Generating Utility JCL using DB2I is done in two steps:

Step 1: Create a sequential data set (or PDS member) with control statements.

cl

Step 2: Create the Utility JCL in DB2I.

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

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

DB2 utility panel (1 of 2)


DB2 UTILITIES

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

(SUBMIT job, EDITJCL, DISPLAY, TERMINATE)


(A unique job identifier string)
(CHECK DATA, CHECK INDEX, CHECK LOB,
COPY, DIAGNOSE, LOAD, MERGE, MODIFY,
QUIESCE, REBUILD, RECOVER, REORG INDEX,
REORG LOB, REORG TABLESPACE, REPORT,
REPAIR, RUNSTATS, STOSPACE, UNLOAD)
4 STATEMENT DATA SET ===> UTIL.SYSIN(LOAD)
5 RESTART

===> NO

(NO, CURRENT, PHASE or PREVIEW)

6 LISTDEF? (YES|NO) ===> NO

TEMPLATE? (YES|NO) ===> NO

7 lib == >

(BLANK or DB2 Library name).

* 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

Copyright IBM Corporation 2011

Figure 4-3. DB2 utility panel (1 of 2)

CV8317.0

Notes:

When choosing option 8 in DB2I, this is what you will see.

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.

Copyright IBM Corp. 1993, 2011

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-5

Student Notebook

DB2 utility panel (2 of 2)


DATA SET NAMES

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

Copyright IBM Corporation 2011

Figure 4-4. DB2 utility panel (2 of 2)

CV8317.0

Notes:

pr

Ex

cl

Some utilities require you to fill this panel.

4-6

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Figure 4-5. DSNUxxx.CNTL

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

Copyright IBM Corp. 1993, 2011

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-7

Student Notebook

pr

Ex

cl

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

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

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

DSNUPROC

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

//DSNUPROC PROC LIB='DSN1010.SDSNLOAD',


//
SYSTEM=DSN1,
//
SIZE=0K,UID='',UTPROC=''
//*
//*******************************************************************************
//*
*
//* PROCEDURE-NAME:
DSNUPROC
*
//*
*
//* DESCRIPTIVE-NAME:
UTILITY PROCEDURE
*
//*
*
//* FUNCTION:
THIS PROCEDURE INVOKES THE ADMF UTILITIES IN THE
*
//*
BATCH ENVIRONMENT
* //*
//*******************************************************************************
//*
//DSNUPROC EXEC PGM=DSNUTILB,REGION=&SIZE,
//
PARM='&SYSTEM,&UID,&UTPROC'
//STEPLIB DD
DSN=&LIB,DISP=SHR
//*
//**********************************************************************
//*
*
//* THE FOLLOWING DEFINE THE UTILITIES' PRINT DATA SETS
*
//*
*
//**********************************************************************
//*
//SYSPRINT DD
SYSOUT=*
//UTPRINT DD
SYSOUT=*
//SYSUDUMP DD
SYSOUT=*
//*DSNUPROC PEND
REMOVE * FOR USE AS INSTREAM PROCEDURE
*

Copyright IBM Corporation 2011

Figure 4-6. DSNUPROC

CV8317.0

Notes:

This is the DSNUPROC cataloged procedure.

The DB2I generated job stream will invoke this procedure.

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

All online utilities will be invoked through this common program.

Copyright IBM Corp. 1993, 2011

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-9

Student Notebook

SYSIBM.SYSUTILX

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

A DB2 directory table


JOB
NAME

UTILID

UTILITY
NAME

CHECKPOINT
INFORMATION...

Unique Identifier

Row inserted at Utility Start

Row updated at Utility Checkpoint


Utility commit and
Utility Stop

Row deleted at Utility Termination

Copyright IBM Corporation 2011

Figure 4-7. SYSIBM.SYSUTILX

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.

4-10 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

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

DB2 utility commands

-DIS UTIL(CF8CHKD)

DSNU105I

DSN9022I

DSNUGDIS - USERID = BEDB2M


UTILID = CF8CHKD
PROCESSING UTILITY STATEMENT 1
UTILINIT = CHECKDAT
PHASE = UTILITY COUNT 0
STATUS = ACTIVE
DSNUGCCC '-DIS UTIL' NORMAL COMPLETION

STATUS can be:


ACTIVE
STOPPED
TERMINATING

-TERM UTIL(CF8CHKD)

DSNU166I

DSN9022I

DSNUGTER - CHECK DATA UTILITY


UTILID = CF8CHKD NOT EXECUTING,
CLEANUP COMPLETE
DSNUGCCC '-TERM UTIL' NORMAL COMPLETION

All resources are released

Copyright IBM Corporation 2011

Figure 4-8. DB2 utility commands

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

A utility is identified by a UID (Utility ID).

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

Be careful with the -TERM UTIL command.

Copyright IBM Corp. 1993, 2011

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-11

Student Notebook

DB2 utility flow


Utility #2
Utility started
* Row added to SYSUTILX
(key = utilid)

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

Copyright IBM Corporation 2011

Figure 4-9. DB2 utility flow

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

4-12 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

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

4.2. The LOAD utility

pr

Ex

cl

Uempty

Copyright IBM Corp. 1993, 2011

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-13

Student Notebook

Loading data into one table


POSITION(36)
CHAR(6)

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

input field type and length

input field location


target column name

Copyright IBM Corporation 2011

Figure 4-10. Loading data into one table

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.

4-14 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

Copyright IBM Corp. 1993, 2011

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-15

Student Notebook

Data type conversion


Suppose that:

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

MGRNO was defined as INTEGER in the DEPT table


The input data set is identical to the one in the previous example
Character format

0 . . . . 1 . . . . 2 . . . . 3 . . . . 4 . . .
E21

SOFTWARE SUPPORT

031435

E01

Defined as INTEGER when


CREATE TABLE was performed

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

Copyright IBM Corporation 2011

Figure 4-11. Data type conversion

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

The LOAD utility will perform the necessary data conversion.

4-16 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Data conversion
Numeric Data Conversion
SMALLINT
INTEGER
BIGINT
DECIMAL
FLOAT
DECFLOAT

Output Data Types

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

Output Data Types

Character Data Conversion


Input
Data
Types

CHAR
VARCHAR
GRAPHIC
VARGRAPHIC
BINARY
VARBINARY

CHAR
D
Y
Y*
Y*
N
N

VARCHAR GRAPHIC VARGRAPHIC BINARY


Y
Y*
Y*
Y
D
Y*
Y*
Y
Y*
D
Y
N
Y*
Y
D
N
N
N
N
D
N
N
N
Y

Time Data Conversion


Input
Data
Types

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

Output Data Types


TIME
N
D
Y

TIMESTAMP
N
N
D

Copyright IBM Corporation 2011

Figure 4-12. Data conversion

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

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-17

Student Notebook

Loading NULL values


Setting columns to NULLs

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

The MGRNO column


allows NULLs

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)

Copyright IBM Corporation 2011

Figure 4-13. Loading NULL values

CV8317.0

Notes:

Column allows NULLs

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.

NOT NULL WITH DEFAULT

pr

With this table definition...

CREATE TABLE TAB1


(COL1 INTEGER NOT NULL WITH DEFAULT,
............
............

4-18 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

Copyright IBM Corp. 1993, 2011

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-19

Student Notebook

Loading multiple tables in a table space


LOAD Utility Control Statement

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

LOAD DATA INDDN SYSREC


RESUME NO
DISCARDDN SYSDISC
CONTINUEIF(80)='X'
INTO TABLE COURSE
WHEN(1)='C'
(COURSENO POSITION(2) CHAR(5),
SHTITLE POSITION(7) VARCHAR,
DESCRIPT POSITION(*) VARCHAR,
DURATION POSITION(*) DECIMAL EXTERNAL,
PREREQ1 POSITION(*) CHAR(4) NULLIF(PREREQ1='
PREREQ2 POSITION(*) CHAR(4) NULLIF(PREREQ2='
PREREQ3 POSITION(*) CHAR(4) NULLIF(PREREQ3='
INTO TABLE OFFERING
WHEN(1)='O'
(COURSENO POSITION(2) CHAR(5),
WEEKNO POSITION(7) DECIMAL EXTERNAL(2),
DATESUFF POSITION(9) CHAR(1),
STRTDATE POSITION(10) DATE EXTERNAL(10),
ENDDATE POSITION(20) DATE EXTERNAL(10),
ROOMNO POSITION(30) DECIMAL EXTERNAL(3))

'),
'),
'))

Two Input Records to be Concatenated for COURSE

CCV831 DB2 10 FOR Z/OS DATABASE ADMINISTRATION WORKSHOP PART 1 THIS CO URSE PRX
CCEFFF03CCF4FF4CDD4E6DE4CCECCCEC4CCDCDCEEDCECDD4EDDDECDD4DCDE4F04ECCE4CD EDEC4DDE
335831074220100669091620413121250144959239139650669228670719301013892036 49250797

OVIDES BASIC SKILLS FOR DB2 DATABASE ADMINISTRATION5.0CE030CE120


DECCCE4CCECC4EDCDDE4CDD4CCF4CCECCCEC4CCDCDCEEDCECDDF4FCCFFFCCFFF4
6594520212930229332066904220413121250144959239139655B035030351200

Input Record for OFFERING

OCV83104018.10.201122.10.2011201
DCEFFFFFFFF4FF4FFFFFF4FF4FFFFFFF
63583104018B10B201122B10B2011201
Copyright IBM Corporation 2011

Figure 4-14. Loading multiple tables in a table space

CV8317.0

Notes:

Multiple INTO TABLE specs and WHEN clauses

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.

Loading VARCHAR columns

pr

Column SHTITLE is defined in the table COURSE as VARCHAR(60).

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.

4-20 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

The LOAD control statement uses RELATIVE POSITIONING (indicated by POSITION(*))


rather than ABSOLUTE POSITIONING to describe the locations of input fields that come
after the first VARCHAR input.
Loading BINARY and VARBINARY columns

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

PREREQ3 will be set to NULL.

CONTINUEIF

Allows you to treat each input record as a portion of a table row.

In the visual, two input records are concatenated into one row for COURSE.

RESUME NO

pr

Ex

cl

See the next visual.

Copyright IBM Corp. 1993, 2011

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-21

Student Notebook

LOAD RESUME, REPLACE,


and REUSE at TS level
RESUME NO
LOAD

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

Copyright IBM Corporation 2011

Figure 4-15. LOAD RESUME, REPLACE, and REUSE at TS level

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

This is the default.

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.

4-22 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

LOAD REPLACE of a parent table sets dependent tables in CHKP.


Note

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.

REPLACE with REUSE

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

LOG YES is required.

Copyright IBM Corp. 1993, 2011

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-23

Student Notebook

LOAD RESUME, REPLACE,


and REUSE at partition level
RESUME NO
INTO-TABLE table-name

PART integer
REPLACE

REUSE

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

RESUME YES

Example

RESUME, REPLACE and REUSE


LOAD DATA CONTINUEIF(72:72)='X'
Can be used when loading
INTO TABLE DSN8810.EMP
individual partitions
PART 1
Must come after the INTO TABLE
REPLACE
REPLACE with REUSE
Resets and reuses existing DB2managed data sets for the
partitions
That is, no delete / redefine

LOAD will serialize at the partition level


Other partitions can be processed
concurrently

WHEN(1)='0'
(EMPNO POSITION(1:6) CHAR(6),
FIRSTNME POSITION(7:18) CHAR(12),
.
.
.
)

INTO TABLE DSN8810.EMP


PART 2
RESUME YES

WHEN(1)='1'
(EMPNO POSITION(1:6) CHAR(6),
FIRSTNME POSITION(7:18) CHAR(12),
.
.
Copyright
CopyrightIBM
IBMCorporation
Corporation2011
2007
)

Figure 4-16. LOAD RESUME, REPLACE, and REUSE at partition level

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.

4-24 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Other LOAD options

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

Allow or suppress enforcement of the table check or


referential constraints while LOADing. ENFORCE NO sets
the CHECK PENDING state
LOAD DATA
ENFORCE CONSTRAINTS/NO

LOG NO LOADs faster but impact on RECOVERY


LOAD DATA
LOG YES/NO

LOAD stops after 25 errors


LOAD DATA
DISCARDS 25

Copyright IBM Corporation 2011

Figure 4-17. Other LOAD options

CV8317.0

Notes:

ENFORCE CONSTRAINTS

cl

The LOAD utility enforces RI and table check constraints.

Ex

This is the default.

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

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

This reduces the number of bytes that must be logged.


The time and resources required to follow the LOAD with an image copy are almost always
less than the cost of logging the data as it is loaded.
Copy pending (COPY) is set for the table space or partition when a table space is loaded
with LOG NO. SQL INSERTS, UPDATES, and DELETES cannot be executed, and the
table space cannot be recovered. SQL SELECTs are still permitted.
You can remove the copy pending restrictive state in one of these ways:

Following the LOAD take a full image copy with SHRLEVEL REFERENCE.
REPAIR....NOCOPYPEND

-START DB(........) SPACENAM(........) ACCESS(FORCE)

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.

4-26 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Copyright IBM Corporation 2011

Figure 4-18. LOAD phases

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.

LOAD RESUME YES

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.

These deleted rows can optionally be placed in a discard data set.

Copyright IBM Corp. 1993, 2011

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Copyright IBM Corporation 2011

Figure 4-19. DISCARD file

CV8317.0

Notes:

A number of conditions can cause records to be discarded.


During RELOAD phase

cl

- WHEN condition not satisfied

Ex

- Conversion errors

- Table check constraint violations

During INDEXVAL phase

pr

- Duplicate key in unique index

During ENFORCE phase


- RI constraint violation

4-28 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Unique index violations


TABLE ROWS
LOADED

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

RESULTS OF LOAD RESUME NO

Table and Index both loaded


Duplicate key rows not loaded
Error message indicates record number of input record

USER CORRECTION

Correct and reload unique index violation records by either:


SQL INSERT
For correct positioning of the records, or:
LOAD RESUME YES
Which appends to the end of the table space
Copyright IBM Corporation 2011

Figure 4-20. Unique index violations

CV8317.0

pr

Ex

cl

Notes:

Copyright IBM Corp. 1993, 2011

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Sort input data into clustering sequence by table


Load does not sort data

Avoid data conversions


Create all your indexes before loading your data
For LOAD REPLACE use:
LOG NO to avoid excessive log I/O, and
Take an inline image copy

Otherwise, use LOG NO and then take a full image copy


Load parent tables before dependent tables
Specify ENFORCE CONSTRAINTS (or let default)

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

Run RUNSTATS utility afterwards or use inline statistics


Rebind affected plans/packages
Copyright IBM Corporation 2011

Figure 4-21. LOAD considerations

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

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

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

DB2 family cross-loader function

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

DB2 for z/OS

Copyright IBM Corporation 2011

Figure 4-22. DB2 family cross-loader function

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

Copyright IBM Corp. 1993, 2011

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-31

Student Notebook

LOAD - delimited input

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

CREATE DATABASE DELDB;


CREATE TABLESPACE DELTS IN DELDB;
CREATE TABLE DELT( FILENO CHAR(3),
NAME VARCHAR(30),
DATE1 DATE EXTERNAL,
TIME1 TIME EXTERNAL)
IN DELDB.DELTS;

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

Copyright IBM Corporation 2011

Figure 4-23. LOAD - delimited input

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.

4-32 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

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

4.3. Ensuring referential and check constraints

pr

Ex

cl

Uempty

Copyright IBM Corp. 1993, 2011

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-33

Student Notebook

Check pending (CHKP)

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

What is Check Pending (CHKP)?


What is the purpose of Check Pending?
What does it apply to?

What is permitted or not permitted?


What causes Check Pending?

How can Check Pending be reset?

Copyright IBM Corporation 2011

Figure 4-24. Check pending (CHKP)

CV8317.0

Notes:

What is check pending (CHKP)?

cl

CHECK PENDING is a DB2 restrictive state that is set on if there is a potential violation of
a constraint.

Ex

What is the purpose of check pending?

The purpose is to ensure the integrity of DB2 data.

pr

What does it apply to?


Check Pending applies to:

Dependent table spaces of an RI structure


Table check constraints

4-34 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

What is permitted/not permitted?

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

No SELECT, INSERT, UPDATE, or DELETE


- But DROP and ALTER allowed
No update of PK when FK table is in CHKP
No COPY or REORG
- But CHECK DATA, LOAD REPLACE, and RECOVER allowed

What causes check pending?


CHKP set on by:

LOAD of dependent table with ENFORCE NO


LOAD REPLACE of parent table in its own table space
- Applies to dependents, not descendents
Adding FK or table check constraint to populated table
Running CHECK DATA...DELETE NO and violations are detected

How can check pending be reset?

Some of the ways Check Pending can be reset are:

pr

Ex

cl

CHECK DATA...DELETE YES.


CHECK DATA...DELETE NO and no violating rows are found.
-START DB(...) SPACENAM(...) ACCESS FORCE
- This resets all restrictive states on the table space.
REPAIR.....NOCHECKPEND

Copyright IBM Corp. 1993, 2011

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-35

Student Notebook

Using the CHECK DATA utility

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

1. CHECK DATA TABLESPACE DB1.TS1

2. CHECK DATA TABLESPACE DB1.TS1


FOR EXCEPTION
IN UID1.TBL1 USE UID1.E_TBL1
IN UID1.TBL2 USE UID1.E_TBL2
DELETE NO

3. CHECK DATA TABLESPACE DB1.TS1


FOR EXCEPTION
IN UID1.TBL1 USE UID1.E_TBL1
IN UID1.TBL2 USE UID1.E_TBL2
IN UID1.TBL3 USE UID1.E_TBL3
IN UID1.TBL4 USE UID1.E_TBL4
DELETE YES

Copyright IBM Corporation 2011

Figure 4-25. Using the CHECK DATA utility

CV8317.0

Notes:

1. CHECK DATA TABLESPACE DB1.TS1

- Always produces a report with details of each violation:

cl

RID of invalid row. This cross-references to RID in exception table.

Ex

Name of table

Constraint name

2. CHECK DATA TABLESPACE DB1.TS1

IN UID1.TBL1 USE UID1.E_TBL1


IN UID1.TBL2 USE UID1.E_TBL2

pr

FOR EXCEPTION
DELETE NO

- Invalid rows can optionally be copied to a user-specified exception table.


- Option is invoked by specifying FOR EXCEPTION.

- All dependent (not descendent) tables must be specified with the IN keyword.
4-36 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

Prior to DB2 10:

CHECK DATA utility places the table space in the CHECK-pending status.
DB2 10:

DSNZPARM CHECK_SETCHKP setting determines the utility action as follows:

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.

3. CHECK DATA TABLESPACE DB1.TS1


FOR EXCEPTION

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.

- FOR EXCEPTION must include all dependent and descendent tables.

cl

- All dependents and descendants of deleted row copied to respective exception


tables and deleted.

Ex

- CHECK PENDING is set off.

SCOPE ALL causes all rows to be checked.

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.

Copyright IBM Corp. 1993, 2011

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-37

Student Notebook

CHECK DATA - exception tables

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

Exception table requires same column names and descriptions


as table being checked
1. Create an exception table with same columns
CREATE TABLE XCPTA LIKE DATATABA
IN DATABASE PERSDB

2. Add column for RID of row (optional)


ALTER TABLE XCPTA
ADD ANYNM1 CHAR(5)

3. Add column for utility start time (optional)


ALTER TABLE XCPTA
ADD ANYNM2 TIMESTAMP

If DELETE YES is specified, exception tables are required for


the tables being checked, the dependent tables, and the
descendent tables
Copyright IBM Corporation 2011

Figure 4-26. CHECK DATA - exception tables

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.

Some considerations regarding exception tables are:


The exception table cannot have more than 750 columns.
The names of the CHAR(5) and TIMESTAMP columns do not matter.
4-38 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

Copyright IBM Corp. 1993, 2011

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-39

Student Notebook

CHECK DATA - phases


JCL

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

Copyright IBM Corporation 2011

Figure 4-27. CHECK DATA - phases

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

An Index on the foreign key will speed up this utility.

4-40 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

CHECK DATA - example


CHECK DATA TABLESPACE INVDB11.INVSINL

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

- OUTPUT START FOR UTILITY, UTILID = CF8CHKD


- CHECK DATA TABLESPACE INVDB11.INVSINL

- TABLESPACE INVDB11.INVSINL HAS NO CHECK PENDING FOR SCOPE


- CHECKING TABLE INVDB111.T_INVOICE_LINE
- SORT PHASE STATISTICS NUMBER OF RECORDS=4
ELAPSED TIME=00:00:02

- SORT PHASE STATISTICS NUMBER OF RECORDS=1


ELAPSED TIME=00:00:02

- ROW (RID=X00000202) HAS NO PARENT FOR INVDB111.T_INVOICE_LINE.INVRINL3


- CHECK TABLE INVDB111.T_INVOICE_LINE COMPLETE, ELAPSED TIME=00:00:03
- TABLESPACE INVDB11.INVSINL IS IN CHECK PENDING
- CHECK DATA COMPLETE, ELAPSED TIME=00:00:06

- UTILITY EXECUTION COMPLETE, HIGHEST RETURN CODE=4


SORT TECHNIQUE SELECTED

Copyright IBM Corporation 2011

Figure 4-28. CHECK DATA - example

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

Was an index defined on the foreign key?

pr

Was DELETE YES specified for this run?

Copyright IBM Corp. 1993, 2011

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-41

pr

Ex

cl

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

Student Notebook

4-42 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

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

4.4. The UNLOAD utility

pr

Ex

cl

Uempty

Copyright IBM Corp. 1993, 2011

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-43

Student Notebook

DB2 UNLOAD utility


Full
Image Copy

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

Copyright IBM Corporation 2011

Figure 4-29. DB2 UNLOAD utility

CV8317.0

Notes:

You can unload data from:

An entire table space


A list of table spaces
Multiple table spaces with one run of UNLOAD
Specific partitions using parallelism
Certain tables only
Certain rows only
Certain columns only

Ex

cl

1.
2.
3.
4.
5.
6.
7.

pr

Data is unloaded into BSAM sequential data sets in external format.

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

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

UNLOAD can unload data in delimited format.

pr

Ex

cl

UNLOAD can also generate LOAD utility control statements.

Copyright IBM Corp. 1993, 2011

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-45

Student Notebook

Table space input specification


Header field which will
prefix the output records

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

Variable length data will not be


padded in the output records

Data will be unloaded


in ASCII

UNLOAD TABLESPACE DBDAVID.TS1 ASCII NOPAD


FROM TABLE DAVID.EMP
HEADER CONST 'EMP'
SAMPLE 50
LIMIT 4000
(EMPNO, LASTNAME, SALARY DECIMAL EXTERNAL)
WHEN (WORKDEPT = 'D11' AND SALARY > 25000)

Maximum number of rows


which will be unloaded from
table EMP

The column order specifies


the field order in the output
records

Copyright IBM Corporation 2011

Figure 4-30. Table space input specification

CV8317.0

Notes:

An example of the UNLOAD utility control statement is shown on the visual.

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

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

Copyright IBM Corp. 1993, 2011

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-47

Student Notebook

Example of the unloaded records

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

NULL indicator byte for


nullable column SALARY

EMP000060@@SHEARER# 32250.00
EMP000150@@BECKHAM# 25280.00
EMP000200@@SCHOLES# 27740.00
EMP000220@@OWEN# 29840.00
EMP200220@@HALL# 29840.00

2-byte length field for varchar column LASTNAME

record header
character string

Copyright IBM Corporation 2011

Figure 4-31. Example of the unloaded records

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.

4-48 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Output field specification

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

For each column selected, you can optionally specify the


following attributes for the column's corresponding output field:
Start position

Data type (for example, DECIMAL EXTERNAL)


Length

TRUNCATE - used to truncate column values when they


are longer than the output field length specified
STRIP - used to remove leading and/or trailing blanks
(or the specified character) from column values

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.

Copyright IBM Corporation 2011

Figure 4-32. Output field specification

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.

Copyright IBM Corp. 1993, 2011

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-49

Student Notebook

UNLOAD - delimited output

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

UNLOAD TABLESPACE DELDB.DELTS


DELIMITED CHARDEL '#' COLDEL ';' DECPT '.'
PUNCHDDN SYSPUNCH
Optional keywords
UNLDDN SYSREC EBCDIC
FROM TABLE DELT
POSITION(*)
(FILENO
POSITION(*) CHAR(3),
keywords and
NAME
POSITION(*) VARCHAR(30),
char field lengths
DATE1
POSITION(*) DATE EXTERNAL,
TIME1
POSITION(*) TIME EXTERNAL)
optional

Unloaded data looks like:

#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

Figure 4-33. UNLOAD - delimited output

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.

4-50 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

Some of the reasons why a row cannot be unloaded:


The output field is too small for a column value and the TRUNCATE
option was not specified (or was not available)
The row is compressed but no dictionary exists in the input image
copy data sets
UNLOAD ... MAXERR n ...

UNLOAD terminates (RC=8) when "n" records are found to be


in error
The default is MAXERR 1

UNLOAD terminates when it encounters the first record in error


Copyright IBM Corporation 2011

Figure 4-34. Error handling

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.

Copyright IBM Corp. 1993, 2011

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

4-51

Student Notebook

Considerations

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

The UNLOAD utility does not support:


Index spaces
Views

Concurrent image copies


FlashCopy image copies
Directory table spaces

Copyright IBM Corporation 2011

Figure 4-35. Considerations

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.

4-52 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

1. What would happen if the LOAD utility encountered duplicate


keys for a table on which a unique index had been created?
2. What types of error would cause input records to be written
to a discard data set?
3. Incorrect specification of input field position / length could
cause the LOAD utility to do a lot of work to no avail. How
could you prevent this?

4. If the LOAD failed before completion, what could you do after


correcting the cause of the failure?

Copyright IBM Corporation 2011

Figure 4-36. Checkpoint quiz (1 of 3)

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.

Copyright IBM Corp. 1993, 2011

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

5. It may sometimes be necessary to LOAD new data and


overwrite all the original data. How would you do this at the:
TS level

Partition level

6. What effect does a clustering index have on the way in which


the data is loaded?
7. What recommendations would you suggest with regard to
logging during the LOAD? Would this have any effect on the
LOAD utility restart capabilities?

Copyright IBM Corporation 2011

Figure 4-37. Checkpoint quiz (2 of 3)

CV8317.0

Notes:

Write down your answers here:

6.

7.

pr

Ex

cl

5.

4-54 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Tables T1, T2, T3 contain data

How would you...

Add data to table T2 from


a sequential data set?
__________________
__________________

Replace the data in table T2


by the contents of the
sequential data set?
__________________
__________________

Copyright IBM Corporation 2011

Figure 4-38. Checkpoint quiz (3 of 3)

CV8317.0

Notes:

Write down your answers here:

pr

Ex

cl

8.

Copyright IBM Corp. 1993, 2011

Unit 4. Getting data into and out of DB2

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Copyright IBM Corporation 2011

Figure 4-39. Unit summary

CV8317.0

pr

Ex

cl

Notes:

4-56 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Unit 5. Keeping your DB2 data in good shape


What this unit is about

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

This unit describes how to keep your data in good shape.

What you should be able to do

After completing this unit, you should be able to:

Describe and interpret the statistics collected by RUNSTATS and


real-time statistics
Run RUNSTATS with an appropriately chosen set of options
Describe how DB2 collects statistics in real time
Describe why it is necessary to periodically REORG your
table spaces and indexes
Run REORG with an appropriately chosen set of options

How you will check your progress


Accountability:

Machine exercises

SC19-2968

DB2 10 for z/OS Administration Guide

SC19-2978

DB2 10 for z/OS Managing Performance

SC19-2983

DB2 10 for z/OS SQL Reference

SC19-2984

DB2 10 for z/OS Utility Guide and Reference

SC18-9840

DB2 Version 9.1 for z/OS Administration Guide

SC18-9851

DB2 Version 9.1 for z/OS Performance Monitoring and


Tuning Guide

SC18-9854

DB2 Version 9.1 for z/OS SQL Reference

SC18-9855

DB2 Version 9.1 for z/OS Utility Guide and Reference

SC18-7413

DB2 UDB for z/OS Version 8 Administration Guide

SC18-7426

DB2 UDB for z/OS Version 8 SQL Reference

SC18-7427

DB2 UDB for z/OS Version 8 Utility Guide and


Reference

pr

Ex

cl

References

Copyright IBM Corp. 1993, 2011

Unit 5. Keeping your DB2 data in good shape

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-1

Student Notebook

Unit objectives

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

After
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

Copyright IBM Corporation 2011

Figure 5-1. Unit objectives

CV8317.0

pr

Ex

cl

Notes:

5-2

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

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

5.1. RUNSTATS and real-time statistics

pr

Ex

cl

Uempty

Copyright IBM Corp. 1993, 2011

Unit 5. Keeping your DB2 data in good shape

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Number of rows in a table?


Clustering OK?
Index levels?
Number of distinct values in column?
...
Copyright IBM Corporation 2011

DBA

Reorganize?
...

Figure 5-2. RUNSTATS

CV8317.0

Notes:

The DB2 RUNSTATS utility collects statistics used by:

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

UPDATE ALL is the default.

REPORT indicates if the collected statistics should be written to SYSPRINT.


The Optimizer is called by BIND, REBIND, and PREPARE.
Refer to DB2 catalog tables section in the Appendix of DB2 10 for z/OS SQL Reference or
Chapter 11 in Part 2 of DB2 10 for z/OS Managing Performance for a complete list of the
columns updated by RUNSTATS.
5-4

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

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

RUNSTATS versus real-time statistics

RUNSTATS ...

Almost real-time

Catalog

Catalog

Space-related
statistics

Space-related
statistics
and more

Copyright IBM Corporation 2011

Figure 5-3. RUNSTATS versus real-time statistics

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

Copyright IBM Corp. 1993, 2011

Unit 5. Keeping your DB2 data in good shape

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-5

Student Notebook

Enabling real-time statistics (1 of 2)

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

DSNZPARM STATSINT controls for how long DB2 waits


before attempting to write out page set statistics to the
real-time statistics tables in the catalog
SYSIBM.SYSTABLESPACESTATS

Contains information about table spaces and table space partitions

SYSIBM.SYSINDEXSPACESTATS

Contains information about indexes and index partitions

Copyright IBM Corporation 2011

Figure 5-4. Enabling real-time statistics (1 of 2)

CV8317.0

Notes:

The default value for STATSINT is 30 minutes.

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

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Enabling real-time statistics (2 of 2)

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

All non-utility related numbers, such as #rows and #pages, are


initialized with CREATE and therefore do not need a starting
point.

REORG-related numbers are also initialized with CREATE and


column REORGLASTTIME is set to the creation time.
Only for RUNSTATS and COPY the utility must be run once to
activate the counters.

Copyright IBM Corporation 2011

Figure 5-5. Enabling real-time statistics (2 of 2)

CV8317.0

pr

Ex

cl

Notes:

Copyright IBM Corp. 1993, 2011

Unit 5. Keeping your DB2 data in good shape

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-7

Student Notebook

When are space-related statistics still needed?

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

Space-related RUNSTATS statistics are still needed if one of


the following applies:
Use of REORG keywords
OFFPOSLIMIT
INDREFLIMIT
LEAFDISTLIMIT

Tools using space-related RUNSTATS statistics from the catalog

Copyright IBM Corporation 2011

Figure 5-6. When are space-related statistics still needed?

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

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

Copyright IBM Corp. 1993, 2011

Unit 5. Keeping your DB2 data in good shape

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-9

Student Notebook

Important RUNSTATS options (1 of 2)


Only table space and table statistics, like the
number of rows and the number of pages, will be
collected

TABLE

Column statistics (cardinality, second lowest and


highest value) will be collected for all columns in
all tables in the specified table space(s)

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 statistics will be collected for the


specified columns (ALL for all columns) in the
specified tables

COLUMN (...)
INDEX (...)

Index statistics will be collected for the specified


indexes (ALL for all indexes, this is the default)

Copyright IBM Corporation 2011

Figure 5-7. Important RUNSTATS options (1 of 2)

CV8317.0

pr

Ex

cl

Notes:

5-10 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Important RUNSTATS options (2 of 2)


SHRLEVEL

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

Indicates if, during RUNSTATS, only readers


are allowed to access the objects (SHRLEVEL
REFERENCE) or if writers are also allowed
(SHRLEVEL CHANGE, the default)

HISTORY

Allows old statistics to be kept in separate


catalog tables called SYSIBM.SYS..._HIST

PART

For partitioned table spaces and indexes,


allows the collection of statistics for the
specified partition only

Copyright IBM Corporation 2011

Figure 5-8. Important RUNSTATS options (2 of 2)

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.

Copyright IBM Corp. 1993, 2011

Unit 5. Keeping your DB2 data in good shape

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

LOAD REPLACE and LOAD RESUME NO


REORG TABLESPACE and REORG INDEX
REBUILD INDEX

Reduces the overall elapsed time required to:


Run the utility and
Collect statistics

Inline statistics enabled by using the STATISTICS keyword


Table space statistics are collected during the RELOAD phase
Discarded records might be counted

Index statistics are collected during the BUILD phase

All RUNSTATS options except COLGROUP can be specified


Copyright IBM Corporation 2011

Figure 5-9. Inline statistics

CV8317.0

pr

Ex

cl

Notes:

5-12 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

RUNSTATS TABLESPACE DB.TS


SHRLEVEL CHANGE
TABLE (ALL) INDEX (ALL)
KEYCARD HISTOGRAM

Copyright IBM Corporation 2011

Figure 5-10. Recommended RUNSTATS

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.

Copyright IBM Corp. 1993, 2011

Unit 5. Keeping your DB2 data in good shape

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

5-14 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Copyright IBM Corp. 1993, 2011

Unit 5. Keeping your DB2 data in good shape

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-15

Student Notebook

Reasons for reorganizing table spaces


Recluster the data

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

SYSTABLESPACESTATS column REORGUNCLUSTINS


SYSINDEXPART columns NEAROFFPOSF AND FAROFFPOSF

Eliminate overflow records

SYSTABLEPART columns NEARINDREF and FARINDREF


SYSTABLESPACESTATS column REORGFARINDREF

Restore free space

Modify PRIQTY, SECQTY


Eliminate extents

Eliminate overallocations

Realign segments in segmented table spaces

Copyright IBM Corporation 2011

Figure 5-11. Reasons for reorganizing table spaces

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.

5-16 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

Copyright IBM Corp. 1993, 2011

Unit 5. Keeping your DB2 data in good shape

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-17

Student Notebook

Reasons for reorganizing indexes


Restore physical ordering of leaf pages

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

SYSINDEXSPACESTATS column REORGLEAFFAR


SYSINDEXPART column LEAFDIST

Restore free space

Reduce number of levels

SYSINDEXSPACESTATS column REORGNUMLEVELS

Modify PRIQTY, SECQTY


Eliminate extents

Eliminate overallocations

Copyright IBM Corporation 2011

Figure 5-12. Reasons for reorganizing indexes

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

The advisory REORG-pending (AREOR) status indicates that an index needs to be


reorganized for optimal performance and to apply pending definition changes. This state is
applicable only in DB2 10.

5-18 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

SHRLEVEL NONE / REFERENCE / CHANGE


LOG YES / NO
REUSE
STATISTICS

UNLOAD
PHASE

RELOAD
PHASE

SORT
PHASE

BUILD
PHASE

TABLE
SPACE

INLINE FULL IMAGE COPY IF


COPYDDN and/or
RECOVERYDDN

SYSIBM.
SYSCOPY

INDEX
SPACE(S)

Correct Nonpartitioned Indexes if


REORG TABLESPACE PART

Copyright IBM Corporation 2011

Figure 5-13. REORG TABLESPACE

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

This is the default.

The phases shown on the visual apply to SHRLEVEL NONE.

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.

Copyright IBM Corp. 1993, 2011

Unit 5. Keeping your DB2 data in good shape

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Inline statistics have been discussed in the previous topic.


Note

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.

5-20 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Copyright IBM Corporation 2011

Figure 5-14. REORG INDEX

CV8317.0

Notes:

The options for REORG INDEX are a subset of those for REORG TABLESPACE.

pr

Ex

cl

Refer to the previous page for an explanation of these options.

Copyright IBM Corp. 1993, 2011

Unit 5. Keeping your DB2 data in good shape

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-21

Student Notebook

REORG examples

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

REORG TABLESPACE PERSDB.PERSTSP1


REORG TABLESPACE PERSDB.PERSTSP2
PART 3

REORG TABLESPACE PERSDB.PERSTSP2


PART 3:6
SHRLEVEL NONE
REORG INDEX DBA01.DEPTNO_INDEX

REORG INDEX DBA01.DEPTNO_INDEX


PART 3
REUSE

Copyright IBM Corporation 2011

Figure 5-15. REORG examples

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.

SHRLEVEL NONE See the earlier description.


REUSE See the earlier description.
5-22 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

REORG TABLESPACE ... DISCARD


DISCARD keyword

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

Mutually exclusive with UNLOAD EXTERNAL


Discarded rows

Written to SYSDISC data set or


Data set specified with DISCARDDN keyword or
Not written (for example, lost)

LOAD statements generated as with UNLOAD EXTERNAL

FROM TABLE ... WHEN clause

Rows meeting the conditions will be discarded (not reloaded)


REORG TABLESPACE HR.PERS
DISCARD
FROM TABLE COMPANY.EMPLOYEE
WHEN(EMPDEPT=1CA
OR EMPDEPT='1CF')

Discarded records are in external format

Copyright IBM Corporation 2011

Figure 5-16. REORG TABLESPACE ... DISCARD

CV8317.0

pr

Ex

cl

Notes:

Copyright IBM Corp. 1993, 2011

Unit 5. Keeping your DB2 data in good shape

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-23

Student Notebook

Determining the need for REORG


REORG TABLESPACE

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

OFFPOSLIMIT (percentage of rows no longer in clustering order)


INDREFLIMIT (percentage of overflow records)

REORG INDEX

LEAFDISTLIMIT (value of column LEAFDIST in SYSIBM.SYSINDEXPART)

REPORTONLY keyword

Checks limits and sets return code without reorganizing the data

Return codes

(1) No limit met

REORG not performed or recommended

(2) Some limit was met

REORG performed or recommended

Relies on space-related RUNSTATS statistics

REORG TABLESPACE DB.TS


INDREFLIMIT 15
==> REORG performed only if more
than 15% of the rows in the table
space (cardinality) have overflow
records.
Copyright IBM Corporation 2011

Figure 5-17. Determining the need for REORG

CV8317.0

Notes:

This is a way to automate decision-making to REORG.

The criteria for REORG are specified through parameters.

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.

5-24 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Copyright IBM Corporation 2011

Figure 5-18. Unit summary

CV8317.0

Notes:

pr

Ex

cl

The visual summarizes what we have covered in this unit.

Copyright IBM Corp. 1993, 2011

Unit 5. Keeping your DB2 data in good shape

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

5-25

pr

Ex

cl

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

Student Notebook

5-26 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Unit 6. Application data recovery basics


What this unit is about

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.

What you should be able to do

After completing this unit, you should be able to:

Make DB2 user data available again following media failures.

How you will check your progress


Accountability:

Machine exercises

SC19-2968

DB2 10 for z/OS Administration Guide

SC19-2983

DB2 10 for z/OS SQL Reference

SC19-2984

DB2 10 for z/OS Utility Guide and Reference

SC19-2972

DB2 10 for z/OS Command Reference

SC18-9840

DB2 Version 9.1 for z/OS Administration Guide

SC18-9854

DB2 Version 9.1 for z/OS SQL Reference

SC18-9855

DB2 Version 9.1 for z/OS Utility Guide and Reference

SC18-9844

DB2 Version 9.1 for z/OS Command Reference

SC18-7413

DB2 UDB for z/OS Version 8 Administration Guide

SC18-7426

DB2 UDB for z/OS Version 8 SQL Reference

SC18-7427

DB2 UDB for z/OS Version 8 Utility Guide and


Reference

pr

Ex

cl

References

SC18-7416

Copyright IBM Corp. 1993, 2011

DB2 UDB for z/OS Version 8 Command Reference

Unit 6. Application data recovery basics

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-1

Student Notebook

Unit objectives

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

After
Aftercompleting
completingthis
thisunit,
unit,you
youshould
shouldbe
beable
ableto:
to:
Make
MakeDB2
DB2user
userdata
dataavailable
availableagain
againfollowing
followingmedia
mediafailures
failures

Copyright IBM Corporation 2011

Figure 6-1. Unit objectives

CV8317.0

pr

Ex

cl

Notes:

6-2

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Application data recovery - overview


Media
Failure
E

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

Copyright IBM Corporation 2011

Figure 6-2. Application data recovery - overview

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.

Copyright IBM Corp. 1993, 2011

Unit 6. Application data recovery basics

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Log data sets


recorded in
Bootstrap Data
Set (BSDS)

Archive
Log Data
Set n+2

Copyright IBM Corporation 2011

Figure 6-3. DB2 logging

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

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

CHECK DATA LOG NO

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.

LOAD and REORG LOG NO

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.

CREATE/ALTER TABLESPACE NOT LOGGED

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.

Copyright IBM Corp. 1993, 2011

Unit 6. Application data recovery basics

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Start RBA End RBA

Start LRSN End LRSN

DBID OBID

Start RBA End RBA

Start LRSN End LRSN

DBID OBID

Start RBA

End RBA

Start LRSN End LRSN

DB2 Directory

SYSIBM.SYSLGRNX
Copyright IBM Corporation 2011

Figure 6-4. SYSIBM.SYSLGRNX

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

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Backing up table spaces


Full Image Copy

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

Incremental Image Copy

C = Changed

Copyright IBM Corporation 2011

Figure 6-5. Backing up table spaces

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

Full image copy

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

FULL YES is the default.

Incremental image copy


An incremental image copy just contains the pages changed since the previous full or
incremental image copy.
Incremental image copies require that a previous full copy exists for the table space.
Copyright IBM Corp. 1993, 2011

Unit 6. Application data recovery basics

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-7

Student Notebook

You specify FULL NO on the COPY utility control statement.

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

A full image copy has an ICTYPE of F.


An incremental image copy has an ICTYPE of I.

6-8

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

Partitioned Table Space or Partitioned Index

PART
1

Image Copy

PART
2

COPY . . .
DSNUM 2

PART
2

PART
3

Copyright IBM Corporation 2011

Figure 6-6. Backing up partitions

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.

Copyright IBM Corp. 1993, 2011

Unit 6. Application data recovery basics

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

6-10 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Copy data sets


optional
Local Primary

Local Backup

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

Table Space or
Index Space

COPY . . . COPYDDN ( local-primary, [ local-backup ])

Copyright IBM Corporation 2011

Figure 6-7. Copy data sets

CV8317.0

Notes:

DB2 gives you the capability to take multiple copies.

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

Unit 6. Application data recovery basics

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-11

Student Notebook

Specify a large block size for performance.


Note
For tape data sets the large block interface is supported (greater than 32760 bytes). This
can significantly improve COPY and the restore phase of RECOVER.

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.

6-12 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

//UTIL EXEC DSNUPROC,SYSTEM=DBV8,UID='COPY',UTPROC=''


//DSNUPROC.TSDEPTP DD DSN=WWCF83DB.TSDEPT.P2001056,...
//DSNUPROC.TSDEPTB DD DSN=WWCF83DB.TSDEPT.B2001056,...
//DSNUPROC.TSEMPLP DD DSN=WWCF83DB.TSEMPL.P2001056,...
//DSNUPROC.TSEMPLB DD DSN=WWCF83DB.TSEMPL.B2001056,...
//DSNUPROC.TSLOCNP DD DSN=WWCF83DB.TSLOCN.P2001056,...
//DSNUPROC.TSLOCNB DD DSN=WWCF83DB.TSLOCN.B2001056,...
//DSNUPROC.TSPROJP DD DSN=WWCF83DB.TSPROJ.P2001056,...
//DSNUPROC.TSPROJB DD DSN=WWCF83DB.TSPROJ.B2001056,...
//DSNUPROC.TSRSMEP DD DSN=WWCF83DB.TSRSME.P2001056,...
//DSNUPROC.TSRSMEB DD DSN=WWCF83DB.TSRSME.B2001056,...
//DSNUPROC.SYSIN
DD *
COPY TABLESPACE WWCF83DB.TSDEPT
COPYDDN(TSDEPTP,TSDEPTB)
TABLESPACE WWCF83DB.TSEMPL
COPYDDN(TSEMPLP,TSEMPLB)
TABLESPACE WWCF83DB.TSLOCN
COPYDDN(TSLOCNP,TSLOCNB)
TABLESPACE WWCF83DB.TSPROJ
COPYDDN(TSPROJP,TSPROJB)
TABLESPACE WWCF83DB.TSRSME
COPYDDN(TSRSMEP,TSRSMEB)

FULL
FULL
FULL
FULL
FULL

YES
YES
YES
YES
YES

Example 2

//UTIL EXEC DSNUPROC,SYSTEM=DBV8,UID='COPY',UTPROC=''


//DSNUPROC.SYSIN
DD *
LISTDEF RECSET INCLUDE TABLESPACES
TABLESPACE WWCF83DB.TSEMPL RI ALL
TEMPLATE RECTMP DSN &DB..&SN..&PB.&DATE.
COPY LIST RECSET COPYDDN(RECTMP,RECTMP) FULL YES
Copyright IBM Corporation 2011

Figure 6-8. COPY - examples

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

Adding a table space to a database


Space allocation changes due to growing table spaces

LISTDEF and TEMPLATE are discussed in course CV870.

Copyright IBM Corp. 1993, 2011

Unit 6. Application data recovery basics

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

COPYTOCOPY TABLESPACE table-space-name


FROMLASTINCRCOPY
COPYDDN (, backup)

COPYTOCOPY allows to establish missing copies from existing copy


Established copies recorded in SYSIBM.SYSCOPY
Same TIMESTAMP, ICDATE, ICTIME, and START_RBA as input copy

FROMLASTCOPY, FROMLASTFULLCOPY, FROMLASTINCRCOPY or FROMCOPY,


that is, from specified copy data set

Copyright IBM Corporation 2011

Figure 6-9. COPYTOCOPY utility - establishing copies of image copies

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.

6-14 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

Copyright IBM Corp. 1993, 2011

Unit 6. Application data recovery basics

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-15

Student Notebook

Table space recovery concept


DB2 Catalog
SYSIBM.SYSCOPY

Full Image Copy

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

Copyright IBM Corporation 2011

Figure 6-10. Table space recovery concept

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.

6-16 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Nevertheless, point-in-time recoveries might be unavoidable and possible in cases as the


one described above.

pr

Point-in-time recovery is not covered in this course.

Copyright IBM Corp. 1993, 2011

Unit 6. Application data recovery basics

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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 DB01.TS01


TABLESPACE DB01.TS02
Example 3

RECOVER TABLESPACE
TABLESPACE
TABLESPACE
TABLESPACE

DB01.TS01
DB01.TS01
DB01.TS01
DB01.TS01

DSNUM
DSNUM
DSNUM
DSNUM

1
2
3
4

Example 4

RECOVER TABLESPACE DB01.TS01 DSNUM ALL


Example 5
RECOVER TABLESPACE DB01.TS01 DSNUM 3
TABLESPACE DB01.TS02 DSNUM 3
Copyright IBM Corporation 2011

Figure 6-11. Recovery - examples

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.

6-18 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Preparing for a recovery


Use the REPORT RECOVERY utility to determine:

DB2 catalog

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

Image copies, which must be restored

SYSIBM.SYSCOPY

Log ranges that need to be applied

DB2 Directory

Archive log data sets containing the log records

SYSIBM.SYSLGRNX
BSDS

You can obtain the information for:

A specific table space


A partition of a partitioned table space
A data set for a nonpartitioned table space

Log Data Sets

Limit the information you receive:

CURRENT only what is needed for the last recoverable point


SUMMARY only volume serial numbers for the image copy data
sets and the archive log data sets
Copyright IBM Corporation 2011

Figure 6-12. Preparing for a recovery

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

Image copy information is contained in SYSIBM.SYSCOPY, and the REPORT utility


displays the information of the corresponding rows.

Ex

Log range information is extracted from SYSIBM.SYSLGRNX.


Archive log data set information is extracted from the BSDS.

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.

Copyright IBM Corp. 1993, 2011

Unit 6. Application data recovery basics

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-19

Student Notebook

Combining image copies for table spaces


Full Image Copy

Full Image Copy


2

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

Full Image Copy


1

MERGECOPY . . . NEWCOPY YES

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

Incremental Image Copy


3

10

11

24

11

Copyright IBM Corporation 2011

Figure 6-13. Combining image copies for table spaces

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

The MERGECOPY utility gives you two possibilities:

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

In both cases, the new image copy is recorded in SYSIBM.SYSCOPY.

6-20 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

REBUILD INDEX utility


Allows you to rebuild an index from scratch
Uses data in the table

Table
Space

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

You can rebuild:


An entire index

UNLOAD

A partition

A logical partition

Key/RID pairs
for all rows
and indexes

Multiple indexes or partitions

For tables of same table space


With single REBUILD INDEX

All indexes for all tables of a table space

SORT

SORTBLD

BUILD

By specifying the table space

REBUILD INDEX (creator.ixname,....)


REBUILD INDEXSPACE
(dbname.isname,..)

Index 1

Index 2

Copyright IBM Corporation 2011

Figure 6-14. REBUILD INDEX utility

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.

Copyright IBM Corp. 1993, 2011

Unit 6. Application data recovery basics

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-21

Student Notebook

REBUILD INDEX examples


Example 1: Rebuild a single index

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

REBUILD INDEX (DSN8710.XDEPT1)

Example 2: Rebuild all partitions of a partitioning index


REBUILD INDEX (DSN8710.XEMP1)

Example 3: Rebuild multiple partitions of a partitioning index

REBUILD INDEX (DSN8710.XEMP1 PART 2, DSN8710.XEMP1 PART 3)

Example 4: Rebuild all indexes on a partitioned table space


REBUILD INDEX (ALL) TABLESPACE DB01.PTS01

Example 5: Rebuild one index partition specifying index space name


REBUILD INDEXSPACE (DB1.IS1 PART 9)
Copyright IBM Corporation 2011

Figure 6-15. REBUILD INDEX examples

CV8317.0

Notes:

pr

Ex

cl

The visual shows some examples of REBUILD INDEX and REBUILD INDEXSPACE.

6-22 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

REBUILD INDEX considerations

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

REUSE option avoids deletion and redefinition of data sets


If you are rebuilding an index because of media failure:
Do not use the REUSE option

Remove defective volume from storage group before


rebuilding the index

You can gather index statistics during REBUILD INDEX


DB2 Catalog updated and/or statistics printed
Rebind required to make statistics effective

Copyright IBM Corporation 2011

Figure 6-16. REBUILD INDEX considerations

CV8317.0

pr

Ex

cl

Notes:

Copyright IBM Corp. 1993, 2011

Unit 6. Application data recovery basics

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-23

Student Notebook

Recovery / rebuild pending


Partitioned
Index

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

Copyright IBM Corporation 2011

Figure 6-17. Recovery / rebuild pending

CV8317.0

Notes:
RECP

cl

RECOVER Pending status

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

REBUILD Pending status

Set, for example, by RECOVER, LOAD, or REORG abend


Can be set for:
Index

Physical Index Partition

6-24 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Logical Index Partition of NPI


- Can still access other logical index partitions via SQL and utilities
- Reset by REBUILDing only the logical partition

RBDP*

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

REBUILD pending status

Set, for example, by RECOVER TORBA / TOCOPY of data partitions


Can be set for:

Logical Index Partition of NPI only

- Entire index is inaccessible to SQL applications


- Reset by REBUILDing only the logical partition

PSRBD

Page set REBUILD pending status

Set, for example, by LOADing data partition and abending in BUILD phase for the NPI
Can be set for:

pr

Ex

cl

Entire NPI only

Copyright IBM Corp. 1993, 2011

Unit 6. Application data recovery basics

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

6-25

Student Notebook

MODIFY RECOVERY

Specify Retention Criteria


AGE
DATE

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

For mixed lists

Works on dates not timestamps

SYSLGRNX records deleted even if no SYSCOPY records


deleted
Example:

MODIFY RECOVERY TABLESPACE DBKQBL01.TPKQBL01 RETAIN


GDGLIMIT
Copyright IBM Corporation 2011

Figure 6-18. MODIFY RECOVERY

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

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

Copyright IBM Corp. 1993, 2011

Unit 6. Application data recovery basics

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Copyright IBM Corporation 2011

Figure 6-19. Unit summary

CV8317.0

Notes:

pr

Ex

cl

The visual summarizes what we have covered in this unit.

6-28 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Unit 7. Program preparation / bind


What this unit is about

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.

What you should be able to do

After completing this unit, you should be able to:

Describe the main steps needed to prepare a program with


embedded static SQL to access DB2.

How you will check your progress


Accountability:
Discussion

SC19-2968

DB2 10 for z/OS Administration Guide

SC19-2969

DB2 10 for z/OS Application Programming & SQL


Guide

SC19-2972

DB2 10 for z/OS Command Reference

SC18-9840

DB2 Version 9.1 for z/OS Administration Guide

SC18-9841

DB2 Version 9.1 for z/OS Application Programming &


SQL Guide

SC18-9844

DB2 Version 9.1 for z/OS Command Reference

SC18-7413

DB2 UDB for z/OS Version 8 Administration Guide

SC18-7415

DB2 UDB for z/OS Version 8 Application


Programming and SQL Guide

SC18-7416

DB2 UDB for z/OS Version 8 Command Reference

pr

Ex

cl

References

Copyright IBM Corp. 1993, 2011

Unit 7. Program preparation / bind

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

7-1

Student Notebook

Unit objectives

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

After
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

Copyright IBM Corporation 2011

Figure 7-1. Unit objectives

CV8317.0

pr

Ex

cl

Notes:

7-2

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

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

Program preparation flow for DB2 precompiler

Copyright IBM Corporation 2011

Figure 7-2. Program preparation flow for DB2 precompiler

CV8317.0

Notes:

Embedded static SQL

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

In this section, we deal with embedded static SQL statements.

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

Unit 7. Program preparation / bind

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

What the BIND PACKAGE process does


It takes a single DBRM as input

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.

What the BIND PLAN process does

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.

What happens at runtime?

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

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Program preparation flow for multiple modules


Source
Main

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

BIND PLAN (Plan1)


PKLIST (Col1.*,Col2.Sub2)

Copyright IBM Corporation 2011

Figure 7-3. Program preparation flow for multiple modules

CV8317.0

Notes:

Modularization using packages

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:

Copyright IBM Corp. 1993, 2011

Unit 7. Program preparation / bind

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

7-5

Student Notebook

BIND PACKAGE(collection ID) MEMBER(dbrm-member).


The name of the resulting package is the name of DBRM member.
When associating the three packages with the application plan PLAN1, you use the
PKLIST option of BIND PLAN.
The plan PLAN1 merely consists of pointers to the three packages.

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

At execution time the MAIN load module works with PLAN1.

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

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Let BIND determine object to be accessed


Source
EXEC SQL
SELECT ... FROM V1;

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

PROG1

DBRM

PROG1

Precompile

Comp & Link

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

Copyright IBM Corporation 2011

Figure 7-4. Let BIND determine object to be accessed

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.

Copyright IBM Corp. 1993, 2011

Unit 7. Program preparation / bind

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Copyright IBM Corporation 2011

Figure 7-5. Unit summary

CV8317.0

pr

Ex

cl

Notes:

7-8

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Unit 8. Security
What this unit is about

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

This unit describes the DB2 security mechanisms.

What you should be able to do

After completing this unit, you should be able to:

Describe DB2 global security


Describe the various DB2 authids and how they are established
Understand how DB2 security controls access to data
Describe and implement DB2 privileges and authorities
appropriately
Describe how ownership is established in DB2
Describe DB2 authorization checking
Describe and implement security for plans and packages

How you will check your progress


Accountability:

Machine exercises

SC19-2968

DB2 10 for z/OS Administration Guide

SC18-2983

DB2 10 for z/OS SQL Reference

SC18-9840

DB2 Version 9.1 for z/OS Administration Guide

SC18-9854

DB2 Version 9.1 for z/OS SQL Reference

SC18-7413

DB2 UDB for z/OS Version 8 Administration Guide

SC18-7426

DB2 UDB for z/OS Version 8 SQL Reference

pr

Ex

cl

References

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

Describe DB2 global security

Describe the various DB2 authids and how they are


established

Understand how DB2 security controls access to data

Describe and implement DB2 privileges and authorities


appropriately
Describe how ownership is established in DB2
Describe DB2 authorization checking

Describe and implement security for plans and packages

Copyright IBM Corporation 2011

Figure 8-1. Unit objectives

CV8317.0

pr

Ex

cl

Notes:

8-2

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

DB2 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

Copyright IBM Corporation 2011

Figure 8-2. DB2 authorization

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.

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-3

Student Notebook

Who is the user?

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

SET CURRENT SQLID

CURRENT
SQLID

ROUTINE

AUTHID
LIST

(0 - 1012)

Copyright IBM Corporation 2011

Figure 8-3. Who is the user?

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

Authorization IDs can be assigned to a process by default procedures or user-written exit


routines. Methods of assigning these IDs are covered in the next visual.
Primary authorization ID

Every process is assigned one mandatory ID called the primary authorization ID.

pr

It represents the user:

TSO logon userid


CICS / IMS signon userid
It holds personal privileges and is used by accounting and performance traces.

8-4

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Secondary authorization IDs


You can have up to 1,012 additional, optional authids called secondary authorization IDs.
Typically, these may be project, function, team, or group-related.
They can mean additional privileges for the user and allow an object to be created by one
ID and owned by another ID.

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:

- Exit DSN3@ATH - for TSO, Batch, and Distributed


- Exit DSN3@SGN - for IMS and CICS

To succeed with SET CURRENT SQLID:

- SQLID must match primary or secondary authorization IDs

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)

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-5

Student Notebook

Establishing authorization IDs


TSO / CICS / IMS

DB2

Initial Primary Authid

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

Copyright IBM Corporation 2011

Figure 8-4. Establishing authorization IDs

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

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

USER and CURRENT SQLID special registers


USER

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

Always refers to your PRIMARY AUTHID


Cannot be changed

CURRENT SQLID

Use SET CURRENT SQLID SQL statement to set to:


Your primary authid or
One of your secondary authids

For dynamic SQL, has 3 purposes:

Provides implicit qualifier for unqualified objects:


> SELECT * FROM T1

Used for authorization checking of:


> CREATE, GRANT, REVOKE

Becomes owner of a table, .....


> CREATE TABLE T1........

Copyright IBM Corporation 2011

Figure 8-5. USER and CURRENT SQLID special registers

CV8317.0

Notes:

USER special register

Always refers to your PRIMARY AUTHID.

cl

Cannot be changed.

Ex

CURRENT SQLID special register

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

1. Provides implicit qualifier for unqualified objects:


- SELECT * FROM T1
2. Used for authorization checking of:
- CREATE, GRANT, REVOKE
3. Becomes the owner of a table, table space, database, storage group or synonym:
- CREATE TABLE T1........

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-7

Student Notebook

Querying the values of special registers


You can use this query to show the values of USER and CURRENT SQLID.

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

SELECT 'PRIMARY AUTHID = '||USER,


'CURRENT SQLID = '||CURRENT SQLID
FROM SYSIBM.SYSDUMMY1
This will result in:

PRIMARY AUTHID = primary authid CURRENT SQLID = current sqlid

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

With SYSADM authority it can be set to any value.

8-8

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

What can be authorized? (1 of 2)


Privileges

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, )

Implicit through object ownership

Copyright IBM Corporation 2011

Figure 8-6. What can be authorized? (1 of 2)

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.

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-9

Student Notebook

What can be authorized? (2 of 2)


Authorities

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

Copyright IBM Corporation 2011

Figure 8-7. What can be authorized? (2 of 2)

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.

8-10 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

What do authorities contain?


Install SYSADM

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

Included in SYSCTRL authority

Copyright IBM Corporation 2011

Figure 8-8. What do authorities contain?

CV8317.0

Notes:

pr

Ex

cl

This is only valid if the DSNZPARM SEPARATE_SECURITY is set to NO.


This DSNZPARM is presented later in this unit.

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-11

Student Notebook

Install SYSADM and install SYSOPR

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

Install SYSADMs and Install SYSOPRs can


Run with ACCESS(MAINT)

Run all allowable utilities against DSNDB01 or DSNDB06


Install SYSADMs

Can start DSNDB01, DSNDB06 after stopped or in restricted


status
Can run CATMAINT

Suggestion:

SYSADM - Primary authid of main SYSADM

SYSADM2 - Secondary authid for other SYSADMs


Avoid GRANTing SYSADM!

Copyright IBM Corporation 2011

Figure 8-9. Install SYSADM and install SYSOPR

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:

Use all the privileges of DBADM over any database

pr

Use EXECUTE and BIND on any plan or package, COPY on any package

Use privileges over views that are owned by others


Set the current SQL ID to any valid value, whether it is currently a primary or secondary
authorization ID
Create and drop synonyms and views for others on any table
Use any valid value for OWNER in BIND or REBIND
8-12 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Drop database DSNDB07


Grant any of the privileges listed above to others
Holders of SYSADM authority can also drop or alter any DB2 object, except system
databases, issue a COMMENT ON or LABEL ON statement for any table or view, and
terminate any utility job, but SYSADM cannot specifically grant those privileges.

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

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-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

NOTE: Install SYSADM always includes SECADM


independently of the setting of
SEPARATE_SECURITY

Authority implicitly held

Copyright IBM Corporation 2011

Figure 8-10. Separation of duties

CV8317.0

Notes:

cl

DSNZPARM SEPARATE_SECURITY, can be used to separate the security authority from


system and database administration authority. This DSNZPARM can be set to YES or NO.
The default value is NO.

Ex

If SEPARATE_SECURITY is NO (the default), the install SYSADM and SYSADM


authorities implicitly have the SECADM authority and can continue to manage all security
objects and grant and revoke privileges granted by others. Additionally, the SYSADM
authority manages security objects, including the row-level and column-level access
control.

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

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

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-15

Student Notebook

Where is authorization information kept?


Tablename in
DB2 Catalog

Columnname

Remarks

xxxAUTH cols contain:


Y if privilege held
G if GRANTable

Many additional columns


indicate command
privileges
and create privileges

SYSADMAUTH
SYSCTRLAUTH
SYSOPERAUTH
BINDADDAUTH
BINDAGENTAUTH

SYSDBAUTH
Database privileges

DBADMAUTH
DBCTRLAUTH
DBMAINTAUTH

Additional columns indicate


create TS, create Table,
Utility and command privileges

SYSRESAUTH
Resource privileges

OBTYPE

B - buffer pool; S - stogroup;


R - table space; C - collection
D - distinct type

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

Additional columns indicate


other table and view
privileges

For column update

EXECUTE authority on UDFs,


functions and stored procedures

Copyright IBM Corporation 2011

Figure 8-11. Where is authorization information kept?

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

8-16 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

Accounting and Audit Records


Default Owner for BIND

Composite list (any ID used):


Utility Execution
Plan Execution
Dynamic DML
Commands

Current SQLID (not used for static SQL):


Default owner for created objects (DDL)

Default qualifier for table or view name (DML)

The GRANTOR authid for GRANT and REVOKE


Copyright IBM Corporation 2011

Figure 8-12. Which ID is checked?

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.

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-17

Student Notebook

How do you acquire authority?

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

Initially, by Install SYSADM


or SECADM

GRANT by authid (could be secondary ID)


holding privilege on the object
CURRENT SQLID (using dynamic SQL)
PLAN OWNER (using static SQL)

Owning an object

Can GRANT to PUBLIC

Minimize use of WITH GRANT OPTION


Copyright IBM Corporation 2011

Figure 8-13. How do you acquire authority?

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.

8-18 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Secondary Authorization IDs

GRANT CREATETAB, LOAD


ON DATABASE DBMANUF
TO MANUF
WITH GRANT OPTION

Copyright IBM Corporation 2011

Figure 8-14. Granting privileges

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

1. All composite privileges in the list, or

pr

2. Only at the privileges granted to your CURRENT SQLID.

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-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

CREATE TABLE T_ORDER


(...)
IN DB11.TS0001

Database Privileges

Resource Privileges

SYSIBM.SYSDBAUTH
GRANTOR

GRANTEE

NAME

CREATETABAUTH

DBADM1

TSOUDxx

DB11

...

SYSIBM.SYSRESAUTH
GRANTOR

GRANTEE

NAME

OBTYPE

QUALIFIER

DBADM1

TSOUDxx

TS0001

DB11

...

Copyright IBM Corporation 2011

Figure 8-15. Privileges example

CV8317.0

Notes:

pr

Ex

cl

The visual shows privileges required to create a table.

8-20 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

How do you control it?


Losing Authority

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

SYSADM, SECADM,
SYSCTRL, or
ACCESSCTRL revokes

Grantor REVOKEs

Object is DROPped or FREEd

Disconnected from RACF Group

RETAIN is omitted on BIND PLAN ... REPLACE

Copyright IBM Corporation 2011

Figure 8-16. How do you control it?

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

SYSADMs and SYSCTRLs can REVOKE anything that can be revoked.


If an object is DROPped, then all GRANTs associated with it go away.

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.

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

REVOKE must be issued with GRANTORs CURRENT SQLID


REVOKE SELECT can DROP VIEWS
Timestamp-based cascading effect

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

Copyright IBM Corporation 2011

Figure 8-17. Revoking privileges

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

Avoid using the WITH GRANT OPTION whenever possible.

pr

Ex

In DB2 10, cascading revokes may be disabled by DSNZPARM parameter


REVOKE_DEP_PRIVILEGES and SQL clause NOT INCLUDING DEPENDENT
PRIVILEGES in the REVOKE statement.

8-22 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Create privileges are granted


before the object exists
When the object is created:
The CURRENT SQLID
becomes the owner *
Privileges of ownership
cannot be revoked
Owner automatically has
GRANT authority

* The authorization ID using CREATE DATABASE

Granted on existing objects

The authority or privilege


remains until revoked or until
the object no longer exists

Can GRANT authority and


privilege only if authority or
privilege is held
WITH GRANT OPTION
only

receives DBCTRL if the original authority was CREATEDBC.

Copyright IBM Corporation 2011

Figure 8-18. CREATE privileges

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

BINDADD Grants the privilege to create plans and packages.

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-23

Student Notebook

Example - ownership of a table


Implicit privileges:

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

Alter / drop:
Table or any of its indexes

Lock the table, comment on it, or label it


Create an index or view

SELECT, UPDATE, DELETE, or INSERT


Use the LOAD utility

Define referential constraints


Define a trigger

Privileges implicit in ownership cannot be revoked


Ownership cannot be changed

Owner can GRANT explicit privileges

Copyright IBM Corporation 2011

Figure 8-19. Example - ownership of a table

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.

8-24 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

TABLE with unqualified name


CURRENT SQLID:
>
>
>
>
>

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:

> Recorded in CREATEDBY column in SYSTABLES

TABLE with qualified name


QUALIFIER:
>
>
>
>

Must have privileges needed to create table


Becomes OWNER
Must be your primary authid or one of your sec authids
Can use any qualifier if current SQLID has at least DBCTRL

Primary Authid:

> Recorded in CREATEDBY column in SYSTABLES


Copyright IBM Corporation 2011

Figure 8-20. Establishing ownership

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.

8-26 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Copyright IBM Corporation 2011

Figure 8-21. Ownership examples

CV8317.0

Notes:

The visual shows some examples of establishing ownership in DB2.

cl

In catalog table SYSDATABASE, column CREATOR contains the authorization ID of the


owner of the database and column CREATEDBY contains the primary authorization ID of
the user who created the database. Column OWNER is not present in this table.

Ex

In catalog table SYSTABLESPACE, column CREATOR contains the authorization ID of the


owner of the table space and column CREATEDBY contains the primary authorization ID of
the user who created the table space. Column OWNER is not present in this table.

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.

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-27

Student Notebook

Checking authorization to execute


DYNAMIC SQL

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

Both SELECTs are valid In dynamic


SQL in, say, SPUFI bound with
DYNAMICRULES (RUN)

GRANT ALL
ON TABLE
A.TABLE2
TO PAYROLL

PAYROLL

ACCNTG

MANUF

Secondary
Authorization IDs

Copyright IBM Corporation 2011

Figure 8-22. Checking authorization to execute

CV8317.0

Notes:

GRANT and REVOKE

For a GRANT issued dynamically, the CURRENT SQLID is checked for:

cl

The privilege with the grant option, or

Ex

An authority that includes the privilege with the grant option, or


Ownership that implicitly includes the privilege.

For a REVOKE issued dynamically, the CURRENT SQLID:

pr

Must have granted the privilege that is being revoked, or


Hold SYSCTRL or SYSADM authority

8-28 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

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-29

Student Notebook

Controlling access for dynamic DML

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

Dynamic DML requires an ID in the composite


list to have one of the following:
SYSADM, DBADM, or DATAACCESS authority or
Ownership of Table or View or
Specific table privileges:
SELECT
INSERT
UPDATE
DELETE
DYNAMICRULES Option on BIND

RUN: Use composite privileges


BIND: Use OWNER for authid checking:
Dynamic DDL, GRANT, REVOKE not allowed
SET CURRENT SQLID not allowed
Copyright IBM Corporation 2011

Figure 8-23. Controlling access for dynamic DML

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.

8-30 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

Controlling access for static SQL


TSO and BATCH

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

Usually requires (non-public) DB2 Authorization List


Could control access to program library with RACF

CICS and IMS

Transaction manager controls use of transaction code


Could GRANT EXECUTE ON PLAN...TO PUBLIC
CICS AUTHTYPE parm controls SIGNON and DB2 accounting
BIND keywords can control the execution environment:

ENABLE / DISABLE controls connections:

Connection-ID (IMS,IMSBATCH,BATCH,DB2CALL,REMOTE ...)


By subsystem (specific imsids or CICS applids ...)
Copyright IBM Corporation 2011

Figure 8-24. Controlling access for static SQL

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.

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-31

Student Notebook

Authorizations for BIND (overview)


PACKAGE
BIND PACKAGE loc.collection-id ACTION( )
COPY coll.pkg-id

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

Package Owner has


- CREATE IN Collection-id
and
- BINDADD if (ADD) or
- BIND if (REPLACE) or
- PACKADM or BINDAGENT

Package Owner has


- COPY on Package
and
Authorization for all
static SQL
- OR -

PLAN

BIND PLAN ACTION()


Plan Owner has
- BIND if (REPLACE)
- BINDADD if (ADD)
- BINDAGENT

PKLIST (...)

CREATE THREAD/SIGNON

MEMBER(dbrm)

Package Owner has


authorization for
all static SQL

Plan Owner
has
EXECUTE on
local/remote
Packages

DBRMLIB

Some Authid of User


has EXECUTE on Plan
And Connection and
Connection-id Enabled
for Plan and Package

PGMC ...

Copyright IBM Corporation 2011

Figure 8-25. Authorizations for BIND (overview)

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

PACKADM grants package administration authority for the designated collections.


BINDAGENT grants the privilege to BIND, FREE PACKAGE, REBIND, and DROP
PACKAGE.
At thread creation time, the plan is allocated if all security checks are in place.
8-32 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4
Student Notebook

Uempty

DB2 authorization summary

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

Security implementation requires planning

Establish DB2 authorization guidelines:

Who will own your objects?


Functional or individual authids?
RACF supplies group names or hard-coded in exits?

Establish DB2 authorization procedures:

Grant (WITH GRANT OPTION) / revoke


Use of other DB2 options:

View (With Check Option)


Table check constraints
CREATE TABLE ... WITH RESTRICT ON DROP
Encoding via Editproc/Fieldproc
Erase Yes option on table space

Use AUDIT TRACE to monitor GRANTs and REVOKEs


Copyright IBM Corporation 2011

Figure 8-26. DB2 authorization summary

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.

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

Describe DB2 global security

Describe the various DB2 authids and how they are


established

Understand how DB2 security controls access to data

Describe and implement DB2 privileges and authorities


appropriately
Describe how ownership is established in DB2
Describe DB2 authorization checking

Describe and implement security for plans and packages

Copyright IBM Corporation 2011

Figure 8-27. Unit summary

CV8317.0

pr

Ex

cl

Notes:

8-34 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

This unit is about basic serialization mechanisms and their objectives.

What you should be able to do

After completing this unit, you should be able to:

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

How you will check your progress


Accountability:

Machine exercises

References

DB2 10 for z/OS Managing Performance

SC18-9851

DB2 Version 9.1 for z/OS Performance Monitoring and


Tuning Guide

SC18-7413

DB2 UDB for z/OS Version 8 Administration Guide

pr

Ex

cl

SC19-2978

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-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

Copyright IBM Corporation 2011

Figure 9-1. Unit objectives

CV8317.0

pr

Ex

cl

Notes:

9-2

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Why does DB2 have serialization mechanisms?


What might happen without serialization mechanisms?
Lost Updates

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

Read Uncommitted Data

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

Copyright IBM Corporation 2011

Figure 9-2. Why does DB2 have serialization mechanisms?

CV8317.0

Notes:

Why does DB2 have serialization mechanisms?

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.

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-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

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

DB2 serialization mechanisms


DB2 has several serialization mechanisms:

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

STOP, START UT, CHKP, UTUT, UTRO, UTRW...

Claims and drains


Latching

These mechanisms should be used to:


Protect data integrity
Maximize concurrency and
Minimize costs
CPU, I/O and storage

In this topic we shall be looking at


transaction locking.

Copyright IBM Corporation 2011

Figure 9-3. DB2 serialization mechanisms

CV8317.0

Notes:

DB2 serialization mechanisms

cl

DB2 has a number of different serialization mechanisms which can be used to:
Prevent loss of data integrity and

Ex

Maximize concurrency and

Minimize cost in CPU, I/O and storage resources.

They are:

pr

1. Transaction locking

In this topic we shall be looking primarily at 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

3. Claims and drains


Claims and drains enable DB2 utilities and commands to take over access to some
objects independently of any transaction locks held on the objects.
4. Latching

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

Concurrency refers to the sharing of resources by multiple users or application programs at


essentially the same time.

pr

Ex

cl

This access is controlled by DB2s serialization mechanisms which prevent the undesirable
effects discussed above.

9-6

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

What gets locked?


User data

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

The data you target with SQL


Some exceptions
You have the most control here

The data in tables related by RI

Catalog / directory

Subject to update activity

CREATE, ALTER, DROP, GRANT, REVOKE


BIND, REBIND and FREE

Designed to minimize contention

Note:

Indexes on user tables are not locked by transaction locks

Copyright IBM Corporation 2011

Figure 9-4. What gets locked?

CV8317.0

Notes:

What gets locked?

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

Indexes on user tables are not locked by transaction locks.

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-7

Student Notebook

DB2 subsystem objects


The DB2 Catalog and directory are subject to update activity that must be serialized.
Although the catalog and directory are designed to minimize contention among application
processes, you need to be aware of some of the user activities that cause locking in these
objects.

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

CREATE, ALTER, DROP, GRANT, and REVOKE

pr

Ex

cl

BIND, REBIND, and FREE

9-8

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Main characteristics of transaction locking


Size

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

How much data is locked?

Mode

Does the lock allow others to read and/or update the locked object?

Duration

How long is the lock held?

Copyright IBM Corporation 2011

Figure 9-5. Main characteristics of transaction locking

CV8317.0

Notes:

The main characteristics of transaction locking are:


Size of the lock

cl

Mode of the lock

Ex

Duration of the lock

Knowing the characteristics helps you understand why a process suspends or times out or
why two processes deadlock.

pr

We shall be considering each of these in the following pages.

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-9

Student Notebook

Size of transaction locks


Segmented table space

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

Table space lock

Table lock

Row lock

Page lock

Partitioned and Universal table space


Partition lock

Row lock

Page lock

Copyright IBM Corporation 2011

Figure 9-6. Size of transaction locks

CV8317.0

Notes:

Size of transaction locks

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.

9-10 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

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-11

Student Notebook

Concurrency versus CPU overhead


High

Contention problems

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

CPU consumption

Low

Table space Table

Page

Row

Locksize

Copyright IBM Corporation 2011

Figure 9-7. Concurrency versus CPU overhead

CV8317.0

Notes:

Concurrency versus CPU overhead

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

9-12 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Two locking strategies

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

Segmented table space example


Table Space
Table

Table

Page(s)
or
Row(s)

OR

Copyright IBM Corporation 2011

Figure 9-8. Two locking strategies

CV8317.0

Notes:

Strategy 1

DB2 locks only the table.

cl

In this case, all rows in the table will be affected to the same degree.

Ex

Strategy 2

DB2 locks the table and the underlying pages or rows.


Note

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.

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-13

Student Notebook

Page or row locks


Page / row lock modes of S, U and X

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)

Lock owner can read but not update


Others may hold S
Owner can promote U to X

May cause a suspension if others hold S

Reduces deadlocks

Exclusive (X)

Only lock owner can read or update


Others can read with UR (see later)

Copyright IBM Corporation 2011

Figure 9-9. Page or row locks

CV8317.0

Notes:

Page or row lock mode

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.

9-14 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

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-15

Student Notebook

Lock mode compatibility

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

Page or row locks

Held
Lock

Requested Lock
S

Yes

Yes

No

Yes

No

No

No

No

No

Copyright IBM Corporation 2011

Figure 9-10. Lock mode compatibility

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.

9-16 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Table, table space and partition locks


User 1 uses strategy 2 locking

User 2 uses strategy 1 locking

Example: SELECT . . .

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

Example:
LOCK TABLE . . .
IN EXCLUSIVE MODE

Assume LOCKSIZE PAGE or ROW

Table / table space / partition

IS

Page
or
row

Page
or
row

Page
or
row

Page
or
row

Page
or
row

Copyright IBM Corporation 2011

Figure 9-11. Table, table space and partition locks

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

1. IS, IX or SIX, sometimes known as intent lock mode


2. S, U or X, sometimes known as gross lock mode
IS, IX or SIX (Intent lock mode)

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

More about table, table space and partition locks


IS, IX or SIX (Intent lock mode)

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

Intent share (IS)


Intent exclusive (IX)

Share with intent exclusive (SIX)

S, U or X (Gross lock mode)


Share (S)

Update (U)

Exclusive (X)

Copyright IBM Corporation 2011

Figure 9-12. More about table, table space and partition locks

CV8317.0

Notes:

1. IS, IX or SIX (Intent lock mode)


Intent share (IS)

cl

The lock owner can read data in the table, table space or partition but not change it.

Ex

The lock owner acquires S-locks on pages or rows accessed.

Concurrent processes may both read and change data.


Intent exclusive (IX)

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.

9-18 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Share with intent exclusive (SIX)


SIX is actually a hybrid state. From a read perspective it is a gross lock and from a write
perspective it is an intent lock.
The lock owner can read and change data in the table, table space or partition.

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.

2. S, U or X (Gross lock mode)


Share (S)

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.

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-19

Student Notebook

Lock mode compatibility


Table, table space and partition locks

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

Copyright IBM Corporation 2011

Figure 9-13. Lock mode compatibility

CV8317.0

Notes:

pr

Ex

cl

The visual shows whether or not table / table space / partition locks of any two modes are
compatible.

9-20 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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 want only committed


data, possibly avoiding
locking overheads and
with minimal impact on
other users
User 1

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

WHERE SALARY >


(SELECT AVG (SALARY) FROM EMPLOYEE)
WITH RR

SELECT AVG (SALARY)


FROM EMPLOYEE
WITH UR

Use ISOLATION UR

Use ISOLATION RR

Copyright IBM Corporation 2011

Figure 9-14. Isolation levels

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

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-21

Student Notebook

Cursor Stability (CS)


DECLARE C1 CURSOR FOR
SELECT COLA,COLB,....
FROM TABLEX
WITH CS

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

OPEN C1

FETCH C1 INTO :HVCOLA,....


FETCH C1 INTO :HVCOLA,....
.
.
.
.
.
CLOSE C1

User 1 does not want to read uncommitted data.

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

DB2 never practises lock avoidance with U locks.

Repeatable Read (RR)

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.

9-22 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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
.

Read Stability (RS)


A row or page lock is held only for qualifying rows or pages that are returned to an
application at least until the next commit point.

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)

User 3 acquires no page or row locks.


Advantages:

Less overhead

Any updaters in the system do not cause lock waits to user 3.


User 3 does not cause lock waits to any updaters.

Considerations:

User 3 is at risk from reading uncommitted data.

pr

Ex

cl

A moving row may be counted twice or not at all.

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-23

Student Notebook

SKIP LOCKED DATA

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

SKIP LOCKED DATA option allows a transaction to skip rows or pages


that are incompatibly locked by other transactions
Can be specified in SELECT, SELECT INTO, Searched UPDATE,
Searched DELETE, and PREPARE statements and in UNLOAD utility
Isolation levels (CS and RS) and lock sizes (Row and Page)
Lock mode compatibility (same as before)

Example of inconsistent or incomplete result


Table T(C1 INTEGER
C2 CHAR(4))
C1
C2
1
AAAA
2
BBBB
3
CCCC
4
DDDD

Transaction A: UPDATE T SET C1=99 WHERE C1<3


Transaction B: SELECT COUNT(*) FROM T
WHERE C2>=AAAA SKIP LOCKED DATA
Assuming no index on C2, returns a value 2
Copyright IBM Corporation 2011

Figure 9-15. SKIP LOCKED DATA

CV8317.0

Notes:

DB2 transaction locking:

DB2 locking allows inter-process concurrency and avoids data integrity problems

cl

A transaction is suspended when it requests a lock that is already held by another


transaction and that lock cannot be shared

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.

9-24 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

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-25

Student Notebook

Duration of page and row locks


Influenced by choice of isolation level

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

Note: With CS, S locks may be avoided as shown by brackets (S)


Copyright IBM Corporation 2011

Figure 9-16. Duration of page and row locks

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

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Duration of table, table space and partition locks


Use/
Deallocate
TS TS TS
A B C

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

LOCK TBL TSB

UPDATE

TSB

COMMIT
SELECT

TSC

COMMIT

Plan deallocation

Copyright IBM Corporation 2011

Figure 9-17. Duration of table, table space and partition locks

CV8317.0

Notes:

BIND options ACQUIRE(USE) and RELEASE(DEALLOCATE / COMMIT) control the point


at which the table, table space or partition locks are acquired and released.

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.

RELEASE(DEALLOCATE / COMMIT) option

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

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-27

Student Notebook

Lock avoidance

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

DB2 knows the row is committed


- without acquiring an S lock

'timestamp' of last update

page header

If 'timestamp' of last update older than


start time of any open unit of recovery
updating the table space containing page
X, all rows on the page are committed.

TABLE
PAGE X

Applies only to S locks


Applies only to isolation level CS
Needs BIND ... CURRENTDATA(NO)
Copyright IBM Corporation 2011

Figure 9-18. Lock avoidance

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.

9-28 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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 space lock

Table lock

Row lock

Page lock

Partitioned and Universal table space


Partition lock

Row lock

Page lock

Copyright IBM Corporation 2011

Figure 9-19. Lock escalation

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.

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-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

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Uempty

Lock escalation example


Partitioned table space with 7 partitions
Before Escalation

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

Copyright IBM Corporation 2011

USER 2 USER 1
USER 2

Figure 9-20. Lock escalation example

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.

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-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

Share lock can now be granted

Copyright IBM Corporation 2011

Figure 9-21. COMMIT interval

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.

9-32 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Copyright IBM Corporation 2011

Figure 9-22. Timeouts

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

All processes that hold the conflicting lock release it or

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

The DSNZPARM IRLMRWT corresponds to RESOURCE TIMEOUT field on the DB2


installation panel INSTALL DB2 - IRLM PANEL 1 (DSNTIPI). It specifies the number of
seconds IRLM waits before detecting a timeout. Acceptable values are 1 to 3600 seconds.
The default is 30 seconds.

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-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

Copyright IBM Corporation 2011

Figure 9-23. Deadlocks

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.

9-34 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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.

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-35

Student Notebook

Some factors which affect locking


1. DML

SQL statements

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

2. DDL

LOCKSIZE

Specification for each table space accessed

LOCKMAX
MAXROWS

3. BIND

Release time for table space, partition and table locks

(RELEASE - DEALLOCATE / COMMIT)

Duration of page/row locks

Lock avoidance

(ISOLATION - CS / RS / RR)

(Possibly with ISOLATION - CS)


(Always with ISOLATION - UR)

4. Programming

Design and coding


COMMIT frequency
LOCK TABLE IN S/X MODE

5. DSNZPARM thresholds

NUMLKTS and NUMLKUS

Copyright IBM Corporation 2011

Figure 9-24. Some factors which affect locking

CV8317.0

Notes:

pr

Ex

cl

Here is a summary of the main tuning knobs for locking.

9-36 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

Copyright IBM Corporation 2011

Figure 9-25. Unit summary

CV8317.0

pr

Ex

cl

Notes:

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-37

pr

Ex

cl

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

Student Notebook

9-38 DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

Bibliography
DB2 10 for z/OS system reference library
Administration Guide

SC19-2969

Application Programming & SQL Guide

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

SC19-2968

Application Programming Guide and Reference for Java

GC19-2971

Codes

SC19-2972

Command Reference

SC19-2973

Data Sharing: Planning and Administration

GC19-2974

Installation and Migration Guide

SC19-2975

Internationalization Guide

SC19-2976

Introduction to DB2 for z/OS

GC19-2977

Licensed Program Specifications

SC19-2978

Managing Performance

GC19-2979

Messages

SC19-2980

ODBC Guide and Reference

SC19-2981

pureXML Guide

SC19-2982

RACF Access Control Module Guide

SC19-2983

SQL Reference

SC19-2984

Utility Guide and Reference

GC19-2985

Whats New?

GC19-2986

IBM Spatial Support for DB2 10 for z/OS

GC19-2987

IBM OmniFind Text Search Server for DB2 for z/OS

GC19-2666

IRLM Messages and Codes for IMS and DB2 for z/OS

LY37-3220

Diagnosis Guide and Reference

GI10-8829

DB2 10 for z/OS Program Directory

GI10-8830

z/OS Application Connectivity to DB2 for z/OS

GI10-8840

DB2 Utilities Suite for z/OS, V10, Program Directory

GI10-8842

DB2 Accessories Suite for z/OS Program Directory

cl

SC19-2970

pr

Ex

bibl

Copyright IBM Corp. 1993, 2011


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

Bibliography

X-1

Student Notebook

DB2 Version 9.1 for z/OS system reference library


Administration Guide

SC18-9841

Application Programming & SQL Guide

SC18-9842

Application Programming Guide and Reference for Java

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

Data Sharing: Planning and Administration

LY37-3218

Diagnosis Guide and Reference

LY37-3219

Diagnostic Quick Reference

GC18-9846

Installation Guide

SC19-1161

Internationalization Guide

SC18-9847

Introduction to DB2

GC18-9848

Licensed Program Specifications

GC18-9849

Messages

SC18-9850

ODBC Guide and Reference

SC18-9851

Performance Monitoring and Tuning Guide

GI10-8737

Program Directory

SC18-9852

RACF Access Control Module Guide

SC18-9853

Reference for Remote DRDA Requesters and Servers

SX26-3854

Reference Summary

SC18-9854

SQL Reference

SC18-9855

Utility Guide and Reference

GC18-9856

Whats New?

SC18- 9857

XML Extender Administration and Programming

SC18-9858

XML Guide

pr

Ex

cl

SC18-9844

X-2

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

V5.4.0.3
Student Notebook

DB2 UDB for z/OS Version 8 system reference library


Administration Guide

SC18-7415

Application Programming and SQL Guide

SC18-7414

Application Programming Guide and Reference for Java

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

Data Sharing: Planning and Administration

LY37-3201

Diagnosis Guide and Reference

GC18-9602

Installation Guide

GC18-7422

Messages

SC18-7423

ODBC Guide and Reference

SC18-7433

RACF Access Control Module Guide

SC18-7424

Reference for Remote DRDA Requesters and Servers

SX26-3853

Reference Summary

SC18-7425

Release Planning Guide

SC18-7426

SQL Reference

SC18-7427

Utility Guide and Reference

GC18-7428

What's New?

SC18-7431

XML Extender Administration and Programming

cl

SC18-7416

pr

Ex

bibl

Copyright IBM Corp. 1993, 2011


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

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

DB2 10 for z/OS DB Admin Workshop Part 1

Copyright IBM Corp. 1993, 2011

Course materials may not be reproduced in whole or in part


without the prior written permission of IBM.

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

backpg

pr

cl

Ex
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