Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Intelligence
Data
Centre
Cloud
Mobility
Student Manual
Comunidad de Madrid
UNIN EUROPEA
FONDO SOCIAL EUROPEO
El Fondo Social Europeo invierte en tu futuro
EDUCATION
S
V6.0
cover
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Front cover
DB2 Family
Fundamentals
(Course code CE031)
Student Notebook
pr
Ex
cl
ERC 7.0
Student Notebook
Trademarks
IBM is a registered trademark of International Business Machines Corporation.
The following are trademarks of International Business Machines Corporation in the United
States, or other countries, or both:
CICS
DB2
IMS
MQSeries
OS/390
QMF
REXX
z/OS
Cube Views
DRDA
Informix
MVS
OS/400
RACF
S/390
zSeries
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
AIX
DataPropagator
eServer
iSeries
MVS/SP
Parallel Sysplex
Rational
WebSphere
Linux is a registered trademark of Linus Torvalds in the United States, other countries, or
both.
Ex
cl
Other company, product, or service names may be trademarks or service marks of others.
pr
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.
V6.0
Student Notebook
Contents
Trademarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
Course description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Agenda . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
cl
pr
Ex
TOC
Contents
iii
Student Notebook
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
iv
V6.0
Student Notebook
Checkpoint (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-76
Checkpoint (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-77
Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-78
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
TOC
Contents
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
vi
V6.0
Student Notebook
What is XML? (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The XML document tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Two ways to query XML data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Checkpoint (1 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Checkpoint (2 of 2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Unit summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4-48
4-49
4-51
4-53
4-54
4-55
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
cl
pr
Ex
TOC
Contents
vii
Student Notebook
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
viii
V6.0
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
cl
pr
Ex
TOC
Contents
ix
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Student Notebook
V6.0
Student Notebook
Trademarks
The reader should recognize that the following terms, which appear in the content of this
training document, are official trademarks of IBM or other companies:
IBM is a registered trademark of International Business Machines Corporation.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
The following are trademarks of International Business Machines Corporation in the United
States, or other countries, or both:
AIX
DataPropagator
eServer
iSeries
MVS/SP
Parallel Sysplex
Rational
WebSphere
CICS
DB2
IMS
MQSeries
OS/390
QMF
REXX
z/OS
Cube Views
DRDA
Informix
MVS
OS/400
RACF
S/390
zSeries
cl
Linux is a registered trademark of Linus Torvalds in the United States, other countries, or
both.
Other company, product, or service names may be trademarks or service marks of others.
pr
Ex
TMK
Trademarks
xi
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Student Notebook
xii
V6.0
Student Notebook
Course description
DB2 Family Fundamentals
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Duration: 2 days
Purpose
This course provides you with information about the functions of IBM's
DB2, a relational database manager which may be installed under a
variety of operating systems on many hardware platforms.
DB2 runs under the z/OS, VM, Linux, UNIX, and Windows operating
systems, to name a few.
Audience
Prerequisites
Objectives
cl
pr
Ex
pref
Course description
xiii
Student Notebook
Contents
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Understanding a table
pr
Ex
cl
xiv
V6.0
Student Notebook
Agenda
Day 1
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Welcome
Understanding a Table
Data Modeling and Database Design
How does a User use DB2?
Day 2
cl
pr
Ex
pref
Agenda
xv
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Student Notebook
xvi
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
This unit introduces the DB2 Database Manager and the basic
concepts behind a table.
pr
Ex
cl
Checkpoint questions
1-1
Student Notebook
Unit objectives
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
cl
Database managers are sophisticated software programs that maintain our data. Each
database manager provides a set of automated services. We will discuss some of the more
commonly used DB2 services in this course.
Ex
The word relation is simply a synonym for the word table. A relational database is one in
which the data is perceived as being stored in rows and columns. Relational database
managers understand SQL, the programming language for relational databases. End
users and applications access DB2 data through SQL.
pr
In this unit we will discuss the basic structure of a table, and will mention three
characteristics that are assigned to all columns. We will discuss potential tasks associated
with DB2, and the various I/T roles that support and use the DB2 environment.
1-2
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Phone book
COMPANY
DIRECTORY
CE0317.0
Notes:
cl
We work with tables of data every day. An Excel spreadsheet usually consists of rows and
columns. A phone book is another example of a table of data. Each page has the same
columns (name, address, and phone number), just different rows. Phone books are in
alphabetical sequence. The rows in a relational database may or may not be in a particular
order.
pr
Ex
1-3
Student Notebook
DB2, manage
the company phone
directory
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DB2
Tables - R - Us
Integrity and
Recovery
Phone book
COMPANY
DIRECTORY
Continuous
Operation
DBA
Logging
Optimizer
Lock
Manager
Security
CE0317.0
Notes:
cl
Logging Saving before and after images (called UNDO and REDO records) of any
DB2 rows changed by SQL (INSERT, UPDATE, DELETE).
Ex
Optimization Minimizing the use of system resources (CPU cycles and physical I/O)
when performing SQL queries and doing other DB2 work
pr
Locking Serializing the access to DB2 data in order to allow concurrency, while
guaranteeing data integrity
Recovery Restoring DB2 data due to outages, system failures or corruption by incorrect
program logic
Data integrity See the discussion below
1-4
V5.4
Student Notebook
Uempty
Database theory demands that all changes to data follow the ACID model. From examples
during this course, you will see that DB2 does apply the characteristics of this model:
ATOMICITY refers to the ability of the DBMS to guarantee that either all of the tasks of a
transaction are performed or none of them are. A transfer of funds can be completed or it
can fail for a multitude of reasons, but atomicity guarantees that one account won't be
debited if the other is not credited as well. It is the concept of all or nothing.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CONSISTENCY refers to the database being in a legal state when the transaction begins
and when it ends. This means that a transaction can't break the rules, or integrity
constraints, of the database. If an integrity constraint states that no salaries can exceed
$100,000, then any transaction violating this rule will be aborted. DB2 ensures that only
valid data is allowed within DB2.
pr
Ex
cl
IBMs DB2 adheres to these guidelines, and therefore passes the ACID test!
1-5
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
"RELATION"
TABLE
(Data is stored in
columns and rows)
OWNER1.TABLE_A
COL1
ABC
DEF
GHI
JKL
MNO
COL2 COL3
7
4
6
7
0
1987-11-14
1963-01-01
1946-07-12
1951-12-30
1992-08-12
PROD.PARTS
A
Relational
Database"
is a "Table
Database"
PROD
BOLT
NUT
SCREW
BOARD
WIRE
QTY
7
4
6
7
0
SUPPLIER
ABC CORP
THE NUT CO
FASTEN INC
ECM CORP
BINDINGS
CE0317.0
Notes:
Ex
cl
pr
1-6
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Seattle
PARTS
Chicago
Atlanta
PROD. PARTS
ATLANTA.PROD. PARTS
CE0317.0
Notes:
cl
Every table in DB2 must be given a unique name. A table always has two parts, separated
by a period a schema/owner/creator name, followed by the table name. Optionally, if
there are multiple DB2 systems connected together, it may become necessary to qualify
the table with a location name as well. When that is the case, the location name comes
first.
pr
Ex
In the visual above, we refer to PARTS as the simple table name. There could be many
schemas or owners with a DB2 table named PARTS. There could be a TEST.PARTS, a
QA.PARTS and a PROD.PARTS a PARTS table each for a Test, a Quality Assurance
and a Production environment. PROD.PARTS, which is referred to as a qualified table
name, is the PARTS table that was created by the schema/owner known as PROD. Finally,
if our company had manufacturing facilities in Atlanta, Chicago and Seattle, and we needed
to maintain a PROD.PARTS table and DB2 system at each of those locations, we could
fully qualify the table name for example, ATLANTA.PROD.PARTS.
Each simple table name must be unique within a schema. Each qualified table name must
be unique within a location.
Copyright IBM Corp. 1997, 2011
1-7
Student Notebook
DB 2
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Tables - R - Us
DB A
CE0317.0
Notes:
pr
Ex
cl
Talking to DB2 in English, French, or German, and so forth, would be too cumbersome.
What is needed in order to more effectively communicate with DB2 is a language that is
more precise (accurate and specific), and more concise (shorter, abbreviated).
1-8
V5.4
Student Notebook
The language of
Relational Database Management Systems
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Uempty
WHAT data to
TRUCTURED
retrieve,
NOT HOW to
retrieve it
UERY
ANGUAGE
CE0317.0
Notes:
Ex
cl
SQL is a non-navigational language. What does that mean? With some databases, the
calls to access the data instruct the system to navigate the structure of the database
objects. But with DB2, through SQL, the user tells DB2 what data to access, but not how
to retrieve it. Rather, with SQL, the access path to the data is determined by the DB2
Optimizer. DB2 always strives to give the user the best possible performance when
accessing the data, by minimizing the consumption of system resources (I/Os and CPU
cycles).
pr
The SQL language is quite powerful, and is continually being enhanced with new functions.
Oftentimes, there may be more than one way to write a query in order to obtain the results
dictated by the logic requirements of the application program. So in that sense, the
language is somewhat flexible.
However, as the S implies in the acronym, the language is structured. There is a format
that needs to be followed, there are syntax rules, there are keywords that must be used,
etc.
We will go into some more detail about SQL later on during the course.
Copyright IBM Corp. 1997, 2011
1-9
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Table name
DBA
DBA
CE0317.0
Notes:
cl
Name
Ex
pr
Some datatypes automatically imply a certain length, so for those datatypes, no length
specification is necessary (or allowed). With other datatypes, the user needs to specify the
desired length (and for decimal numbers, both a precision and scale).
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
( ID
INTEGER
NOT NULL
LASTNAME
CHAR(15)
FIRSTNAME
CHAR(15)
ADDRESS
VARCHAR(45)
NOT NULL
NOT NULL
Column
description
area
WITH DEFAULT,
CE0317.0
Notes:
cl
Here is the basic syntax required for creating a table in DB2, using SQL. The CREATE
statement above tells DB2 to make a definition in the DB2 catalog. Here are some of the
details regarding this table.
Regarding the name of the table:
Ex
pr
1-11
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
All values in the third column will be stored with the actual length of the value.
This column must always contain a valid value (i.e. not be NULL). If the INSERT
application does not provide a value for ADDRESS, DB2 will place blanks (which is the
default for CHAR and VARCHAR datatypes) into the column.
Notice that the column definitions immediately follow the table name, and are delimited by
an open parenthesis and a close parenthesis. If the user needs to specify other
parameters or keywords for this table, it must follow the column definitions. This is part of
the syntactical requirement of the CREATE TABLE statement.
pr
Ex
cl
Also notice that in the example above, the name, datatype and NULL characteristic for
each row is aligned with the other rows, for readabilitys sake. That is NOT a syntactical
requirement of SQL, but it is a highly recommended practice to follow.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Tables - R - Us
Integrity
&
Recovery
Application
Manager
Continuous
Operation
Logging
Optimizer
Lock
Manager
Application
Application
Application
Security
Application
Application
Application
Data
Tables
Application
Programs
CE0317.0
Notes:
The user must be authorized to log on to the system, which normally would involve a
security mechanism that is external to DB2.
cl
The application manager loads the program into memory, and begins execution.
Ex
The application manager passes the authorization ID to DB2. DB2 then does an internal
security check, by reading the data in the DB2 directory tables that controls the access to
all DB2 objects in the system.
pr
If DB2 verifies that the authorization ID is authorized to do what it is trying to do, then the
SQL request is executed on behalf of the application.
DB2 returns messages (and perhaps data) to the application.
1-13
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DB2
Tables - R - Us
Lock
Integrity
&
Recovery
Passes user's ID
to DB2
Continuous
Operation
Lock
Manager
Logging
Optimizer
Lock
Manager
Security
Read
into DB2
buffer
Data
Is user permitted
to update salary?
Catalog
CE0317.0
Notes:
Here the scenario is more complex, so we will explain it in the next two visuals.
The user logs on.
cl
Ex
DB2 verifies that user is permitted to UPDATE the column named SALARY.
DB2 locks the data that will be changed, since it must ensure that no other users can
change it at the same time, in order to maintain data integrity.
pr
DB2 reads the data that will be changed into the buffer pools.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Tables - R - Us
DB2
Integrity
&
Recovery
Continuous
Operation
Logging
Optimizer
Write
Lock
Manager
Data
Security
Write/Commit
Logging
LOG
Figure 1-12. How DB2 handles an SQL Change request (part II)
CE0317.0
Notes:
DB2 writes a before update copy of the data to the log (an UNDO record).
DB2 updates the row.
cl
DB2 writes an after update copy of the data to the log (a REDO record).
Ex
DB2 writes the data to the table (the writes to external DASD may be batched and may
not actually occur at this time it may be an asynchronous, rather than a synchronous
process).
pr
DB2 releases the lock on the data, thereby making the DB2 data available once again to
other DB2 users and application programs.
DB2 returns a message to the user/application as to the success or failure of the SQL
statement.
1-15
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Business
Information
Dept
1 11
1 M1
Employees
M 1M
A
location
has many
departments
Many
employees
work on many
projects
Locations
Ah, so
that's how
it all works
together!
Projects
1 M
Skills
Long-term framework
CE0317.0
Notes:
cl
Ex
Data modeling then transforms the business entities into data entities. The
characteristics (attributes) of the business entities become columns in those tables.
pr
The characteristics (attributes) of the business entities become columns in the tables.
In order to implement business rules, tables may also then subsequently contain keys,
check constraints, referential integrity constraints, and triggers.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Installation
of RDBMS
System
Admin
Original grantor
of all privileges
Creation of
system-level
objects
System
performance
CE0317.0
Notes:
The system administrator does the physical installation of the DB2 product onto the
hardware platform, under the operating system.
cl
Ex
The system administrator grants other authorized DB2 users the appropriate privileges
needed by them to perform their roles and responsibilities.
The system administrator monitors the overall DB2 system performance.
pr
Be aware that with the most current versions of DB2 for z/OS and LUW, there is now a new
security administrator authority, called SECADM. Customers may opt to choose this
alternative approach to administer the security function, instead of the approach listed in
bullet three above.
1-17
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Grant privileges:
INSERT, SELECT,
UPDATE, DELETE,
LOAD, BIND, etc.
Creating
Database objects:
tables, indexes,
views, triggers, etc.
DBA
Maintenance of
database objects:
Loading data
gathering statistics
making backups,
recovering data,
Etc.
Binding:
packages,
plans
CE0317.0
Notes:
The Database Administrator (DBA) is responsible for building and maintaining the
application-related (as contrasted with system-level) DB2 objects within the database.
cl
The DBA grants to other authorized DB2 users, the privileges to access and/or change
DB2 objects to fulfill their responsibilities.
Ex
The DBA may perform the production binds (preparing application programs to work with
DB2).
pr
The DBA is usually responsible for the running of most DB2 utilities for purposes of
backup and recovery, re-organizing data, keeping statistics current, etc.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Creating
test
environment
Programmer
Coding:
programs,
stored
procedures,
SQL, and so on
Prototyping
SQL
"explaining"
SQL
Binding:
Packages,
Plans
CE0317.0
Notes:
Programmers may be involved with creating test environments for new applications.
cl
Programmers will prototype SQL (write and test the SQL before it is used in production),
to ensure proper logic and functioning of those SQL statements..
Ex
Programmers may be concerned with the efficiency of their SQL. EXPLAIN is a DB2
function that tells you how DB2 is going to execute our SQL.
Programmers physically embed SQL statements into host language programs.
pr
Programmers will prepare (pre-compile and bind) their programs for execution in the test
environment.
1-19
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Run business
applications
End User
Change
DB2 data
Produce
reports
Write SQL
queries
CE0317.0
Notes:
cl
The SQL within the applications may be adding data (INSERT), reading data (SELECT),
changing data (UPDATE) or removing data (DELETE).
pr
Ex
The user may construct (write) their own queries that manipulate data.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Load
Backup
Operator
Production
Application
Reorg
CE0317.0
Notes:
cl
Operators are often granted the authority to run many application programs and utilities.
Operators take some of the maintenance burden off the shoulders of the DB2 system
administrators and DBAs, by running routine, thoroughly-tested, regularly- scheduled
utilities and production applications.
pr
Ex
Those utility and application programs were developed and tested by the DBAs and
application programmers for proper functioning, before becoming part of the regular
production schedule. If any problem arises during the execution of such programs or
utilities, the operator will contact the person responsible for the program or utility, before
proceeding further.
Operators normally do not have data access authority (they can't look at the data, unless
specifically authorized to do so).
1-21
Student Notebook
Checkpoint
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
cl
2.
Ex
3.
4.
pr
5.
V5.4
Student Notebook
Uempty
Unit summary
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
pr
Ex
cl
Every column has a column name, a data type assigned to it, and a null characteristic.
1-23
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Student Notebook
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
Checkpoint questions
2-1
Student Notebook
Unit objectives
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
pr
Ex
cl
Notes:
2-2
V5.4
Student Notebook
Uempty
Business modeling
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Hmm, must
focus
on the business!
Business
Information
CE0317.0
Notes:
cl
Ex
pr
Instead, the business model can span an entire enterprise, or a division of the organization;
it requires a solid understanding of the business, and can serve as a verification of the
users' view of the business before the database is even designed.
There are many parts in a business model, and there is no formal definition of what parts
must exist to be a valid model. It can be as simple as just naming the major business
entities or objects (customer, product, invoice, order, salesperson, vendor, purchase
2-3
Student Notebook
order, and so forth) and their relationship to each other (a customer has many invoices, but
an invoice has only one customer). This is commonly manifested in an Entity Relationship
Diagram (ERD): a pictorial representation of the user's view of the business.
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
The ERD model and process may go further and convert the business entities into data
entities whereby many-to-many relationships are resolved, primary and foreign keys are
identified, and repeating data elements are discovered and result in additional data entities.
There are also ER tools that can transform the data model into the SQL DDL (data
definition language) statements that will create the necessary objects for a specified DB2
platform, to implement the data model.
2-4
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Problem Statement
Entity Relationship
Model
Process Inventory
Conceptual
View
Data Inventory
Tables
Indexes
Integrity Rules
Logical View
Storage
View
Triggers
CE0317.0
Notes:
Ex
cl
When designing the database for an application domain, you start with a problem
statement for the application domain, that is, a document describing the types of business
objects for the application domain, the relationships between them, and the business
constraints for them. The problem statement must be established by an application domain
expert (analyst). In general, it is not produced by the database designer who does not have
the domain expertise; rather it is input for the data designer.
pr
Starting with the problem statement, a series of steps is performed during the design.
These steps look at the data of the application domain from three different angles, called
views:
The conceptual view
The storage view
The logical view
For each of these views, a set of results is produced during database design. You can
associate a view with its results and describe it by its results.
Copyright IBM Corp. 1997, 2011
2-5
Student Notebook
The conceptual view scrutinizes and structures the data of the application domain based
on their semantic meaning, that is, their meaning for the business (application domain). It
does this independently of the business processes accessing the data and without regard
to any existing or planned method for storing the data. Thus, during the conceptual view,
the process and implementation-independent architecture of the data of the
application domain is established.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
The storage view looks at the data of the application domain from a storage point of view.
During the storage view, in a series of steps, the objects of the conceptual view are
mapped into objects (in particular, tables and associated DB2 objects) of the relational
database management system chosen for the implementation of the data. Thus, the
storage view is not an implementation-independent view of the data. Rather, it is an
implementation-oriented view of the data during which the conceptual view is physically
implemented in the selected relational database management system.
The logical view looks at the data of the application domain from a process point of view.
Generally, a particular business process does not access all data of the application
domain, but only a part of the data. Thus, it has its own process-dependent view of the data
of the application domain. Accordingly, during the logical view, the process-dependent
views for the business processes of the application domain are established.
pr
Ex
cl
Of course, it is very important that you solicit and consider the opinions and input from
management, business analysts, end-users, DBAs, etc. during the modeling process.
2-6
V5.4
Student Notebook
Uempty
Problem Statement
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
GOAL:
Implement a
personnel database
system
Employees
Projects
Departments
Locations
Skills
CE0317.0
Notes:
cl
A problem statement declares the business purpose of the database and its related
applications in a few sentences. This problem or purpose statement can be referred to
during the modeling process. The statement may also be a dynamic document, since over
time there may be circumstances which arise that necessitate changes in your database
design and applications.
Ex
A short textual description of the application domain: what it does, and what it proposes to
accomplish
pr
A listing of all business object types (know as entities), such as Employees, Departments,
Projects, etc.
2-7
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Problem Statement
Entity Relationship
Model
Process Inventory
Conceptual
View
Data Inventory
Tables
Indexes
Integrity Rules
Storage
View
Logical View
Triggers
CE0317.0
Notes:
cl
The development of an entity-relationship model for an application domain is the first step
of the conceptual view during which the process and implementation-independent
architecture of the data of the application domain is established.
Ex
To develop the database from here, you must then determine the data that should be
contained in the database. This means that you must establish a list of all data for the
application domain, that is, the data inventory. Also a list of all business processes for the
application domain must be established, in a process inventory. These both should be
independent of any implementation considerations.
pr
In order to determine what should go into the data inventory, a formal meeting (or series of
meetings) should be conducted, involving all interested parties. A list of all the data items
you want DB2 to store needs to be produced.
2-8
V5.4
Student Notebook
Uempty
Many
employees
work on many
projects
Locations
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Dept
A
location
has many
departments
1 11
1 M1
Employees
M 1M
Projects
1 M
Skills
CE0317.0
Notes:
In entity-relationship (ER) modeling, you identify the components of the business, the
attributes of those entities, and the relationship between the various entities.
cl
Draw lines between related entities and notate the relationships. Relationships between
business entities can be one-to-one, one-to-many, or many-to-many.
Ex
Business modeling, then, includes at least two steps of the database design process:
Defining the entities
pr
In this diagram, we see that there are relationships between several pairs of tables. The
relationships can be verbalized by reading from the base of an arrow toward the point of
the arrow. (For instance, the first horizontal relationship line can be read "One location can
have many departments". The second horizontal relationship line can be read "One
department can have one location". The word following can have should represent the
maximum number of related occurrences.)
2-9
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Database designer
CE0317.0
Notes:
cl
The data inventory contains a detailed description of all data for the application domain: It
describes all abstract data types, data elements, and data groups for the application
domain.
Ex
Data elements are indivisible pieces of data. They cannot be divided into smaller pieces
meaningful for the application domain.
pr
In contrast, data groups are sets of logically related (for the application domain) data
elements and/or data groups. This is a recursive definition and implies that data groups can
contain data groups. The data elements or data groups of a data group are referred to as
components of the (owning) data group.
A data group can be viewed as a tree structure of one or more levels whose lowest level
nodes (terminal nodes) are data elements. An example of a data group may be Name of
Person, that is, the name of a person, consisting of data elements Last Name, First Name,
and Middle Initial. Another example of a data group would be Time, consisting of the data
elements Hour, Minute and Seconds.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Employee Number
Name:
Type:
Description:
Datatype:
Lengths:
Employee Number
Data element
Personnel Number of Employee
ALPHANUMERIC
Minimum Length: 6
Maximum Length: 6
Average Length: 6
Number of Digits: -
Domain:
Data Group:
Entity Type: EMPLOYEE
Role: Cardinality: Minimum = 1, Maximum = 1, Average = 1
CE0317.0
Notes:
cl
The above visual illustrates a sample data element for our personnel system - the
employee number. The important attributes of each data element would need to be
determined and recorded.
Ex
Ideally, this information could be placed in a central repository, which would act as an
enterprise-wide data dictionary. In addition to the attributes shown in the visual above, it
may be very useful to also include the meaning of the data element, the location of the
data, any relevant naming standards, etc.
pr
If there is no data dictionary software product in the enterprise, this inventory would need to
be properly documented by other means, and made available to designers and other
persons involved in the processes of design, the creation of database objects and
application development.
2-11
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Problem Statement
Entity Relationship
Model
Process Inventory
Conceptual
View
Data Inventory
Tables
Indexes
Integrity Rules
Logical View
Storage
View
Triggers
CE0317.0
Notes:
pr
Ex
cl
The logical view involves the determination and listing of all the business processes that
will affect or impact our database. This part of the process needs to include the business
expert.
V5.4
Student Notebook
Uempty
Process Inventory
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
inventory
Data inventory also input for application programmers
CE0317.0
Notes:
cl
As already mentioned before, the process inventory contains a detailed description of all
business processes for the application domain. The descriptions should be completely
business oriented so that they are independent of any implementation considerations.
These are the regular day-to-day activities, including entering new data and modifying
existing data.
Ex
The process inventory is input for the application programmers. It must allow them to
understand the business processes for the application domain and to develop the required
programs, queries, reports, etc.
pr
The descriptions for the business processes must identify all data elements and data
groups for the business processes. Since the data elements and data groups are process
independent, they are not described in the process inventory, but in the data inventory. The
business processes only refer to the data elements and data groups in data inventory.
Therefore, the data inventory is also input for the application programmers.
Except for assisting the application domain expert in verifying the entity-relationship model,
the data base designer is not involved in the establishment of the process inventory.
Copyright IBM Corp. 1997, 2011
2-13
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
cl
A business-domain expert will need to identify and document all of the business processes
that impact the business entities within your enterprise. Some processes may impact only
one business entity. Other processes may impact multiple business entities.
pr
Ex
During the verification of the entity-relationship model for a business process, it is checked
to see which entity types would contain the data affected by the business process. It is also
verified that the entity-relationship model contains all relationship types necessary to
navigate from one entity type to another entity type as required by the business process.
V5.4
Student Notebook
Uempty
Ah, so
that's how
it all works
together!
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
A
location
has many
departments
Dept
1 11
Many
employees
work on
many
projects
1 M1
Employees
M 1M
Locations
Projects
1 M
Skills
Long-term framework
CE0317.0
Notes:
Here are some of the principal benefits of modeling your data and the business processes
that access your data:
Ex
cl
It facilitates agreement. When everyone agrees that the model represents the way
business processes are really performed, it ensures that all participants have the same
understanding of those processes.
pr
It provides a base for future growth and enhancement. Over time, the models can be
modified as new applications are developed, and when maintenance of existing
applications becomes necessary. The model serves as a long-term framework
(processes are usually stable and many applications will be developed relative to the
processes) that spans more than one application.
2-15
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Problem Statement
Entity Relationship
Model
Process Inventory
Conceptual
View
Data Inventory
Tables
Indexes
Integrity Rules
Storage
View
Logical View
Triggers
CE0317.0
Notes:
pr
Ex
cl
As part of the conceptual view, the entity-relationship model and the data and process
inventories for the application domain were established. The first step of the storage view
uses the data inventory to construct the entity types and relationship types of the
entity-relationship model and normalizes them. Thus we establish the tables - the
containers for the data of the application domain. Following that will be ancillary database
objects such as integrity rules, indexes, triggers and views (logical data structures).
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Denormalized EMPL
EMP
NO
000030
000250
000270
000300
LAST
NAME
KWAN
SMITH
PEREZ
SMITH
WORK
DEPT
GRE
BLU
RED
BLU
DEPTNAME
SKILL1
SKILL2
OPERATIONS
PLANNING 002
MARKETING
PLANNING 011
141
011
415
032
067
447
EMPL_SKILL TABLE
LAST
KWAN
SMITH
PEREZ
SMITH
WORK
NAME
GRE
BLU
RED
BLU
SKILLN...
DEPTNAME
DEPT
OPERATIONS
PLANNING
MARKETING
PLANNING
EMPNO
000030
000250
000250
000250
000270
000270
000300
000300
SKILL
141
002
011
067
415
447
011
032
SKILLDESC
RESEARCH
BID PREP
NEGOTIATION
PROD SPEC
BENEFITS ANL
TESTING
NEGOTIATION
INV CONTROL
CE0317.0
Notes:
cl
Initial database design may contain anomalies, which could potentially cause data
inconsistencies during SQL change requests (i.e. INSERT, UPDATE and DELETE
statements). In order to ensure that our applications do not impact data integrity, we test
our database design with the rules of normalization.
Ex
First Normal Form: A table is in the First Normal Form if all its attributes, elementary or
composite, can have at most one value.
pr
Continuing in the process of database design: once the entities, their relationships, the
primary keys, and foreign keys are identified, the next step is to define all the non-key
elements or properties for a given entity. Once the columns are defined, it is time to
consider normalizing your tables. There are six different levels of normalization; the first
three levels are generally followed by serious designers seeking a robust data model.
1. Eliminate Repeating Groups - One of the first rules of relational tables is that all the rows
must have the same number of columns, and each column must mean the same thing in
the same sequence in each row.
2-17
Student Notebook
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
In our example of the employee table, if you wanted to represent the fact that each
employee has multiple skills, this would need to be represented in an additional table
where each skill for a given employee is represented in a row (the unique identifier being
the employee number and the skill number). If a new skill is created and an employee
already has the maximum number of skills allocated in the data structure, then the entire
file structure and all the programs have to be changed (in the denormalized file structure). If
this table is normalized, then the new skill simply becomes a new row.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Normalized - 1NF
Normalized - 2NF
EMPL_SKILL TABLE
EMPNO
000030
000250
000250
000250
000270
000270
000300
000300
SKILL
141
002
011
067
415
447
011
032
SKILLDESC
RESEARCH
BID PREP
NEGOTIATION
PROD SPEC
BENEFITS ANL
TESTING
NEGOTIATION
INV CONTROL
EMPL_SKILL TABLE
EMPNO
000030
000250
000250
000250
000270
000270
000300
000300
SKILL
141
002
011
067
415
447
011
032
DATE
CERT
SKILL
002
011
032
067
141
415
447
SKILLDESC
BID PREP
NEGOTIATION
INV CONTROL
PROD SPEC
RESEARCH
BENEFITS ANL
TESTING
CE0317.0
Notes:
cl
All its elementary non-key attributes are functionally dependent on the entire primary key
Ex
2. Eliminate all attributes (columns) that depend only on part of a multi-valued primary key.
pr
To extend our example above: where we have taken the skills of the corporation and
matched them with the employees in a separate table, we have included the skill
description. Yet, that skill description depends only upon the skill number (not the employee
number/skill number combination). Three problems become apparent:
1. The skill description will be repeated every time the skill is repeated.
2. If this description for the skill changes, then multiple rows will need to be changed,
risking some update anomalies (data inconsistencies due to not changing all the rows).
3. If an employee is the sole source for a given skill and that person leaves the company,
that particular skill and skill description is lost forever (delete anomaly).
Copyright IBM Corp. 1997, 2011
2-19
Student Notebook
These problems can be solved by creating another table that holds the skill number and
skill description.
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Notice that these new tables can have more columns than what is illustrated. For example,
the EMPL_SKILL table can have a column that holds information such as the date when
this employee acquired this particular skill. In this example, the new column
(DATE_CERTIFIED) depends upon both the employee and the specific skill.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Denormalized EMPL
EMP
NO
000030
000250
000270
000300
LAST
NAME
KWAN
SMITH
PEREZ
SMITH
DEPTNAME
MGRNO ...
OPERATIONS
PLANNING
MARKETING
PLANNING
000080
000010
000020
000010
Normalized to 3NF
EMPL
EMP
NO
000030
000250
000270
000300
WORK
DEPT
GRE
BLU
RED
BLU
LAST
NAME
KWAN
SMITH
PEREZ
SMITH
WORK
DEPT
GRE
BLU
RED
BLU
DEPT
...
DEPTNO
BLU
GRE
RED
DEPTNAME
PLANNING
OPERATIONS
MARKETING
MGRNO
000010
000080
000020
CE0317.0
Notes:
cl
Ex
3. Eliminate columns not dependent on the key at all: that is, the column is a fact about
another non-key column.
pr
An example of this is reflected in our Employee table. Notice that we are carrying the
Department Number in the Employee table (necessary to reflect the relationship between
the Employee and Department tables: many employees work for one department), and that
the Department Name column is a fact about the Department Number column (not the
primary key which is the employee number). The same three problems exist here as they
do with the second normal form example (redundant data, update, and delete anomalies)
and can be solved the same way: that is, an additional table where the Department
Number is the primary key, and the Department Name is dependent upon it.
2-21
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Normalization recommendation
CE0317.0
Notes:
As in almost everything we do related to computers, there are some reasons to trade off
one thing for another; this is true with the process of normalization.
pr
Ex
cl
V5.4
Student Notebook
Uempty
Questions to answer
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
pr
Ex
cl
The decision whether to denormalize or not is very dependent upon an analysis of the SQL
statements that will be accessing the table you are desinging. The answers to the
questions above help to determine whether data should be denormalized. If data is
read-only, carrying some redundant data may improve access to the data. If tables are
often accessed together, carrying some redundant data may eliminate having to join tables
in some cases.
2-23
Student Notebook
Normalization/Denormalization example
(EMPNO, WORK DEPT, LAST, FIRST, JOB)
100,000 rows
87 chars / row
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
EMPL
DEPT
CE0317.0
Notes:
pr
Ex
cl
V5.4
Student Notebook
Uempty
Denormalization example
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DEPT
CE0317.0
Notes:
pr
Ex
cl
2-25
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
36 characters
CE0317.0
Notes:
The costs are 900 additional pages of storage (4K each) and additional updates to the
second DEPTNAME column.
pr
Ex
cl
The benefit is avoiding joins of the EMPL and DEPT tables 20,000 times each day. This
will mean a faster response time for the critical transaction, and also more systems
resources being made available for other work in the system.
V5.4
Student Notebook
Uempty
Business entity
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DB2 table
Table 1
Table 3
Entity attributes
Columns in table
Table 2
Entity instances
Rows in table
Entity 1
Entity 2
Entity 3
CE0317.0
Notes:
pr
Ex
cl
When the ER model is complete, the next step is to transform the model objects and
specifications into storage objects in the DB2 catalog. With many ER tools, this is an
automatic process. The tool will use the specifications in the model and automatically
create the SQL DDL (Data Definition Language) statements to create the tables, with the
columns, proper relationships between the tables, etc.
2-27
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
EMPL
DEPT
PROJ
CE0317.0
Notes:
pr
Ex
cl
Each of the business entities that were identified and documented during the modeling
process become the respective tables in DB2. The employee entity becomes the EMPL
table, the department entity becomes the DEPT table, etc.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
EMPNO
EMPL
DEPTNO
DEPTNAME MGRNO
ADMRDEPT ...
DEPT
PROJNO
PROJNAME DEPTNO
RESPEMP ...
PROJ
CE0317.0
Notes:
cl
Each of the business entities that were identified and documented during the modeling
process have certain characteristics or attributes. In other words, there are certain things
that we want to track for each entity. For an employee, we want to have their name, their
date of birth, home address, telephone numbers, etc. We will give them a salary,
determine their job responsibilities, assign them to a particular department, etc.
Ex
All of these data elements related to the employee become the columns in the EMPL table.
pr
After the table has been defined, with all of its column definitions, keys and relationships, it
will be populated with instances or occurrences of the business entity with which the table
is associated. So, Bob Jones and Sally Smith will become rows in the EMPL table.
2-29
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Names
First character
must be:
A-Z, $, @, #
Remaining:
A-Z, $, @, #, 0-9, _
Table name
unique to
owner/schema
Column
names
unique to
table
CE0317.0
Notes:
Column and table names must conform to the following rules, as shown in the visual
above:
cl
Every table, and every column within a table, must be given a name
Each table name must be unique within a schema
Ex
Any name in DB2 may include the alphabetic characters A - Z, the numbers 0 9, and
the special characters @, #, $, and _ (These are known as the standard characters.)
pr
A DB2 name may begin with any of the characters in the bullet point above, EXCEPT
for the underscore
V5.4
Student Notebook
Uempty
Name examples
OK as is
SOC_SEC_#
OK as is
"SAL+COM"
Special character
"UPDATE"
A reserved word
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
TABLE_3
CE0317.0
Notes:
cl
When non-standard characters are used in table or column names, or when using a DB2
reserved word (i.e., a word that has a special meaning to DB2, part of the SQL syntax), the
names must be enclosed in double quotes. When DB2 sees the double quotes, it will
accept the non-standard character, and/or treat the name as a literal, rather than interpret it
as a keyword in SQL syntax.
pr
Ex
In the example above, the first two table names are valid as written, since they contain only
characters described as being included in the standard naming set on the previous slide.
But third and fourth names shown above require quotation marks. We need to enclose
SAL+COM in double quotes because the + is not part of the standard character set for DB2
names. Likewise, we need to use the double quotes around UPDATE, which is a reserved
word in DB2. UPDATE, when not enclosed by double quotes, will be interpreted as a
request to invoke the UPDATE statement in SQL.
For a complete listing of DB2 reserved words, refer to the SQL Reference manual for your
DB2 environment (z/OS or LUW).
2-31
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Schema/Owner
PROD
EMPL
Qualified table-name
PROD.EMPL
Table user
(ANY_OTHER_ID)
PROD.EMPL
Copyright IBM Corporation 2007
CE0317.0
Notes:
cl
Table names consist of two parts, separated by a period. If your userid is identical to the
table qualifier (very often the case because you own the table), you can use just the simple
table name within an SQL statement. In the visual above, a user with an authorization id of
anything other than PROD, must use the qualified table name, PROD.EMPL, in his/her
SQL statements.
pr
Ex
For example, a user who is known to the system as PROD can refer to the simple table
name SELECT * FROM EMPL; A user that currently has an authorization id that is not
equal to PROD must enter the query as SELECT * FROM PROD.EMPL;
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Assigned
Column name
LASTNAME
Table name
EMPL
CE0317.0
Notes:
pr
Ex
cl
Column names can be optionally prefixed (in any query) with their table name. In a join
operation (a SELECT that accesses multiple tables) the column names must be qualified if
those column names appear in more than one of the tables accessed in the query.
2-33
Student Notebook
Correlation names
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
OK column names
are qualified by the
correlation names
CE0317.0
Notes:
cl
A correlation name is a substitute (usually abbreviated) name that you give to a table in an
SQL SELECT statement. In the FROM clause, if you code a separate character string after
the real table name (and before the comma delimiting the name of the next table
accessed), that becomes a correlation name. The other parts of your query can now refer
to the abbreviated table name, rather than the real table name.
pr
Ex
In the second statement above, the user is qualifying the column DEPTNO with the real
table names EMPL and DEPT. In the third statement, the user is substituting the letters E
and D for the tables EMPL and DEPT. This technique uses what we term correlation
names.
The qualification of column names in a join is required if any columns referred to within the
query are identically named. To do this, the query writer may employ either the technique
of using real table names, or correlation names, to qualify the columns.
It is commonly recommended to use either of the techniques in a join, even if there are no
identically-named columns in the query, to self-document and make the query easier to
read and understand.
2-34 DB2 Family Fundamentals
V5.4
Student Notebook
Uempty
Data types
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Numeric
Character
Date/Time
Graphic
LOBs (Large Objects)
User-Defined Distinct Types
XML
CE0317.0
Notes:
pr
Ex
cl
These are all of the general categories of DB2 data types. The next few visuals will
describe the more specific data types within each general category.
2-35
Student Notebook
Numeric formats
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Whole
numbers
SMALLINT
INTEGER
BIG INTEGER
Approximate
numbers
REAL - Single
FLOAT - Double
Fractional
numbers
DECIMAL
(p,s)
CE0317.0
Notes:
cl
There are three integer (whole number) data types. SMALLINT can be used for numbers
between minus 32K and plus 32K. INTEGER accommodates numbers between minus 2
GB and plus 2 GB. More recently BIGINT was added, in order to handle even larger
numbers from minus 8 exabytes to plus 8 exabytes.
Ex
Fractional numbers may be represented by the DECIMAL data type. Precision (p)
determines how many digits (up to a maximum of 31) to be reserved for storing/expressing
the number. Scale (s) must be a non-negative (i.e. either 0 or positive), and indicates how
many places to reserve to the right of the decimal point when displaying the result.
pr
Floating point describes a system for representing real numbers which supports a wide
range of values. Numbers are in general represented approximately to a fixed number of
significant digits and scaled using an exponent.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CHAR (X)
Fixed length
X bytes
VARCHAR (X)
Variable, up to a
max. of X bytes
X bytes
CE0317.0
Notes:
Ex
cl
Fixed character (CHAR) is always stored as a value with X number of bytes. Variable
character (VARCHAR) is stored as the real length of the value for the column, up to a
maximum of X bytes. In order for DB2 to determine the actual length and location of a
VARCHAR, DB2 first creates (at LOAD or INSERT time), and then examines (when
accessing the existing rows) a pointer/displacement within the row for each VARCHAR
column in the row.
pr
The general recommendation is to not user VARCHAR unless the nature of the data stored
in a column is truly going to be variable in nature (such as comments, or another type of
data that could be either quite long or quite short).
2-37
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Date/Time formats
Time
'HH:MM:SS'
'HH:MM AM / PM'
'HH.MM.SS'
Date
'YYYY-MM-DD'
'MM/DD/YYYY'
'DD.MM.YYYY'
Timestamp
'YYYY-MM-DD-HH.MM.SS.NNNNNN'
CE0317.0
Notes:
cl
There are a number of formats for the display of date and time information: Some
common examples are ISO, USA, EUR (for Europe), and JIS (Japanese International
Standard).
pr
Ex
Internally, date, time and timestamp information is stored in a single format packed
decimal. The designations of ISO, USA, etc. only affects how the information is formatted
when displayed to the user or returned to the application program. At the time DB2 is
installed, the default format for date and time is set by the system programmer who is
configuring your DB2 system. That format can be overridden by the application
programmer or query writer, as needed.
DB2 accepts any of the formats (USA, ISO, EUR, JIS) in an SQL statement (character
string representation of dates, times, and timestamps). Thus, all three of the queries below
would execute successfully:
SELECT EMPNO, LASTNAME, BIRTHDATE
FROM EMPLOYEE
2-38 DB2 Family Fundamentals
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
2-39
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
GRAPHIC (X)
VARGRAPHIC
(X)
CE0317.0
Notes:
cl
The word graphic may connote to some users the idea of images bit streams of data
representing the pixels in some kind of picture, map, diagram, etc. But in DB2 the term
graphic does not have this meaning at all.
Ex
Rather, graphic data types are analogous to the CHAR data types, except that in the
graphic data types it takes two bytes to represent each character being stored. The
alternate name for these data types is double byte character sets (DBCS).
pr
Perhaps the most common situation requiring the use of graphic data types is the nature of
Oriental languages, such as Japanese, Chinese, Korean, etc. The Indo-European
languages (French, Italian, Spanish, English, German, Russian, etc.) utilize a set of
characters that are not symbolic the do not represent anything intrinsically. Rather, the
letters only have meaning when strung together in specific combinations. For instance, the
letters A, E, L and P by themselves does not mean anything.
But when a person literate in English sees APPLE, then those same characters denote a
fruit that is somewhat spherical in nature, red/yellow/green when ripe, and grows on trees.
But in the Oriental languages, each object, each action, each word has a specific character
2-40 DB2 Family Fundamentals
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Linguists have judged that in order to have a detailed, effective means of communicating, a
language needs to have approximately 20,000 - 2,5000 words. Therefore, the Oriental
languages need that many characters in order to provide meaningful communication. In
contrast, the Indo-European languages can be accommodated by a much smaller number
of characters, as their alphabets consist in the neighborhood of 22 to 30 letters.
pr
Ex
cl
One byte, consisting of 8 bits, yields 256 (2 raised to the 8th power) combinations.
Therefore it will support all the letters A Z (both upper and lower case), the numbers 0
9, and a relatively robust set of punctuation marks and other special symbols. In contrast,
the Oriental languages need to support a much larger character set. Two bytes, consisting
of 16 bits, yields the necessary amount of combinations (64K, or 2 raised to the 16th power)
to support the larger character set.
2-41
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Action
News
DB2
by the Book
B L OB
I
N
A
R
Y
A
R
G
E
J
E
C
T
C L OB
D B C L OB
H
A
R
A
C
T
E
R
O
U
B
L
E
A
R
G
E
J
E
C
T
Y
T
E
H A
A R
R G
A E
C
T
E
R
J
E
C
T
DB2
CE0317.0
Notes:
cl
BLOBs (Binary Large Objects) can store images and sound data (images, video, audio,
etc).
Ex
CLOBs (Character Large Objects) can store large amounts of character data (usually
text).
pr
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Create table
not null,
with default 'abc',
not null,
logged,
not logged compact)
CE0317.0
Notes:
Ex
cl
The general format of the CREATE statement is similar, regardless of the type of DB2
object that you are defining. The syntax is CREATE object-type object-name options. To
create a table, you would code CREATE TABLE tablename, to create a view you would
code CREATE VIEW viewname, to create a trigger, you would code CREATE TRIGGER
triggername, etc. Of course, in order to give a complete definition, there are other options
and keywords that must follow the object name.
pr
All CREATE statements result in one or more rows being inserted into one or more of the
DB2 catalog tables (such as SYSTABLES, SYSVIEWS, SYSINDEXES, SYSKEYS,
SYSTRIGGERS, etc.)
In the visual above there is a relatively simple example of a CREATE TABLE statement. All
of the column definitions follow the tablename in the syntax, enclosed by parentheses.
Notice that a table can consist of many columns, each storing a different data type:
numeric SMALLINT,
character CHAR, VARCHAR,
Copyright IBM Corp. 1997, 2011
2-43
Student Notebook
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DB2 built-in
Data Types
Customer
Unique
Data Types
Customer
Unique
Functions
CE0317.0
Notes:
cl
DB2 automatically provides its users with a wide variety of data types (known as built-in
data types). However, often there arises a situation where the requirements of the
business application cannot be met with just those built-in or DB2-provided data types.
pr
Ex
For example, an enterprise may be international in scope, having to deal with the
currencies of different nations and different measuring systems (metric vs. English). In that
case, it may become necessary for the DBA to create a new data type, known as
user-defined distinct data type (abbreviated as UDT). It usually then also becomes
necessary for a programmer to write code (known as a user-defined function, or UDF) in
order to take full advantage of the newly created UDT. More about that later in this lecture.
2-45
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DECIMAL
(9,2)
EMPNO
LASTNAME
US_SAL
CAN_SAL
000010
000020
000030
HAAS
THOMPSON
KWAN
52750.00
41250.00
38250.00
31000.86
293.79
29843.29
US_COMM CAN_COMM
4220.00
3300.00
3060.00
344.00
9487.00
4353.00
DBA
CE0317.0
Notes:
Ex
cl
In this example, we have a company whose employees receive parts of their salary and
parts of their commissions in two currencies (US and Canadian dollars). If the data type of
each currency column was DECIMAL, DB2 would let a user add together any of the
decimal columns, as well as allowing the user to compare a column holding US dollars
against a column containing Canadian dollars.
pr
Since the DBA knows that the data in these columns should not be allowed to mingle, the
User-Defined Distinct Types will be defined in order to address this problem.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DBA
CE0317.0
Notes:
Ex
cl
A UDT is created with a CREATE SQL statement. The new data type must be based on an
existing data type. In this example, the DBA is creating a data type called CAN_DOL which
is to have the characteristics of (and be based on) the DECIMAL data type. This is known
as strong typing. In other words, the new data type CAN_DOL is very similar to or
behaves very much like DECIMAL (9,2).
pr
A column assigned this type will allow up to 9 total digits with 2 of those digits to the right of
the decimal point. The WITH COMPARISONS clause allows two columns of this data type
to be compared to each other with logical operators. (... WHERE CAN_SAL > CAN_COMM
...).
2-47
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DBA
NOT NULL,
NOT NULL,
,
,
,
,
CE0317.0
Notes:
pr
Ex
cl
UDTs can be used in a CREATE or ALTER statement just like other data types. In the
example above, the columns CAN_SAL and CAN_COMM have the data type of
CAN_DOL, which was previously defined by the DBA.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DBA
CE0317.0
Notes:
cl
Functions can be created that will allow us to add, subtract, SUM, AVG, and so forth,
columns with the CAN_DOL data type. Creating a User-Defined Function (UDF) is a very
straightforward process.
pr
Ex
In this example, the DBA is creating a function called "+" (plus). It takes as input two
columns of the CAN_DOL data type and returns a value in the CAN_DOL data type. What
happens to the two inputs is the same thing that happens to any two decimal inputs to the
standard "+" function that comes with DB2 (the actions this new function takes are to be
SOURCEd on the existing "+" function). By specifying SOURCE some_function, we are
telling DB2 to make the new function do what the some_function does, but with these new
data types.
This kind of UDF is known as a sourced function, because it was based upon a built-in
function that comes natively with the DB2 software when the RDBMS is installed. So by
coding sourced functions, we manipulate Canadian dollars as we do US dollars (add,
subtract, multiply, divide, etc.) However, we cannot yet exchange or convert or compare
those two data types, as they are distinct from one another.
Copyright IBM Corp. 1997, 2011
2-49
Student Notebook
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Nulls
NULLS: A
column
constraint
telling DB2
what it should
do when the
value for a
column is
unknown
NOT NULL
CE0317.0
Notes:
cl
NULL has the meaning of unknown, or the empty result set. It does NOT mean blanks for
character date and zeroes for numeric data. Be aware that NULL is the default, so that
when you omit the third parameter in a column definition, that column will allow the NULL
value.
Ex
NOT NULL WITH DEFAULT Column must always have a value - if we don't supply
one, DB2 does.
pr
Please note that the way you code a column definition to allow the null attribute is to leave
the third parameter of the column blank. In other words, you simply code the column name
and data type, followed by a comma (delimiting the beginning of the next column to be
defined in the table). In fact, if you key in the word NULL as the third parameter for a
column definition, it results in a syntax error.
Copyright IBM Corp. 1997, 2011
2-51
Student Notebook
It is generally recommended that you do not define columns as being nullable, unless that
is specifically dictated by the database model or the application requirements. Here are the
reasons for avoiding nulls:
1. A null attribute byte takes up one byte of space for each column defined as nullable.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
2. More importantly, DB2 has to do extra work in navigating the row, and checking the null
attribute byte (CPU cycles)
3. A single null value can invalidate a major arithmetic calculation
pr
Ex
cl
4. Programmers usually need to write extra code for error checking and condition handling
when nulls are possibly present.
V5.4
Student Notebook
Uempty
Related data
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
pr
Ex
cl
During the modeling process, the relationships between tables were noted and
documented. The reason you create a relationship between two tables is so that data
entered into one table refers to corresponding data in another table. A row in one table
may be associated with a row or rows in another table. Those relationships can be
specifically defined in DB2.
2-53
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
The primary key is the key that best identifies the data being stored in the table.
Ex
cl
Each occurrence or instance in a table (i.e. a row) must be uniquely identified. Some
thought and analysis should go into the decision as to how to uniquely identify each
instance. That column value (simple key), or that combination of column values
(compound key) which you choose to distinguish one row in the table from all of the other
rows in the table is called the primary key.
pr
For example, our data model might result in the creation of an EMPLOYEE table. How can
we distinguish one employee from every other employee? It is very possible that different
people in the enterprise have the same surname and the same given name. There may be
two Larry Smiths, therefore eliminating the columns LASTNAME and/or FIRSTNAME as
viable candidates for a primary key.
Perhaps a more suitable candidate for the primary key may be Social Security Number, or
state drivers license number. Better still, each employee at the time of hiring can be given
a unique identifier (number or character string).
V5.4
Student Notebook
Uempty
Primary Keys
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Types
SIMPLE - 1 column
COMPOUND multiple columns
CE0317.0
Notes:
A key is termed simple if it consists of values from only one column in the DB2 table. A key
is said to be compound if it consists of values from two or more columns in the DB2 table.
pr
Ex
cl
A primary key must contain unique values, and cannot be NULL. A DB2 table may have
one, and only one, primary key defined.
2-55
Student Notebook
Unique Keys
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Types
SIMPLE - 1 column
COMPOUND multiple columns
CE0317.0
Notes:
cl
A key is termed simple, if it consists of values from only one column in the DB2 table. A
key is said to be compound if it consists of values from two or more columns in the DB2
table.
pr
Ex
A unique key must contain unique values. A DB2 table may have more than one unique
key defined in it. If UNIQUE is specified, then only one row is allowed to contain the NULL
value. If UNIQUE WHERE NOT NULL is specified, then multiple rows may contain NULLs
in the key.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Foreign Key
CE0317.0
Notes:
cl
Foreign keys are incorporated into our tables to define the relationships between tables. A
column from one table may contain the same information stored in the column of a different
table. It is our desire to track this relationship, and to define a set of rules for this
relationship. That is the very core of Referential Integrity (or RI, for short), which we will
discuss shortly.
pr
Ex
In essence, a foreign key is a column or set of columns that contains values from some
table's parent key (either a primary key or a unique key) and is dependent on them.
Parent key is the general term we give to the [either] primary or unique key upon which the
foreign key is dependent. A good analogy is to think of it as a parent/child relationship.
One parent may have many dependents, or children. A parent key often has many foreign
keys depending upon (and related) to it.
2-57
Student Notebook
Locations
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Dept
1 11
1 M1
Many
employees
work on many
projects
Employees
M 1M
1 M
Skills
Projects
CE0317.0
Notes:
Ex
cl
Which table carries the foreign key is determined from the business model. For example,
there is a relationship between the Employees table and the Dept table. Each employee is
assigned to a department. There is a column (WORKDEPT) in the Employee table that
stores the number of the department to which the employee is assigned. The DEPTNO
column in the Dept table is the primary key, and a parent key for WORKDEPT in the
Employee table. In other words, the WORKDEPT foreign key depends upon the DEPTNO.
pr
That relationship is called a referential constraint. Many Employees may work for the same
department, so there is a 1 to many relationship between Dept and Employee. The table
that contains the many is the table that contains the Foreign Key.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
EMPL
PK parent
EMPNO
DEPT
FK
LASTNAME
FIRSTNAME
WORKDEPT
REMAINING
COLUMNS
SUPPRESSED
FK child/dependent
PK
DEPTNO
child/dependent
DEPTNAME
MGRNO
DIVISION
parent
CE0317.0
Notes:
cl
The protocol of Referential Integrity states that when a foreign key is defined, DB2 will not
let a row be added to the dependent table unless the Foreign Key value for that row already
exists in the parent table.
Ex
In the diagram above are two tables, EMPL and DEPT, with two different referential
constraints defined between them.
pr
In the EMPL table, EMPNO is the primary key. It is also the parent key in an RI
constraint, because the MGRNO column in the DEPT table is the foreign key that
depends upon EMPNO. This relationship is highlighted in red in the visual above. In
this RI relationship, EMPL is the parent table and DEPT is the child or dependent table.
In the DEPT table, DEPTNO is the primary key. It is also the parent key in an RI
constraint, because the WORKDEPT column in the EMPL table is the foreign key that
depends upon DEPTNO. This relationship is highlighted in green in the visual above.
In this RI relationship, DEPT is the parent table and EMPL is the child or dependent
table.
2-59
Student Notebook
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
In the EMPNO/MGRNO relationship, DB2 will not allow a DEPT row to be inserted, unless
the value for MGRNO already exists in the EMPNO column of a row in the EMPL table. In
the DEPTNO/WORKDEPT relationship, DB2 will not allow an EMPL row to be inserted,
unless the value for WORKDEPT already exists in the DEPTNO column of a row in the
DEPT table.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
cl
You can define Referential Integrity when you CREATE the table. It can also be added after
the tables have been created, via the ALTER TABLE statement. In the visual above is an
example of defining a referential constraint between two tables.
Ex
The first CREATE TABLE statement defines the DEPT table with DEPTNO as the primary
key.
pr
The second CREATE TABLE statement defines the EMPL table with a foreign key. The
name of the referential constraint is DEPT_VALUE, and that name is determined by the
CONSTRAINT keyword. The WORKDEPT column is designated as the foreign key. The
REFERENCES keyword points to the parent table in the relationship (DEPT). By default,
the parent key is normally the primary key of the table (as in this case, the parent key is
the DEPTNO column in the DEPT table). Thus the WORKDEPT column in the EMPL table
is the foreign key, and the DEPTNO column in the DEPT table is the parent key.
If the parent key is a column (or set of columns) other than the primary key, then the
syntax must specify the column(s), enclosed in parentheses, immediately after the parent
tablename. For example, if the foreign key being defined depended upon a column name
Copyright IBM Corp. 1997, 2011
2-61
Student Notebook
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
PARENT_KEY in the DEPT table, that part of the clause would be . REFERENCES
DEPT (PARENT_KEY).
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
EMPNO
000010
000020
000030
LASTNAME
HAAS
THOMPSON
KWAN
FIRSTNAME
CHRISTINE
MICHAEL
SALLY
WORKDEPT
RED
BLUE
GREEN
GOVT_ID
888-88-2794
888-89-4261
888-88-9456
SALARY
52750.00
31000.00
33000.00
Primary/Unique
(Parent) Key
DEPTNO
BLUE
GREEN
RED
DEPTNAME
PLANNING
OPERATIONS
MARKETING
MANAGER
000020
000080
000010
DIVISION
EASTERN
WESTERN
EASTERN
Figure 2-51. The rules DB2 enforces to in order to maintain Referential Integrity
CE0317.0
Notes:
cl
Primary key values cannot be null. A foreign key contains a set of values that already exists
in a parent key, which is a table's unique or primary key (usually another table, but not
always the case).
pr
Ex
For instance, when a new employee is hired if the HR clerk attempted to insert the new row
and have YELLOW as the value for column WORKDEPT, he/she would receive an error
message from DB2. In order for the INSERT to work, the DEPT table would first need to
have a row with DEPTNO = YELLOW inserted into it.
2-63
Student Notebook
Terminology
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Referential constraint:
the limiting of a set of
foreign key values
to a set of
parent key values
Referential integrity:
the automatic enforcement
of referential constraints
CE0317.0
Notes:
cl
The referential constraint in essence defines the relationship between the two tables
how are they related to each other, which column from each table shares common data,
and which table is the parent and which is the child or dependent.
pr
Ex
V5.4
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Uempty
CE0317.0
Notes:
When you define a referential constraint, referential integrity causes DB2 to enforce these
rules:
cl
1. The key values in a parent key (whether it is a primary key, or just a unique key), must
all be unique and not null.
pr
Ex
2. The key values in a foreign key must either match an existing value in the parent key, or
be null.
2-65
Student Notebook
FIRSTNAME
CHRISTINE
MICHAEL
SALLY
GOVT_ID
888-88-2794
RED
BLUE 888-89-4261
GREEN 888-88-9456
SALARY
52750.00
31000.00
33000.00
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
EMPNO
000010
000020
000030
WORK
DEPT
DEPTNO
BLUE
GREEN
RED
DEPTNAME
PLANNING
OPERATIONS
MARKETING
MANAGER
000020
000080
000010
DIVISION
EASTERN
WESTERN
EASTERN
CE0317.0
Notes:
pr
Ex
cl
The failure is caused by a duplicate EMPNO value (primary key). 000010 is already
assigned to Christine Haas. Attempting to add Ann Dean with the same EMPNO value
returns an SQL error code.
V5.4
Student Notebook
Uempty
FIRSTNAME
CHRISTINE
MICHAEL
SALLY
WORK
DEPT
DEP
GOVT_ID
888-88-2794
RED
BLUE 888-89-4261
GREEN 888-88-9456
SALARY
52750.00
31000.00
33000.00
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
EMPNO
000010
000020
000030
DEPTNO
BLUE
GREEN
RED
DEPTNAME
PLANNING
OPERATIONS
MARKETING
MANAGER
000020
000080
000010
DIVISION
EASTERN
WESTERN
EASTERN
CE0317.0
Notes:
Why did this insert fail? We have now specified a unique value 000110 for Ann Dean as a
new employee. However, now we have a different problem.
pr
Ex
cl
The value for the department to which Ann is being assigned, BROWN, does not exist in
the DEPT table. In order to add a row to EMPL, the value for column WORKDEPT must
already exist in the DEPTNO column of the DEPT table.
2-67
Student Notebook
Delete rules
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
RESTRICT/NO ACTION
CE0317.0
Notes:
There are three delete rules to choose from. We will discuss them in more detail on the
following visuals.
pr
Ex
cl
ON DELETE NO ACTION is the default in DB2 LUW. In DB2 z/OS, you can specify the
default to be either to ON DELETE RESTRICT or ON DELETE NO ACTION, by means of
setting the special register CURRENT RULES.
V5.4
Student Notebook
Uempty
DEPTNAME
PLANNING
OPERATIONS
MARKETING
MANAGER
000020
000080
000010
DIVISION
EASTERN
WESTERN
EASTERN
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DEPTNO
BLUE
GREEN
RED
EMPL
EMPNO
000010
000020
000030
DEPT
EMPL
LASTNAME
HAAS
THOMPSON
KWAN
FIRSTNAME
CHRISTINE
MICHAEL
SALLY
WORK
DEPT
DEP
RED
GOVT_ID
888-88-2794
888-89-4261
BLUE
GREEN 888-88-9456
DEPTNO DEPTNAME
MANAGER
DIVISION
GREEN
RED
000080
000010
WESTERN
EASTERN
OPERATIONS
MARKETING
EMPNO
000010
LASTNAME
HAAS
FIRSTNAME
CHRISTINE
WORK
DEPT
DEP
GOVT_ID
RED
888-88-2794
000030
KWAN
SALLY
GREEN 888-88-9456
SALARY
52750.00
31000.00
33000.00
SALARY
52750.00
33000.00
CE0317.0
Notes:
pr
Ex
cl
In this example, the referenced table is the department table. The foreign key
EMPL.DEPTNO was defined with the ON DELETE CASCADE rule. What happens when
the BLUE WORKDEPT row is deleted from the department table with ON DELETE
CASCADE as the delete rule? All employees in the BLUE DEPTNO are also deleted.
2-69
Student Notebook
DEPTNAME
PLANNING
OPERATIONS
MARKETING
MANAGER
000020
000080
000010
DIVISION
EASTERN
WESTERN
EASTERN
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DEPTNO
BLU
GRE
RED
EMPL
EMPNO
000010
000020
000030
LASTNAME
HAAS
THOMPSON
KWAN
DEPT
WORK
DEP
FIRSTNAME DEPT
CHRISTINE RED
BLUE
MICHAEL
GREEN
SALLY
SALARY
52750.00
31000.00
33000.00
DEPTNO DEPTNAME
MANAGER
DIVISION
GRE
RED
000080
000010
WESTERN
EASTERN
OPERATIONS
MARKETING
EMPL
EMPNO
000010
000020
000030
GOVT_ID
888-88-2794
888-89-4261
888-88-9456
LASTNAME
HAAS
THOMPSON
KWAN
FIRSTNAME
CHRISTINE
MICHAEL
SALLY
WORK
DEPT
RED
NULL
GREEN
GOVT_ID
888-88-2794
888-89-4261
888-88-9456
UNKNOWN!
SALARY
52750.00
31000.00
33000.00
CE0317.0
Notes:
pr
Ex
cl
In this example, the referenced table is the department table. The foreign key
EMPL.WORKDEPT was defined with an ON DELETE SET NULL rule. When the BLUE
DEPTNO row is deleted from the department table, DB2 will set the WORKDEPT value for
all BLUE employees to NULL.
V5.4
Student Notebook
Uempty
DEPTNAME
PLANNING
OPERATIONS
MARKETING
MANAGER
000020
000080
000010
WORK
FIRSTNAME DEPT
DEP
CHRISTINE
RED
MICHAEL
BLUE
SALLY
GREEN
DIVISION
EASTERN
WESTERN
EASTERN
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DEPTNO
BLU
GRE
RED
EMPL
EMPNO
000010
000020
000030
DEPT
EMPL
LASTNAME
HAAS
THOMPSON
KWAN
DEPTNO
BLU
GRE
RED
EMPNO
000010
000020
000030
DEPTNAME
PLANNING
OPERATIONS
MARKETING
LASTNAME
HAAS
THOMPSON
KWAN
MANAGER
000020
000080
000010
FIRSTNAME
CHRISTINE
MICHAEL
SALLY
WORK
DEPT
DEP
RED
BLUE
GREEN
GOVT_ID
888-88-2794
888-89-4261
888-88-9456
SALARY
52750.00
31000.00
33000.00
DIVISION
EASTERN
WESTERN
EASTERN
SOC_SEC
888-88-2794
888-89-4261
888-88-9456
SALARY
52750.00
31000.00
33000.00
CE0317.0
Notes:
pr
Ex
cl
In this example, the referenced table is the department table. The foreign key
EMPL.WORKDEPT was defined with an ON DELETE RESTRICT rule. Before DB2 will
allow the BLUE DEPT row to be deleted from the department table, it checks to ensure that
there are no employee rows where WORKDEPT = BLUE. If DB2 finds even one employee
row with a value of BLUE in the WORKDEPT column, the delete fails (is restricted).
2-71
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Triggers
CE0317.0
Notes:
cl
If your application environment needs to enforce particular business rules, before or after
changes to DB2 tables take place, DB2 offers you the ability to automatically activate a
user-defined set of actions (called a trigger) which will implement your business rules.
Ex
When a trigger is fired (the set of actions is performed), the application that fired the
trigger will wait for its completion, as if the trigger had actually been called by the
application itself. Triggers are associated with specific tables, and will be activated no
matter how the table change took place.
pr
The advantage of using triggers is that they are database objects, associated with a
specific table. Therefore a trigger is maintained in one place, and is consistently applied,
regardless of the application which causes the trigger to fire. Without the use of triggers,
every application that issues a change SQL statement (INSERT, UPDATE or DELETE)
would have to design and code the logic necessary to implement the business rules
associated with the tables being changed.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
This rather simple example shows how a set of two triggers can be used to keep the total
number of employees in the company current.
pr
Ex
cl
The NEW_HIRED trigger will perform an update each time a new employee row is added
to the EMPL table. The trigger adds 1 to a running count of employees. The second trigger
decrements the employee count each time a row is removed from the EMPL table. We will
present additional information about triggers in the DBA unit of this course.
2-73
Student Notebook
FAX
668723
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CUST
ORDER AAAA
QTY
Insert Trigger
Orders File
40
Input Parameter
WRITE DOC:
SNDFAX for
Confirmation
Fax Machine
Conf. #
DB2
E-mail msg
CE0317.0
Notes:
The picture shows an example of how triggers can be used to integrate a traditional
application, such as an order entry, with automation techniques.
pr
Ex
cl
This could be a very common case of a company acquiring orders over the phone,
validating them, and sending a confirmation fax or email message to their customer
afterwards. Using triggers, the fax or email could be automatically sent to the customer as
soon as the order is inserted into the database file, without changing the order entry
application. The insert trigger is invoked every time an order enters the orders file; the new
records are automatically passed to the trigger program as input parameters. The program
might produce a predefined document with the new order and customer data, which is sent
as a fax or email message to the customer.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
At this stage in the design process, you could also consider the following types of DB2
objects to add to a table:
Ex
cl
View: A view has many uses, but it is often used on a table containing information which
will interest a variety of users, as a means of ensuring that each group of users accesses
only the data that they are or authorized to see. A view enables users to see a particular
subset of the base data without the need to create copies of the tables for each group of
users. We will present more detailed information about views in the DBA unit of this course.
pr
Index: Basically, an index on a particular column can improve access performance on that
column. There are various types of index: a unique index was created as a result of
creating the primary key on the previous page. Apart from performance advantages, a
unique index also ensures there are no duplicate values in that column. Views and indexes
can be identified at design time, and created at the same time as the table; but they can
also be identified and created later on. Views and indexes are often implemented by the
DBA, therefore they are covered in more detail in the DBA unit of this course and on DB2
DBA courses.
2-75
Student Notebook
Checkpoint (1 of 2)
1. What is a business entity?
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
cl
2.
Ex
3.
4.
pr
5.
V5.4
Student Notebook
Uempty
Checkpoint (2 of 2)
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
cl
7.
pr
Ex
8.
2-77
Student Notebook
Unit summary
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
pr
Ex
cl
Notes:
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
This unit explains some of the tasks which a user may perform while
working with DB2.
pr
Ex
cl
Checkpoint questions
3-1
Student Notebook
Unit objectives
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
pr
Ex
cl
Notes:
3-2
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Run business
applications
End User
Produce
reports
Change
DB2 data
Write SQL
queries
CE0317.0
Notes:
Anyone executing an SQL statement is a user of DB2. The end-user may or may not know
the SQL language. The end-user may not even be aware that he/she is using DB2.
cl
Ex
The SQL within the applications may be changing data, adding data, removing data, or
reading data.
SQL may return DB2 data, which may then be formatted to produce reports, with paging,
headers and footers, breakpoints, etc.
pr
The user may construct (write) their own queries that change data, add data, remove
data, or read data.
3-3
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
End-Us
er
CE0317.0
Notes:
pr
Ex
cl
Programs can contain SQL statements. People running those programs are users. The
users may or may not be aware that the applications that they are executing contain SQL
statements which access DB2 data.
3-4
V5.4
Student Notebook
Uempty
INSERT clause
INTO clause
VALUES clause
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SELECT clause
FROM clause
WHERE clause
CREATE clause
(column description area)
IN clause
DELETE clause
FROM clause
WHERE clause
UPDATE clause
SET clause
WHERE clause
CE0317.0
Notes:
pr
Ex
cl
SQL statements are made up of clauses. The sequence in which the clauses are written is
the structure of the language.
3-5
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
No Need to
Fly in the Dark!!
User
Guide?
DB2 Catalog
SYSTABLES
SYSINDEXES
Data Dictionary
Software ?
SYSVIEWS
CE0317.0
Notes:
cl
pr
Ex
Ideally, you should be provided with a User Guide to the tables. Also, a customer
environment might deploy a software product that serves as an enterprise-wide data
dictionary. If you have neither a user guide nor a data dictionary to help you understand
the DB2 structures and the meaning of the data, the DB2 catalog tables provide a
significant amount of basic information about tables, columns, indexes, keys, views and
other DB2 objects.
3-6
V5.4
Student Notebook
Uempty
DATA TYPE
ID
NAME
DEPT
JOB
"YEARS"
SALARY
COMM
SMALLINT
VARCHAR
SMALLINT
CHAR
SMALLINT
DECIMAL
DECIMAL
LENGTH
NULLS
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
COLUMN NAME
(7,2)
(7,2)
NO
YES
YES
YES
YES
YES
YES
CE0317.0
Notes:
cl
The STAFF table is one of several sample tables that can be made available for users to
practice SQL statements and also relate to the SQL examples in the manuals. This table
may also be used as part of the IVP, Installation Verification Process. The IVP is a series of
jobs and queries that ensure that DB2 has been properly installed and is correctly
functioning.
pr
Ex
The implementation of the STAFF table (schema name, table contents, etc.) may differ
slightly depending on the DB2 platform. In this case, the schema of the table is Q (from
QMF - see later) and all the data is in upper case.
3-7
Student Notebook
DEPT
-----20
20
38
38
15
38
15
20
42
42
15
38
42
51
51
10
15
.
.
JOB
----MGR
SALES
MGR
SALES
MGR
SALES
SALES
CLERK
SALES
MGR
CLERK
CLERK
CLERK
MGR
SALES
MGR
CLERK
.
.
YEARS
-----7
8
5
6
10
7
6
7
5
6
6
6
7
4
.
.
SALARY
---------18357.50
18171.25
17506.75
18006.00
20659.80
16808.30
16502.83
13504.60
18001.75
18352.80
12508.20
12954.75
10505.90
21150.00
19456.50
22959.20
12258.50
.
.
COMM
---------612.45
846.55
650.25
1152.00
128.20
1386.70
206.60
180.00
75.60
637.65
110.10
.
.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
ID
-----10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
.
.
CE0317.0
Notes:
cl
Here is a listing of some of the contents of the STAFF table, in column order as it was
defined at CREATE time. After the table was defined, users either executed SQL INSERT
statements or submitted a LOAD utility to populate the table.
pr
Ex
The primary key of the STAFF table is the column named ID. We will talk in more depth
about keys (primary, unique, compound, parent, foreign etc.) in a later unit during this
course.
3-8
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
COLUMN NAME
DATA TYPE
DEPTNUMB
DEPTNAME
MANAGER
DIVISION
LOCATION
SMALLINT
VARCHAR
SMALLINT
VARCHAR
VARCHAR
LENGTH
14
10
13
NULLS
NO
YES
YES
YES
YES
CE0317.0
Notes:
pr
Ex
cl
Here is the structure of the ORG table. Q.ORG is another sample table that is installed on
most DB2 systems that are accessed by QMF. Like the STAFF table, it is used as part of
the IVP process, and also used to practice SQL statements and also relate to the SQL
examples in the manuals.
3-9
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DEPTNUMB
-------10
15
20
38
42
51
66
84
DEPTNAME
-------------HEAD OFFICE
NEW ENGLAND
MID ATLANTIC
SOUTH ATLANTIC
GREAT LAKES
PLAINS
PACIFIC
MOUNTAIN
MANAGER
------160
50
10
30
100
140
270
290
DIVISION
---------CORPORATE
EASTERN
EASTERN
EASTERN
MIDWEST
MIDWEST
WESTERN
WESTERN
LOCATION
------------NEW YORK
BOSTON
WASHINGTON
ATLANTA
CHICAGO
DALLAS
SAN FRANCISCO
DENVER
CE0317.0
Notes:
cl
Here is a listing of the contents of the ORG table, in column order as it was defined at
CREATE time. After the table was defined, users either executed SQL INSERT statements
or submitted a LOAD utility to populate the table.
pr
Ex
The primary key of the ORG table is the column named DEPTNUMB.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
cl
The SQL SELECT statement is the means by which a user can read data in DB2 tables.
SELECT statements are commonly referred to as queries. An asterisk in a SELECT clause
requests that DB2 return all columns from this table. This could also be written: SELECT
Q.STAFF.*
Ex
A SELECT clause and a FROM clause is required in every query. All other clauses are
optional.
pr
The absence of a WHERE clause tells DB2: we want data from every row in the table. In
most SELECT statements, the user or application will return only a subset of the rows in
the table. Via the WHERE clause, the statement implements selection criteria (conditions
that the row must meet in order to be SELECTed). These criteria are referred to as local
predicates.
Two hyphens, side by side, denote a comment. Everything following to the right of the two
consecutive hyphens is treated as a comment. The SQL parser then resumes syntax
checking on the next line of the query.
3-11
Student Notebook
FROM Q.STAFF
1 to N
TABLES,
VIEWS
WHERE DEPT = 38
1 to N TESTS,
CONDITIONS,
PREDICATES
ORDER BY NAME
OUTPUT ROW
SEQUENCE
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
YEARS, SALARY
1 to N COLUMNS,
FUNCTIONS,
LITERALS,
EXPRESSIONS
SELECT NAME,
CE0317.0
Notes:
Every SELECT statement must have a SELECT and a FROM clause. The remaining
clauses are optional, and only used when we need their services.
Ex
cl
The SELECT clause tells DB2 which column(s) of information you want on your report. In
addition to retrieving column values, the user may request literals, functions (either scalar
or column functions) and expressions. In the SELECT above, the user is requesting data
values from the NAME, YEARS and SALARY columns in the table.
pr
The FROM clause specifies which table(s) contain(s) the requested data. When
requesting data from more that one table, the SQL statement is know as a join. When
joining tables, the user should specify how the tables are related (what column information
they share in common). These join relationships are known as join predicates.
The WHERE clause tells DB2 from which row(s) the SELECT clause information is to be
retrieved, such as information about department 38. DEPT = 38 is an example of a local
predicate.
V5.4
Student Notebook
Uempty
The ORDER BY clause specifies the sequence in which the qualified rows should be
presented to the user or application. There are additional clauses and options for the
SELECT statement, but these four (SELECT, FROM, WHERE, ORDER BY) are the most
commonly used.
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
The sequence of the clauses must be coded as shown here: SELECT followed by FROM,
WHERE, then ORDER BY. If you do not code the clauses in this specific order, you will
receive a syntax error.
3-13
Student Notebook
EXECUTION
ORDER
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SYNTACTICAL
ORDER
VW
6(/(&7
UG
QG
)520
VW
UG
:+(5(
QG
WK
25'(5%<
WK
~
G
f
FROM EMPLOYEE
z
lH
CE0317.0
Notes:
Although the user must write the clauses of the SELECT in a specific order, DB2 does not
execute them in the same sequence.
cl
DB2 must first locate the table(s) and/or view(s) that the user or application requires.
Therefore, the FROM clause is executed first.
Ex
Next, DB2 needs to apply the selection criteria, filtering out any rows that are not
needed or wanted by the user. So the WHERE clause is executed next.
As a third step in the process, DB2 is now in a position to retrieve the desired columns
from the qualified rows, which is accomplished by the SELECT clause.
pr
Finally, DB2 sequences the column values in the order designated by the user in the
ORDER BY clause.
V5.4
Student Notebook
syntax error to the user. It would be necessary to rewrite part of the query (WHERE
SALARY + BONUS > 100000) in order for the query to run. Referring to the label
INCOME, rather than the full expression (SALARY + BONUS) in the ORDER BY clause is
valid syntax, since the ORDER BY clause is evaluated by DB2 after the SELECT clause.
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Uempty
3-15
Student Notebook
NAME
--------SANDERS
PERNAL
MARENGHI
O'BRIEN
HANES
QUIGLEY
ROTHMAN
JAMES
KOONITZ
PLOTZ
NGAN
NAUGHTON
.
ABRAHAMS
SNEIDER
SCOUTTEN
.
DEPT
-----20
20
38
38
15
38
15
20
42
42
15
38
.
38
20
42
.
JOB
----MGR
SALES
MGR
SALES
MGR
SALES
SALES
CLERK
SALES
MGR
CLERK
CLERK
.
CLERK
CLERK
CLERK
.
YEARS
-----7
8
5
6
10
7
6
7
5
.
3
8
.
SALARY
---------18357.50
18171.25
17506.75
18006.00
20659.80
16808.30
16502.83
13504.60
18001.75
18352.80
12508.20
12954.75
.
12009.75
14252.75
11508.60
.
COMM
---------612.45
846.55
650.25
1152.00
128.20
1386.70
206.60
180.00
.
236.50
126.50
84.20
.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
NAME
SANDERS
PERNAL
JAMES
SNEIDER
Final Results
Intermediate Table
10
20
80
190
SANDERS
PERNAL
JAMES
SNEIDER
20
20
20
20
MGR
SALES
CLERK
CLERK
NAME
2nd
7
8
8
18357.50
18171.25
13504.60
14252.75
612.45
128.20
126.50
JOB
1st
CE0317.0
Notes:
pr
Ex
cl
This query returns two columns of data from the set of rows whose DEPT value is 20 (as
indicated by the small arrows). The WHERE clause identifies the set of rows from which
the data is to be returned. DB2 applies the SELECT clause against the conceptual
intermediate table retrieving the JOB and NAME values (in that left-to-right sequence) from
every row in the intermediate table.
V5.4
Student Notebook
Uempty
Numeric comparisons
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
WHERE DEPT = 20
-- (integer)
-- (decimal point)
-- (no comma)
OR SALARY = 18000)
-- (same as 18000.00)
CE0317.0
Notes:
cl
Ex
For decimal numbers, the p and s designate precision and scale. Precision is the total
number of digits possible for the number, and the scale represents how many positions to
the Right of the decimal point (in other words, the fractional part of the number).
pr
Please note that when designating numeric data or values within SQL, you do NOT
enclose the numbers with apostrophes (single quotes). Also, the only punctuation allowed
in a number is the decimal point. There are no delimiters for thousands, millions, billions,
etc. Be advised that in some nations (especially Europe), the convention is to use the
period as the delimiter (for thousands, millions, etc.) and to use the comma as the decimal
point.
3-17
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Basic Operators
Boolean Logic
Partial Value Search
Values in other rows
Calculated values
SELECT *
FROM Q.STAFF
WHERE ((SALARY + COMM) > 20000)
AND JOB NOT LIKE 'M%'
Copyright IBM Corporation 2007
CE0317.0
Notes:
cl
Be aware of some idiosyncrasies in SQL. For instance, when using GREATER THAN OR
EQUAL TO, and LESS THAN OR EQUAL TO, you must specify them as >= and <=. If you
use => or =< you will receive a syntax error.
Ex
Further, the <> (Less Than or Greater Than) is used to make comparisons that are NOT
EQUAL. It may not be intuitively obvious at first glance, but when you think about it if A
is either less than or greater than B, A is not equal to B. Perhaps this warrants some
further discussion.
pr
You can negate a condition or comparison by placing the SQL keyword NOT in front of the
entire condition. For instance, you can code WHERE NOT NAME = Jones; However, you
cannot code WHERE NAME NOT = Jones. The keyword NOT can be placed directly
before other comparison functions, such as LIKE, IN, and BETWEEN, but NOT can never
be coded immediately before EQUAL (whether you spell out the word or use the symbol
=). Therefore, the combination Less Than/Greater Than, <>, has become the
conventional way to express a not equal condition in SQL.
V5.4
Student Notebook
Partial value searches using the LIKE keyword can only be performed on character data.
There are two special characters that enable the LIKE keyword to provide a generic search
function. The underscore represents exactly 1 character/byte position, and the percent
sign represents 0 or more character positions.
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Uempty
3-19
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
FUNCTION_NAME(argument)
Column Functions
(AKA SUMMARY functions)
Scalar Functions
(most of the rest)
function
function
SUM
value
AVG
MIN
MAX
COUNT(*)
COUNT(DISTINCT column_name)
value
value
value
value
CE0317.0
Notes:
Column functions produce one summary or aggregate row for each set of rows.
Scalar functions produce one value for each row within the set of qualified rows.
pr
Ex
cl
The above column functions are the most commonly-used column functions. Each
release of DB2 provides additional functions. Check the SQL manuals for your release of
DB2 to see the supported functions.
V5.4
Student Notebook
Uempty
Column functions
SUM(SALARY) AS SUM_SALARY,
COUNT(*) AS COUNT
Q.STAFF
DEPT = 20
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SELECT
FROM
WHERE
DEPT
20
20
20
20
SALARY
18357.50
18171.25
13504.60
14252.75
One
Summary
Row per
Set
SUM
SALARY
64286.10
COUNT
4
CE0317.0
Notes:
cl
Ex
1. DB2 searches the catalog for the table with a schema of Q and the name STAFF.
2. DB2 chooses only the rows where the DEPT = 20, by applying the local predicate in the
WHERE clause. This produces an intermediate result all of the column values for
qualified rows (DEPT = 20).
pr
3. According to the SELECT clause, DB2 is asked to compute the total of the salaries, and
to count the number of rows that meet the WHERE condition(s).
3-21
Student Notebook
Scalar functions
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
NAME
--------Sanders
Marenghi
Hanes
Plotz
Fraye
Molinare
Lu
Daniels
Jones
Lea
Quill
DEPT
-----20
38
15
42
51
10
10
10
10
66
84
JOB
----M
M
M
m
M
M
M
M
m
M
M
SALARY
---------18357.50
17506.75
20659.80
18352.80
21150.00
22959.20
20010.00
19260.25
21234.00
18555.50
19818.00
CE0317.0
Notes:
Ex
cl
pr
SUBSTR is one of the most widely used of the scalar functions. SUBSTR is followed by
three arguments DB2 searches/reads the first argument, beginning at the nth byte
indicated by the second argument, and retrieves n bytes of characters as specified in the
third argument.
In the SELECT above, SUBSTR(JOB,1,1) retrieves the first letter of the JOB column,
regardless of the combinations of case for the letters MGR. In other words, the UPPER
scalar function would transform any case combinations into uppercase MGR before
comparing with the literal string MGR. Therefore, if the JOB column contained any of the
following, the row would be selected:
3-22 DB2 Family Fundamentals
V5.4
Student Notebook
Uempty
MGR
MGr
MgR
Mgr
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
mGR
mGr
mgR
mgr
Notice that some of the output rows in the report display a lowercase m (as indicated by
the red arrows). That means that the first character of the JOB column for those two rows
was a lowercase m. The SELECT clause simply asked for the first character. In order to
ensure that the output displayed JOB as uppercase, you would have to nest the scalar
functions like this .
SELECT ID, NAME, DEPT, SUBSTR(UPPER(JOB),1,1) AS JOB
FROM Q.STAFF
pr
Ex
cl
Some additional scalar functions are: MONTH, DAY, DAYS, COALESCE, SUBSTR, DATE,
DECIMAL, DIGITS, ROUND, TRIM, STRIP, NULLIF, to name a few. With the most current
versions of DB2, there are close to 200 scalar functions.
3-23
Student Notebook
SELECT examples
SALARY
18171.25
18006.00
16808.30
16502.83
13504.60
18001.75
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SELECT SALARY
FROM Q.STAFF
WHERE JOB <> 'MGR'
SELECT AVG(SALARY)
FROM Q.STAFF
WHERE JOB <> 'MGR'
ETC.
15240.986666
DEPT
66 16880.175000
51
84
16235.200000
15443.000000
ETC.
CE0317.0
Notes:
cl
First, a simple SELECT statement returning the SALARY values for all non-manager
employees in the STAFF table. Only the first six rows are displayed on the visual above.
The actual number of total rows returned by DB2 would equal the number of non-manager
employees in the entire STAFF table.
Ex
The second SELECT returns the average salary for all non-managers.
pr
The third SELECT demonstrates the ability of SQL to summarize data by a certain column
(in this case, DEPT). You can invoke that function by issuing the GROUP BY clause. This
produces a report with one line for each department (the DEPT column), with that
departments average salary for non-managers. The output for this third SELECT returns
the rows in sequence of highest to lowest average SALARY by DEPT (ORDER BY 2
DESC). The 2 in the ORDER BY clause refers to the 2nd argument found in the SELECT
(that is AVG SALARY), and DESC specifies descending order. As with the first SELECT,
not all of the rows returned by DB2 are displayed in the visual (i.e. there are more than
three departments).
V5.4
Student Notebook
Uempty
There is a specific syntax rule that you should be aware of when you are invoking functions
in the SELECT clause: If there are column functions in the SELECT list along with columns
not in a column function, then every column in the SELECT list that is not an argument of a
column function must appear in the GROUP BY clause.
The clauses are evaluated in the following sequence (only SELECT and FROM are
mandatory, the other clauses are optional):
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
FROM
pr
Ex
cl
3-25
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
In a SELECT statement:
SELECT
something AS new_name,
expression AS "multi part name" ...
SELECT DEP AS DEPARTMENT,
SAL + COMM AS EARNINGS
FROM ...
DEPARTMENT
BLU
RED
.
EARNINGS
64000
58000
.
CE0317.0
Notes:
cl
pr
Ex
In the example above, DEP is the actual column name, but we want the output to have
DEPARTMENT as the header for that column of information. In similar fashion, the query
shows us that the AS keyword is going to use EARNINGS as the header for the
compensation information, which represents the sum of columns SAL and COMM (salary
and commission). If you want to have the header include characters other than A-Z, 0-9,
@, # $ and _ (underscore), you must enclose the header name with double quotes. For
example, if you wanted SAL + COMM to display as TOTAL EARNINGS, you would code
your AS clause . AS TOTAL EARNINGS.
V5.4
Student Notebook
Uempty
CASE expressions
'Small'
'Medium'
'Large'
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
PARTNO
12-234
12-235
14-276
DESCRIPT
SWEATER
SWEATER
JACKET
COLOR
BLUE
RED
GREEN
SIZE
Small
Medium
Unknown
CE0317.0
Notes:
cl
CASE expressions must begin with the keyword CASE and conclude with the keyword
END.
Ex
The first WHEN condition that is found to be True results in the THEN value being
retrieved into the result set row.
pr
If none of the WHEN conditions is True, then the ELSE value is retrieved into the result
set row.
If none of the WHEN conditions is True, and no ELSE is coded, the NULL value is placed
into the result set row.
In the SELECT statement in the visual above, we are expecting to find an uppercase S, M,
or L in the one-byte column SIZE in the CATALOG table. If any other character value is
found in SIZE, then we place the literal string Unknown for that row in the results set.
3-27
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
WHERE clause
ON clause, an extension of the FROM clause
CE0317.0
Notes:
Ex
cl
Naming multiple tables in the FROM clause tells DB2 to join information from these
multiple tables into the result set rows. Adding a JOIN predicate tells DB2 to join only those
rows that are related in this way. Without the appropriate join predicate(s), DB2 will
produce a Cartesian product by matching each row in the first table with each and every
row of the other table(s). This results in a substantial consumption of system resources,
and is seldom (if ever) what the requirements of the application designated in the first
place.
pr
There are two formats or syntaxes for the join predicates. The first syntax employs an
equal predicate in the WHERE clause. The second syntax utilizes the ON keyword as an
extension in the FROM clause.
V5.4
Student Notebook
Uempty
Example tables
Empl
LASTNAME
HAAS
ADAMSON
THOMPSON
SMITH
SPENSER
PULASKI
NATZ
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
EMPNO
000010
000150
000020
000250
000100
000070
200140
Dept
DEPTNAME
MGRNO
SPIFFY COMPUTER
000010
DEVELOPMENT
SOFTWARE
000100
ADMINISTRATION
000070
BRANCH OFFICE F2
CE0317.0
Notes:
cl
We can find the name of a department's manager by looking in the EMPNO column of the
Empl table for values that are contained in the MGRNO column of the Dept table. When
we find a matching value, reading across the row to the LASTNAME column provides us
with the manager's surname. These two tables are related by the MGRNO and EMPNO
columns (these columns carry common values).
pr
Ex
There may be departments with no manager yet assigned, and there may be employees
that are not assigned to any department. When the hyphen is displayed in a result set of
an SQL query, it indicated the NULL value. Null means unknown, or the empty set. It
does NOT mean blanks for character data, nor does it mean zeroes for numeric data. In
the Dept table above, there are two departments without an assigned manager.
3-29
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DEPTNAME
SPIFFY COMPUTER
SOFTWARE
ADMINISTRATION
CE0317.0
Notes:
cl
These tables have common values between the EMPNO and MGRNO columns. MGRNO
contains the employee number of the person that manages that department. This join
predicate (EMPNO = MGRNO) is telling DB2 to connect department rows to the row of the
person who manages it.
pr
Ex
The syntax requires that you separate the table names with a comma. Forgetting the
comma between the table names tells DB2 to rename the first table (a correlation name)
by assigning it the name of the second table (it works in a similar fashion to the assigning of
custom column-header names in a SELECT list using the AS keyword).
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DEPTNAME
SPIFFY COMPUTER
SOFTWARE
ADMINISTRATION
CE0317.0
Notes:
cl
Here is the second (more recent) syntax for a join statement. Instead of separating the
multiple tables in the join with commas, the JOIN keyword is used. JOIN implies INNER
JOIN, and either of those is permissible in this syntax. When the JOIN keyword is used in
the FROM clause, the join predicate must be written using the ON keyword, which is an
extension to the FROM clause.
pr
Ex
This second syntax is always required for OUTER JOINs. The first syntax can be used
only for INNER JOINs.
3-31
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
EMPNO
000111
LASTNAME
SMITH
WORKDEPT
C01
HIREDATE
1998-06-25
SALARY
25000.00
BONUS
-
CE0317.0
Notes:
cl
Use an INSERT statement to add rows to a table. Inserting a row via a view inserts the row
into the table on which the view is based. Therefore, inserts via a view are only possible if
the view contains all of the NOT NULL columns in the base table.
Ex
When using the syntax of the first example in the visual above, a value for each of the
table's columns must be provided, and those values must be in the sequence that the
columns were defined at the time of the CREATE TABLE or CREATE VIEW statement,
originally defining the DB2 object.
pr
In the second example, a column list is specified. The number of values provided for a row
via the VALUES clause must be equal to the number of column names in the column list.
The first value is inserted in the first column of the list, the second value in the second
column, and so on.
Some versions of DB2 enable you to add more than one row in your INSERT statement.
Instead of ending your one set of VALUES with a semi-colon, you simply put a comma and
then add another set of values, in brackets. Check the SQL Reference manual for your
environment to ensure support of multi-row inserts.
3-32 DB2 Family Fundamentals
V5.4
Student Notebook
Uempty
Update columns
Before:
LASTNAME
SMITH
JONES
THOMPSON
HAAS
THOMPSON
KWAN
GEYER
WORKDEPT
C01
C01
C01
A00
B01
C01
E01
HIREDATE
1998-06-25
2001-06-25
2001-06-25
1965-01-01
1973-10-10
1975-04-05
1949-08-17
SALARY
25000.00
25000.00
25000.00
52750.00
41250.00
38250.00
40175.00
BONUS
1000.00
800.00
800.00
800.00
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
EMPNO
000111
000113
000114
000010
000020
000030
000050
UPDATE TESTEMP
SET BONUS = 500,
SALARY = 26000
WHERE EMPNO = '000111'
After:
EMPNO
000111
000113
000114
000010
000020
000030
000050
LASTNAME
SMITH
JONES
THOMPSON
HAAS
THOMPSON
KWAN
GEYER
WORKDEPT
C01
C01
C01
A00
B01
C01
E01
HIREDATE
1998-06-25
2001-06-25
2001-06-25
1965-01-01
1973-10-10
1975-04-05
1949-08-17
SALARY
26000.00
25000.00
25000.00
52750.00
41250.00
38250.00
40175.00
BONUS
500.00
1000.00
800.00
800.00
800.00
CE0317.0
Notes:
cl
The UPDATE statement changes the values of the specified columns for one or more rows
of a table or view. Our new colleague, Mr. Smith (employee number 000111) receives a first
bonus and a salary raise. Therefore, we have been directed to set his BONUS to 500 and
his SALARY to 26000.
Ex
If more than one column is changed, the columns must be separated by commas. The SET
keyword must precede the first column and must not be repeated.
To set BONUS to NULL, the UPDATE statement must be coded as follows: UPDATE
TESTEMP SET BONUS = NULL WHERE EMPNO = '000111
pr
Be careful when constructing UPDATE and DELETE statements. If you do not include the
correct WHERE clause, you may be changing data that should not be altered. If you totally
omit the WHERE clause, then every row of the DB2 table will be updated or deleted.
3-33
Student Notebook
Delete rows
Before:
LASTNAME
SMITH
JONES
THOMPSON
HAAS
THOMPSON
KWAN
GEYER
WORKDEPT
C01
C01
C01
A00
B01
C01
E01
HIREDATE
1998-06-25
2001-06-25
2001-06-25
1965-01-01
1973-10-10
1975-04-05
1949-08-17
SALARY
27000.00
26000.00
26000.00
52750.00
41250.00
39250.00
40175.00
BONUS
500.00
1000.00
800.00
800.00
800.00
SALARY
26000.00
26000.00
52750.00
41250.00
39250.00
40175.00
BONUS
1000.00
800.00
800.00
800.00
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
EMPNO
000111
000113
000114
000010
000020
000030
000050
After:
EMPNO
000113
000114
000010
000020
000030
000050
LASTNAME
JONES
THOMPSON
HAAS
THOMPSON
KWAN
GEYER
WORKDEPT
C01
C01
A00
B01
C01
E01
HIREDATE
2001-06-25
2001-06-25
1965-01-01
1973-10-10
1975-04-05
1949-08-17
CE0317.0
Notes:
cl
Mr. Smith left the company. Therefore, we delete the row with employee number 000111.
The DELETE statement removes entire rows from a table. Depending on the search
condition of the WHERE clause, the DELETE statement removes zero, one, or more rows
of a table.
pr
Ex
Be careful when constructing UPDATE and DELETE statements. If you do not include the
correct WHERE clause, you may be changing data that should not be altered. If you totally
omit the WHERE clause, then every row of the DB2 table will be updated or deleted.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
cl
In DB2 LUW, click the appropriate icons to invoke interfaces to DB2 such as the Command
Line Processor (CLP), the Command Editor, or QMF for Windows. The CLP is a
character-based interface, but other tools provide GUI function.
Ex
In z/OS, SPUFI and QMF are usually invoked from the ISPF menu. SPUFI and QMF on
the mainframe are tools that use a character-based, menu-driven interface.
pr
There are many GUI-based interfaces, available from numerous software vendors, that run
on workstations (Intel platform) and can access a wide variety of relational databases on
many different platforms.
3-35
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
End Use
r
CE0317.0
Notes:
cl
The user above has invoked CLP in an interactive mode. This is accomplished by
entering db2 and pressing the Enter key. The user then receives a db2 => prompt, and
may key in and enter any valid SQL statement or DB2 command.
pr
Ex
The command line processor will also accept SQL statements in a non-interactive mode,
by prefixing the SQL statement with the term db2. Instead of typing db2 and pressing
Enter, which invokes the interactive mode of CLP, start each SQL statement with db2, as in
the following example: db2 connect to sample; db2 select * from department; Although
only required when multiple SQL statements are executed together, it is recommended
practice to end each SQL statement with the delimiter character (the semi-colon is the
default).
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Command Editor
CE0317.0
Notes:
With the Command Editor you can perform the following actions:
Execute an SQL statement or DB2 CLP command
cl
Ex
Save what you write as a script, store it in the Task Center and run it on demand, or even
schedule it to run at a specific time
See the results of your command or query, and save them if required
pr
Ask DB2 to show the Access Plan for an explainable query, in other words, how the query
was optimized by DB2
3-37
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SQL Assistant
CE0317.0
Notes:
The SQL Assistant allows you to compose SQL statements easily. This software prompts
the user through the process of building a query, one step at a time:
cl
Ex
Specifying what information (column values and other things) should be returned in
each row of output (SELECT clause)
Filtering out any rows not needed in the result set (WHERE clause)
pr
Telling DB2 how to group rows when the column function is invoked (GROUP BY
clause)
Setting conditions on the groups to be returned by DB2 (HAVING clause, which acts like
a WHERE, but for groups of rows, instead of single rows)
Instructing DB2 how to sequence the result set, when it is presented to the user or
returned to the application (ORDER BY clause)
V5.4
Student Notebook
Remember that for a SELECT statement, only the SELECT and FROM clauses are
mandatory, The other clauses, although frequently used, are all optional, depending upon
the requirements of your query.
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Uempty
3-39
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Data Studio
CE0317.0
Notes:
cl
Data Studio is an Eclipse-based tool that provides similar functionality to Control Center
and SQL Assistant. Those older tools are being deprecated in the more recent versions of
DB2, so customers are encouraged to migrate to the newer tool.
Ex
IBM Data Studio provides foundational database development and administration support
for the DB2 family of products. Data Studio provides developers and DBAs with basic
capabilities for database management and development.
With Data Studio the user may:
pr
Launch the task launcher that guides users through common tasks and gives users
access to best practices
Invoke enhanced administration support for DB2 on Linux, UNIX, and Windows
including commands, utilities, and configuration support for DB2
Create, alter, and drop objects, manage privileges, view data distributions, and update
statistics on those objects
V5.4
Student Notebook
Uempty
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Create Web services that easily expose database operations (SQL SELECT and DML
statements, XQuery expressions, or calls to stored procedures) to client applications.
3-41
Student Notebook
z/OS SPUFI
SSID: DSNA
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SPUFI
(Y/N
(Y/N
(Y/N
(Y/N
(Y/N
CE0317.0
Notes:
The user must navigate the ISPF menu structure to reach the SPUFI panels.
cl
SQL statements (SQL source) are written to the [sequential] file or member [of a
partitioned dataset, or PDS] named on line 1. This file must be pre-allocated before the
SPUFI session.
pr
Ex
The output of the SQL statement will be written to the file or data set named on line 4.
This sequential file is created automatically for the user, and therefore does not need to be
pre-allocated. This file is overwritten after each SPUFI execution, so if for some reason the
user needs to save the results (not the SQL source itself), he/she would need to save to a
different output dataset at the next execution, or copy the output to another file (sequential
or PDS) before the next execution.
A YES on line 6 tells SPUFI that you want to invoke an ISPF Edit session, to write or
change an SQL statement in the file named on line 1.
Pressing Enter while at the SPUFI panel causes the first function for which a YES is
specified to be invoked.
3-42 DB2 Family Fundamentals
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Compose query
**********************************************
EDIT URID.DB2FUN.LIB(TEST) Columns 00001 00072
Command ===>
Scroll ===> PAGE
****** ********** Top of Data ****************
000100 SELECT DEPT, JOB, NAME, SALARY
000200 FROM Q.STAFF
000300 WHERE DEPT IN (15, 20)
000400 ORDER BY NAME, JOB
****** ********* Bottom of Data **************
CE0317.0
Notes:
Always end queries with a semicolon (default delimiter character) when more than one
query is in the data set.
pr
Ex
cl
3-43
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SPUFI
===>
(Y/N
(Y/N
(Y/N
(Y/N
(Y/N
CE0317.0
Notes:
With Option 6 set to *, pressing the Enter key causes the remaining YES options to be
invoked.
Ex
cl
Be careful about the AUTOCOMMIT setting, option 8. If a user sets it to NO, and executes
a query, and then does not respond to the prompt (a panel giving the user the option to
either COMMIT or to ROLLBACK the SQL they just executed), the locks are still being held
on the data, and this may cause serious contention for other users or applications
attempting to access the same data.
pr
Option 9 places the user into a BROWSE session on the dataset specified in option 4.
V5.4
Student Notebook
Uempty
Output
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
cl
The BROWSE output lists the SQL query, followed by the results set. If the results do not
totally fit on the screen, the user may scroll by pressing Function keys 8 (scroll DOWN) and
7 (scroll UP).
Ex
SQL Return codes and messages will be displayed at the bottom of the output, including
the number of rows in the output, and whether a COMMIT or ROLLBACK was performed.
Return code 100 tells the user that there were no more rows to return, essentially indicating
an end-of-file (EOF) condition.
pr
3-45
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
______________________________________________________________________________
IBM*
Licensed Materials - Property of IBM
5706-254 5706-255 5648-061
(c) Copyright IBM Corp. 1982, 1995 All Rights Reserved.
* Trademark of International Business Machines
______________________________________________________________________________
QMF HOME PANEL
Version X Release Y
******
**
**
*********
**
**
***
***
**
**
**
**** ****
*******
**
**
** ** ** **
**
**
* **
** **** ** **
******
**
**
** **
*
________________________________________
Query
Management
Facility
Connected to
DSNA
Type command on command line or use PF keys. For help, press PF1 or type HELP.
______________________________________________________________________________
1=Help
2=List
3=End
7=Retrieve
8=Edit Table 9=Form
OK, you may enter a command.
COMMAND ===>
4=Show
10=Proc
5=Chart
11=Profile
6=Query
12=Report
CE0317.0
Notes:
pr
Ex
cl
Here is the home panel for the Query Management Facility, a commonly-used reporting
tool for DB2 z/OS environments. Notice at the bottom of the panel, the various options that
can be invoked by the function keys. Pressing F6 will allow the user to enter an SQL
statement for execution.
V5.4
Student Notebook
Uempty
LINE
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SQL QUERY
1=Help
2=Run
7=Backward 8=Forward
12=Report
OK, cursor positioned.
COMMAND ===>
3=End
9=Form
4=Print
10=Insert
5=Chart
11=Delete
6=Draw
CE0317.0
Notes:
cl
SQL cannot be typed on the command linethe user must use F6 to navigate to the SQL
query panel. Likewise, QMF commands cannot be typed in the query area these
commands must be entered into the COMMAND line(s). A completed query is executed by
pressing F2.
pr
Ex
Reports requiring subtotaling of the data MUST be sorted by the columns for which we
want the subtotals. This means, if we want subtotals of the salary column for each
department, we must ORDER BY DEPT.
3-47
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
REPORT
DEPT
---15
15
15
15
20
20
20
20
JOB
----SALES
MGR
CLERK
CLERK
SALES
MGR
CLERK
CLERK
NAME
--------ROTHMAN
HANES
KERMISCH
NGAN
PERNAL
SANDERS
JAMES
SNEIDER
SALARY
---------16502.83
20659.80
12258.50
12508.20
18171.25
18357.50
13504.60
14252.75
CE0317.0
Notes:
pr
Ex
cl
By default, QMF produces a tabular report, similar to the SPUFI Browse output. Column
headings are the same names as the names specified in the SELECT list (either the actual
column name, or the pseudo-name given by the AS clause for each expression, column
function and/or literal in the SELECT clause).
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
FORM.MAIN
COLUMNS:
Total Width of Report Columns: 39
NUM COLUMN HEADING
USAGE
INDENT WIDTH EDIT
--- ---------------------- ------- ------ ----- ----1 DEPT._NUMBER
BREAK1 2
6
L
2 JOB
2
5
C
3 EMPLOYEE_NAME
2
9
C
4 SALARY
SUM
2
11
D2
*** END ***
PAGE:
HEADING
FOOTING
FINAL:
TEXT
BREAK1: NEW PAGE
FOOTING
BREAK2: NEW PAGE
FOOTING
OPTIONS: OUTLINE?
SEQ
--1
2
3
4
CE0317.0
Notes:
Pressing F9 brings the user to the main formatting panel in QMF. In the example above,
many of the formatting options are being used:
cl
Column headings are customized by typing over the current column headings.
Ex
An underscore in a column heading tells QMF to take the characters following the
underscore and print them on the next line.
The BREAK1 USAGE code tells QMF to generate a subtotal line any time the value in the
DEPT._NUMBER column changes.
pr
The &1 on the BREAK1 FOOTING option is a variable pulling values from the first (1)
column and placing them in the break footing text.
There are various edit codes that are available to the QMF user. The edit code D2,
associated with the SALARY column, adds the currency symbol and decimal punctuation
(displaying 2 digits to the Right of the decimal point) to the SALARY values in the report.
3-49
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DEPT.
NUMBER
-----15
JOB
----SALES
MGR
CLERK
CLERK
DEPT 15 TOTAL
20
SALES
MGR
CLERK
CLERK
PERNAL
SANDERS
JAMES
SNEIDER
SALARY
----------$16,502.83
$20,659.80
$12,258.50
$12,508.20
----------$61,929.33
DEPT 20 TOTAL
$18,171.25
$18,357.50
$13,504.60
$14,252.75
----------$64,286.10
GRAND TOTAL
===========
$126,215.43
COMPANY CONFIDENTIAL
CE0317.0
Notes:
cl
QMF gives the user interactive access to the report formatting instructions. By making
some simple changes to the formatting instructions, the tabular report on the previous page
is converted to the tailored report above. The queries and report formatting instructions
may be saved.
pr
Ex
QMF for Windows and mainframe QMF have access to the same queries when connected
to the same DB2 system.
V5.4
Student Notebook
Uempty
QMF procedures
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
1=Help
7=Backward
2=Run
8=Forward
3=End
9=Form
4=Print
10=Insert
5=Chart
11=Delete
6=Query
12=Report
CE0317.0
Notes:
QMF procedures are comprised of stacks of QMF commands. The user must enter only
one command per line, and no SQL is allowed in a procedure, only valid QMF commands.
pr
Ex
cl
QMF commands can run saved queries, which contain SQL statements (QUERY1 and
QUERY2 above) and format the results with saved formatting instructions. Additional QMF
commands allow the user to print reports, save output data to MVS datasets, etc.
3-51
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
pr
Ex
cl
QMF for Windows is a GUI interface that connects to any DB2 system. Click the button with
the piece of paper icon to open a query window. Multiple query windows can be open at
any time, each query window running a query against a different database. Click the button
with the exclamation point/report icon to run the query.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
pr
Ex
cl
Data can be saved to the same database, a file, or a different database. From the Results
menu, click Save to Database.
3-53
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
By making changes to the report formatting instructions of QMF for Windows, we have
changed the format of the retrieved data.
pr
Ex
cl
The output in the visual looks very similar to the formatted report seen earlier for the QMF
mainframe product. The above report now has a page header and footer, the report has
been broken apart where the department numbers changed, and subtotals and grand
totals were added.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
pr
Ex
cl
When QMF for Windows is installed, an add-on can be applied to make local copies of the
report and save them into Microsoft Excel. The spreadsheet is opened first, then QMF for
Windows is started by clicking the QMF for Windows add-on icon. Click File, Move Data to
Microsoft Excel.
3-55
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
cl
The example above is of QMF for Windows accessing DB2 for z/OS data and using
Business Information tools such as Cube Views and OLAP (OnLine Analytical Processing)
analytics to provide information from across the enterprise.
These products deliver the following capabilities:
Ex
pr
V5.4
Student Notebook
Uempty
Checkpoint (1 of 2)
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
cl
2.
pr
Ex
3.
3-57
Student Notebook
Checkpoint (2 of 2)
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
pr
Ex
cl
5.
V5.4
Student Notebook
Uempty
Unit summary
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
pr
Ex
cl
Notes:
3-59
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Student Notebook
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
Accountability:
4-1
Student Notebook
Checkpoint questions
Unit objectives
After completing this unit, you should be able to:
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
pr
Ex
cl
Notes:
4-2
V5.4
Student Notebook
Uempty
Test tables
with ~ 1000 rows
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Production tables
with millions of
rows
Programmer
Statistical
sample
CE0317.0
Notes:
Ex
cl
In a test environment, we want our test tables to look exactly like the production tables. The
tables should have the same names (if the tables are on the same DB2 subsystem in z/OS
or in the same database on UNIX or Windows, then the schema/qualifier names will be
different), the same column names, lengths, data types, null characteristics, sequence,
constraints everything about the test tables and the production tables should be the
same.
This can be accomplished by the following SQL statements:
pr
to create a table with the same structure, and then another SQL statement:
2. INSERT INTO new_table_name SELECT * FROM old_table_name with a WHERE
clause
to populate the newly defined smaller table with the test data. It is always wise to keep a
hard copy of your test data close at hand.
4-3
Student Notebook
The test tables should be populated with a statistically accurate representation of the
production data, but with significantly fewer rows. The test data should be designed so
that:
1. All possible forms of input are verified/validated
2. All logic paths in the code are exercised
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
It is very common to privatize the data from production before loading it into test. A
product like Optim is ideal for performing that task.
4-4
V5.4
Student Notebook
Uempty
ATE
VIE
..... W V1
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CRE
Programmer
CREATE INDEX
INDEX_B
.....
CREATE VIEW V2
.....
CR
EA
T
IND E IN
EX D E
.... _C X
.
3
WV
E
I
V
ATE ...
E
..
CR
CE0317.0
Notes:
cl
When replicating a production system, be sure to create the same indexes and views as
are used on that system. Also, consider building indexes on additional columns to see if the
DB2 Optimizer (we will discuss that later in the course) would use them.
pr
Ex
If creating a test environment for a new system, meet with the DBA and users to determine
how the data will be accessed (which columns will be included in WHERE, ORDER BY,
and GROUP BY clauses, and which columns should contain unique values). These are
columns which are the primary candidates for use in creating indexes.
4-5
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Updating
test
statistics
Gathering
production
statistics
Programmer
DBA
Catalog
CE0317.0
Notes:
Ex
cl
Although our test tables have very few rows, and DB2 will process them very quickly when
we run queries against the test tables, we want the DB2 Optimizer to make the same
access strategy decisions (how to access the data most efficiently) it would make when
running against production data. We want DB2 to think that the test tables contain
production quantities of data. In other words, we are replicating the statistics from the
production system to test, in order to simulate the production-level volume of data.
pr
In most shops, only DBAs and system administrators (SYSADMs) have the authority to
update catalog values. The programmer can assist in gathering the production level
statistics that are needed.
4-6
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
ROLLBACK ...;
CE0317.0
Notes:
cl
Let's take a minute to address the issue of change SQL statements i.e., INSERT,
UPDATE, and DELETE statements. These statements change the test tables. However,
remember that we want to stabilize our test environment change.
Ex
To avoid a permanent change to the data in our tables, we run several SQL statements as
a unit of work, which means that the work is not made permanent unless all SQL
statements finish successfully. If one statement fails, all work done by the statements that
completed up to that point will be undone. Semicolons are used to end a query when
multiple queries are stacked within a unit of work.
pr
Lets describe the statements above in a little more detail. The first SELECT statement
(known as the BEFORE SELECT) displays what the table data looks like before the
change SQL statement is executed. The second SELECT statement (known as the
AFTER SELECT) displays the table data as it is after INSERT, UPDATE or DELETE
statement has run. By comparing the results of the two SELECTs, the application
programmer should be able to determine if the effects of the SQL were what was expected.
4-7
Student Notebook
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
The ROLLBACK statement will undo all of the changes to the data affected by the INSERT,
UPDATE or DELETE. Therefore, this unit of work approach is an effective technique for
testing the logic of change SQL statements, while at the same time preserving your test
data in its original state.
4-8
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SELECT ....
FROM ..., ..., ...
WHERE ..... AND
.....
ORDER BY ..., ...
Programmer
CE0317.0
Notes:
cl
The application programmer should consider whether or not there are alternative SQL
statements that could be utilized to meet the logic requirements of the program. If so, then
he or she should try to choose the SQL statements that would perform most efficiently.
pr
Ex
When the program is prepared for execution, the DB2 optimizer will choose the access
path which is most likely to consume the fewest system resources (I/Os and CPU cycles).
If DB2 perceives that a different SQL statement will perform better than the one coded by
the programmer (and yet still guarantee the same results set), DB2 will actually change the
statement so that it will perform better this is called Automatic Query Rewrite, or AQR for
short.
The SQL Workshops (CE12 and CE13) teach alternate methods of coding SQL
statements. The Performance and Tuning Workshops deal with evaluating and monitoring
the performance impact of these methods. See Appendix B for further courses and details.
4-9
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DB2
Tables - R - Us
Integrity
&
Recovery
Programmer
Continuous
Operation
Logging
Optimizer
Lock
Manager
Security
CE0317.0
Notes:
cl
Suppose that we've written three queries that will bring back the same data. Built into DB2
is a facility called EXPLAIN. We can use this facility to ask DB2 to tell us how it will go
about getting to the data we want. EXPLAIN can be invoked with SQL or from a graphical
user interface (GUI).
pr
Ex
Of the three proposed access paths in the visual above, which one do you think is the
best? Why?
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Program
write to terminal
|
read from terminal
|
read/write - file
|
SQL - read/write - DB2
|
write to terminal
|
end
Data in a file
Data in DB2
An
additional
source of
data
CE0317.0
Notes:
pr
Ex
cl
Programs with embedded SQL have all their normal capabilities, plus access to relational
data managed by DB2. Such programs do require some additional preparation, because
of the SQL statements contained therein.
4-11
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
Very briefly, here are the major considerations when coding SQL statements into a
program:
cl
Delimiters are used by the precompiler in order to identify the embedded SQL statements
for special processing.
Ex
Host variables are the means by which the program and DB2 pass data back and forth.
An SQLCA is normally included. This set of control blocks provides return codes and
other important information to the program, after each SQL statement is executed.
pr
If there was an error performing the SQL, the programmer needs to take appropriate
action for the error by coding routines for handling the various error conditions that may
occur.
The following visuals will explain these points in more detail.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Program A
Answering Machine
COBOL delimiters
...
Start flag
Precompiler,
SQL starts
here!
End flag
...
Start flag
Precompiler,
SQL ends
here!
.
.
EXEC SQL
DECLARE GLOBAL
TEMPORARY TABLE T1
AS (...)
END-EXEC.
End flag
EXEC SQL
SELECT ...
INTO ...
FROM T1
WHERE ...
END-EXEC.
...
End
CE0317.0
Notes:
The opening delimiter is always EXEC SQL. The ending delimiter varies by programming
language.
cl
pr
Ex
In the program, note the DECLARE GLOBAL TEMPORARY TABLE T1 AS (...) SQL
statement. Within a program, SQL can be used to define temporary tables that may be
accessed by SQL statements in other areas of the same program. Between the
parentheses, an SQL SELECT statement is written that defines the set of columns and
rows that will be known as T1.
4-13
Student Notebook
Host variables
3'
EXEC SQL
NEW
PHONE
UPDATE COMPANY.DIRECTORY
777'
'555-1
323
'555-
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
EXEC SQL
UPDATE COMPANY.DIRECTORY
SET PHONE = '555-3233'
WHERE PHONE = '555-1777'
END-EXEC.
OLD
PHONE
SET PHONE =
WHERE PHONE =
END-EXEC.
CE0317.0
Notes:
Ex
cl
When imbedding an SQL statement in a host language program, our SQL statements
become more flexible. Instead of hardcoding values in the SQL statement (as in the first
UPDATE statement above), we can use host variables (as in the second UPDATE
statement) in their place. The first UPDATE is fixed, and can only be used once with the
hard-coded values. The second UPDATE is re-useable, because the program logic can
repeatedly replace the values in the host variables.
pr
As depicted in the visual, host variables can be thought of as buckets which can hold
information. Host variables are areas of storage that are defined within your program,
which can be used to pass data to DB2, or to receive data retrieved from DB2. The
programmer needs to define host variables with the same data type and length as the
columns with which they will be used.
V5.4
Student Notebook
Uempty
In COBOL, C
COBOL
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
WORKING-STORAGE SECTION.
.
char newphone [10];
char oldphone [10];
.
fscanf (stdin, newphone, %s);
.
fscanf (stdin, oldphone, %s);
.
EXEC SQL
UPDATE FRANCE.PARIS
SET PHONE = :newphone
WHERE PHONE = :oldphone ;
CE0317.0
Notes:
pr
Ex
cl
All host variables must begin with a colon when used within an SQL statement.
Elsewhere in your program logic (outside of the SQL statement), do not prefix the host
variable name with a colon.
4-15
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
EMPNO
LASTNAME
FIRSTNAME
EXEC SQL
000020
THOMPSON
MICHAEL
000010
HAAS
CHRISTINE
CE0317.0
Notes:
A query that returns a maximum of one row in the results set is known as a singleton
SELECT.
cl
Selects that return one or no rows are written with an additional clause, the INTO clause.
The INTO clause must name the proper number of host variables to hold the result row.
Ex
The first column in the result row will be placed in the first host variable named, the
second column in the result row will be placed in the second host variable named, and so
forth.
pr
If it is possible for the SELECT to return multiple rows in the results set, another
programming technique is required, called cursor processing.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DECLARE CUR1
CURSOR FOR
SELECT EMPNO, LASTNAME FROM EMPL
WHERE WORKDEPT = :DEP
END-EXEC.
END-EXEC.
CE0317.0
Notes:
When retrieving multiple rows from DB2, the program uses a technique known as cursor
processing. It is comprised of the following four steps.
cl
Ex
2. OPEN the cursor (DB2 executes the SQL and accesses the qualifying rows).
Sometimes the results set is placed into a temporary file, and at other times DB2 will
retrieve the rows when they are individually FETCHed by the application.
3. FETCH the rows from the stack. Conceptually, at OPEN time, you can conceive of the
rows as being placed into a stack, from which your program can retreive.
pr
4. CLOSE the cursor, when the application no longer needs to fetch any more rows.
Keep the following points in mind, regarding cursor processing:
SELECTs that could potentially return more than one row must be coded as a cursor.
A program may have any number of cursors.
Conceptually, the SELECT statement is executed at OPEN time.
4-17
Student Notebook
Normally, your program will retrieve rows in an iterative loop, until you reach the end of
the stack of qualifying rows (SQLSTATE = 02000)
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
V5.4
Student Notebook
Uempty
Fetch
EXEC SQL
FETCH CUR1
INTO :ID, :NAME
END-EXEC
1 st
LASTNAME
000020
000010
THOMPSON
HAAS
SQLCA
SQLSTATE
ID
NAME
First Fetch
'00000'
000020
THOMPSON
Second Fetch
'00000'
000010
HAAS
Third Fetch
'02000'
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
EMPNO
2 nd
CE0317.0
Notes:
cl
pr
Ex
SQLSTATE codes are character return codes. A return code of 00000 means successful
completion of the SQL statement. An SQLSTATE of 02000 means no more rows meet
our criteria (an end-of-file condition).
4-19
Student Notebook
Scrollable cursors
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
EXEC SQL
DECLARE CURSOR K9
SENSITIVE STATIC SCROLL FOR
SELECT ...
END-EXEC.
...
EXEC SQL OPEN K9 END-EXEC.
...
EXEC SQL FETCH LAST FROM K9 INTO :VN:VI END-EXEC.
...
EXEC SQL FETCH RELATIVE -10 FROM K9 INTO :VN:VI
END-EXEC.
...
EXEC SQL FETCH BEFORE FROM K9 INTO :VN:VI END-EXEC.
...
EXEC SQL CLOSE CURSOR K9 END-EXEC.
CE0317.0
Notes:
cl
Scrollable cursors allow us to fetch forwards, backwards, using a relative number forwards
and backwards, or using an absolute number. Scrollable cursors are currently only
available in DB2 for z/OS.
When the cursor is opened, DB2 builds the result table in a temporary database.
Ex
The word SCROLL in the cursor definition makes the cursor scrollable. The word STATIC
in this cursor's definition tells DB2 that the rows in the result set are to remain fixed. The
word SENSITIVE in the cursor's definition tells DB2 to keep the result set synchronized
with changes to the actual rows in the table (as opposed to INSENSITIVE).
pr
The first fetch returns the last row in the result set.
The second fetch returns the row 10 rows above the current cursor position.
The third fetch returns the row above the current cursor position.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Program A
Program Logic
SQL
read a row
A call
DB2
to DB
2
Tab l es - R - U s
Integrity
&
Recovery
SQLCA -
Continuous
Operation
essage Return m
r failure?
Success o
Logging
Optimizer
Program Logic
Lock
Manager
Security
End
CE0317.0
Notes:
cl
The SQLCA is a structure of specifically named host variables to which DB2 writes after
each call to DB2. Many shops have standard error handling routines that can be copied
into your program and performed, or called, after each SQL statement. It is recommended
that a DB2 application program check the SQL return codes and information after each call
to DB2.
Ex
Within SQLCA, the first thing you will probably want to check is the SQL return code. You
have a choice of:
SQLCODE, the original return code, which may be platform-specific.
pr
Both SQLCODEs and SQLSTATEs are documented in the DB2 Message Reference
manual or the DB2 Messages and Codes manual. The general categories of these codes
are explained on the next page.
4-21
Student Notebook
SQLCA codes
CHAR(5)
SQLSTATE
REQUEST STATUS
<0
>"02nnn"
FAILED
"01nnn"
SATISFIED WITH
SPECIAL CONDITION
(SQLWARN0 = 'W')
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
INTEGER
SQLCODE
CONDITION
ERROR
WARNING
>0 &
<> 100
+100
NOTFOUND
SUCCESS
"02nnn"
(MORE)
DATA NOT FOUND
"00000"
SUCCESS
CE0317.0
Notes:
cl
The SQLCA (SQL Communications Area) is set of control blocks that provides return
codes and other important information to the program, after each SQL statement is
executed.
All negative return codes are considered errors.
Ex
All positive return codes not equal to 100 are considered warnings. Also, a 'W' in
SQLWARN0 indicates a warning, even if SQLCODE = 0.
The meaning of SQLCODE values other than 0 and 100 is product specific.
pr
Here is a listing of some of the more commonly-encountered SQLCODEs in DB2 for z/OS:
Some common SQLCODE values:
-551 - <authorization-ID> does not have the privilege to perform operation <operation> on
object <name>. (SQLSTATE 42501)
-818 - A timestamp conflict occurred. (SQLSTATE 51013)
-805 - Package <package-name> was not found. (SQLSTATE 51002)
4-22 DB2 Family Fundamentals
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
-305 - The NULL value cannot be assigned to a host variable in the SELECT or FETCH
statement because no indicator variable is specified. (SQLSTATE 22002)
-811 - The result of a scalar fullselect, SELECT INTO statement, or VALUES INTO
statement is more than one row. (SQLSTATE 21000)
+100 - No row was found for FETCH, UPDATE, or DELETE; or the result of a query is an
empty table. (SQLSTATE 02000)
pr
Ex
cl
Detailed descriptions of all error and warning messages can be found in the DB2
Messages and in the DB2 Codes reference manuals.
4-23
Student Notebook
Precompile
Program A
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Answering Machine
Precompiler,
SQL starts
here!
Program A
Statement 27
200110271345050558
SQL - read a row
.
.
.
.
Precompiler
copies SQL
CE0317.0
Notes:
There is one input to the precompiler (source code), and two outputs (modified source
code, and the DBRM or bind file).
cl
Ex
Performs some syntax checking on the SQL statements (a more thorough check occurs
at BIND time)
Copies the source code to a new file
pr
V5.4
Student Notebook
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Uempty
4-25
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Program A
Modified
source
code
Answering Machine
Compiler
Precompiler,
SQL starts
here!
Link edit
Load
module
CE0317.0
Notes:
pr
Ex
cl
After the precompile has successfully completed, the modified source code goes through
normal compile and link edit steps, to create an executable load module.
V5.4
Student Notebook
Uempty
Bind package (1 of 4)
Bind Package
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DB2
Tables - R - Us
Integrity
&
Recovery
Continuous
Operation
Logging
Optimizer
Lock
Manager
Security
Catalog
Syntax Check
CE0317.0
Notes:
pr
Ex
cl
The bind package process can be thought of as a compile of the SQL portion of the
application program. During the bind, DB2 does a thorough check of the SQL syntax. If
there are errors that were not detected during the precompile, the SQL parser will find them
and flag them as the first major step of the BIND process.
4-27
Student Notebook
Bind package (2 of 4)
Bind Package
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DB2
Tables - R - Us
Integrity
&
Recovery
Continuous
Operation
Logging
Optimizer
Lock
Manager
Security
Catalog
Object Validation
CE0317.0
Notes:
pr
Ex
cl
It is very common for a programmer to misspell a table or column name when coding SQL
statements. This second major step of the BIND process checks to see if all of the tables
and columns referenced in the call actually exist in the DB2 Catalog. If no errors are
detected during this phase, then the next step is a security check.
V5.4
Student Notebook
Uempty
Bind package (3 of 4)
Bind Package
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DB2
Tables - R - Us
Integrity
&
Recovery
Continuous
Operation
Logging
Optimizer
Lock
Manager
Security
Catalog
Security Check
CE0317.0
Notes:
pr
Ex
cl
DB2 checks the catalog authorization tables to ensure that the application and user has the
proper authorization to execute the program's SQL statements. If there are no
authorization errors or violations, then the BIND process can continue to the last step in
preparing the applications SQL for execution by DB2.
4-29
Student Notebook
Bind package (4 of 4)
Bind Package
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DB2
Program A
Statement 27
199710271345050558
SQL - read a row
.
.
.
.
Tables - R - Us
Integrity
&
Recovery
Continuous
Operation
Logging
Optimizer
Directory
Lock
Manager
Security
Packages
Catalog
Optimization
CE0317.0
Notes:
It is during the last step of the BIND process that DB2's optimizer decides how it is going to
access the data.
Ex
cl
Earlier in this course, we mentioned that SQL is a non-navigational language, meaning that
we tell DB2 WHAT data we want to retrieve, but not HOW to retrieve that data. It is at
optimization time that DB2 determines how it will execute the SQL.
pr
To make the best access choices, DB2 must learn about the data that we're trying to
access. The optimizer goes to the catalog, and looks up information about the table(s) and
the table's indexes. The optimizer finds out how big the table is (how many pages and how
many rows). For each index, it finds how many unique values it contains, and for each
non-unique index DB2 finds, and what that index's most common values are.
With this, and additional information, the optimizer begins the task of finding the least cost
route to the data based on our SQL query's requirements. DB2 evaluates the WHERE
clause to see if, and which, indexes might be used to speed up access to the data. DB2
calculates the cost (in I/O and CPU cycles) of reading all the rows in the table (a table
V5.4
Student Notebook
Uempty
scan). It calculates the cost of using index x, and of using index y. If appropriate, it
calculates the cost of using multiple combinations of indexes.
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
The bottom line is that DB2 comes up with every route to the data it can think of, and then
calculates the cost of each route. It then compares the cost of each route, and the least
cost route is chosen as the access strategy for that SQL statement. Think of that route as
being a map to the data that our SQL statement requires. DB2 writes the map out to a
package which is kept in DB2's directory. This map will never change unless we rebind the
DBRM. A rebind asks DB2 to recalculate the cost of each possible route to the data, and
again, choose the least cost route and write it out to a package.
4-31
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Bind Plan
DB2
Tables - R - Us
Integrity
&
Recovery
Directory
Continuous
Operation
Optimizer
Logging
Plans
Plan A
Directory
Pkg A
Plans
Lock
Manager
Directory
Packages
Security
Catalog
Package Pointers
CE0317.0
Notes:
With DB2 z/OS, a plan is the means by which the SQL statements are executed, when the
application program (which is associated with that SQL) is run.
Ex
cl
There are two types of plans. The original plan consisted of a monolithic structure, into
which all of the SQL statements were directly bound. This structure could grow very large,
and even a slight modification to the SQL within the plan required that the entire plan go
through the BIND process. This proved to be cumbersome and resulted in the
consumption of considerable system resources.
pr
In version 2.3 of DB2 on the mainframe, an alternative method was made available to
users. A single program containing SQL could be bound into a structure called a package,
which provided users with a more modular approach. With this new capability, program
maintenance became easier and less resource consumptive, since only the program with
the changes to its SQL needed to be re-bound, rather than the entire plan structure.
However, there was still the requirement to execute the SQL by means of a plan. The new
plan could now consist simply of a directory list of the individual packages within the plan.
A plan which consists of a package list is analogous to a PDS directory pointing to
4-32 DB2 Family Fundamentals
V5.4
Student Notebook
Uempty
individual members. Inside a plan are the names of the packages with which the load
module may work. The programmer or DBA creates a plan, and in the plan they list the
packages that the load module is allowed to work with. Plans are stored in the DB2
directory.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Over time DB2 shops migrated to this new methodology of binding packages. With the
most current version of DB2 z/OS (DB2 10), the older method of binding DBRMs directly
into a plan is no longer supported. It is required of DB2 10 users to use the newer package
approach.
pr
Ex
cl
The use of plans and packages is explored in more detail in the Application Programming
Workshop courses.
4-33
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Directory
Load Module
(Program A)
Plans
Plan A
Pkg A
At the first
call to DB2
Directory
Packages
Pkg A
CE0317.0
Notes:
cl
The PLAN option of the RUN command is not required if the plan name is the same as the
program name. In the example in the visual above, since the plan-name and the
program-name are both A, the PLAN keyword is not mandatory.
pr
Ex
In DB2 LUW (which was developed a number of years after the availability of DB2 for the
mainframe environment), the concept of a plan does not exist. In the LUW environment,
only packages are used .there are no plans.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Directory
Load Module
(Program A)
At the first
call to DB2
Packages
Pkg A
CE0317.0
Notes:
pr
Ex
cl
Since plans are not used by DB2 for Linux, UNIX, or Windows, running the program
instructs DB2 to locate the appropriate package. DB2 locates the package in the DB2
directory when the application program encounters the first CALL to DB2.
4-35
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Include Libraries
Precompiler
Bind Package/Plan
Program A
Modified
Source
Code
Answering Machine
.
.
Tables - R - Us
DB2
Integrity
&
Recovery
Compiler
Continuous
Operation
Precompiler,
SQL starts
here!
** SQL - read a
row
**
**
**
Logging
Link Edit
Precompiler,
SQL ends
here!
DBRM Program A
Statement 27
199710271345050558
SQL - read a row
.
.
.
.
Optimizer
Directory
Lock
Manager
Security
Packages
Catalog
DBRM
Load
Module
End
At the first
call to DB2
Syntax Check
Security check
Access Strategies
CE0317.0
Notes:
pr
Ex
cl
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Client
EXEC SQL INSERT ...
Process SQL
Process SQL
Process SQL
Process SQL
Presentation Logic
+ Business Logic
Data Management
CE0317.0
Notes:
cl
pr
Ex
Network traffic can have a very noticeable impact upon overall application performance.
Ideally, applications should be designed to minimize message traffic across the network.
4-37
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Server
Parameters
DB2
PROCA
SQL
SQL
Stored
Procedure
SQL
Presentation Logic
Returned values
from PROCA
Data Management
+ Business Logic
CE0317.0
Notes:
cl
This visual illustrates a different programming approach. Here we have a thin client,
consisting primarily of the presentation logic. The business logic is encapsulated at the
database server location. The business logic is invoked by the client issuing a CALL to a
program resident at the database, called a stored procedure.
pr
Ex
The call across the network includes parameters required by the stored procedure. The
stored procedure uses the parameters to complete its logic. The stored procedure
executes, and, when finished, may return a set of values or rows to the calling program.
When comparing this programming approach with the previous one, it becomes obvious
that this model makes more efficient use of the network bandwidth, by significantly
reducing the number of transmissions between the client and server.
Additionally, program maintenance is enhanced. Since the main program logic resides at
the single server location, rather than at each of the clients, it is much easier to make
changes to the application, and we can ensure consistency of the logic for all of the clients
that execute the program.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
cl
The call to the stored procedure must be delimited. The above call example is calling a
stored procedure named SPNAME and passing three parameters (P1, P2, P3). The stored
procedure could be written in any number of languages, such as COBOL, C, C++, SQL
Procedure Language, Java, and so on.
pr
Ex
The stored procedure could have been written and tested by utilizing the Data Studio
software. Data Studio is an Eclipse-based GUI tool that makes it easier for the
programmer to develop stored procedures in DB2.
4-39
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Data Studio
CE0317.0
Notes:
cl
IBM Data Studio is an Eclipse-based tool that replaces the DB2 Developer Workbench
from DB2 for Linux, UNIX, and Windows. IBM Data Studio is a comprehensive
development environment for creating, editing, debugging, deploying, and testing DB2
stored procedures and user-defined functions. You can also use IBM Data Studio to
develop SQLJ applications, and to create, edit, and run SQL statements and XML queries.
Ex
The IBM Data Studio includes functionality that is comparable to Developer Workbench,
including these items:
pr
Database code designers for SQL statements, stored procedures, and user-defined
functions
Support for running statements in the database
Debugging support for DB2 stored procedures
The IBM Data Studio also provides cross-platform support for IBM DB2 Data Server and
Informix Dynamic Server.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Program A
Answering
Machine
Modified
Source
Code
.
.
Tables - R - Us
Compiler
DB2
Precompile
r,
SQL starts
here!
Integrity
&
Recovery
Link
Edit
Continuous
Operation
here!
Optimizer
Logging
Directory
Load
Module
.
End
Plans
Plan A
At the first
call to DB2
Pkg A
Lock
Manager
Security
Directory
Packages
Pkg A
Catalog
CE0317.0
Notes:
cl
pr
Ex
When the program is executed, DB2 locates the predetermined access strategy and
follows that strategy to the data. DB2 will not consider changing the access strategy until
the program is once again bound.
4-41
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
ACCEPT
WHATS-IN-THIS-BUCKET.
.
UPDATE FRANCE.PARIS
SET PHONE = '555-2727'
WHERE PHONE = '555-1778'
** EXEC SQL
**
EXECUTE IMMEDIATE
**
:WHATS-IN-THIS-BUCKET
** END-EXEC.
199710270842542532.
CE0317.0
Notes:
Dynamic SQL statements are statements that DB2 acquires during the execution of the
program: statements of which DB2 was not aware at bind time.
Ex
cl
In this example, the program contains some presentation logic that requests an SQL
statement from the user. The user's SQL statement (a string of characters) will be placed in
a host variable. The SQL that is hard-coded in the program tells DB2 to EXECUTE
IMMEDIATE the string of characters in the host variable. INSERT, UPDATE, and DELETE
statements can be handled almost this easily. SELECT statements, however, may require
much more complex coding.
pr
The SQL that DB2 evaluates at bind time is the EXECUTE IMMEDIATE
:host_variable_name. At bind time, the host variable contains nothing. At execution time,
the user supplies the SQL, the call to DB2 passes the contents of the host variable (the
user's SQL statement) to DB2, and DB2 must dynamically bind that statement (check the
syntax, check the user's authorization, and determine the least cost access strategy). That
access strategy does not become a permanent part of the package.
V5.4
Student Notebook
Uempty
Static SQL
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Dynamic SQL
CE0317.0
Notes:
pr
Ex
cl
4-43
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CLIENT
APPLICATION PROGRAM
Embedded SQL
CLI/ODBC
Java
Run-Time Client
Static SQL
Dynamic SQL
APIs
DATABASE ENGINE
SERVER
CE0317.0
Notes:
cl
A client/server application program gets access to data through the Run Time Client (RTC)
code (for Windows-based applications there is even DB2 RTCL (Lite)). This can run static
or dynamic programs from a client accessing a DB2 server. It can also run program APIs.
Ex
Application programs can issue SQL via the Run-Time Client component using one of
several possible programming interfaces.
Embedded SQL
CLI
pr
- ODBC
Java
- JDBC
- SQLJ
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DB2 SERVER
CLIENT
DB2 Storage:
Storage:
DB2
SQL/XML
Relational
Relational
Interface
DB2 Client /
DB2 Client /
Customer
Customer
Client
Application
Relational
Relational
DB2 Engine
XQuery
XML
XML
XML
Interface
CE0317.0
Notes:
Beginning with DB2 9: Integration of XML and relational capabilities so that applications
can combine XML and relational data.
cl
Ex
There is deep integration as the figure above shows. This is not an add-on, extender, or
wrapper.
pr
The DB2 infrastructure is reused wherever it made sense, with new system code where
needed.
DB2 9 for LUW supports a new query language: XQuery, with a new parser (side by side
of SQL parser).
XML data is natively storage: hierarchical, optimized for parent/child traversal.
XML permeates the engine. You can use SQL or XQuery to query a mix of relational and
XML data stored in DB2. The same complier handles both XML and relational data, so
Copyright IBM Corp. 1997, 2011
4-45
Student Notebook
SQL and XQuery queries can be mixed. Because XML is not just another numeric type,
but a significant change, special efforts are necessary in almost all system components,
tools and utilities to handle XML.
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Xquery: Xquery is an emerging standard for access to XML data. It is designed, from the
ground up, to handle semi-structured data. DB2 supports both the Xquery language and
the SQL extensions for accessing XML data in a DB2 table.
V5.4
Student Notebook
Uempty
What is XML? (1 of 2)
XML = eXtensible Markup Language
XML is self-describing data
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
47; John Doe; 58; Peter Pan; Database systems; 29; SQL;
relational
<book>
<authors>
<author id=47>John Doe</author>
<author id=58>Peter Pan</author>
</authors>
<title>Database systems</title>
<price>29</price>
<keywords>
<keyword>SQL</keyword>
<keyword>relational</keyword>
</keywords>
</book>
CE0317.0
Notes:
cl
The example of data above taken from a flat file shows a line of raw data. We see the
numbers 47, 58, 29 and the words John Doe, Peter Pan but we dont know what each of
these pieces of data means; the context of the data is external to the data itself.
Ex
The XML markup shown above is in blue and the actual data is in red. By viewing both, we
know what this data means. XML is eXtensible Markup Language. The markup shows the
tags used to describe the data. XML is a self describing kind of data.
The key difference between HTML and XML markup languages:
XML describes the relationship of the data.
pr
XML is extensible this means you can make up your own markup and tags to describe
the data. You can create your own tags if they are not tied to a certain schema.
HTML has a fixed vocabulary of tags and can only use those tags.
4-47
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
What is XML? (2 of 2)
CE0317.0
Notes:
cl
Elements and attributes make up an XML document. These elements always have a start
and stop tag. Tags are always in angle brackets <> and the end tag is denoted by the slash
/. Elements can be nested. The root element is the outer most element for the document
as shown above, the book element is the root element.
Ex
Attributes can be specified for an XML element. As shown above, the author element has
an id attribute assigned to it. The attributes are always defined in the start tag. In the code
shown above, the value of the id attribute for the first author is 47 and assigned to the
author element in the first set of angle brackets.
pr
Note that the authors element (denoted by the authors tag) contains two nested elements
named author.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
cl
Every XML document can be viewed as a tree a hierarchy of nodes. Every XML
document can be seen as a higher key of nodes. It may be intuitive that this represents a
hierarchy of nodes. In the example above, book is the root node of the tree. The data
elements are always the leaf nodes in the tree (such as the title element in the example
above).
Ex
XML parsing typically produces this tree form. This is how XML can be manipulated by
software storing XML in trees in DB2 and not as text, which is more efficient and faster
to manipulate. This is done by parsing the tree.
pr
Serialization is reversing the parsing operation, which takes a tree and returns it to XML
document format.
In the graphic shown above, each node would have the following paths:
root node (book element)
/book
- authors node (authors element) /book/authors
Copyright IBM Corp. 1997, 2011
4-49
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
You may use a new query language XQuery to retrieve XML documents (and parts of
documents) from the database.
cl
Example 1 Xquery:
Ex
pr
<name>
Jim Noodle
</name>
<addr country="Canada">
<street>
Copyright IBM Corp. 1997, 2011
4-51
Student Notebook
25 EastCreek
</street>
<city>
Markham
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
</city>
<prov-state>
Ontario
</prov-state>
<pcode-zip>
N9C 3T6
</pcode-zip>
</addr>
<phone type="work">
905-555-7258
cl
PID
----------
Ex
100-100-01
100-101-01
100-103-01
pr
100-201-01
V5.4
Student Notebook
Uempty
Checkpoint (1 of 2)
1. Why is it a good idea to have a test environment?
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
2. How can we determine what access path DB2 will take when it
executes an SQL statement?
3. Why must a host language program have an SQLCA (SQL
Communications Area)?
4. Why must SQL statements in a host language program be
delimited?
5. What is the opening delimiter for every SQL statement (except for
the Java language)?
6. List several things the PRECOMPILER does for us (minimum of
three things).
7. List several of the things DB2 does during the bind of a package.
CE0317.0
Notes:
cl
2.
Ex
3.
4.
5.
pr
6.
7.
4-53
Student Notebook
Checkpoint (2 of 2)
Name two benefits of using stored procedures.
9.
Host variables, when used within SQL delimiters, must all begin
with what?
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
8.
CE0317.0
Notes:
cl
9.
Ex
10.
pr
11.
V5.4
Student Notebook
Uempty
Unit summary
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
pr
Ex
cl
Notes:
4-55
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Student Notebook
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
Accountability:
5-1
Student Notebook
Unit objectives
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
After completing this unit, you should be able to list some of the tasks performed by a DB2
System Administrator:
cl
Ex
pr
5-2
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Product
installation
System
Admin
Grantor
of initial
privileges
Creation of
system
objects
DB2 system
performance
CE0317.0
Notes:
pr
Ex
cl
The are the ones who will be installing the RDBMS software on the system. They are
responsible for creating the system level objects, and for the initial granting of authorities
within the DB2 environment. They are also responsible for the overall performance of DB2.
5-3
Student Notebook
DB2 products
Linux/UNIX
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Windows
DB2
DB2
iSeries
zSeries
DB2
DB2
CE0317.0
Notes:
cl
Designed to meet the information needs of small and large businesses alike, DB2 is
available on a variety of platforms, including large systems such as z/OS, VM, VSE and
Linux for zSeries; mid-sized systems such as OS/400, AIX, Linux, HP-UX, and Solaris; and
single or LAN-based systems such as Windows 2000, Windows NT, Windows XP, and
Windows .NET.
pr
Ex
Because of DB2s ability to run on so many different hardware platforms, DB2 is naturally
very scalable. DB2 is capable of supporting hardware platforms from uniprocessor laptops
to massively parallel systems with hundreds of nodes and many processors per node. In
between, it can support SMP machines or clusters of SMP machines. This provides both
extensive and granular growth.
All members of the DB2 family have similar external architecture and use many of the
same key algorithms, which enables DB2 on different platforms to integrate into a cohesive
system. Additionally, the SQL is highly standardized, thereby ensuring portability of DB2
applications across the various platforms.
5-4
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
System
Admin
INSTALL
PANELS
CE0317.0
Notes:
The system programmer must ensure that all supporting software is at the proper release
level for the version or release of DB2 which you are installing.
cl
Calculations must be done to determine the amount of disk storage needed to contain the
projected data volumes.
pr
Ex
The system programmer may need to gather information that will affect their installation
(such as how many databases we'll be creating, the average number of expected tables
per database, the maximum number of concurrent users, etc.), in order to select the
appropriate settings and parameters when configuring the DB2 system environment
5-5
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CREATE STOGROUP...
System
Admin
CE0317.0
Notes:
cl
The system programmer is normally the person responsible for defining certain
system-level objects (such as storage groups, databases, etc.) Once those objects are in
place, the database administrator (DBA) is usually the one who will create those DB2
objects that are related to the business applications (tables, indexes, views, triggers, etc.)
pr
Ex
Most DB2 objects are created via SQL, or through panels that generate the SQL under the
covers. Each CREATE statement that is successfully executed results in the insertion of a
row (or rows) into one or more DB2 tables comprising the DB2 catalog, where all of the
database objects within the DB2 system are defined.
5-6
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
cl
The Control Center tool could be used to define objects in the DB2 system. Using the DB2
Control Center GUI the system administrator could right-click on the type of an object of to
be defined, and then would click CREATE from the drop-down menu. Objects can be
created by typing the SQL statement in proper syntax, or created by a wizard and mouse
clicks.
Ex
The DB2 Control Center has the look and feel of the Windows file manager. Left-clicking
items in the left pane displays sub-items in the DB2 hierarchy of objects in the right pane.
Right-clicking an item opens a menu of actions that can be taken on those objects.
pr
Be aware that the Control Center is being phased out in the most recent versions of DB2,
and being replaced by Data Studio, an Eclipse-based GUI which will provide similar
functionality to the DB2 Control Center.
5-7
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Data Studio
CE0317.0
Notes:
pr
Ex
cl
IBM Data Studio is an Eclipse-based tool that is intended to provide the functionality of
Control Center. It is a GUI tool for systems administrators, database administrators,
application programmers in fact, Data Studio provides features and functions that will
benefit nearly all DB2 users.
5-8
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
z/OS
Catalog
DB2 subsystem
Directory
Buffer pools
Storage groups
DB2 log
Database
Blue
Database
Green
Database
Red
JOIN
CE0317.0
Notes:
Installing DB2 on the z/OS platform involves the following major steps:
cl
1. Loading the DBMS software onto the system, using SMP/E (the RECEIVE, APPLY and
ACCEPT processes).
Ex
2. Setting all of the system configuration and application programming parameters via
interactive panels.
3. Running a series of jobs which are generated by the panel dialog in step 2, which builds
the DB2 subsystem.
pr
4. Executing a series of batch jobs, queries and transactions, to ensure the software was
properly installed and is functioning correctly. This step is know as the Installation
Verification Process, or IVP for short.
Each DB2 subsystem has its own catalog and directory, log, set of bufferpools, set of
storage groups, etc.
Very briefly, the DB2 catalog and directory contain the definitions of all DB2 objects within
the system. The DB2 log is a continuous record of all changes made to DB2 data. The
Copyright IBM Corp. 1997, 2011
5-9
Student Notebook
bufferpools are allocations of virtual storage which act as a go-between external DASD and
the DB2 applications. Storage groups are sets of DASD volumes which are available for
storing DB2 data.
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
In the z/OS environment, it is very easy to join tables (an SQL statement that needs to
access data from more than one table) from different databases. All that is required is for
the user or application to be authorized to access the tables in the databases, and (of
course) to code a syntactically correct SQL join statement.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Instance
Database Red
Database Blue
Catalog
Catalog
Buffer pools
DB2 log
Buffer pools
DB2 log
JOIN
(Requires federation)
CE0317.0
Notes:
Ex
cl
To install DB2 on LUW, the system programmer must first run the install .exe in order to
establish what is known as a DB2 instance. There are certain parameters and defaults that
must be chosen in order to tailor DB2 to the customers specific needs and requirements.
This process is accomplished through a database manager configuration file, which
determines the setting for the entire instance. If a second instance of DB2 is installed, the
system programmer will again customize the instance by means of another database
manager configuration file.
pr
Within an instance of DB2, multiple databases may be configured. In DB2 for Linux, UNIX,
and Windows, each database is considered as a separate entity, and each has its own
catalog, set of bufferpools, etc. Each database undergoes further customization by means
of a database configuration file. Each data base may be tailored as needed by the
systems programmer.
In DB2 LUW, in order to accomplish joins of tables within separate databases, it is
necessary to federate the databases. This involves additional configuration of the
database parameters, setting up nicknames, etc.) in order to accomplish this federation.
Copyright IBM Corp. 1997, 2011
5-11
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Table/Index definitions
Column data types
Defined constraints
Object dependencies
Object privileges
Statistical information about objects
CE0317.0
Notes:
Here are some key points to understand about the DB2 catalog:
cl
DB2 stores all information about your tables, indexes, columns, and other objects in the
DB2 catalog.
Ex
The DB2 catalog consists of normal tables and views, just like the ones in your own
databases. They can be read via SQL, just like your own tables.
pr
On some platforms, including DB2 for z/OS, catalog information is presented in tables. On
other platforms, including DB2 for Linux, UNIX and Windows and OS/400, catalog
information is also presented in views. The SQL Reference for that platform contains full
documentation of the catalog tables or views, whichever is preferred for that platform.
Catalog tables and views can be distinguished by their high-level qualifiers, that is,
authorization ID or schema names. The authorization ID used for catalog tables in DB2 for
z/OS is SYSIBM. SYSIBM.SYSTABLES is a commonly-used catalog table in DB2 for z/OS.
SYSCAT and SYSSTAT are the schema names used for catalog views in DB2 for Linux,
UNIX and Windows. SYSCAT.TABLES is a commonly-used catalog view in DB2 for Linux,
5-12 DB2 Family Fundamentals
V5.4
Student Notebook
Uempty
UNIX and Windows. SYSIBM is the schema name used for catalog views in DB2 for
iSeries.
SYSIBM.SYSTABLES/SYSCAT.TABLES contains information about every table and view.
SYSIBM.SYSCOLUMNS/SYSCAT.COLUMNS describes every column of every table and
view, and SYSIBM.SYSTABAUTH/SYSCAT.TABAUTH lists table privileges granted to
authorization IDs.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DB2 uses the catalog extensively as you work. For example, if you execute a query
against a table named EMPLOYEE, DB2 does the following:
- Looks in (SYS)TABLES to see if the EMPLOYEE table exists.
- Looks in (SYS)TABAUTH to see whether you are allowed to access the EMPLOYEE
table.
- Uses (SYS)COLUMNS to get the column names and the column sequence for the
EMPLOYEE table so that the data can be obtained for you.
If the table exists and you are authorized to use it, DB2 will present the result to you.
Otherwise, it will return an error message identifying the problem.
Since the DB2 catalog is such a critical component of the DB2 system, take good care of
the catalog tables:
1. Backup the catalog tables at least once daily, so that the catalog is readily recoverable.
2. REORG (a DB2 utility) the catalog tables on a regular basis for improved performance
3. Execute the RUNSTATS utility on the catalog tables, as needed, to ensure good
performance.
4. For z/OS, it is highly recommended to reserve the use of Bufferpool 0 for only the DB2
catalog and directory.
pr
Ex
cl
Note The DB2 Catalog is fully documented in the SQL Reference for each platform.
5-13
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Database 1
Table1
Table2
Database 2
Table3
Table2
Index1
View1
View1
BLOBs
View2
Index1
Index2
View3
CE0317.0
Notes:
Within the DB2 system, there is a hierarchy of objects. Therefore be aware that:
cl
Each database may have multiple table spaces. Table spaces are used to manage
placement of data on physical storage. Table spaces contain indexes and table data.
Ex
pr
Each table may have multiple views. Views may be associated with more than one base
table.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Granting privileges
GRANT DBADM ON
DATABASE RED
TO SALLY
WITH GRANT OPTION;
GRANT USE OF
TABLESPACE A
TO BOBBY
System
Admin
CE0317.0
Notes:
cl
Once the system-level objects have been created, the system administrator can grant
needed privileges to others (oftentimes the security personnel or the DBAs) Then the
security personnel or DBAs can GRANT privileges on objects to users, for those objects
that those users are authorized to access.
Ex
pr
The WITH GRANT OPTION is a very powerful capability, but use with caution. In the first
GRANT statement above, the user SALLY has DBADM (database administration) authority
on database RED. She can also then GRANT that same authority to another user, for
example SAMMY. If SALLY includes the WITH GRANT OPTION in her GRANT statement,
then SAMMY can likewise pass the authority to another user, etc.
5-15
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Performance Monitor
Trace Facility
Statistics
Locking Activity
etc...
System
Admin
CE0317.0
Notes:
Ex
cl
pr
Are the thresholds for those bufferpools set for good performance?
How efficient is the DB2 logging process?
How many threads are concurrently running?
V5.4
Student Notebook
Uempty
Checkpoint
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
cl
2.
Ex
3.
pr
4.
5-17
Student Notebook
Unit summary
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
pr
Ex
cl
Notes:
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
Checkpoint questions
6-1
Student Notebook
Unit objectives
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
After completing this unit, you should be able to list some of the tasks performed by a DB2
Database Administrator:
cl
Ex
pr
6-2
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Grant privileges:
select, insert,
update, delete,
and so on
Create objects:
tables, indexes,
views, and so on
DBA
Maintenance:
load data
gather statistics
copy data
reorganize data,
and so on
Bind:
packages,
plans
CE0317.0
Notes:
pr
Ex
cl
A DBA normally works within the scope of a database or a few databases. Instead of
having responsibilities related to the overall DB2 system, the DBA performs the necessary
functions that pertain to a specific set of applications, or are related to a certain part of the
business or organization.
6-3
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
End
User
DBA
System
Admin
Programmer
Meetings!
CE0317.0
Notes:
cl
DBAs meet with data owners, data users, and with people having system design skills,
database design skills, and application design skills. There may be many types of
meetings (data modeling, database design, application requirements, application design,
etc.) that the DBA should attend, in conjunction with those knowledgeable about the
business processes.
pr
Ex
During the course of these meetings, the resultant requirements documents and database
models provide the DBA with the knowledge of what tables and other DB2 objects will be
needed in order to meet the requirements.
6-4
V5.4
Student Notebook
Uempty
CHAR(6)
CHAR(2)
CHAR(6)
SMALLINT
INT
CHAR(1)
DECIMAL(9,2)
DATE
TIMESTAMP
NOT NULL
NOT NULL
NOT NULL
,
,
,
,
,
,
,
,
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
NOT NULL
WITH DEFAULT
CITY_DESCRIPT
VARCHAR(300)
PRIMARY KEY (CITY_ID)
FOREIGN KEY (COUNTRY_CODE) REFERENCES COUNTRY
,
,
,
)
CE0317.0
Notes:
cl
Tables are created via a CREATE statement. However the person creating the tables
might not need to physically type the CREATE statement and its syntax. There are various
Windows products that will build the CREATE statement for you as you fill out information
in panels via a prompting dialog.
Ex
pr
NULL characteristic
6-5
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CHAR(30)
SMALLINT
DEFAULT
DEFAULT
'UNKNOWN',
-1
.
Default values may be:
constant
USER
CURRENT SQLID
NULL
CE0317.0
Notes:
Ex
cl
In DB2, we can specify a default value for any column, using the DEFAULT option. As part
of the table's definition, you can tell DB2 that, if someone INSERTS a row into this table
and doesn't provide a value for this column, you want DB2 to plug in this value. On the
visual, we are assigning the value of UNKNOWN to the column COUNTRY, and the value
-1 to the column MILES_TO_LA. Default values can be a constant of the column's data
type, the value of a DB2 special register, or null.
pr
The constant must be data type compatible, i.e. character constants must be in 'single
quotes'. DATE constants must be in 'single quotes' and in one of the acceptable date
formats. Numeric constants are not enclosed by quotes, and may include only the
numbers 0-9 and the period (decimal point).
USER and CURRENT SQLID in the visual above are two of the many special registers
available to DB2 users. These two special registers contain the default (USER) and
modifiable (CURRENT SQLID) value of the users current DB2 authorization ID.
6-6
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
( .
HEMISPHERE CHAR(1)
CHECK
(HEMISPHERE IN ('N','S')) ,
( .
PRIMARY KEY (CITY_ID)
,
CONSTRAINT POP_MILES CHECK
(POPULATION BETWEEN 1 AND 50000000
AND MILES_TO_LA >=-1 AND
MILES_TO_LA <=13500) )
Figure 6-6. Adding business rules to a table definition - (at CREATE TABLE time)
CE0317.0
Notes:
Ex
cl
Business rules can be applied to specific columns or to the table itself. The word
CONSTRAINT in the bottom example tells DB2 that we are supplying a name for the
business rule. The name being supplied is POP_MILES. In the top example (the
hemisphere check), DB2 will generate the constraint name (usually the name of the
column), since we have coded the CHECK keyword, but have not provided the
CONSTRAINT keyword.
pr
When a constraint is simply associated with the values for a single column, it may be
referred to as a column constraint. If the constraint references more than one column, it
may referred to as a table constraint.
6-7
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Figure 6-7. Adding business rules to an existing table - (with the ALTER TABLE statement)
CE0317.0
Notes:
pr
Ex
cl
It may be very likely that long after a table has been defined in DB2, there are new
requirements for maintaining business rules in the data in that table. With an ALTER
statement, constraints can be added to a table after its creation, thus enforcing the new
rules. If there is already data in the table, DB2 must be sure that rows already in the table
conform to the business rules. If it does not, then the table will be left in CHECK PENDING
state.
6-8
V5.4
Student Notebook
Uempty
Address
Phone
I need to call Larry Jones... now,
2253 DAHLIA
555-6098
where can I find his phone
6094 BEAKER
555-0945
number!
45 RIVER WALK
555-8495
214 CISCO LANE
555-4297
956 BILLINGS
555-4311
504 1ST AVE.
555-1778
567 YALE AVE.
555-6932
9023 COLORADO BLVD. 555-3242
SOUTH STAR ROUTE
555-2345
504 1ST AVE.
555-1777
1426 MAIN
555-1423
240 BOSTON BLVD.
555-5390
983 FAIRFAX
555-8764
122 42ND ST
555-5367
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Last
Name
SMITH
GAINES
BROWN
ADAMS
FRIDRICH
MITCHELL
JONES
WISE
JONES
MITCHELL
ADAMS
JONES
SMITH
CALDWELL
CE0317.0
Notes:
pr
Ex
cl
With no indexes to aid in the search for the information you need, every row in the table
must potentially be read. Keep in mind that row order is not guaranteed as rows in DB2
tables are inserted, updated and deleted. When returning a result set to the user, the
SELECT statement must specify the ORDER BY clause in order to ensure that the rows
are presented in any particular sequence.
6-9
Student Notebook
An index is:
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
cl
An index is a DB2 object which consists of an ordered set of values, and pointers to the
table rows from which those index values were extracted. DB2 uses indexes for four basic
reasons:
To optimize access to data (faster response, fewer resources consumed)
Ex
pr
V5.4
Student Notebook
Uempty
oy si
v
ec o
to fo
C rm
.F a
.T ci
.I. n
C
.
Page n
Page 5
Row 2
Row 1
Row 3
Row 4
Hdr
Page 5
Page 4
Page 3
Page 2
Page 1
CE0317.0
Notes:
cl
DB2 navigates to specific rows in a table by means of a RID (Record IDentifier). Each
index value is associated with a RID (or RIDs for non-unique indexes), which has two
components a page number, followed by a row number.
Ex
In order to illustrate how DB2 retrieves a particular row from an index, please refer to the
visual. DB2 does the following:
Read the RID value, in the format page, row (in this case the RID value is 5,4)
Retrieve page 5 of the table (which contains some header and trailer information, as
indicated in black)
pr
Locate the 2-byte pointer for row 4, which shows the displacement from the beginning
of the page to the first byte of row 4.
6-11
Student Notebook
What is an index?
ADAMS,BOBBY
Multiple
RIDs
1,11
ADAMS,SANDY
BROWN,TERRY
1,4
1,3
CALDWELL,SIMONE
FRIDRICH,JOHN
GAINES,LOIS
1,14
1,5
1,2
JONES,LARRY
JONES,LAVERNE
1,7 - 1,12
1,9
MITCHELL,JEAN
MITCHELL,TED
SMITH,DIANA
1,10
1,6
1,1
SMITH,GRACE
WISE,SARA
1,13
1,8
Last
Name
First
Name
Phone
SMITH
DIANA
LOIS
TERRY
SANDY
JOHN
TED
LARRY
SARA
LAVERNE
JEAN
BOBBY
LARRY
GRACE
SIMONE
555-6098
555-0945
555-8495
555-4297
555-4311
555-1778
555-6932
555-3242
555-2345
555-1777
555-1423
555-5390
555-8764
555-5367
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
NON-UNIQUE INDEX
GAINES
BROWN
ADAMS
FRIDRICH
MITCHELL
JONES
WISE
JONES
MITCHELL
ADAMS
JONES
SMITH
CALDWELL
CE0317.0
Notes:
pr
Ex
cl
For each unique set of values in an index (unique across the set of columns that comprise
the index), there are one or more pointers (RIDs). The above represents a non-unique
index. Conceptually, a RID is comprised of a page number and a row locator within the
page.
V5.4
Student Notebook
Uempty
Unique index
UNIQUE INDEX
ONE RID
PER UNIQUE
VALUE
First
Name
Phone
1,11
ADAMS,SANDY
1,4
SMITH
BROWN,TERRY
1,3
CALDWELL,SIMONE
1,14
FRIDRICH,JOHN
1,5
GAINES,LOIS
1,2
JONES,LARRY
1,7
JONES,LAVERNE
1,9
MITCHELL,JEAN
1,10
MITCHELL,TED
1,6
SMITH,DIANA
1,1
GAINES
BROWN
ADAMS
FRIDRICH
MITCHELL
JONES
WISE
JONES
MITCHELL
ADAMS
DIANA
LOIS
TERRY
SANDY
JOHN
TED
LARRY
SARA
LAVERNE
JEAN
BOBBY
555-6098
555-0945
555-8495
555-4297
555-4311
555-1778
555-6932
555-3242
555-2345
555-1777
555-1423
SMITH,GRACE
1,13
WISE,SARA
1,8
SMITH
CALDWELL
GRACE
SIMONE
555-8764
555-5367
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
ADAMS,BOBBY
Last
Name
CE0317.0
Notes:
cl
DB2 can use an index to ensure uniqueness of data values. A unique index allows only one
pointer (RID) per index value (no duplicate values). A unique index on LASTNAME,
FIRSTNAME would prevent a row from being added to the table if its LASTNAME,
FIRSTNAME values matched those of an existing row.
pr
Ex
6-13
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Index structure
Non-clustered Indexes
Clustered Indexes
CE0317.0
Notes:
An index in DB2 can be clustered or non-clustered. A table can have only one clustered
index.
pr
Ex
cl
A clustered index is one in which the physical sequence of the rows in the table matches
the sequence of the index entries. An index (by definition) is always ordered. Clustering
has to do with the order of the rows in the table, not the entries in the index.
V5.4
Student Notebook
Uempty
Non-clustered index
Howell, Thurston (1)
Peters, Joan (2)
Zidler, Bob (3)
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Root
Page
L
e
a
f
P
a
g
e
s
D
a
t
a
P
a
g
e
s
Jones, Bob
Adams, Mark
Davis, Mac
Jefferson, Tom
Reeves, Susan
Mitchell, David
Howell, Thurston
Smith, Steve
Peters, Joan
Bennett, Barb
Smith, Stanley
Lee, Spike
Adams, Mark
Alviani, Carl
Lincoln, Abe
Quinn, Tony
Thompson, Tommy
CE0317.0
Notes:
cl
The index is always in sequence, whether the index is clustered or not. For a non-clustered
index, DB2 does not care in which sequence rows are placed into the table. Lets observe
the consequences of retrieving rows from a table using a non-clustered index.
Ex
Lets retrieve all the rows for Mark Adams. For the non-clustered index, here are the I/Os
necessary to accomplish this:
One I/O to read the index root page
One I/O to read the index leaf page containing the key Adams, Mark. There are two
RIDs: 2,1 and 5,2
pr
One I/O to read the 2nd page, where row 1 is Mark Adams
One I/O to read the 5th page, where row 2 is Mark Adams
That is a total of four I/O operations in order to retrieve 2 rows, to satisfy the query:
SELECT FROM PHONEBOOK WHERE LASTNAME = Adams and FIRSTNAME =
Mark ;
6-15
Student Notebook
Clustered index
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Root
Page
L
e
a
f
P
a
g
e
s
D
a
t
a
P
a
g
e
s
Adams, Mark
Bennett, Barb
Howell, Thurston
Lee, Spike
Adams, Mark
Chapman, Mike
Jefferson, Tom
Lincoln, Abe
Alviani, Carl
Davis, Mac
Jones, Bob
Mitchell, David
Peters, Joan
Smith, Stanley
Zidler, Bob
Quinn, Tony
Smith, Steve
Reeves, Susan
Thompson, Tommy
CE0317.0
Notes:
cl
When data rows are clustered on a specific index, DB2 tries to maintain their sequence as
new rows are added to the table. In this visual the table rows are in order of LASTNAME,
FIRSTNAME.
Lets now observe the advantage of retrieving rows from a table using a clustered index.
Ex
Lets retrieve all the rows for Mark Adams. For the non-clustered index, here are the I/Os
necessary to accomplish this:
One I/O to read the index root page
pr
One I/O to read the index leaf page containing the key Adams, Mark. There are two
RIDs: 1,1 and 1,2
One I/O to read the 1st page, where rows 1 and 2 are both Mark Adams
That is a total of three I/O operations (not four as before) in order to retrieve 2 rows, to
satisfy the query: SELECT FROM PHONEBOOK WHERE LASTNAME = Adams
and FIRSTNAME = Mark ; This advantage would be even more pronounced when
retrieving more qualifying rows.
6-16 DB2 Family Fundamentals
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Clustered Index
Root Page
25 61 88
25 61 88
8 13 25
33 45 61
67 75 88
Non-leaf
Pages
8 13 25
33 45 61
67 75 88
Leaf Pages
data row
data row
data row
data row
data row
data row
data row
data row
data row
data row
data row
data row
data row
data row
data row
data row
data row
data row
data row
data row
data row
data row
data row
data row
data row
data row
data row
data row
data row
data row
data row
data row
CE0317.0
Notes:
pr
Ex
cl
Retrieval of a set of rows based on a clustering index is often faster than retrieval of a set of
rows based on a non-clustered index. There will be no advantage for a random read of one
table row. But for sequential processing, when an application or user is retrieving a range
of rows, the performance improvement using a clustered index can indeed be significant.
6-17
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Creating indexes
DBA
CE0317.0
Notes:
pr
Ex
cl
When the DBA defines an index, he/she must provide a name for the index, specify the
table upon which the index is to be built, and list the column (or columns) whose values will
be used for the creation of the index keys. There are many additional index parameters
that may be specified when an index is created. These parameters are discussed in the
Database Administration Workshop courses offered by IBM Education.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Views
A view is just a
description of a set of
columns and rows with
which we want to work
CE0317.0
Notes:
A view is a way to look at data that exists in one or more tables. It can be conceived of as
a virtual table, and enables user access to a subset of the data in DB2 tables.
pr
Ex
cl
However, a view does not involve copying any data. A view is not a physical object. Other
than the definition of the view in the DB2 catalog, creating a view does not consume any
additional storage space. There is no separate storage object created to contain view data.
6-19
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Creating a view
DBA
CATALOG
BLUEDEP =
CE0317.0
Notes:
cl
When a view is created, its definition is stored in the DB2 catalog. When the view name is
used in an SQL statement, it is like telling DB2 to run my query against the set of data
defined by the view's SELECT statement.
Ex
DB2 lets us treat a view name as if it were a table that contains the data in the columns and
rows that the SELECT defines. The view definition may limit the columns of the table that
are accessible via the SELECT clause. The view definition may limit the rows of the table
that are accessible via the WHERE clause. A view may also limit both the columns and
rows that are accessible.
pr
In the example above, the view named BLUEDEP includes all of the columns of the real
or base table EMPL, because of the SELECT *, but the rows returned in the result set are
limited to those where the DEPT begins with the uppercase letters BLU.
A view can be based on another view. Of course, the top view in the hierarchy must be
referencing a real DB2 table.
V5.4
Student Notebook
Uempty
Using a view
EMPL
SAL
31000
32000
30000
27000
33000
31000
32000
28000
30000
33000
31000
35000
27000
29000
29000
DEPT
BLU
RED
BLU
GRE
GRE
BLU
RED
GRE
RED
GRE
RED
GRE
BLU
RED
BLU
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
JOB
S
M
S
C
S
M
S
C
S
M
S
S
C
S
S
31000
30000
31000
27000
29000
BLU
BLU
BLU
BLU
BLU
JOB
S
S
C
S
SAL
31000
30000
27000
29000
CE0317.0
Notes:
When an SQL statement references a view, DB2 locates the SELECT that defines the view,
and then applies the SQL to the real table.
Ex
cl
In the visual above, we see three sets of data. The leftmost represents the entire contents
of table EMPL. The middle one displays the subset of the table data which is defined by
the view. The rightmost shows the final result set that is returned to the user issuing the
SELECT statement.
pr
In this example, using the view BLUEDEP tells DB2 to apply the SELECT statement to the
set of rows in table EMPL whose DEPT value begins with the letters BLU. There are only
five rows in EMPL where DEPT begins with BLU, as shown in the center of the visual.
The WHERE clause in the query filters out any rows where the job is equal to M. In the
SELECT, only columns JOB and SAL are specified, so the final result is a listing of the JOB
and SAL column values, for the four rows in the view where the JOB column value is not
equal to M.
6-21
Student Notebook
EMPL
SAL
31000
32000
30000
27000
33000
31000
32000
28000
30000
33000
31000
35000
27000
29000
29000
DEPT
BLU
RED
BLU
GRE
GRE
BLU
RED
GRE
RED
GRE
RED
GRE
BLU
RED
BLU
UPDATE BLUEDEP
UPDATE SAL =
SAL + 1000
WHERE JOB <> 'M'
JOB
S
M
S
C
S
M
S
C
S
M
S
S
C
S
S
SAL
32000
32000
31000
27000
33000
31000
32000
28000
30000
33000
31000
35000
28000
29000
30000
DEPT
BLU
RED
BLU
GRE
GRE
BLU
RED
GRE
RED
GRE
RED
GRE
BLU
RED
BLU
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
JOB
S
M
S
C
S
M
S
C
S
M
S
S
C
S
S
CE0317.0
Notes:
cl
In addition to reading DB2 data through a view, a user or application may also change data
by coding an INSERT, UPDATE or DELETE statement that refers to a view name rather
than the underlying table(s).
pr
Ex
In this example, DB2 will update the SAL of only those rows in the EMPL table whose
DEPT value begins with the letters BLU, and where the JOB value is not the letter M. The
four rows affected are the same rows that were SELECTed on the previous slide, since the
UPDATE statements is accessing the BLUEDEP view and has the same WHERE clause.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
3-word
cleanup!
OBJECT_TYPE
DROP
OBJECT_NAME
CE0317.0
Notes:
cl
It is very easy to remove a DB2 object (if you have the privilege to do so). The DROP
statement above is issued in order to delete the definition of the table EMPL from the DB2
catalog. But is this all that happens?
Ex
Earlier in this course we mentioned that there is a hierarchy of objects in DB2. Indexes,
views and other objects can be based on (and therefore dependent upon) DB2 tables.
Therefore, when you DROP an object, there may be other implications to consider.
For example, here is what happens when the DROP TABLE EMPL statement is executed:
1. The target object (EMPL table) is deleted.
pr
3. The security/authorization scheme for both target and dependent objects is deleted.
4. All data in both the target and dependent objects is deleted.
5. Any DB2 programs (Plans, Packages) that reference either the target or dependent
objects are invalidated.
Copyright IBM Corp. 1997, 2011
6-23
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Therefore, you want to be cautious with the use of the DROP statement. There is a
preventative measure that the DBA may choose in order to minimize the danger of
inadvertently deleting DB2 objects in the catalog. When you CREATE a table, after the
column specifications, the DBA can include the WITH RESTRICT ON DROP option. When
this option is in effect, DB2 disallows the DROPping of the table, and even disallows the
DROPping of the tablespace or database containing the protected table. This restriction
option can also be added to an already existing table, by issuing an ALTER TABLE
statement, with an ADD RESTRICT ON DROP clause.
pr
Ex
cl
In order to delete such a table, the DBA would first need to issue the statement ALTER
TABLE EMPL DROP RESTRICT ON DROP. If that statement executed successfully, then
the DBA could issue a DROP TABLE EMPL, and DB2 would delete the table definition and
all dependent objects, as described above.
V5.4
Student Notebook
Uempty
DROP dependencies
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
TABLE
EMPL
INDEX
NDX1
VIEW
BLUEDEP
INDEX
NDX2
INDEX
NDX3
CE0317.0
Notes:
pr
Ex
cl
As mentioned earlier, dropping the table causes DB2 either to drop or make inactive all
objects dependent on the table (for example, indexes or views). If you are certain that you
do indeed wish to delete the table EMPL and all of its dependents, it is advantageous that
DB2 automatically takes care of the cleanup. Attempts to cleanup using manual methods
would be much more error-prone and incomplete.
6-25
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DBA
CE0317.0
Notes:
Ex
cl
The most important function of a RDBMS is to maintain data integrity. But it is also very
important to allow multiple users and applications to access the same data at the same
time (This is known as concurrency). Therefore, DB2 must employ methods or techniques
to serialize access to that data. There are a number of serialization techniques used by
DB2 (locking, CLAIMS and DRAINS, etc.). In this next set of visuals, we will illustrate some
of the basic concepts of locking.
pr
The other consideration is cost. The higher the level of locking, the more system resources
which are needed to support the locking. Each unit of data (whether a row, page, or table)
that is being locked consumes memory. Each lock request (such as acquire a lock, check
a lock status, upgrade a lock, release a lock, etc.) requires CPU cycles.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Read
Only
App
Read
Only
App
Read
Only
App
__________
__________
__________
__________
__________
__________
__________
__________
__________
__________
Applicatio
n
Read
Only
App
Read
Only
App
__________
__________
__________
__________
__________
__________
__________
__________
__________
__________
Application
Read
Only
App
Read
Only
App
Read
Only
App
Applications
Queue Here
For Data Access
Lock
Manager
CE0317.0
Notes:
For the following visuals, some explanation of what is being illustrated is needed:
cl
1. The computer display looking at a book represents an application that wants to READ
DB2 data.
Ex
2. The computer display with glasses and a handbag represents an application that needs
to CHANGE DB2 data.
3. The DB2 table that the applications are accessing consists of four pages, with 5 rows
on each page.
pr
4. The lock manager uses the letters R and C to indicate whether an application is simply
Reading DB2 data, or Changing DB2 data.
5. The wooden structure indicates that initially, DB2 is locking at the table level. In
subsequent visuals, the size or scope of the lock is reduced to the page or row level.
If the DBA tells DB2 to maintain locking at the table level, conceptually DB2's lock enforcer
will look at the first application in the queue to see what it wants to do to the data in the
6-27
Student Notebook
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
table. In the above example, the first application wants to READ the data. The lock
enforcer sets up a sign indicating that the gate to the table is open for reading purposes.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Read
Only
App
Read
Only
App
Read
Only
App
__________
__________
__________
__________
__________
__________
__________
__________
__________
__________
__________
__________
__________
__________
__________
__________
__________
__________
__________
__________
Read
Only
App
Read
Only
App
Applicatio
n
Read
Only
App
Read
Only
App
Application
Applications
Queue Here For
Data Access
Read
Only
App
App. 1
App. 2
App. 3
Lock
Manager
CE0317.0
Notes:
For each consecutive application in the queue that wants to read, DB2 will let it into the
table. DB2 keeps track of which applications it has let into the table.
pr
Ex
cl
In the above example, the first three applications in the queue wanted to read only, so DB2
allows them access to the table. The fourth application wanted to make changes to the
table, so it and the applications behind it in the queue must wait until all the readers
finish their work and leave the table.
6-29
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Read
Only
App
__________
__________
__________
__________
__________
__________
__________
__________
__________
__________
Read
Only
App
Read
Only
App
Read
Only
App
Applicatio
n
__________
__________
__________
__________
__________
__________
__________
__________
__________
__________
Read
Only
App
Read
Only
App
Read
Only
App
App. 1
App. 2
App. 3
APPS
WORKING
Application
Applications
Queue Here For
Data Access
Lock
Manager
CE0317.0
Notes:
pr
Ex
cl
As each of the reading applications finishes its work and leaves the table, the Lock
Manager crosses it off the blackboard.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Read
Only
App
__________
__________
__________
__________
__________
Read
Only
App
Read
Only
App
__________
__________
__________
__________
__________
Applicatio
n
__________
__________
__________
__________
__________
Read
Only
App
Read
Only
App
Application
Applications
Queue Here For
Data Access
__________
__________
__________
__________
__________
App. 1
App. 2
App. 3
APPS
WORKING
Lock
Manager
CE0317.0
Notes:
pr
Ex
cl
When the last read application leaves, the lock enforcer will change the access sign to the
table. Applications that change data require EXCLUSIVE access to that data. Since the
DBA told DB2 that locking was to be maintained at the table level for this table, only one
lock enforcer was hired. The lock enforcer will let in only one change application at a time.
6-31
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
__________
__________
__________
__________
__________
Read
Only
App
Read
Only
App
Application
__________
__________
__________
__________
__________
Read
Only
App
__________
__________
__________
__________
__________
Application
Read
Only
App
Read
Only
App
Applications
Queue Here For
Data Access
APPS
WORKING
__________
__________
__________
__________
__________
App. 1
App. 2
App. 3
App. 4
Lock
Manager
CE0317.0
Notes:
pr
Ex
cl
Application 4 is making its changes while all other applications remain in the queue. The
lock enforcer indicates this with a C on his sign. Only one change application is allowed
into the table at a time.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Read
Only
App
__________
__________
__________
__________
__________
__________
__________
__________
__________
__________
__________
__________
__________
__________
__________
__________
__________
__________
__________
__________
Read
Only
App
Read
Only
App
Read
Only
App
Read
Only
App
Application
Applications
Queue Here For
Data Access
APPS
WORKING
App. 1 App. 6
App. 2
App. 3
App. 4
App. 5
Lock
Manager
CE0317.0
Notes:
cl
Once the change application has completed its work, the lock enforcer checks what the
next application in the queue wants to do. Since it wants to read, as does the next
application in the queue, both applications are let into the table, and the sign reverts to R.
pr
Ex
Application 7 wants to make changes to the data (requiring EXCLUSIVE access), so it and
subsequent applications remain in the queue.
6-33
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
__________
__________
__________
__________
__________
Change
Mode
Read
Only
__________
App
__________
__________
__________
__________
Application
Read
Only
App
App. 4
Read
Mode
This
application
wants
exclusive use
of the table
Lock
Manager
App.
1
App.
2
Application
Lock
Manager
Read
Only
App
Read
Only
__________
App
__________
Read
__________
Only
__________
App
__________
__________
__________
Read
Only
App
__________
__________
__________
Read
Only
App
Read
Mode
Read
Only
App
Read
Mode
App. 5
App. 6
App. 3
Lock
Manager
Lock
Manager
Applications
Queue Here For
Data Access
Application
R/C
App. 1 - R
App. 2 - R
App. 3 - R
App. 4 - C
App. 5 - R
App. 6 - R
App. 7 - C
App. 8 - R
App. 9 - R
App. 10 - R
Lock
Manager
CE0317.0
Notes:
cl
Suppose that, instead of choosing TABLE locking, the DBA chose page locking. Page
locking is like hiring a lock enforcer for the table, along with a lock enforcer for each page.
Again, all applications queue up for access. The lock enforcer for the table sets the sign for
both reading and change activity to occur concurrently within the table, so long as all
applications are willing to work together.
pr
Ex
In this example, the first 10 applications in the queue are willing to work concurrently. The
11th application is demanding EXCLUSIVE access to the table. The table lock enforcer has
documented which applications are working within the table. Inside the table, each page
has its own lock enforcer that is ensuring concurrency at the page level.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
App. 4
Application
App. 4
App. 4
Read
Only
App
Read
Only
App
App. 4
Read
Only
App
Read
Only
App
App. 4
Application
App. 4
Read
Only
App
This
application
wants
exclusive use
of the table
App. 4
Read
Only
App
App. 4
Read
Only
App
App. 4
Read
Only
App
Application
Applications
Queue Here For
Data Access
App. 1 - R
App. 2 - R
App. 3 - R
App. 4 - C
App. 5 - R
App. 6 - R
R/C
App. 7 - C
App. 8 - R
App. 9 - R
App. 10 - R
Lock
Manager
CE0317.0
Notes:
cl
This example assumes that the DBA chose row level locking for this table. When row level
locking is in place, it is like hiring a lock enforcer for the table and one lock enforcer for
each row.
pr
Ex
The example shows that there is one application waiting outside the table because it
demands EXCLUSIVE access to the table; however, within the table, all applications are
working because, for this example, there is no conflict at the row level. Conflict at the row
level would occur if a change application and any other application wanted access to the
same row at the same time. When this occurs, the lock enforcer gives access to the first
application queued for the row.
6-35
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DBA
CE0317.0
Notes:
cl
There are some differences in the type of locking supported on the various DB2 platforms.
Ex
DB2 for z/OS supports LOCKSIZE ROW, TABLE, or TABLESPACE. The default setting
is PAGE.
DB2 for Linux, UNIX, and Windows supports ROW or TABLE level locking. You define
LOCKSIZE via ALTER TABLE statement. The default setting is ROW.
pr
The DBA should monitor DB2 applications for timeouts and deadlocks (these are two
events that indicate that there is some contention for the shared data). Some minor
amount of contention is normal, but at some point contention may become a major
performance issue. In that case, the DBA and programmer will need to re-evaluate the
locking strategy.
V5.4
Student Notebook
Uempty
Isolation Level
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
pr
Ex
cl
When a DB2 application program is prepared for execution via the BIND process, one of
the BIND parameters that the DBA can set is known as the isolation level. This controls the
duration of read locks during the execution of the program. The BIND option for isolation
sets the default for the program, but an individual SQL statement can override this default
by using the WITH clause (i.e. with RR, with CS, etc.)
6-37
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
No row locks
acquired for
R/O cursors
COMMIT point
RR or RS
UR
CS
CE0317.0
Notes:
Ex
cl
The visual above shows the various isolation levels, from most restrictive on the left, to
least restrictive on the right. More restrictive reduces both the concurrency and data
integrity exposure. The least restrictive increases both the concurrency to the data and the
data integrity exposure. The programmer and DBA need to understand the business
requirements of the applications and database structures in order to select the appropriate
isolation level.
pr
As shown above, with both RR and RS, the row locks are held until the next COMMIT. The
difference between them is that with RR, all of the rows read are locked, whereas with RS,
only the rows that qualify (meeting the WHERE clause criteria) are locked. With CS, the
lock on the row is held only when it is FETCHed. When the cursor fetches the next row, the
previous rows lock is released. With UR (sometimes called dirty read), no read locks are
held.
Although UR enables the highest level of concurrency, you must ensure that the application
can tolerate the reading of uncommitted DB2 data.
Note
6-38 DB2 Family Fundamentals
V5.4
Student Notebook
Starting with DB2 LUW 9.7, the option to use 'currently committed' with CS locking allows
read-only requests to bypass row locking and avoid lock contention with write requests.
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Uempty
6-39
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Setup
or
Move
Maintenance
Recovery
CE0317.0
Notes:
cl
Most DB2 utilities work at the table space (data set or file) level. So if there is more than
one table in a table space, a single execution of a utility will operate against all of those
tables.
pr
Ex
For the purposes of our discussion the utilities are grouped into the above three rather
broad categories.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
z/OS
Unload
Populate
DSNTIAUL
LOAD
(SAMPLE)
DSN1COPY
Populate
IMPORT
LOAD
UNLOAD
EXPORT
CE0317.0
Notes:
Unload programs download the data that is in a DB2 table into a sequential file.
EXPORT (LUW) and DSNTIAUL (z/OS) are based on the SQL SELECT statement.
cl
UNLOAD and DSN1COPY (z/OS) are based more on the underlying DB2 objects and
VSAM data sets.
Ex
Both IMPORT and LOAD upload data from a sequential file of various formats into a DB2
table. They can insert or replace data in tables. The LOAD programs in particular have high
performance and can load millions of rows very quickly.
pr
Note
With DB2 LUW the load utility can use a defined Cursor for input data, which would retrieve
local data or remote data using a nickname.
6-41
Student Notebook
Recovery-related Utilities
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
z/OS
COPY
MERGECOPY
QUIESCE
REPORT
RECOVER
REPAIR
CHECK
COPYTOCOPY
BACKUP
QUIESCE
RESTORE
ROLLFORWARD
CE0317.0
Notes:
cl
Ex
Note
pr
There is a RECOVER DATABASE command in DB2 LUW that combines Restore and
Rollforward processing for simple database level recovery
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Linear Log
Changes
to data
Data
Data not
accessible
Log
Recovered
data
Copy
CE0317.0
Notes:
pr
Ex
cl
In the first scenario (a device is damaged, but data is good), we want to move all of the
data, as it is at the current time, to a different device. The approach to this is to restore the
data, from the latest copy taken, to another device, and then to apply any committed
changes that were made after the most recent copy. These changes are on the DB2 log,
and the process of applying the changes that are on the log is called forward recovery. This
is sometimes called full recovery.
6-43
Student Notebook
Speeding up recovery
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Time
Data
Copy
data
Copy
data
Copy
data
Copy
Copy
Copy
Incr
copy
Incr
copy
Incr
copy
Data not
accessible
Copy
Full
copy
Merge
copy
New
Image
Copy
Log
Recovered
data
Recover
Report
CE0317.0
Notes:
In DB2 for z/OS, you can take either full or incremental copies of a table space. The
MERGECOPY utility allows you to merge either:
cl
pr
Ex
In DB2 for Linux, UNIX, and Windows, full, incremental and delta backups are also
available at a database or tablespace level.
V5.4
Student Notebook
Uempty
Point-In-Time recovery
Time
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Lost
processing
Changes
to data
Data
Logically
damaged
Recovery
point
Log
Recovered
data
Copy
CE0317.0
Notes:
cl
Point-in-time recovery is used when data has been logically damaged (as opposed to the
storage medium being physically damaged), normally by a DB2 application program with
flawed logic. The goal for logically-damaged data is to recover the data to a point-in-time
just prior to where the processing that caused the logical damage occurred.
pr
Ex
Once the data has been restored, there is another issue to deal with . Even though the
flawed program damaged the DB2 data, the business transactions which invoked the
program needed to be processed. Now that the recovery has been done, all of those
transactions have been lost, and must somehow be processed again, as soon as the
program is corrected.
6-45
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Time
Backup
Backup
Backup
Backup
Circular Log
Backup
Log
Backup
Backup
Log
Log
Backup
Log
CE0317.0
Notes:
cl
Version recovery is a DB2 for Linux, UNIX, and Windows option. With version recovery,
implemented recovery is limited to the point-in-time of the last copy of the database.
Logging is used for application rollback processing and crash recovery.
Note
pr
Ex
For DB2 LUW, version recovery could be performed on a database using archive logging
as long as the backup was produced offline. There is an option to rollforward to the END
OF BACKUP which could use a set of logs imbedded in the backup to recover a copy of a
database.
V5.4
Student Notebook
Uempty
Maintenance Utilities
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
z/OS
Reorganize
the data
Gather statistics
REORG
RUNSTATS
Do we need to?
Reorganize
the data
Gather statistics
REORGCHK
REORG
RUNSTATS
CE0317.0
Notes:
cl
Here is a brief description of those DB2 utilities which enable the DBA to keep the data in
good shape. These maintenance utilities keep the data well-organized and statistically
accurate, so that optimum performance is achieved, and DASD is most efficiently utilized.
Ex
REORG frees up space in tables or indexes, eliminates fragmentation, and may also
sequence the data rows based on a clustering index.
pr
REORGCHK (LUW only) is a report that can be run advising whether a REORG is
advisable, based on certain calculations. For the z/OS environment, there is an option on
the REORG utility that can inform the DBA as to whether a REORG is recommended,
based upon the criteria that the DBA passes to the REORG job. This is known as
conditional REORG.
RUNSTATS updates DB2 catalog statistics with current information on tables and indexes
that normal database activity does not update automatically. Afterwards the DB2 optimizer
can take advantage of more up-to-date information (although static SQL may need to be
rebound to do this).
6-47
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
________________
________________
_____|___________
_|_______________
_________|_______
_|____________|__
________________|
___________|_____
________________
_______|_________
___|_____________
_________|_______
__________|______
___|_____________
____________|
____|
Free space
Copyright IBM Corporation 2007
CE0317.0
Notes:
pr
Ex
cl
After the execution of the REORG utility, all rows have been put into their clustering
sequence, and free space has been reestablished in order to accommodate additional
rows being added to the table. The frequency with which a DBA needs to run the REORG
is primarily dependent upon the volatility (i.e. how often does it change?) of the table data.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
RUNSTATS
Table
space
# pages
# rows
# unique values
% free
# overflow rows
cluster ratio
Catalog
CE0317.0
Notes:
pr
Ex
cl
The RUNSTATS utility examines the actual table data and indexes, gathers statistics and
makes calculations, and then writes that information into the DB2 catalog tables. The
statistical information in the catalog provides significant input to the DB2 optimizer, so that
an efficient access path is chosen to access the data.
6-49
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
To get to DB2
Tables - R - Us
DB2
Integrity
&
Recovery
RA
CF
Op or
e
Sy ratin
ste g
m
Continuous
Operation
Logging
Optimizer
Password
Required
Lock
Manager
Security
CE0317.0
Notes:
pr
Ex
cl
It is necessary to get through the operating systems security manager before we can get to
DB2. This authentication processes normally requires a valid ID and current password.
This is often referred to as external security. Once a person has been authorized to use
the system, there is another layer of security, pertaining specifically to accessing DB2
objects and functions. This is referred to as DB2 internal security, and that is the focus of
the next set of visuals.
V5.4
Student Notebook
Uempty
DB2 security
Peter
No way,
Peter!
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
UPDATE EMPL
SET SALARY =
47000
WHERE ID =
'000030'
DB2
Tables - R - Us
Integrity
&
Recovery
CATALOG
Continuous
Operation
Logging
Optimizer
Lock
Manager
Security
CE0317.0
Notes:
Before a user can access a DB2 object, DB2 goes to the catalog and checks to see if the
user has the authority or privilege to do what they are trying to do.
pr
Ex
cl
Lets look at the example above. DB2 has several catalog tables that are involved in
maintaining DB2 security. DB2 objects and users are registered in the catalog, along with
the actions each user may take against an object. In this example, Peter is trying to update
a SALARY in the EMPL table. DB2's security manager goes to the catalog and looks up
Peter's authorizations against the EMPL table. Peter only has the privilege to update the
LASTNAME column in the EMPL table, thus his request is rejected.
6-51
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Everything is protected!
Plans
Utilities
Packages
Tables
Buffer
Pools
Commands
CE0317.0
Notes:
DB2 internal security determines whether or not a particular user or application can:
Define, modify or delete objects in the DB2 catalog
cl
Ex
pr
Objects on DB2 for z/OS can be more granularly controlled than in DB2 for Linux, UNIX,
and Windows.
Note Plans are DB2 for z/OS objects.
6-52 DB2 Family Fundamentals
V5.4
Student Notebook
Uempty
Privileges
IMPLICIT - Owner's privileges
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
pr
Ex
cl
These are the three ways to acquire DB2 privileges. These will be explained in more detail
on the following visuals.
6-53
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
All DML
TABLE
ALTER
DROP
SELECT
INSERT
UPDATE
DELETE
INDEXES
CREATE
DROP
CONSTRAINTS
UTILITIES
CE0317.0
Notes:
cl
The individual who CREATEs an object is referred to as the owner, and the authorization
ID used at the time of the CREATE becomes the qualifier for the table name. The owner is
automatically granted privileges such as:
ALTERing that table, when changes are needed
Ex
pr
DML stands for Data Manipulation Language, and is the subset of SQL statements which
contains SELECT, INSERT, UPDATE, and DELETE.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DBA
CATALOG
CE0317.0
Notes:
Explicit table privileges are given via a GRANT SQL statement. Privileges can be taken
away with the REVOKE SQL statement.
pr
Ex
cl
In the example above, the DBA is giving Peter and Susan the privilege to read all of the
data in the EMPL table. However, they can only update the LASTNAME and FIRSTNAME
columns within the table. When the GRANT statement is executed, DB2 writes those
privileges into the catalog.
6-55
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Revoke
DBA
CATALOG
CE0317.0
Notes:
REVOKE removes privilege(s) from the catalog which had been previously given
(GRANTed) to a DB2 user or group of users.
pr
Ex
cl
In the example above, the REVOKE statement removes both the SELECT and UPDATE
privileges that user PETER had on the EMPL table. Susans privileges on EMPL remain in
tact.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Bind Package
Program A
Statement 27
199710271345050558
SQL - read a row
.
.
.
.
DB2
Tables - R - Us
CATALOG
Integrity
&
Recovery
Continuous
Operation
Logging
Optimizer
Lock
Manager
Security
Catalog
If Sally Binds..
Security Check
If Sally GRANTS...
GRANT EXECUTE ON PACKAGE TEST.A TO RITA
Copyright IBM Corporation 2007
CE0317.0
Notes:
pr
Ex
cl
Rita gets to execute package TEST.A which contains static SQL, because Sally granted
her the execute authority on the package. Sally did not have to GRANT Rita the privilege to
execute the SQL within the package. However, it is only from package TEST.A that Rita
can execute those SQL statements. If she attempts to submit the same SQL statements to
DB2 through another means (CLP in LUW, SPUFI in z/OS, etc.) those statements will fail,
returning an error to Rita explaining that she is not authorized to execute the statements.
6-57
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Alice
Bob
Public
MGRGROUP
Bob
Carol
Ted
Alice
TED
Carol
GRANT SELECT
ON TABLE EMPL
TO MGRGROUP
Copyright IBM Corporation 2007
CE0317.0
Notes:
cl
Everyone with access to DB2 is part of the PUBLIC group. To GRANT a privilege to
everyone, GRANT it to PUBLIC. A privilege can be GRANTed to a group level ID
(MGRGROUP) which will make that privilege available to each member of the group,
without having to grant the privilege to specific users.
Ex
Note
pr
Starting with DB2 LUQ 9.5 database ROLES can be defined to support group type
authorization that is defined in the database rather than outside of the database like OS
Groups.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Administrative authorities
SYSADM
SYSCTRL
DBADM DB1
DBADM DB2
Data
access
Data
access
CE0317.0
Notes:
cl
Note
pr
Ex
Starting with DB2 LUW 9.7 and DB2 for z/OS 10, the new SECADM authority can be the
primary grantor of access and authority to DB2 databases in the system. The DBADM user
can be granted with or without authority to access data and with or without the ability to
perform grants.
6-59
Student Notebook
DEPT
BLU
RED
BLU
GRE
GRE
BLU
RED
GRE
RED
GRE
RED
GRE
BLU
RED
BLU
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
JOB
S
M
S
C
S
M
S
C
S
M
S
S
C
S
S
GRANT SELECT,
UPDATE (SALARY)
ON BLUEDEP
TO CLERK
CE0317.0
Notes:
Views play an important part of the DB2 security scheme for most customers. Views allow
a more granular control of privileges in DB2.
pr
Ex
cl
In the example above, our view BLUEDEP limits the rows that can be accessed. When
combined with the example GRANT, we have limited the column(s) (SALARY) and rows in
which SALARY can be changed (those rows whose DEPT value begins with the characters
BLU).
V5.4
Student Notebook
Uempty
Checkpoint
Who is responsible for creating DB2 databases?
What is a DB2 index?
List a couple of reasons why indexes are used in DB2.
What is a view?
To recover data that is no longer accessible, the DBA must
start with what?
Which DB2 utility gathers information about our tables and
indexes?
What must be true if USER1 can successfully update the
ADDRESS column in table USER7.TAB_1?
List some of the things a table's owner can do to/with their
table.
Name a couple of ways in which you can get privileges
indirectly.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
1.
2.
3.
4.
5.
6.
7.
8.
9.
CE0317.0
Notes:
cl
2.
Ex
3.
4.
5.
pr
6.
7.
8.
9.
6-61
Student Notebook
Unit summary
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
pr
Ex
cl
Notes:
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
pr
Ex
cl
Checkpoin questions.
7-1
Student Notebook
Unit objectives
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
pr
Ex
cl
Notes:
7-2
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Local Server
DB2
Tables - R - Us
Tables - R - Us
DB2
Integrity
&
Recovery
Integrity
&
Recovery
Continuous
Operation
Continuous
Operation
Application
Logging
Logging
Optimizer
Optimizer
Lock
Manager
A DRDB (distributed
relational database)
can be a server,
a requester, or both!
Lock
Manager
Security
Security
Remote
Data
Local
Data
CE0317.0
Notes:
cl
An application can access data at the local server. When the application needs data stored
in a remote database manager, the local database manager becomes a requester (client)
to the remote database manager. To the user of the data, it appears that the data is at the
local system. The actual location of the data is transparent to the user.
Ex
Distributing data and providing secure access to that data with adequate performance is
not a trivial task. It requires thorough planning, implementation, monitoring, and
maintenance.
Here are some of the questions that the enterprise should attempt to answer:
pr
How is your Information Technology department structured; your network; your user
community (are LANs already in place?)
How do you perform systems management, data backup, and recovery?
If distributed, will you replicate or actually move data to a single remote site?
7-3
Student Notebook
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Do you want to allow multiple site updates within a single logical unit of work?
7-4
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Applications
Personnel
DBA
LAN
Administrator
Strategist
User
Security?
Management?
Backups?
CE0317.0
Notes:
cl
The strategist must have the global perspective (big picture view) of what needs to be
accomplished. The strategist must have an understanding of all the pieces of the
distributed processing puzzle, and must know how the pieces fit together. People with
many diverse skills must be coordinated by a person with strong leadership skills.
Ex
Policies and the appropriate software must be put into place to ensure the security of:
pr
Network: How will we ensure that an outside access (a phone call from a hotel room) is
legitimate?
Workstations: These are now palmtops and Web-enabled phones; how do we ensure
their security?
Data access: Document who has access to the data.
Auditing: Ensure that access to the data can be tracked.
7-5
Student Notebook
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Coordination across the distributed systems is also important. Backups and recovery must
be coordinated. System date and time across systems must be synchronized.
7-6
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SAVINGS
FUNDS
TRANSFER
CHECKING
CE0317.0
Notes:
Ex
cl
A unit of work (also known as a logical unit of work or unit of recovery) is a sequence of
SQL operations that is handled in an all or nothing way. Either all the operations are
successfully performed, or the sequence as a whole is considered unsuccessful. If all the
operations are successful, changes to data are put into effect. If the operations are
unsuccessful, changes to data are removed (as though the changes were never made). To
put the changes into effect, the RDBMS performs a commit operation; to remove the
changes, it does a rollback operation. In either case, the data remains at a point of
consistency.
pr
The ability to commit or roll back is important in maintaining the integrity of related data. In
the example, the application subtracts from the savings account table, and adds to the
checking account table. All the subtract and add operations either happen (they are
committed), or do not happen (they are rolled back). During a unit of work, updates to data
are seen only by the updating program.
Only committed work can be seen by applications not requesting an uncommitted read.
7-7
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Remote Request
Remote
Unit of Work
Application
SQL
SQL
Distributed
Unit of Work
Application
SQL
SQL
SQL
Boston
Distributed
Request
Application
SQL
SQL
SQL
Chicago
Atlanta
CE0317.0
Notes:
Remote request allows a user or DB2 application to read or update data at one remote
server. Only one SQL statement is allowed in a remote request.
Ex
cl
Remote unit of work lets a user or application program read or update data at one remote
location per unit of work. There may be multiple SQL statements within the unit of work. All
DB2 environments support remote unit of work.
pr
Distributed unit of work lets a user or an application program read or update data at
multiple locations within a single unit of work. There may be many SQL statements within
the unit of work. Distributed unit of work requires two-phase commit (the ability of the
various DB2s to coordinate the committing process). DB2s that connect via distributed
relational database architecture (DRDA) connections support distributed unit of work.
Distributed request is significantly more sophisticated than either remote unit of work or
distributed unit of work. It will let you access data at multiple locations with a single SQL
statement (such as JOIN operations). With distributed request, there may be many SQL
statements within a unit of work, and individual SQL statements can join tables from
multiple locations. This is currently supported by WebSphere Federated Server. This level
7-8
V5.4
Student Notebook
of distributed data requires global optimization: the ability for DB2 to analyze statistics
about all of the tables in all of the locations as well as information about each location's
CPU capacity, network, and other resources, in order to determine the most efficient way to
handle a distributed request.
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Uempty
7-9
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Type of Distributed
Database Request
Number of
Number of Server Servers accessed
SQL statements
Locations
per SQL statement
Remote Request
Special
Requirements
>1
>1
>1
Distributed Request
>1
>1
>1
CE0317.0
Notes:
pr
Ex
cl
Here is another way to look at the various levels of distributed data access, ranging from
the most simple (remote request) to the most complex (distributed request). The four
levels are determined by the combination of the number of SQL statements allowed, the
number of server locations accessed, and the number of server databases accessed within
a single SQL statement.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Information On Demand
Business
Context
Unified Data
& Content
Standards-based
Information On Demand
For example,
XQuery, JCR,
JDBC, Web
Services, ...
Master Data,
Entity Analytics,
Information
Warehouses,
Industry Models
Extracted or
Real-time
CE0317.0
Notes:
cl
Information On Demand is about getting the right information to the right people or
processes at the right time to act on emerging opportunities and competitive threats. It is
about creating business value and reducing risk by integrating, analyzing and optimizing all
types and sources of information throughout its lifecycle to address the following customer
needs:
Ex
pr
At the bottom of this chart, you see repositories typical of the multitude of application silos
many companies have:
Copyright IBM Corp. 1997, 2011
7-11
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
And there's various disparate sources from external suppliers and business partners. Its
all information that needs to be available to any process or person that can benefit from it.
As people build new tools and applications for example, executive dashboards or next
generation procurement applications they do not need to really understand or do not
need to depend upon what sources that information is coming from.
pr
Ex
cl
In fact, information sources change every day and consumption of sources change every
day. Therefore, to make business flexible, and not based on a specific integration project
but a flexible information architecture, we need to deliver information as a service. This
virtualizes access to the information. By separating information from the processes and
applications it is easier and faster to change either.
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
cl
The IBM Information Server is designed to help companies leverage their information
across all its sources. The IBM Information Server delivers all of the functions required to
integrate, enrich and deliver information you can trust for your key business initiatives. The
Information Server allows you to:
Ex
Understand all sources of information within the business, analyzing its usage, quality,
and relationships
Cleanse it to assure its quality and consistency
pr
All of these functions are based on a parallel processing infrastructure that provides
leverage and automation across the platform. The Information Server also provides
connectivity to nearly any data or content source, and the ability to deliver information
through a variety of mechanisms.
7-13
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
IBM Metadata Workbench manages metadata, depicts the relationship between sources
and consumers
WebSphere Information Analyzer profiles source systems and monitors data rules
WebSphere Business Glossary creates/manages/searches metadata definitions
The breadth and flexibility of the platform enable it to address many types of business
problems and meet the requirements of many types of projects. This optimizes the
opportunities for reuse, leading to faster project cycles, better information consistency, and
stronger information governance.
pr
Ex
cl
V5.4
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Ease of Design
and Manage
via Tooling
Data Mining
Extend Value
via Analytics
Simplify
Warehouse
Infrastructure
SQW:
Design and
Deploy
In-Line
Analytics
Cubing
Services
DB2 ESE/DPF/Compression
Unstructured
Analysis
WLM
Workload
Management
Data Warehouse
BCU 1
BCU 2
BCU N
CE0317.0
Notes:
Ex
cl
DB2 Data Warehouse is an integrated platform for warehouse-based analytics that runs on
the DB2 LUW platforms. The Warehouse Tool component is the SQL Warehousing Tool
(SQW) that is designed to move data already in DB2 into data marts. The mining
component is DB2 intelligent Miner and includes a Miningblox component to facilitate
reporting from a mining run via Alphablox. The OLAP component is Cubing Services, which
is designed to be an MDX gateway and an OLAP accelerator for applications accessing a
star schema stored in DB2. Alphablox is used as the in line analytics component.
pr
The SQW, the data mining component, Cubing Services and the new Alphablox blox
builder component all share the same Eclipse-base development environment and all,
except Alphablox, utilize the same common Administration Console, a WebSphere
Application Server application, for administration purposes.
A Rational Data Architect plug-in is used to do physical data modeling.
7-15
Student Notebook
Checkpoint (1 of 2)
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
2. List several of the people (skills) that are involved with the
distribution of data.
3. List some of the security concerns with distributed data.
Copyright IBM Corporation 2007
CE0317.0
Notes:
cl
2.
pr
Ex
3.
V5.4
Student Notebook
Uempty
Checkpoint (2 of 2)
4. Match the following with their definitions.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
pr
Ex
cl
Notes:
7-17
Student Notebook
Unit summary
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
pr
Ex
cl
Notes:
V6.0
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
2. SQL
3. Column name
Data type/length
Null characteristic
4. Table
cl
3. INTEGER
SMALLINT
DECIMAL
REAL
FLOAT
CHAR
VARCHAR
LONG VARCHAR
DATE
TIME
TIMESTAMP
BLOB
CLOB
DBCLOB
User-defined Distinct Types
pr
Ex
AP
8-1
Student Notebook
8. Referential integrity.
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
3. When we wish to retrieve, in addition to data from related rows, data from unmatched
rows in table A, data from unmatched rows in table B, or data from unmatched rows in
both tables.
4. A linear procedure is a stack of QMF commands. Procedures are used to automate
data processing tasks, such as creating the monthly reports by running a single
command (RUN PROCX).
5. SELECT CUSTOMER_NAME, CUSTOMER_ADDRESS, CONTACT_NAME, PHONE_NUMBER,
POSTAL_CODE
FROM MASTER.CUSTOMER
WHERE CITY = 'PARIS' AND
YEAR_TO_DATE_SALES > 2000
ORDER BY POSTAL_CODE
6. A CASE expression.
2. Use the EXPLAIN function, through which DB2 tells us its access strategy for each SQL
statement.
3. It is the vehicle through which DB2 talks to our program (gives it return codes and
messages).
4. So that they can be found by the PRECOMPILER.
cl
5. EXEC SQL
pr
Ex
7. Performs an in-depth syntax check of the SQL (using the DB2 catalog)
Checks to ensure that the owner of the program has the authority to execute the
8-2
V6.0
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
9. A colon
10. An INTO clause
2. An ordered list of values with one or more pointers (called RIDs) to where these values
are found within the table.
3. Improve performance
Uniqueness
Clustering
4. A view is a description of a set of columns and rows with which we want to work. That
description is given a name, and we may treat that name as if it were a table that
actually contained the columns and rows described.
5. A copy of the data taken at a time when we knew that the data was good (primary keys
and foreign keys were in sync).
6. RUNSTATS
7. The owner (USER7) of the table, or an ID with the appropriate authority (SYSADM,
DBADM, and so on) has granted the UPDATE or UPDATE(ADDRESS) privilege to
USER1.
cl
pr
Ex
AP
8-3
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
3. Hardware security
Network security
Workstation security
Data access: who is allowed access to the data
Auditing accesses
Security tools
pr
Ex
cl
4. c, a, b
8-4
V6.0
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
N.B. Course codes are currently being updated. The course listed may be known in your
country by either the course code inside or outside the brackets.
LUW:
CG09(CL31)
Note
Course code changes for the Transition Course for every new version of DB2.
Design:
CF18(CE81)
User:
cl
CF12(CE12)
CF13(CE13)
CF81(CV74)
Ex
AP
Programmer:
pr
z/OS:
CF82(CV72)
CG14(CV14)
B-1
Student Notebook
LUW:
DB2 Programming Workshop for Linux, UNIX and Windows
DB2 Advanced Programming
CF71(CL71)
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CF10(CL10)
CF11(CL11)
Administration:
z/OS:
CF83(CV83)
CF84(CV84)
CF85(CV85)
CG85(CV86)
CF89(CV89)
CF95(CV95)
CF96(CV96)
CF91(CV91)
LUW:
CF10(CL10)
CF41(CL41)
CF46(CF46)
CF49(CL49)
Ex
cl
CF20/CL201
CF21/CL211
CF23/CL231
pr
CF28(CL28)
CF48(CL48)
CF24(CL24)
CF44(CL44)
CF60(CL60)
CF63(CL63)
B-2
V6.0
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DW35
L130
DW14
DW15
DW24
MQ01
CM01
CI01
IMS Fundamentals
CICS Fundamentals
613
757
846
848
cl
DW03
pr
Ex
AP
B-3
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Student Notebook
B-4
V6.0
Student Notebook
Uempty
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Please note that due to the diversity of subjects covered in Appendix C, and the
ever-changing world of Information Management, product details may have been updated
since these notes were published. The material here is therefore provided on as-is basis
and should be checked against the most recent documentation.
C-1
Student Notebook
IBM Informix
TABLE_A
PARTS
"Relational
Database"
is a "Table
Database"
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
COL1
ABC
DEF
GHI
JKL
MNO
COL2
7
4
6
7
0
COL3
1987-11-14
1963-01-01
1946-07-12
1951-12-30
1992-08-12
PROD
BOLT
NUT
SCREW
BOARD
WIRE
QTY
7
4
6
7
0
SUPPLIER
ABC CORP
THE NUT CO
FASTEN INC
ECM CORP
BINDINGS
http://www-306.ibm.com/software/data/informix
Copyright IBM Corporation 2007
CE0317.0
Notes:
pr
Ex
cl
IBM Informix is another relational database system offering similar facilities to DB2. The
various editions available are:
C-2
V6.0
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
cl
pr
Ex
C-3
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
Ex
cl
pr
When SOA implementation is guided by strategic business goals, you ensure the positive
transformation of your business and can realize the chief benefits on an SOA, as follows:
Alignment of IT with the business
Maximal reuse of IT assets
Together, these help assure that investment in expensive IT projects result in lasting value
to the business.
C-4
V6.0
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
cl
pr
Ex
Information as a service is about providing a new level of services that helps add value to
the information. Services that integrate information both data and content, regardless of
location - to provide a unified view. Services that add business context to the raw
information; so transaction data takes the form of a policy, for example. And services that
expose insightful relationships in the information through advanced analysis, so you can
make better decisions.
To achieve this, information as a service must be flexible, therefore standards like XML,
XQuery, JSR170, which is an unstructured content standard, or web services are critical.
Information needs to be delivered in real time, like our federation capabilities allow for
up-to-date information in a call center, or extracted, or to support warehouse or
consolidation activities.
C-5
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Adding value to information takes demand capabilities, above and beyond traditional data
management, for example, Master Data to help organize key information domains like
product or customer data, and provide business context. Or, entity analytics to identify
relationships in data so you really know who is who and can detect AML in Finance or Slip
and Fall fraud in Retail. Or, embedded analytic capabilities so dashboard analysis can be
embedded in a business process and extend analytic insight to a wider audience of
users.and so forth.
So Information as a Service, delivering information more flexibly, is critical to meeting the
goals of any On Demand Business.
pr
Ex
cl
C-6
V6.0
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Human Body:
A System that self-monitors
and self-regulates
A Sympathetic DB2
CE0317.0
Notes:
pr
Ex
cl
C-7
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Focus areas:
Up and running
Expert design
Self-maintaining
Self-healing
Auto-recovering
CE0317.0
Notes:
cl
Just as the human body manages itself without conscious intervention, the objectives of
SMART databases is to make the e-business infrastructure, and in particular, IBM eServer,
storage and software, a self-managing system, one that can "take a lickin' and keep on
tickin' " with the least possible human intervention.
Ex
Some tools that DB2 uses to achieve its SMART objectives are:
Developer Workbench
Health Center
Wizards for creating some DB2 objects, backup, restore, load, etc.
Memory Visualizer, Activity Monitor
Configuration Advisor, Design Advisor
and many multiplatform tools
pr
C-8
V6.0
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
Ex
cl
Web Services is a powerful new programming paradigm that enables the development and
deployment of loosely coupled applications within a company or across industries. Web
services are based on emerging technologies including Simple Object Access Protocol
(SOAP), Web Services Description Language (WSDL), and Universal Description,
Discovery, and Integration (UDDI).
pr
A Web service is created by wrapping an application so that it can be accessed using XML
messages which, in turn, are wrapped to mask the underlying transport protocol. The
service is publicized in a standard-format registry, allowing people and applications to find
and use the service over the Web.
DB2 can be accessed as a Web service provider, and it is usually teamed with IBM
WebSphere family products to provide a complete Web services framework.
C-9
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
For a demonstration of DB2 as a Web service provider, see the IBM Video Central for
e-business tutorial at:
pr
Ex
cl
http://www.ibm.com/developerworks/db2/library/samples/videocentral/videocentral.html
V6.0
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
cl
It has a single Java driver with a common code base for Linux, UNIX, Windows and z/OS.
The functions on all of these platforms are now the same.
pr
Ex
This largely improves DB2 Family compatibility - allowing development across Linux, UNIX,
and Windows, and deployment on z/OS without any changes, eliminating the major cause
of Java porting problems.
C-11
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
SQLJ runtime
Java DRDA
Application
Requester
DB2 for
LUW native
libraries
T2
JCC
T2
T4
DB2
DRDA
DDF
Direct
T4
Tx Driver
type
Figure C-8. IBM DB2 Driver for JDBC and SQLJ Architecture
CE0317.0
Notes:
cl
Whereas before the DB2 Universal Driver, each DB2 client platform came with its own
driver files for JDBC and SQLJ support, the new driver is a single set of Java Archive (.jar)
and .zip files, which can be used on UNIX, Windows, and z/OS. It is a combined driver
providing both JDBC Type 2 and Type 4 connectivity, depending on the URL used in the
getConnection() call (or the underlying data source definition).
Ex
Of course, selecting Type 2 functionality requires that native code has been installed on the
client.
pr
V6.0
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
cl
Ex
pr
Integration Technology is included in the product for linking to Business critical applications
and services such as Messaging Providers including WebSphere MQ and Databases
including DB2.
WebSphere Application Server is the underlying technology platform used by many IBM
WebSphere products, such as WebSphere Portal, WebSphere Process Server and many
more.
C-13
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
Rational Application Developer is the IBM tool for developing application code for the IBM
WebSphere platform.
cl
It allows developers to create, test and deploy Java code, alongside XML Documents,
WebSphere Portal code, SOA Applications, Database schemas and much more.
Ex
Rational Application Developer is based on the universal open source Eclipse Integrated
Development Environments. The Eclipse tool is designed to give a standard Development
Environment for all software development.
pr
It is based around a series of Views, each of which provide specific functionality. Each can
be thought of as a separate program, but within the greater whole of Eclipse.
Plug-ins can be added to Eclipse to give extra functionality, often by providing extra Views.
Eclipse is also the basis of many other IBM Development tools, including WebSphere
Integration Developer, WebSphere Message Broker Toolkit and Rational Software
Architect (which includes Rational Application Developer).
V6.0
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DB2 MQListener
DB2
MQSeries
Networks
DB2 Workload
MQ
MQ UDF
UDF // SPs
SPs
trigger
SP
MQSeries
Networks
Send
Receive
Read
Publish
Subscribe
DB2 Clients
CE0317.0
Notes:
Ex
cl
Data is sent via a message on a queue and can be received either synchronously or
asynchronously from that queue, locally or remotely. Delivery of the message is assured by
WebSphere MQ.
A set of MQSeries functions are provided with DB2 to allow DB2 applications to interact
with asynchronous messaging operations. This means that MQSeries support is available
to applications written in any programming language supported by DB2.
pr
C-15
Student Notebook
Workflow product, or communicate with an existing application package with the optional
MQSeries Integrator product.
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
V6.0
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
Ex
cl
Today, the lines of demarcation have blurred. With the influx of multimedia and graphics,
office documents are becoming so large as to require some of the caching and local
storage techniques used in imaging systems. Scanned images are required to work in
concert with forms and other editable office documents in such a way as to demand the
version capabilities of a document management system. Corporations have both the
historic imaging and document management concerns but they want them in one system.
They require a single system to control all of their documentscradle to grave. Starting
with the creation and on through the editing, publishing, archival and ultimate disposition or
destruction.
pr
The concept of Enterprise Content Management is to bring under the control of one system
a platform independent integrated solution for storing and managing large quantities of
multimedia content as well as the traditional relational database data. This allows database
management facilities such as referential integrity, transactional integrity, robustness and
archival and retrieval to be utilized to manage the full range of data objects within the
modern enterprise.
C-17
Student Notebook
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
In these systems, the number of documents tends to be fewer than in imaging systems.
Each document takes a relatively long time and thought to create. The number of users
using the system can be relatively high, but small at any one time. The value of the
documents is also relatively high, as indicated by the cost of creating and storing them. The
Document Management system must provide value by delivering many ways to categorize
the document holdings and provide tools to easily search for documents of interest. This
implies that the repositories typically have many ways of indexing the data including
possibly full text index of its content.
V6.0
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
cl
pr
Ex
Content Manager provides all of the repository back-end services necessary to address an
Enterprise Content Management system. It offers a scalable architecture that can
cost-effectively address the needs of a small implementation up to one that incorporates
thousands of employees across dozens of locations. It contains a rich set of APIs that
cover the full spectrum of content management functionality needed for an enterprise
solution. All of this architecture is secure and managed through a common system
administration interface that helps reduce operational costs.
DM enables the Content Management system to interface with the author through the
application itself. When it comes to application integration, DM goes well beyond the
ODMA standard and allows the user to have access to any DM command from within their
application. For example, a user creates a document in MS Word. Upon closing or saving
C-19
Student Notebook
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
the document, the content management system can either insist or suggest (based on the
configuration) that the document be stored in the corporate repository. It is not a matter of a
user saving a file to their local PC and then launching a separate application to store it
away. It is integrated: it is not an application, it is infrastructure. This dramatically increases
user acceptance and will serve to shorten any time required to prove usability of the
system.
V6.0
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
DM and CM architecture
CE0317.0
Notes:
cl
pr
Ex
Content Manager provides all of the repository back-end services necessary to address an
Enterprise Content Management system. It offers a scalable architecture that can
cost-effectively address the needs of a small implementation up to one that incorporates
thousands of employees across dozens of locations. It contains a rich set of APIs that
cover the full spectrum of content management functionality needed for an enterprise
solution. All of this architecture is secure and managed through a common system
administration interface that helps reduce operational costs.
C-21
Student Notebook
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
CE0317.0
Notes:
pr
Ex
cl
The Content Manager tier incorporates a DB2 database to manage items. Content
Manager stores documents on a designated server and uses the DB2 database to manage
indexing and access control. Users can navigate to files through folders or alternatively, the
user can search the Content Manager System for an item based on metadata contained in
the DB2 Database.
V6.0
Student Notebook
Uempty
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
IMS
CICS
DB2
CE0317.0
Notes:
pr
Ex
cl
CICS and IMS are both DB (Database) and DC (Data Communication) products. When
dealing with DB2 it is they who are responsible for the entire transaction - the Transaction
Manager. Therefore, when you have an IMS-DB2 or CICS-DB2 application, you will find no
DB2 COMMIT or ROLLBACK statements. The unit of work is coordinated by IMS or CICS,
and it is their SYNCPOINT or ROLLBACK equivalents that will force DB2 to COMMIT or
ROLLBACK at the same time as the transaction manager. WebSphere MQ can also be
used as a Transaction Manager with DB2.
C-23
Student Notebook
DB2B
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Sysplex
Timer
Coupling
Facility
Local
Buffer
Pools
Shared DASD
DB2
CATALOG
DB2
DIRECTORY
DB2A
IRLM
WORK
FILES
Local
Buffer
Tools
BSDS
Sysplex
Query
Parallelism
USER
DATA
DB2B
ACTIVE
LOG
BSDS
IRLM
ACTIVE
LOG
Sysplex
Query
Parallelism
WORK
FILES
CE0317.0
Notes:
Data sharing allows concurrent R/W access to shared DB2 data from multiple DB2
subsystems.
cl
Ex
pr
V6.0
Student Notebook
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
http://www.ibm.com/support/publications/us/library/index.shtml#software
pr
Ex
bibl
X-1
pr
Ex
cl
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
Student Notebook
X-2
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
backpg
pr
cl
Ex
V6.0
Back page
pr
u
oy si
ec vo
to fo
C rm
.F a
.T ci
.I. n
C
.
cl
Ex
CONTACTO
Telfono
91 761 21 78
Pngase en contacto con nuestro equipo y le
informaremos de cualquier duda o cuestin
que pueda surgirle.
Email
formacion@arrowecs.es
Mndenos un email y le atenderemos
enseguida.
Online
@Arrow_Edu_ES
O bien puede contactarnos a travs de
nuestro perfil en Twitter.
Vistenos
Arrow ECS Education Services
Avenida de Europa 21,
Parque Empresarial La Moraleja
28108 Alcobendas, Madrid
EDUCATION
S