This example shows a compound trigger that records a change to the employee salary by defining a compound trigger (named hr_trigger) on the emp table.
Create a table named emp:
Create a compound trigger named hr_trigger. The trigger uses each of the four timing points to modify the salary with an INSERT, UPDATE, or DELETE statement. In the global declaration section, the initial salary is declared as 10,000.
Insert the record into table emp:
The INSERT statement produces the following output:
The UPDATE statement updates the employee salary record, setting the salary to 15000 for a specific employee number:
The UPDATE statement produces the following output:
The DELETE statement deletes the employee salary record:
The DELETE statement produces the following output:
The TRUNCATE statement removes all the records from the emp table:
The TRUNCATE statement produces the following output:
Note
You can use the TRUNCATE statement only at a BEFORE STATEMENT or AFTER STATEMENT timing point.
This example creates a compound trigger named hr_trigger on the emp table with a WHEN condition. The WHEN condition checks and prints the employee salary when an INSERT, UPDATE, or DELETE statement affects the emp table. The database evaluates the WHEN condition for a row-level trigger, and the trigger executes once per row if the WHEN condition evaluates to TRUE. The statement-level trigger executes regardless of the WHEN condition.
Insert the record into table emp:
The INSERT statement produces the following output:
The UPDATE statement updates the employee salary record, setting the salary to 7500:
The UPDATE statement produces the following output:
The DELETE statement deletes the employee salary record:
The DELETE statement produces the following output: