|
|
Database TriggerDatabase triggers are procedures that are stored in a database and are executed or "fired" when a table is modified. They are very powerful tools that can be used to preform many tasks such as restricting access to specific data, preform logging, or auditing of data sets. There are two classes of triggers, they are either "row triggers" or "statement triggers". With row triggers you can define an action for every row of a table, while statement triggers only occur once and are not dependant on the shape of the data. Each class can be of several types. There are "BEFORE triggers" and "AFTER triggers" which alters the time of execution of the trigger. There is also an "INSTEAD OF trigger" which is a conditional trigger that will fire instead of the triggering statement. There are typically three triggering EVENTS that cause trigger to 'fire': - INSERT event (as a new record is being inserted into the database).
- UPDATE event (as a record is being changed).
- DELETE event (as a record is being deleted).
Databses that support triggers typically give programmers access to record variables by means of a syntax such as :OLD.cust_name or :NEW.cust_name. e.g. if a trigger is monitoring for changes to a salary field one could write a trigger BEFORE UPDATE ON employee_table FOR ALL records IF :NEW.salary <> :OLD.salary THEN do something here END IF; END; Further reading
|
 |