Every trigger is associated with an event. Triggers are activated when their corresponding event occurs in the database. This event occurs when the specified triggering SQL operation, either an UPDATE, INSERT, or DELETE (including those caused by actions of referential constraints), is performed on the subject table. For example:
CREATE TRIGGER NEW_HIRE
AFTER INSERT ON EMPLOYEE
FOR EACH ROW MODE DB2SQL
UPDATE COMPANY_STATS SET NBEMP = NBEMP + 1
The above statement defines that the trigger new_hire is activated when an insert operation is performed on table employee.
Every event, and consequently, every trigger, can be associated with exactly one subject table and exactly one triggering operation. The triggering operations are:
If the triggering SQL operation is an UPDATE operation, the event can be associated with specific columns of the subject table. In this case, the trigger is only activated if the update operation attempts to update any of the specified columns. This provides a further refinement of the event that activates the trigger. For example, the following trigger, REORDER, will be activated only if an update operation is performed on the columns ON_HAND or MAX_STOCKED, of the table PARTS.
CREATE TRIGGER REORDER
AFTER UPDATE OF ON_HAND, MAX_STOCKED ON PARTS
REFERENCING NEW AS N_ROW
FOR EACH ROW MODE DB2SQL
WHEN (N_ROW.ON_HAND < 0.10 * N_ROW.MAX_STOCKED)
BEGIN ATOMIC
VALUES(ISSUE_SHIP_REQUEST(N_ROW.MAX_STOCKED -
N_ROW.ON_HAND,
N_ROW.PARTNO));
END