Sub topic 2
Timing and Level: Choosing the Kind of Trigger
Assume you know basic INSERT, UPDATE, and DELETE, PL/SQL blocks, tables, views, sequences, and transaction control. This chapter focuses on choosing when a trigger fires and whether it handles one statement or each affected row.
A database trigger is PL/SQL that Oracle runs automatically when a specified event occurs. For table changes, choose a timing and a level:
- Timing says whether the trigger runs before or after the event.
- Level says whether it runs once for the statement or once for every affected row.
BEFORE Triggers
A BEFORE trigger runs before Oracle stores or changes the row. A row-level BEFORE trigger can validate values or change :NEW values.
For example, this trigger supplies a default identifier and timestamp:
CREATE OR REPLACE TRIGGER employees_bi
BEFORE INSERT ON employees
FOR EACH ROW
BEGIN
IF :NEW.employee_id IS NULL THEN
:NEW.employee_id := employees_seq.NEXTVAL;
END IF;
:NEW.created_at := SYSTIMESTAMP;
END employees_bi;
/Use BEFORE when the row must be checked or prepared before the database operation completes. A BEFORE trigger can assign to :NEW for an insert or update. It cannot assign to :OLD.
AFTER Triggers
An AFTER trigger runs after Oracle applies the row change. Use it when the action depends on the completed change, such as writing an audit record or notifying another process.
An AFTER trigger cannot change :NEW. If validation fails in an AFTER trigger, Oracle rolls back the triggering statement, but the row was already processed before the trigger raised the error.
Statement-Level and Row-Level Triggers
A statement-level trigger runs once for the entire SQL statement, even when the statement affects many rows. It is useful for recording that an operation occurred or for work that does not depend on individual row values.
A row-level trigger includes FOR EACH ROW and runs once per affected row. A single statement such as the following can fire it many times:
UPDATE employees
SET salary = salary * 1.05
WHERE department_id = 10;A row-level trigger can inspect :OLD and :NEW. A statement-level trigger cannot use these correlation values because no single row is associated with the trigger execution.
| Trigger choice | Runs | Typical use |
|---|---|---|
| `BEFORE ... FOR EACH ROW` | Before each affected row | Validate or derive values |
| `AFTER ... FOR EACH ROW` | After each affected row | Audit row changes |
| `BEFORE` or `AFTER` without `FOR EACH ROW` | Once per statement | Statement-wide rules or logging |
| `INSTEAD OF` on a view | In place of each view-row operation | Translate view changes to base-table changes |
INSTEAD OF Triggers
An INSTEAD OF trigger is used on a view that Oracle cannot modify directly, often because the view joins several tables. The trigger replaces the attempted operation with your PL/SQL logic.
Unlike BEFORE and AFTER, INSTEAD OF is not a timing choice around a base-table operation. It is the operation for the view. These triggers are row-level and cannot be statement-level.
Exercise
An update affects 500 rows, and you must record each row's old and new salary. Which trigger level should you choose?
Show answer
Choose an AFTER row-level trigger: AFTER UPDATE FOR EACH ROW. It runs once per affected row and can read :OLD.salary and :NEW.salary.