Triggers
A trigger is a statement that is executed upon modification to the database.
We must specify:
under what condition a trigger must be executed
the action to be performed when the trigger is executed
For example, we can use a trigger to enforce referential integrity:
Say we have a relation called timeslot, and time_slot_id is not a primary key of timeslot, so we cannot create a foreign key constraint from another relation section to timeslot.
We can use a trigger on section and timeslot to enforce referential integrity:
Triggering events can be insert, delete or update
Triggers on update can be restricted to specific attributes
Ex.
after update of takes on grade
Values of attributes before and after an update can be referenced as follows:
referencing old row as: for deletes and updates
referencing new row as: for inserts and updates
Triggers can be activated before an event, which can serve as extra constraints Ex. convert blank grades to null before they are inserted:
Statement-Level Triggers
Instead of executing a separate action for each affected row, a single action can be executed for all rows affected by a transaction:
Use for each statement instead of for each row
Use referencing old table or referencing new table to refer to temporary tables (called transition tables) containing the affected rows
This is more efficient when dealing with SQL statements that update a large number of rows/
Last updated