Sub topic 6
Limiting When a Trigger Fires: `UPDATE OF` and `WHEN`
This chapter assumes you know basic SQL data changes, PL/SQL IF statements, and row-level triggers. It focuses on narrowing an UPDATE trigger so it runs only for relevant changes.
Why Limit a Trigger?
A trigger can run for every update on a table, even when the changed column is unrelated to the trigger's purpose. For example, a salary rule does not need to run when an employee's phone number changes.
Oracle provides two useful filters:
UPDATE OF column_nameruns the trigger only when the named column appears in anUPDATEstatement.WHENevaluates each affected row and runs the trigger body only when its condition is true.
These filters reduce unnecessary work and make the trigger's purpose clearer.
flowchart TD
A[UPDATE employees] --> B{Does the statement update salary?}
B -- No --> C[Trigger does not fire]
B -- Yes --> D{Does the row meet the WHEN condition?}
D -- No --> E[Trigger body does not run]
D -- Yes --> F[Trigger body runs]Using `UPDATE OF`
Place the column list after the UPDATE event:
CREATE OR REPLACE TRIGGER employees_salary_chk
BEFORE UPDATE OF salary ON employees
FOR EACH ROW
WHEN (NEW.salary < OLD.salary)
BEGIN
RAISE_APPLICATION_ERROR(-20001, 'Salary cannot be decreased');
END employees_salary_chk;
/The UPDATE OF salary clause is a statement-level filter. It checks the columns named in the SQL statement, not whether the value actually changed.
For example, this statement qualifies for the trigger because salary appears in the SET list:
UPDATE employees
SET salary = salary
WHERE employee_id = 101;The WHEN condition then compares the old and new values. Since the values are equal, the trigger body does not run.
| Filter | Checks | Example |
|---|---|---|
| `UPDATE OF salary` | Whether `salary` appears in the `UPDATE` statement | `SET salary = 50000` |
| `WHEN` | Whether each affected row satisfies a condition | `NEW.salary < OLD.salary` |
Writing a `WHEN` Clause
A WHEN clause belongs to a row-level trigger, which includes FOR EACH ROW. In the clause, write NEW and OLD without colons:
WHEN (NEW.salary < OLD.salary)Inside the PL/SQL body, use colons:
IF :NEW.salary < :OLD.salary THEN
...
END IF;For an update:
OLDcontains the value before the update.NEWcontains the proposed value after the update.
Oracle evaluates the WHEN condition separately for every row affected by the statement. A multi-row update can therefore cause the trigger body to run for some rows and skip it for others.
A WHEN clause is not a general PL/SQL block. Keep it as a row condition. Do not put a subquery or a statement such as SELECT inside it. Use the trigger body when the decision needs more complex logic.
Choosing Between the Two Filters
Use UPDATE OF when you know which columns are relevant. Use WHEN when the actual old and new values determine whether work is needed. They are often most useful together:
UPDATE OFavoids firing for unrelated column updates.WHENavoids processing rows that do not meet the business rule.
Exercise
In the salary trigger, what happens when an update changes only the employee's last name, and what happens when it sets salary to its current value?
Show answer
The trigger is not selected for a last-name-only update. It is selected when salary appears in the SET list, but the WHEN condition is false if the old and new salaries are equal, so the trigger body does not run.