Sub topic 7
Validating Data and Filling Derived Columns in BEFORE Triggers
Assume you know basic SQL, PL/SQL IF statements and exceptions, tables, sequences, and transaction control. This chapter focuses on row-level BEFORE triggers.
Why Use a BEFORE Trigger?
A BEFORE trigger runs before Oracle stores an inserted or updated row. A row-level trigger uses FOR EACH ROW, so it runs once for every affected row.
Use this timing when you need to:
- Reject invalid data before it reaches the table.
- Fill a missing identifier or timestamp.
- Normalize a value, such as converting a name to uppercase.
- Derive one column from values in the same row.
A constraint is usually better for simple rules such as NOT NULL, CHECK, UNIQUE, and foreign keys. Use a trigger when the rule needs procedural logic or values from a sequence.
The `:OLD` and `:NEW` Values
Inside a row trigger, :NEW represents the value that Oracle is about to store. :OLD represents the value already stored.
| Event | `:OLD` | `:NEW` |
|---|---|---|
| `INSERT` | Not available | New row values |
| `UPDATE` | Existing values | Updated values |
| `DELETE` | Existing values | Not available |
A BEFORE trigger can assign values to :NEW. It cannot change :OLD.
Assigning Derived Values
This trigger assigns an identifier from a sequence when the caller does not provide one. It also records the creation time.
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;
/The assignment changes the row that Oracle will insert. The trigger does not need an UPDATE statement. The sequence must already exist, and created_at must have a compatible timestamp type.
Rejecting Invalid Updates
A trigger can compare the proposed value with the stored value and raise an application error. Raising an error cancels the triggering statement.
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;
/UPDATE OF salary means that the trigger is considered only when the salary column appears in the UPDATE statement. The WHEN clause narrows it further. In a WHEN clause, write NEW and OLD without colons. In the PL/SQL body, use :NEW and :OLD.
A WHEN clause cannot contain arbitrary PL/SQL. Put complex conditions in the body instead.
Choosing a Validation Location
Use a table constraint when the rule is declarative and must always apply. Use a BEFORE trigger when you must calculate a value, compare old and new values, or perform procedural validation. Keep the rule close to the data only when every client of the database should receive the same behavior.
Exercise
In the salary trigger, why does the condition use OLD.salary and NEW.salary?
Show answer
OLD.salary is the value already stored, and NEW.salary is the proposed replacement. Comparing them detects a salary decrease before Oracle stores it.