Sub topic 2
One Trigger, Several Events: INSERTING, UPDATING, DELETING
This chapter assumes you know basic SQL (SELECT, INSERT, UPDATE, DELETE), simple PL/SQL blocks, and the difference between COMMIT and ROLLBACK. You should also have an employees table with employee_id, last_name, salary, department_id, and created_at columns.
One Trigger, Several Events
A trigger can fire on more than one event. List the events after ON, separated by OR:
CREATE OR REPLACE TRIGGER employees_aud
AFTER INSERT OR UPDATE OR DELETE ON employees
FOR EACH ROW
BEGIN
IF INSERTING THEN
DBMS_OUTPUT.PUT_LINE('Inserted ' || :NEW.employee_id);
ELSIF UPDATING THEN
DBMS_OUTPUT.PUT_LINE('Updated ' || :OLD.employee_id);
ELSIF DELETING THEN
DBMS_OUTPUT.PUT_LINE('Deleted ' || :OLD.employee_id);
END IF;
END employees_aud;
/The predicates INSERTING, UPDATING, and DELETING return TRUE for the event that fired the trigger. Use them to choose the right logic.
Limiting When a Trigger Fires
UPDATE OF salary makes the trigger fire only when the salary column appears in the UPDATE. The WHEN clause adds a condition. Inside WHEN, write NEW and OLD without the colon:
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;
/Validating Data and Filling Derived Columns in BEFORE Triggers
A BEFORE row trigger runs before the row is stored, so you can change :NEW values. This trigger assigns an ID from a sequence and sets a timestamp. It assumes that employees_seq exists:
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;
/Building an Audit Trail
An audit trigger copies each change into a log table in the same transaction. Create the log table first:
CREATE TABLE employee_audit (
employee_id NUMBER NOT NULL,
action VARCHAR2(6) NOT NULL,
old_salary NUMBER,
new_salary NUMBER,
changed_by VARCHAR2(128),
changed_at TIMESTAMP NOT NULL
);CREATE OR REPLACE TRIGGER employees_aud
AFTER INSERT OR UPDATE OF salary OR DELETE ON employees
FOR EACH ROW
DECLARE
l_action VARCHAR2(6);
BEGIN
IF INSERTING THEN
l_action := 'INSERT';
ELSIF UPDATING THEN
l_action := 'UPDATE';
ELSE
l_action := 'DELETE';
END IF;
INSERT INTO employee_audit
(employee_id, action, old_salary, new_salary, changed_by, changed_at)
VALUES
(COALESCE(:NEW.employee_id, :OLD.employee_id),
l_action, :OLD.salary, :NEW.salary,
SYS_CONTEXT('USERENV', 'SESSION_USER'), SYSTIMESTAMP);
END employees_aud;
/> The audit trigger is an AFTER trigger, so the row change is final before the log row is written. If the outer transaction rolls back, the log row rolls back too.
Making Views Modifiable with INSTEAD OF Triggers
A view that joins tables cannot take a direct INSERT. An INSTEAD OF trigger runs in place of the statement and writes to the base table. This example assumes emp_dept_v joins employees and departments:
CREATE OR REPLACE TRIGGER emp_dept_v_ioi
INSTEAD OF INSERT ON emp_dept_v
FOR EACH ROW
BEGIN
INSERT INTO employees (employee_id, last_name, department_id)
VALUES (:NEW.employee_id, :NEW.last_name, :NEW.department_id);
END emp_dept_v_ioi;
/Exercise
Exercise
The employeessalarychk trigger uses BEFORE UPDATE OF salary. Why does an UPDATE that changes only last_name not fire it, and what would change if you removed the OF salary part?
Show answer
Because the UPDATE does not name the salary column, the trigger does not fire. Without "OF salary", the trigger fires on every update of the table, and the WHEN test would still compare NEW.salary with OLD.salary. The unchanged salary would pass the test, so the trigger would run needlessly on every update.