Академический Документы
Профессиональный Документы
Культура Документы
eds
Electronic Data Systems Version 1.0
DB2
Volume I
Course Objectives
After completing this course you should be able to List and describe the major functions, components and data management techniques for DB2 Describe DB2s SQL and its efficient use with 3GL languages like COBOL Use DB2 associated facilities like LOAD, RUNSTATS
1983
: DB2 Version 1
IMS/DC
CICS
TSO
TSO
CAF Application
IMS/DB
DB2
MVS
IMS Databases DB2 Databases
Course Plan
Introduction to Relational Model DB2 Data Management DB2 Data Management DB2 Functions and Components SQL Data Definition Language SQL Data Manipulation Language DB2 Application Programming DB2 Program Preparation DB2 Efficient SQL Coding DB2 Security
Relational Model
An overview
Relational Model
Relational Data Structure Table
Relational Data Integrity Entity Integrity Referential Integrity
Emp Name
2000 3000 1000 2500 Vijay Gupta Sridevi Patel
Age
33 24 29 29
Sex
M M F M
Dept#
101 102 101 103
Join_Date
01/01/85 01/01/85 01/02/96 01/01/88
Properties of Tables
Each row-column entry in table consists of an atomic data element All the data elements in a given column are of the dame data type Each column in a table has a unique name Sequence of rows in the table is not meaningful All the rows in the table are unique.
Project GE ? HP
Foreign Key
Department Dept Name 101 Marketing 102 Personnel 103 Admin 104 Training Location Chennai Mumbai Chennai Chennai
Integrity Constraints
Entity integrity
No column in the primary key can have a NULL value Referential Integrity
Every foreign key value must either match a primary key value of its associated parent table or it must be wholly NULL .
Relational Algebra
Restrict Union Project Intersection Properties They operate on all entire table; they do not operate on individual rows of the table. Join Divide Difference Product
When the relational operator is applied to on or more tables, the operation results in another table
RESTRICT
Emp 2000 3000 4000 Name Project Vijay GE Gupta ? Sridevi HP Join_Date 01/01/85 01/01/96 01/02/95
Join_date >= 1/01/96
Project
Emp# 2000 3000 4000
Emp # 2000 3000 4000
Project GE ? HP
JOIN
a1 b1
a2 b1
a3 b2
JOIN
a1 a2 a3
b1 b2 b3
c1 c2 c3
b1 c1 b2 c2 b3 c3
INNER LEFT OUTER JOIN OUTER
INNER JOIN
Emp# 2000 3000 4000 Name Vijay Gupta Sridevi Dept# 101 102 104 Join on Dept # Emp# 2000 3000 Name Vijay Gupta Dept # Join_Date 101 01/01/85 102 01/01/96 Name Marketing Personnel Location Chennai Mumbai Dept # 101 102 103 Name Marketing Personnel Training Join_Date 01/01/85 01/01/96 01/02/95 Location Chennai Mumbai Chennai
Join on Dept #
Join_Date Name Location 01/01/85 Marketing Chennai 01/01/96 Personnel Mumbai 01/02/95 ? ?
DIVIDE
a a a b c
x y z x y
x
y
Divide
a
UNION
Emp # 2000 3000 Name Patel Gupta Emp # 2000 Name Patel
3000
Gupta
Singh Gupta Hari Guru
UNION
Emp #
Name
2500
3000 5000 6000
Singh
Gupta Hari Guru
INTERSECTION
Project 1 Emp # 2000 3000 Name Patel Gupta INTERSECTION
Project 2
5000
6000
Hari
Guru
DIFFERENCE
Project1 Emp # 2000 Name Patel
3000
Gupta
Project 2
Difference
Emp # 2500
Name Singh
3000
5000 6000
Gupta
Hari Guru
Emp #
Name
2000
Patel
CARTESIAN PRODUCT
a x y
a b
c
PRODUCT
b
b
x
y
c
x y c
x
y
An Introduction
DB2 Tablespaces
Logical Representation
Physical Representation
DB2 Transformation
Pages
SPACE
PAGE
HEADER
FOOTER
Data Records
Simple Tablespaces
Table 1 Table 1 Table 2 Table 2 Table 1 Table 3 Row X Row Y Row A Row B Row Z Row M Table 2 Table 3 Table 2 Table 1 Table 2 Table 3 Row C Row N Row D Row W Row E Row O
Page 1
Page 2
Segmented Tablespaces
Segment 1
Segment 2
Segment 3
Table 1
Table 2
Table 3
Partitioned Tablespaces
Part 1 Part 2 Part 3 Part 4
Indexes
101 102 103 104 1 3 4 2 101 104 102
103
KEY
Position
Indexes : Terminology
Indexing Key Unique Index Primary Index Partitioning Index Composite Key Non-Unique Index Secondary Index Clustering Index
B+ Trees
25
Root Node
Intermediate Nodes 8 17 33 40
138
9 11 17
19 21 25
28 31 33
35 38 40 46 51 55
Clustering Index
25
8 17
33 40
138
Data Pages
9 11 17
19 21 25
28 31 33
35 38 40
46 51 55
8 17
33 40
138
Data Pages
9 11 17
19 21 25
28 31 33
35 38 40
46 51 55
Stogroup
SG1
SG2
Dev 001
Dev 001
Dev 001
SG3
Dev 001
Database
Table Space Table Table
Index
Index
Storage Groups
Bufferpools
4 KB Pages 32 KB Pages
Character
Graphic
Fixed Length
Variable Length
Fixed Length
Variable Length
Datetime
Time
Timestamp
Date
Binary Integer
Decimal
Floating Point
Small
Large
Small
Large
DB2 Catalog
Data about data DB2 is managing Metadata Stored in DB2 Tables Can be accessed in the same way as user data
Transactions
SQL Command SQL Command COMMIT SQL Command SQL Command COMMIT ROLLBACK
End Transaciton
All ChangesUNDONE
Transactions : An example
Account 1 Balance = 2000 Transaction Withdraw 1000 from Account 1 Deposit 1000 from Account 2 Account 2 Balance = 5000
Quantity = 1000
Quantity = 4000
Uncommited Read
User 1 Quantity = 2000 User 2
Quantity = 1000
Quantity = 2000
ROLLBACK
Quantity = 3000
102
77
103
91
104
64
Locking
Lock on data indicates data is being used
Attributes of locks Object : How much data is marked in-use Duration : How long is the lock is needed Mode : How severe is the lock
Lock : Object
PAGE or ROW
Great concurrency More locking overhead TABLE or TABLESPACE
Lock : Duration
Different options for ApplicationPrograms
ACQUIRE/RELEASE
ISOLATION LEVEL
Lock : Mode
Shared - Data is being Read Other users can read the same data
Exclusive - Data is being changed No other users allowed
DEADLOCK
User 1
LOCK HELD Lock Requested
User 2
Lock Held
Table 1
Table 2
Failures
* Hardware Failure
* Program Failure * Natural Calamity
Backing up data
* IMAGE COPY * FULL * INCREMENTAL
* MERGECOPY * FUZZYCOPY
System Log
[Begin transaction T1] [T1, table 1, Page 1, Before Image] [T1, table 1, Page 1, After Image] [T1, table 2, Page 3, Before Image] [T1, table 2, Page 3, After Image] [Begin Transaction T2] [T2, table 3, Page 9, Before Image] [T2, table 3, Page 9, After Image] [End Transaction T2 ] [End Transaction T1 ]
Recovery
* RECOVER Utility Tablespace is the unit of recovery * Point of time recovery * Disaster Recovery
Integrity Enforcement
Table Check Constraint Designates the value that columns of a table can contain Referential Constraint The limiting of a set of foreign key values to a set of primary key values.
Security
Outside Db2 - RACF Within DB2 - Privileges - Administrative Authorities
SQL
Data Definition Language Data Manipulation Language Data Control Language DDL DML DCL
Utilities
LOAD
UNLOAD
REORG RUNSTATS
SQL - DDL
DATA DEFINITION
LANGUAGE
DDL
DML
DCL
DDL - Operations
CREATE - Defines a new object
ALTER X
DROP X X
X X
Tablespace
Table Index Synonym View
X
X X X X
X
X X
X
X X X X
* Specifies percentage of each page available for future inserts * Default is PCTFREE 5
DDL-CREATE TABLE
COLUMN- definition: colname coltype (NOT NULL/ NULL / NOT NULL WITH DEFAULT) Primary-key definition: PRIMARY KEY(colname1,colname2,)
DDL-CREATE TABLE
Other parameters: EDITPROC : edit routine for table
CREATE (UNIQUE) INDEX indexname ON tablename (colname [ASC / DESC], . ) [USING STOGROUP stogroupname PRIQTY qty SECQTY qty ERASE YES/NO] [CLUSTER] [SUBPAGES amount] [BUFFERPOOL BP0/BP1/BP2] [CLOSE YES/NO] [DSETPASS password] [PCTFREE amount] [ FREEPAGE amount]
Index
Views
Alias
Synonym
SQL- DML
DATA MANIPULATION LANGUAGE
DML Overview
DML verbs are: * SELECT - Retrieves data * UPDATE - Changes value(s) * DELETE - Remove row(s) * INSERT - Adds new row(s) will be explained using an example
SHIPMENTS
S# S1 S1 S1 S1 S1 S1 S2 S2 S3 S4 S4 S4 P# P1 P2 P3 P4 P5 P6 P1 P2 P2 P2 P4 P5 QTY 300 200 400 200 100 100 300 400 200 200 300 400
SNAME Arun Rao Tarun Sanjay Vinod PNAME Nut Bolt Screw Screw Carn Cog
CITY Madras Hyderabad Hyderabad Madras Calcutta WEIght 12 17 17 14 12 19 CITY Madras Hyderabad Bombay Madras Hyderabad Madras
P# P1 P2 P3 P4 P5 P6
* SELECT P#, PNAME, COLOR, WEIGHT, CITY FROM PARTS WHERE WEIGHT BETWEEN 16 AND 19 * SELECT P#,PNAME, COLOR, WEIGHT, CITY FROM PARTS WHERE WEIGHT NOT BETWEEN 16 AND 19
SELECT SNAME FROM SUPPLIER WHERE EXISTS ( SELECT * FROM SHIPMENT WHERE S# = SUPPLIER.S# AND P# = P2 )
SELECT P# FROM PARTS WHERE WEIGHT > 16 UNION SELECT.P# FROM SHIPMENT WHERE S# = S2
Time duration:
Timestamp duration:
hhmmss
yyyymmddhhmmssnnnnn
CHAR
DAYS
Referential constraint from SHIPMENTS.S# to SUPPLIERS.S# INSERT INTO SHIPMENTS.S#, P#, QTY) VALUES (S20,.)
* S20 should exist in SUPPLIERS.S#
UPDATE SHIPMENTS SET S# =S20 WHERE SUPPLIERS.S# should have an entry S20
UPDATE SUPPLIERS SET S# = S20 WHERE S# = S1