Sie sind auf Seite 1von 51

DB2 Concepts

Unit Objectives
After completing this unit, you should be able to: Identify DB2 family products Explain DB2 workstation component functions Identify DB2 objects Discuss the concepts of relational tables and DB2's implementation of referential integrity Define a unit of recovery and detail the basic locking strategies used by DB2 Identify the key differences between static SQL and other application alternatives for accessing DB2 data Identify DB2's role in X/OPEN

DB2 Family
UNIX Systems Intel Systems

DB2

DB2

DB2 for OS/390 DB2 for OS/400


AS/400

S/390, S/370

DB2 for VSE and VM

DB2 Product Components


DRDA Application Requester Administration Client Run-Time Client Application Development Client

Communication Support DB2 DB2 Relational Connect (EE or EEE) DRDA Application Server Oracle Database DB2 Connect

Connecting to DB2 UDB Database Servers


DB2 Personal Developer's Edition (PDE) includes: DB2 Personal Edition DB2 Connect Personal Edition
Application Development Client

Run-Time Client includes:

Workgroup Edition

ODBC support

Command Line Processor (CLP)

Enterprise Edition

Administration Client

JDBC support

EnterpriseExtended Edition

Extenders Netscape Communicator DB2 Product Information

Accessing DB2
Command Line Processor
Commands Interactive SQL

Administration Tools
Control Center Command Center Alert Center Script Center Event Analyzer Journal Tools Settings

Applications
APIs Embedded SQL Call Level Interface JAVA

SQL/API Database Engine

End-User Access
END USER Application Analyst

Application Programmer

(I/C Consultant)

Query Tool

DBA

Systems Programmer

DATA BASE

What's in an Instance?
DB2 Product

INSTANCE_1
CATALOG

INSTANCE_2
DB CONFIG FILE_1
JOIN (federated)

CATALOG

DB_1
LOG

DB_3
LOG

DB CONFIG FILE_3

CATALOG

DB_2
LOG

DB CONFIG FILE_2

CATALOG

DB_4
LOG

DB CONFIG FILE_4

DBM CONFIG FILE_1

DBM CONFIG FILE_2

Connect to Database

Application Requestor

Application Server

CONNECT TO dbname USER userid USING password

database

DB2 Object Hierarchy


Instance 1
dbm configuration file

Catalog Database 1 Log DB configuration file

Catalog

Database 2

Log DB config. file

Table1 Index1

Table2

Table3

Table2 View1

View1 BLOBs View2


View3

Index1 Index2

VIEW
BASE TABLE
AS IT EXISTS ON STORAGE

VIEW
AS THE PROGRAM's LOGIC SEES IT

Application Alternatives
Static SQL Dynamic SQL Perl Call Level Interface ODBC OLE DB Java Development JDBC and SQLJ Stored Procedures User-Defined Functions Application Programming Interfaces (APIs) Distributed Transaction Processing

Static Embedded SQL

Source code with SQL

Table/Column Descriptions

PREPARE

Authorizations Statistics Package

Package:

Contains SQL statements optimized according to database statistics

Static SQL
Vacation Travel Booking Vacation Code: Origination: Travel Date:

Name: Charge Card: Telephone:

Exp. Date:

SQL statements prepared during application development

Dynamic SQL
Vacation Travel Unlimited Type: Destination: Country: Language: Activities: Cooking, Laundry, Household: Number Traveling: Mode of Transportation: Cost: Date: State:

SQL statements prepared during application execution

Programming in Perl
Perl is an interpreted language All SQL is processed dynamically DBD::DB2 driver available from http://www.ibm.com/db2/perl Perl Database Interface (DBI) Module available from http://www.perl.com

CLI/ODBC
CLI/ODBC .
SQLPrepare(...);

versus

Dynamic
EXEC SQL PREPARE ... EXEC SQL EXECUTE ...

...
SQLExecute(...);

SQLFetch(...);

SQL statements EXEC SQL FETCH ... prepared during application execution

..

...

Prepare

Prepare

Execute

DB2 or Other RDBMS

Execute

DB2

Heterogeneous Data Access - OLE DB


OLE DB Consumer and Provider Support (Windows Only) Ability to use any OLE DB data source as a Read Only DB2 table Ability for OLE DB-based applications to extract or query DB2 data using the native OLE interface

DB2 client

parameters

DB2 client

DB2 OLE DB consumer

OLEDB

DB2 OLE DB provider


Source Data

Virtual Table

Java Applications
SQLJ Application

SQLJ Run-Time Classes

Remote Database Java Application

JDBC

DB2 Client

DB2 Java Applets


Web Browser Web Server Host
HTTPd SQLJ Applet SQLJ Run-Time Classes
HTTP

JDBC Server CLI

Local DB2 Database

Java/ JDBC Applet

JDBC Client

TCP/IP Socket

Remote DB2 Database

Stored Procedures
Client Application Run-Time Client

DB2 Server

DB2 Database

Network
Client Application Run-Time Client Stored Procedure

DB2 Server

DB2 Database

Stored Procedure Builder


Development of DB2 stored procedures made simple Supports development and debugging of Java and SQL PL stored procedures on all DB2 UDB Server platforms Supports static (SQLJ) and dynamic (JDBC) SQL GUI development environment Users can focus on the logic of their SPs Wizards simplify many tasks Tool automates build process Can be launched from MS Visual Studio and MS Visual Basic; Integrated with VisualAge for Java Benefits: Ease of development Portability across DB2 family

Stored Procedure Builder Example

Follow steps in Stored Procedure Builder Wizard to construct a new Java SP

User-Defined Functions SELECT EURO(SALARY) FROM EMP

DB2 Server

DB2 Database Functions EURO Convert ! EURO

EURO Conversion Function

Database Manager APIs


API Start Database Manager Create Database Backup Database Bind SQL Select Insert Update ... ...

X/Open XA Support for Distributed Transaction Processing

Application Program (AP)


(1) (2)

Resource Managers (RMs)

Transaction Manager (TM)

(3)

Referential Integrity
Some Basic Terms
PRIMARY KEY

Parent Table

DEPTNO ... C01 E11

DEPTNAME ........ INFORMATION CENTER OPERATIONS ........

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

PARENT KEY PRIMARY KEY

DEPARTMENT table
FOREIGN KEY UNIQUE CONSTRAINT
TAX_ID 456-78-9101 101-98-7654 489-22-8472 214-32-8045 750-18-1912 ...

Dependent Table

EMPNO 000030 000090 000130 000290 000300 ......

FIRSTNM SALLY EILEEN DOLORES JOHN PHILIP ......

LASTNM KWAN HENDERSON QUINTANA PARKER SMITH ......

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

DEPT C01 E11 C01 E11 E11 ...

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

EMPLOYEE table

Referential Integrity (Cont)


By definition, a FOREIGN KEY in one table matches a PARENT KEY A PARENT KEY value must be UNIQUE, and cannot be NULL A FOREIGN KEY value must match a PARENT KEY or be NULL DB2 enforcement of referential constraints: INSERT/UPDATE/LOAD of FOREIGN KEY Must match PARENT KEY or be NULL UPDATE of PARENT KEY Not allowed if any FOREIGN KEYS match original value

Referential Integrity (Cont)


DELETEing a row When DELETEing a parent key, what should be done to matching foreign keys? DELETE rows with matching values (CASCADE) Change matching values to null (SET NULL) Disallow the DELETE if matching values exist (RESTRICT / NO ACTION)

Referential Structure
TABLES LINKED BY REFERENTIAL CONSTRAINTS
RESTAURANTS .NAME

RATINGS .PLACE

PALS .FAVOR

DISCOUNT_ TICKETS .NAME

DELETE Rules Exercise


ROWS 1 THROUGH 15 CONTAIN THE FOLLOWING RESTAURANT NAMES:
RESTAURANTS .NAME 1 2 3 4 ALDO V&T HORN 4SEAS 5 6 7 8 9 RATINGS .PLACE ALDO V&T ALDO 4SEAS V&T 10 11 12 13 14 PALS .FAVOR V&T V&T ALDO 4SEAS 15 DISCOUNT_TICKETS .NAME ALDO

PARENT

WHAT IS THE EFFECT OF THE REQUEST TO DELETE:


RESTAURANTS RATINGS PALS DISCOUNT TICKETS

ROW ROW

3 4

ROW 12 ROW 1

SQL DDL

D ATA DEFINITION LANGUAGE


CREATE ALTER DROP

COL1 COL2 COL3 NEW

SQL DML

DATA M ANIPULATION L ANGUAGE


EMPNO SALARY NAME ANDREW SMITH JONE CARTER FREED ANGELO

SELECT UPDATE INSERT DELETE

1137 3624 8873 8874 9213 9317

25000 10000 13012 15761 45127 17129

SQL DCL

DATA CONTROL LANGUAGE


REVOKE GRANT CONNECT COMMIT ROLLBACK

Locking Strategies
Table Table

OR

Row(s)

Table Space DB2 employs either strict Table Locking or Table Locking in conjunction with Row Locking for typical Application Processing

Table Lock Modes


IN IS IX SIX S U X Z Intent None Intent Share Intent eXclusive Share with Intent eXclusive Share Update eXclusive superexclusive

Row Locking also used (See next page)

Strict Table Locking

Row Lock Modes


Row Lock S U X W NS NX NW Share Update eXclusive Weak exclusive Next key Share Next key eXclusive Next key Weak exclusive Minimum* Supporting Table Lock IS IX IX IX IS IX IX

An application does not acquire Row locks If it is using Table Locks of S, U, X, or Z

Table Lock versus Row Locking

Lock T able
SE LE C T

Update

UP DA TE
WAIT

Lock Mode Compatibility


MODE OF LOCK A MODE OF LOCK B
IN YES IN YES IS YES S IX SIX U X Z YES YES YES YES NO YES YES YES NO NO NO NO YES NO NO YES NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO NO YES YES NO NO YES NO NO YES YES YES YES YES NO NO IS YES S YES IX YES SIX YES U YES X YES Z NO

Table Locks

LOCK A MODE S U

MODE OF LOCK B S YES YES NO NO YES NO NO U YES NO NO NO YES NO NO X NO NO NO NO NO NO NO W NO NO NO NO NO NO YES NS YES YES NO NO YES YES YES NX NO NO NO NO YES NO NO NW NO NO NO YES YES NO NO

Row Locks

X W NS NX NW

Some Scenarios
BR BATH B'FAST

GARAGE

FAMILY

MASTER BR

KITCHEN LIVING DINING

BATH BATH BR

STOPLETT CONSTRUCTION

CLOSET

Isolation Levels
ID NAME QTY

DISP

10

KEYB

MOUSE

15

CABLE

18

F E T C H

NO ROW LOCKS
3

FOR READ-ONLY
4

CURSORS
5

CPU

1
6

SOUND

COMMIT POINT

Application Bound

RS or RR

CS

UR

X-ROW LOCKS ALWAYS HELD UNTIL COMMIT

Unit of Recovery
SAVINGS POINT OF CONSISTENCY OLD DATA CHECKING NEW POINT OF CONSISTENCY UPDATED DATA COMMIT

BANK TRANSFER DATA UPDATES UNIT OF RECOVERY

POINT OF CONSISTENCY OLD DATA

TERMINATION

NEW POINT OF CONSISTENCY OLD DATA

DATA UPDATES

BACK OUT UPDATES

ABORT

END ABORT

RECOVERY IS TO LAST POINT OF CONSISTENCY

Commit and Rollback


Operates on a "Unit of Recovery" COMMIT Changes will be made Row Locks released Certain Table Locks released ROLLBACK Changes backed out All Locks released

Commit versus Rollback


COMMIT
UNIX or OS/2 Normal prog. term. EXEC SQL COMMIT

ROLLBACK
Prog. abend EXEC SQL ROLLBACK Program termination (normal or abend) EXEC SQL ROLLBACK Trans. abend SYNCPOINT ROLLBACK cmd ABEND cmd

Windows EXEC SQL COMMIT

CICS

Normal trans. term. SYNC POINT cmd.

Data Recovery

Backup Updated Database (failure) "COMMIT" "UPDATE" "UPDATE"

Database

LOG

Data Recovery (Cont)


UTILITY
Backup

Restore

Recovered Database

"UPDATE"

LOG

CLP Syntax

db2 option-flag db2-command sql-statement ? phrase message sql-state class-code

Starting a CLP Session

non-interactive mode
db2 connect to eddb db2 "select * from syscat.tables" more (double quotes may be required)
_ _

interactive mode
db2 db2=> connect to eddb db2=> select * from syscat.tables

Command Center

QUIT versus TERMINATE versus CONNECT RESET


CLP COMMAND Terminate CLP Back-end Process
Disconnect database Connection

quit

No Yes No

No Yes
Yes if CONNECT=1 (RUOW)

terminate

connect reset

Unit Summary
Since completing this unit, you should be able to: Identify DB2 family products Explain DB2 workstation component functions Identify DB2 objects Discuss the concepts of relational tables and DB2's implementation of referential integrity Define a unit of recovery and detail the basic locking strategies used by DB2 Identify the key differences between static SQL and other application alternatives for accessing DB2 data Identify DB2's role in X/OPEN

Das könnte Ihnen auch gefallen