Вы находитесь на странице: 1из 44

DB2

DB2 Day 7

DB2

DB2
TRIGGERS. PERFORMANCE TUNING.

UTILITIES

DB2

Triggers

A trigger is a set of actions that will be executed when a defined event occurs. The triggering event can be the following SQL statements

INSERT UPDATE DELETE Triggers are defined for a specific table and once
defined, a trigger is automatically active.

A table can have multiple triggers defined on it and

the order of activation is based on the trigger creation timestamp

DB2

Triggers
Trigger information is stored in the catalog
tables SYSIBM.SYSTRIGGERS SYSIBM.SYSPACKAGE

Uses of Triggers

Data Validation Data Integrity

DB2

Trigger Activation
A trigger can be activated in one of the two
ways

An before trigger An after trigger

DB2

Before Trigger
An before trigger
Will activate for each row in the set of affected rows before the triggering SQL

statement executes. Therefore the trigger


body is seeing the new data values prior to its being inserted or updated into the table.

DB2

After Trigger
An after trigger
Will activate for each row in the set of affected rows after the triggering SQL statement has executed successfully. Therefore the trigger body is seeing the table as being in a consistent state. (All transactions have been completed)

DB2

Example
EMP Empno Empname

Dept
Deptno

Deptname
NoofEmp

Deptno
Salary

DB2

Example
We want to check that whenever a new
employee joins the company, his salary should be >=5000 actually happens in the table

It has to be done before the insertion So we write an before trigger

DB2

10

Before Trigger Example


CREATE TRIGGER TRGBIEMP

NO CASCADE BEFORE INSERT


ON EMP REFERENCING NEW AS N FOR EACH ROW MODE DB2SQL WHEN (N.SALARY<5000) SIGNAL SQLSTATE 75001 (SALARY SHOULD BE GREATER THAN OR EQUAL TO 5000);

DB2

Example

11

When we issue the following insert statement

Insert into emp Values(E001,ABCDE,D001,4500) As soon as this statement is given, we get the following error message SQLCODE = -438 ERROR =APPLICATION RAISED ERROR SQLSTATE= 75001 DIAGNOSTIC TEXT: QTY SHOULD BE GREATER THAN OR EQUAL TO 0

DB2

12

Example Contd.
Once the insertion is successful the NoofEmp
in the DEPT table should be increased depending on the Deptno for which the employee is added

So we write an AFTER Trigger

DB2

13

After Insert Trigger


CREATE TRIGGER TRGAIEMP AFTER INSERT ON EMP REFERENCING NEW AS N FOR EACH ROW MODE DB2SQL UPDATE DEPT SET NOOFEMP=NOOFEMP+1 WHERE DEPT.DEPTNO=N.DEPTNO

DB2

14

Example Contd.
Whenever we delete an Employee from the
emp table the NoofEmp in the DEPT table should be decreased depending on the Deptno from which the employee was removed

So we write an AFTER Trigger

DB2

15

After Delete Trigger


CREATE TRIGGER TRGADEMP

AFTER DELETE
ON EMP REFERENCING OLD AS O

FOR EACH ROW MODE DB2SQL


UPDATE DEPT SET NOOFEMP=NOOFEMP-1 WHERE DEPTNONO=O.DEPTNO

DB2

16

DROP Trigger
When you no longer want a trigger we can drop
the trigger
DROP TRIGGER TRGAIORD

DB2

17

TUNING THE SQL QUERIES


program

Performnce Tuning

Select only those columns needed in your application Avoid using select * Use as much as singleton select as possible. Avoid Open the cursors only when needed Close the cursors as soon as possible
using cursors

DB2

18

Performnce Tuning
TUNING THE SQL QUERIES
clause wherever possible

Pick up only those rows required. Give a where



Create indexes on the columns used for searching

Use joins instead of subqueries


Use between instead of >= and <= Use union all instead of union

DB2

EXPLAIN Function
Obtains information on how current and

Access Path(INDEX vs Tablespace scan)
Names of indexes used DB2 sort requirements Sequence of steps for subquery processing Tablespace locking requirements

19

projected SQL statement will be executed

..

DB2

EXPLAIN Function
The explain functions puts the information in
So before invoking the EXPLAIN tool create
the plan_table.
a table called plan_table

20

If someone had already created plan_table

you can create a like table with the following command

CREATE TABLE PLAN_TABLE

LIKE abc.PLAN_TABLE

DB2

21

EXPLAIN Function
Can be applied to:
An individual SQL statement
SELECT, UPDATE, INSERT, DELETE

All explainable statements in a plan (or) package

DB2

EXPLAIN Function

22

Syntax for EXPLAIN is:


EXPLAIN plan SET QUERY NO = n FOR SQL Statement

Invokes the DB2 optimizer Rows get inserted into plan_table

DB2

EXPLAIN Function
If you want to apply Explain for a plan
Then set the bind parameter EXPLAIN to yes
EXPLAIN(YES)

23

DB2

24

DB2 Utilities
Utilities are special programs that can run by
submitting JCL. They run in batch and get their control information via control statements (not through SQLs). Utilities work at the object level and not at row or data items level.

DB2

25

DB2 Utilities
Commonly used Online Utilities

Runstats Reorg Check

Copy
Mergecopy Load

DB2

26

Runstats Scans a table or index space to gather

DB2 Utilities

This information is then stored in the DB2


catalog and used by the SQL optimizer to select access paths to data

information about the utilization of space and the efficiency of indexes

Reorg Reorganizes a table space to improve access

performance and reorganizes indexes so that they are more efficiently clustered.

DB2

Check
tables and consistency

DB2 Utilities

27

Checks the integrity of DB2 data structures

Checks the referential integrity between two


also checks DB2 indexes for

Can delete invalid rows and copies them to a


exception table

Use CHECK DATA when loading a table without

specifying the ENFORCE CONSTRAINTS option or after the partial recovery of tablespaces in a referential set

DB2

28

DB2 Utilities
COPY
Used to create an imagecopy for the complete
tablespace or a partition of the tablespace - full imagecopy or incremental imagecopy

Every successful execution of COPY utility places


in the table SYSIBM.SYSCOPY, atleast one row that indicates the status of the imagecopy

DB2

29

DB2 Utilities
MergeCopy
The
MERGECOPY utility combines multiple incremental image copy data sets into a new full or incremental image copy data set

Load
To accomplish bulk inserts into DB2 table Can replace the current data or append to it, i.e.
LOAD DATA REPLACE or LOAD DATA RESUME(S)

DB2

Normalization
A technique that
allows the designer to detect redundancies in a given table

30

It is used to check
the data and/or design your tables

DB2

31

Process of Normalization
Building blocks
database:
Determinant Functional Full

used

to

design

the

dependency

functional dependency dependency dependency

Partial

Transitive

DB2

32

Determinant
Attribute X can be defined as determinant if it uniquely defines the attribute value Y in a given relationship or entity.
Marks Grade

DB2

33

Full Functional Dependency

In a given relation R,X&Y are attributes. Attribute Y is functionally dependent on attribute X if each value of X determines exactly one value of Y. This is represented as XY
Stud#

Marks
Course#

DB2

Partial Dependency
are attributes. Attribute Y is partially dependent on attribute X

34

In a given relation R,X&Y

only if it is functionally dependent on subset of


attribute X.
Stud#
COURSENAME

INAME

Course#

ROOMNO

DB2

Transitive Dependency
COURSE# INAME ROOM#

35

ROOM# depends on INAME and in turn

INAME depends on Course#.

Hence

ROOM#

transitively

depends

on

COURSE#

DB2

36

Types of normal forms

First Normal form


Second Normal form

Third Normal form


Boyce Codd Normal form

DB2

37

Unnormalized Table
STUDENT_DETAILS
101 102 101 103 104 102 105 DAVIS DANIEL DAVIS SANDRA EVELYN DANIEL SUSAN 04NOV8 6 04NOV8 6 04NOV8 6 12AUG88 22FEB86 04NOV8 6 31AUG86 M4 M4 H6 C3 B3 P3 P3

COURSE_DETAILS DURATION IN DAYS


APPLIED MATHS APPLIED MATHS
AMERICAN HISTORY

RESULT
11 NOV04 11 NOV04 22 NOV04 16 NOV 04 26 NOV 04 12 NOV04 12 NOV04 82 82 79 65 77 68 89 A C B B B B A

BASIC MATHS BASIC MATHS

7 DAYS 7 DAYS 4 DAYS

BIO CHEM BOTONY


NUCLEARP HYSICS NUCLEAR PHYSICS

BASIC CHEM

11 DAYS 8 DAYS

BASIC PHYSICS BASIC PHYSICS

13 DAYS 13 DAYS

DB2

First Normal Form


the attributes of the relation R are atomic in nature.
STUDENT NAME

38

A relation R is said to be in the 1NF if and only if all


STUDEN T 101 DATE OF BIRTH 04NOV8 6 04NOV8 6 04NOV8 6 12AUG8 8 22FEB8 6 04NOV8 6 31AUG8 6 COURSE # M4 COURSE NAME APPLIE D MATHS APPLIE D MATHS
AMERICAN HISTORY

PREREQ

DUR IN IN DAYS 7 DAYS

DATE OF EXAM 11 NOV04 11 NOV04 22 NOV04 16 NOV 04 26 NOV 04 12 NOV04 12 NOV04

MARKS

GRADE

DAVIS

BASIC MATHS BASIC MATHS

82

102

DANIEL

M4

7 DAYS

82

101 103 104 102

DAVIS SANDRA EVELYN DANIEL

H6 C3 B3 P3

4 DAYS BASIC CHEM 11 DAYS 8 DAYS BASIC PHYSIC S BASIC PHYSIC S 13 DAYS

79 65 77 68

B B B B

BIO CHEM BOTON Y


NUCLEAR PHYSICS NUCLEAR PHYSICS

105

SUSAN

P3

13 DAYS

89

DB2

39

Second Normal Form


A relation R is said to be in the 2NF if and only if:

It is in the 1NF No partial dependency exist between non-key


and key attributes.

DB2

Table in 2NF
COURSE # M4 H6 C3 B3 P3 COURSE NAME APPLIED MATHS
AMERICAN HISTORY

40

PREREQ BASIC MATHS

DUR IN IN DAYS 7 DAYS 4 DAYS

BIO CHEM BOTONY


NUCLEARPHYSICS

BASIC CHEM

11 DAYS 8 DAYS

BASIC PHYSICS

13 DAYS

STUD 101 102 101 103 104 102 105

STUDENT NAME

DATE OF BIRTH 04NOV86 04NOV86 04NOV86 12AUG88 22FEB86 04NOV86 31AUG86 STUD 101 102 COURSE # M4 M4 MARKS 82 82 GRADE A C COURSE # M4 H6 C3 B3 DATE OF EXAM 11 NOV04 22 NOV04 16 NOV 04 26 NOV 04

DAVIS DANIEL DAVIS SANDRA EVELYN DANIEL SUSAN

101
103 104

H6
C3 B3

79
65 77

B
B B

P3

12 NOV04

102

P3

68

DB2

41

THIRD NORMAL FORM


A relation R is said to be in the 2NF if and only if: It is in the 2NF No transitive dependency exist between non-key and key attributes.

DB2

42

TABLE IN 3NF
STUD 101 102 101 STUD COURSE # M4 M4 H6 C3 B3 P3 MARKS GRADE 103 104 101 102 101 103 104 102 82 82 79 65 77 68 A C B B B B UPP BOUND 94 84 64 54 LOW BOUND 85 70 55 45 GRADE A B C D 102 COURSE # M4 M4 H6 C3 B3 P3 MARKS 82 82 79 65 77 68

DB2

BOYCE CODD NORMAL FORM (BCNF)

43

A relation is said to be in BCNF if and if only if all the determinants


are candidate keys.

DB2

MERITS & DEMERITS OF NORMALIZATION


MERITS:

44

1.Normalization is based on mathematical foundation.


2.Removes the redundancy to the greater extent. 3.Removes the anomalies present in inserts, updates and

deletes.
DEMERITS:
1.Data retrieval (Select) operation performance will be severely affected.

Вам также может понравиться