Introduction to DB2 for z/OS
Previous topic | Next topic | Contents | Glossary | Contact z/OS | PDF


Triggers

Introduction to DB2 for z/OS

A trigger defines a set of actions that are to be executed when an insert, update, or delete operation occurs on a specified table.

When an insert, load, update, or delete is executed, the trigger is said to be activated.

You can use triggers along with referential constraints and check constraints to enforce data integrity rules. Triggers are more powerful than constraints because you can use them to do the following things:

  • Update other tables
  • Automatically generate or transform values for inserted or updated rows
  • Invoke functions that perform operations both inside and outside of DB2®

For example, assume that you need to prevent an update to a column when a new value exceeds a certain amount. Instead of preventing the update, you can use a trigger. The trigger can substitute a valid value and invoke a procedure that sends a notice to an administrator about the attempted invalid update.

You define triggers by using the CREATE TRIGGER statement.

Start of changeINSTEAD OF triggers are triggers that execute instead of the INSERT, UPDATE, or DELETE statement that activates the trigger. Unlike other triggers, which are defined on tables only, INSTEAD OF triggers are defined on views only. INSTEAD OF triggers are particularly useful when the triggered actions for INSERT, UPDATE, or DELETE statements on views need to be different from the actions for SELECT statements. For example, an INSTEAD OF trigger can be used to facilitate an update through a join query or to encode or decode data in a view.End of change





Copyright IBM Corporation 1990, 2010