Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

DB2 10.1 Fundamentals certification exam 610 prep: Part 5: Working with tables, views, and indexes

Rob Strong (rstrong@us.ibm.com), IT Architect, Certified: Information Architecture, IBM Australia
A'uthor photo Rob Strong
Rob Strong provides technical consultation to IBM Business Partners on Information Management products. He has more than twenty years of experience with DB2 databases on multiple platforms, from conception through implementation and support. Much of this experience was acquired creating high performance, high availability solutions for IBM's own manufacturing lines. He is a Certified IT Architect. He is an expert in data systems architecture, data modeling, data access, and in application development and maintenance processes.
Hana Curtis (hcurtis@ca.ibm.com), IBM Certified Solutions Expert, IBM
Author photo of Hana Curtis
Hana Curtis is a member of the DB2 Continuing Engineering team at the IBM Toronto Software Laboratory focusing on product serviceability. Previously, she held positions in DB2 Development and Quality Assurance and was a database consultant working with IBM Business Partners to enable their applications to DB2. Hana is one of the authors of the book: DB2 SQL Procedural Language for Linux, UNIX, and Windows (Prentice Hall, 2003).

Summary:  This tutorial discusses IBM® DB2® 10.1 support for data types, tables, views, triggers, constraints and indexes. It explains the features of these objects, how to create and manipulate them using Structured Query Language (SQL), and how they can be used in an application. This tutorial is the fifth in a series that you can use to help prepare for the DB2 10.1 Fundamentals certification exam 610.

View more content in this series

Date:  25 Oct 2012
Level:  Introductory PDF:  A4 and Letter (388 KB | 29 pages)Get Adobe® Reader®

Activity:  7508 views
Comments:  

Triggers

Triggers are snippets of code associated with a table that execute when an INSERT, UPDATE or DELETE action is performed upon that table.

There are also triggers which can be defined upon a view to allow appropriate alternative actions when the view is used for INSERT, UPDATE, DELETE activity, called INSTEAD OF triggers.

Triggers can be defined to perform the actions BEFORE or AFTER each ROW or each STATEMENT.

Several triggers can be defined for the same action on a table. Only one INSTEAD OF can be defined for each action on a view. When several triggers are defined for the same action on a table, they are executed in the same order that they were defined.

Actions that can be taken by a trigger include taking different actions based upon the values involved, affecting the contents of other tables, invoking stored procedures, or returning user-defined error conditions.

For example, given the prior AUTHORS table, with integer columns NONFICTIONBOOKS and FICTIONBOOKS that are summaries of the counts by BOOKTYPE for that author in BOOKS, one could define one of the triggers needed on the BOOKS table to maintain the summaries in the AUTHORS table, as shown in Listing 41.


Listing 41. Creating triggers on the BOOKS table
 
                    
CREATE TRIGGER BOOKSIA                                               
AFTER INSERT ON BOOKS REFERENCING NEW AS NEW FOR EACH ROW             
MODE DB2SQL                                                           
WHEN (NEW.BOOKTYPE ='N') UPDATE AUTHORS                               
  SET NONFICTIONBOOKS=NONFICTIONBOOKS+1 WHERE AUTHORID=NEW.AUTHORID; 
                

To remove the trigger, issue: DROP TRIGGER BOOKSIA;

8 of 11 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Information Management
ArticleID=840529
TutorialTitle=DB2 10.1 Fundamentals certification exam 610 prep: Part 5: Working with tables, views, and indexes
publish-date=10252012
author1-email=rstrong@us.ibm.com
author1-email-cc=
author2-email=hcurtis@ca.ibm.com
author2-email-cc=