TechTip: A Word About Multiple-Event Triggers

Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Among the uses are generating events and inserting records in tables.

 

Editor's note: This article is an excerpt from the book DB2 10.1/10.5 for Linux, UNIX, and Windows Database Administration (Exams 611 and 311): Certification Study Guide.

 

In DB2 10.1, you can create a multiple-event trigger to fire off a trigger action based on one of three possible triggering events—an INSERT, an UPDATE, or a DELETE statement—instead of creating a separate trigger for each event.

 

Multiple-event triggers are supported for both row-level triggers (FOR EACH ROW) and statement-level triggers (FOR EACH STATEMENT), which allows you to transform a complex business rule into sophisticated trigger logic. It also helps you greatly during the database migration process to migrate other database vendor products to DB2.

 

Consider an example where the HR department wants an audit log to be set for any insert, update, or delete operation on the EMPLOYEE table for the column SALARY. The code to do so looks something like this:

 

CREATE OR REPLACE TRIGGER employeeSalaryMulti

AFTER INSERT OR DELETE OR UPDATE OF salary ON employee

REFERENCING NEW AS N OLD AS O

FOR EACH ROW

MODE DB2SQL

BEGIN

INSERT INTO SALARY_AUDIT

(EMPNO, OLD_SALARY, NEW_SALARY, RATING) VALUES

(N.EMPID, O.SALARY, N.SALARY, N.RATING);--

END

DB20000I The SQL command completed successfully.

 

The EMPLOYEE and SALARY_AUDIT table data extracts before triggering the event are as follows:

 

SELECT * FROM employee

EMPID EMPNAME EMPPHONE EMPDEPT EMPADDRESS EMPSTARTDATE SALARY RATING

----- ------- ------------ ------- ----------------- ------------ ------ ------

1 MILAN 07584203037 IS VENUS, MANCHESTER 07/21/2014 600000 1

2 MOHAN 0161 6382409 IS VENUS, MANCHESTER 07/21/2014 100000 1

 

2 record(s) selected.

 

SELECT * FROM salary_audit

EMPNO OLD_SALARY NEW_SALARY RATING

----------- ----------- ----------- -----------

1 200000 600000 1

2 100000 300000 1

 

2 record(s) selected.

 

A new employee addition into the EMPLOYEE table, as below, will generate an event and trigger an action to insert a record in the SALARY_AUDIT table:

 

INSERT INTO employee VALUES (3, 'KENT', '8173520251','IS','USA', NULL, 200000, 1)

DB20000I The SQL command completed successfully.

SELECT * FROM salary_audit

EMPNO OLD_SALARY NEW_SALARY RATING

----------- ----------- ----------- -----------

1 200000 600000 1

2 100000 300000 1

3 - 200000 1

 

3 record(s) selected.

 

Likewise, deleting or updating an entry in the EMPLOYEE table will generate an event and insert a record in the SALARY_AUDIT table, something like:

 

DELETE FROM employee WHERE EMPID=2

DB20000I The SQL command completed successfully.

 

SELECT * FROM salary_audit

EMPNO OLD_SALARY NEW_SALARY RATING

----------- ----------- ----------- -----------

1 200000 600000 1

2 100000 300000 1

3 - 200000 1

- 100000 - -

 

4 record(s) selected.

 

UPDATE employee SET SALARY = SALARY * 2 WHERE EMPID=3

DB20000I The SQL command completed successfully.

 

SELECT * FROM salary_audit

EMPNO OLD_SALARY NEW_SALARY RATING

----------- ----------- ----------- -----------

1 200000 600000 1

2 100000 300000 1

3 - 200000 1

- 100000 - -

3 200000 400000 1

 

5 record(s) selected.

 

Learn more with the book DB2 10.1/10.5 for Linux, UNIX, and Windows Database Administration (Exams 611 and 311): Certification Study Guide.

 

Mohankumar Saraswatipura

Mohankumar (Mohan) Saraswatipura works as a database solutions architect at Kronsys, Inc., focusing on IBM DB2, Linux, UNIX, and Windows solutions. Prior to his current position, he worked as a database solutions architect at Reckitt Benckiser Group, plc (UK), focusing on IBM Smart Analytics System 5600, Siebel, SQL Server, and SAP HANA solutions. 

Mohan is an IBM Champion (2010–2015) and a DB2’s Got Talent 2013 winner. He has written dozens of technical papers for IBM developerWorks and IBM Data magazine. He is an IBM-Certified DB2 Advanced Database Administrator, DB2 Application Developer, and DB2 Problem Determination Master. Mohan holds a Master’s of Technology (M Tech) degree in computer science and an Executive MBA (IT).


MC Press books written by Mohankumar Saraswatipura available now on the MC Press Bookstore.

DB2 10.1/10.5 for Linux, UNIX, and Windows Database Administration (Exams 611 and 311) DB2 10.1/10.5 for Linux, UNIX, and Windows Database Administration (Exams 611 and 311)
Master DB2 database administration and prepare for IBM's Exams 611 and 311: Certified Database Administrator.
List Price $134.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: