Professional Documents
Culture Documents
Agenda
DB2 Architecture
DB2 Utilities
DB2 Triggers
Stored Procedures
User Defined functions
DB2 locks and Concurrency
DB2 ARCHITECTURE
DB2 Architecture
IMS
Batch
Application
IMS
Terminal
CICS
Terminal
TSO
Terminal
IMS/DC
CICS
TSO
TSO
IMS
Online
Application
CICS
Online
Application
TSO
Online
Application
TSO
Batch
Application
CAF
Application
DB2
System
Services
Locking
Services
Databases
services
Distributed data
facilities
MVS
DB2 Architecture
DB2 contains a very large number of Internal components.
These could be grouped according to their functionality into four groups
or major components.
They are :
System Services
Locking Services
Database Services
Distributed Data Facilities
Each of these components runs in a separate operating system
(MVS ) address space.
DB2 maintains two copies of both the Active and Archived Datasets.
Buffer Manager
Buffer Manager
EDM Manager
Buffer Manager
EDM Manager
Application Plan/Package
DB2 Utilities
DB2-DM-1
DB2 Utilities
RUNSTATS
LOAD
CHECK(INDEX/DATA)
REORG
QUIESCE
COPY
REPORT
MERGECOPY
STOSPACE
COPYTOCOPY
RECOVER
MODIFY (RECOVERY /
Statistics)
REPAIR
DIAGNOSE
UNLOAD Utility
UNLOAD Utility
Phase Flow
UTILINT
UNLOAD
UTILTERM
UNLOAD Utility
LOAD Utility
Input:
LOAD DATA statement describing the data to be loaded
File containing data
Output:
Features:
Automatic data conversion between compatible data types
Data loaded in the sequence presented; no sort invoked
Indexes built; duplicates checked
LOAD Utility
UTILINIT
RELOAD
SORT
BUILD
INDEXVAL
ENFORCE
DISCARD
REPORT
LOAD Utility
Example
33
TRG
SSG
REORG utility
Input:
Name of tablespace or partition or index to be reorganized
Output:
Reorganized tablespace and / or indexes
Table data in LOAD format
REORG utility
Phase Flow:
UTILINIT
UNLOAD
RELOAD
SORT
BUILD
UTILTERM
REORG utility
Example :
Reorganizing a table space
REORG TABLESPACE DSN8D81A.DSN8S81D
Reorganizing a table space partition.
REORG TABLESPACE DSN8D81A.DSN8S81E
PART 3
SORTDEVT SYSDA
REORG TABLESPACE DSN8D81A.DSN8S81E
PART 3:5
SORTDEVT SYSDA
SHRLEVEL NONE
COPYDDN SYSCOPY
RUNSTATS Utility
Gathers information about data in tablespaces and indexes and updates DB2
catalog.
a table is loaded
a table is reorganized
an index is created
RUNSTATS Utility
RUNSTATS Utility
Input:
Tablespace for which statistics is to be updated / reported
Output:
Report containing new statistics
Updated DB2 catalog tables
Phase Flow:
UTILINIT
RUNSTATS
UTILTERM
RUNSTATS Utility
Example
Copy Utility
Input:
Name of tablespace or partition to be copied
Output:
A sequential dataset containing the imagecopy
SYSIBM.SYSCOPY updated
Copy pending status reset
Copy Utility
Outputs
Creates upto 4 imagecopies
Rows in SYSIBM.SYSCOPY for catalog and SYSUTILX , SYSLGRNX for
direct.
Either FULL or INCREMENTAL
Can generate 2 sets of ics - local and remote set
Used by RECOVER TABLESPACE
Copy Utility
COPY utility FULL
Example :
COPY TABLESPACE DSN8D81A.DSN8S81E
Copy Utility
Example
CHECK Utility
Checks the integrity of DB2 data structures
Checks the referential integrity between two tables and also checks DB2
indexes for consistency
CHECK Utility
CHECK DATA TABLESPACE DSN8D81A.DSN8S81D
TABLESPACE DSN8D81A.DSN8S81E
FOR EXCEPTION IN DSN8810.DEPT
USE DSN8810.EDEPT
IN DSN8810.EMP
USE DSN8810.EEMP
IN DSN8810.PROJ
USE DSN8810.EPROJ
IN DSN8810.PROJACT USE DSN8810.EPROJACT
IN DSN8810.EMPPROJACT USE DSN8810.EEPA
DELETE YES
REPORT Utility
Example
REPORT RECOVERY TABLESPACE DSN8D81A.DSN8S81E
QUIESCE Utility
Example
QUIESCE TABLESPACE DSN8D81A.DSN8S81D
Mergecopy
The MERGECOPY utility combines multiple incremental image copy data sets
into a new full or incremental image copy data set
Example:
MERGECOPY TABLESPACE DSN8D81P.DSN8S81C
COPYDDN (COPY1,COPY2)
NEWCOPY NO
Recover
Restore DB2 tablespaces and indexes to a specific instance
Data can be recovered for single page, pages that contain I/O errors, a single
partition or an entire tablespace
Indexes are always recovered from the actual table data, not from image copy
and log data, as in the case of tablespace recovery
Standard unit of recovery is a Tablespace
Explain
This feature can be used to obtain the details about the access paths chosen
by the DB2 optimizer for SQL statements.
Explain (contd...)
To EXPLAIN a single SQL statement precede that SQL statement with the
EXPLAIN Command
EXPLAIN ALL SET QUERYNO = integer
FOR SQL statement
The other method is specifying EXPLAIN YES with the Bind command
Then PLAN_TABLE is to be queried to get the required information.
Explain (contd...)
The information provided include the type of access of particular tables used
in the SQL or Package or Plan, the order in which the tables or joined in a
JOIN, whether SORT is required and so on
Since the EXPLAIN results are dependent on the DB catalog, it is better to run
RUNSTATS before running a EXPLAIN
DB2 TRIGGERS
Trigger
Whats Trigger?
Trigger is a defined set of SQL defined for a table that executes when a specified
SQL event occurs.
A trigger defines a set of action and can be activated when a specified SQL
operation
a DELETE, INSERT or UPDATE -- occurs on a table.
Triggers, unlike referential and check constraints, can even be used to update
other tables.
Trigger
Why Triggers ?!
Validate input data
Note that validation of non-transitional data is usually better handled by check and
referential constraints. By contrast, triggers are appropriate for validation of
transitional data, that is, validations which require comparisons between the
value before and after an update operation.
Automatically generate values for newly inserted rows
Read from other tables for cross-referencing purposes
Write to other tables for audit-trail purposes
Support alerts (for example, through e-mail messages)
Trigger
Trigger Name
Triggering Table
Triggered Action
Trigger
Example
Trigger
Trigger Activation time
SQL Statement
Before
After
SELECT
Yes
Yes
VALUES
Yes
Yes
CALL
Yes
Yes
SIGNAL SQLSTATE
Yes
Yes
SET transition-variable
Yes
No
INSERT
No
Yes
UPDATE
No
Yes
DELETE
No
Yes
Trigger
Trigger Limitations:
Cannot define DB2 triggers on:
a system catalog table
PLAN_TABLE
STATEMENT_TABLE
DSN_FUNCTION_TABLE
View
Alias
Synonym
Any table with a three-part name.
Trigger
Trigger Packages
When a trigger is executed, DB2 creates a trigger package for the statements in
the triggered action.
The trigger package is recorded in SYSIBM.SYSPACKAGE and has the same
name as the trigger and package TYPE =T
To delete the trigger package, you must use the DROP TRIGGER statement
What is UDF?
It is procedural functionality added to DB2 by the user. The UDF, once coded and
implemented , users can specify the UDF in SQL statements just like built-in SQL
functions (SUM , AVG ,COUNT etc )
Type of UDF
Based on value return
Scalar functions - applied to a column or expression and operates on a
single value
Table functions - return an entire table
Based on
External UDFS
Sourced UDFS
Type of UDF
Sourced UDFS
It registers a user-defined function that is based on an existing scalar or aggregate
function with a database server.
Example :
CREATE FUNCTION NTEST1 (SMALLINT)
RETURNS SMALLINT
EXTERNAL NAME 'NTESTMOD'
SPECIFIC MINENULL1
LANGUAGE C
DETERMINISTIC
NO SQL
FENCED
PARAMETER STYLE SQL
RETURNS NULL ON NULL INPUT
NO EXTERNAL ACTION;
Sourced Functions
Stored procedure is program that is sored and executed within control of the
DBMS
It can be executed from local as well as from remote server
Type of stored procedure :
SQL stored procedure : It contains only sql statements
External stored procedure: Wriiten in Host loanguage such as cobol, C , java,
assembler etc
Overuse of nesting
2. Link-edit the application. Your stored procedure must either link-edit or load
one of these language interface modules: DSNALI / DSNRLI
5. Use GRANT EXECUTE to authorize the appropriate users to use the stored
procedure
GRANT EXECUTE ON PROCEDURE SPSCHEMA.STORPRCA TO JONES;
DESCRIPTION
DSNUTILS
DSNUTILU
DSNACCQC
DB2 UDB Control Center - table space and index information stored procedure
DSNACCAV
DSNACCOR
WLM_REFRESH
DSNACICS
DSNLEUSR
DSNAIMS
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
Begin Transaction
SQL Command
COMMIT
SQL Command
SQL Command
Error Condition
ROLLBACK
End Transaction
All Changes DONE
Begin Transaction
End Transaction
Transactions : An example
Account 1
Balance = 2000
Account 2
Balance = 5000
Transaction
Withdraw 1000 from Account 1
Deposit 1000 into Account 2
Account 1
Balance = 1000
Account 2
Balance = 6000
Quantity = 2000
Quantity = 1000
Quantity = 4000
User2
Roll#
Marks
101
80
102
77
103
91
104
64
Percentile
Read
All the
rows
twice
Duration
Mode
ISOLATION LEVEL
DEADLOCK
User 1
Lock
Held
Table 1
User 2
Lock
Requested
Lock
Held
Table 2
Failures
Hardware Failure
Program Failure
Natural Calamity
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
Integrity Enforcement
Table Check Constraint
Designates the values 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.
THANK YOU