Sub topic 8
Building an Audit Trail
This chapter assumes that you know basic SQL, PL/SQL variables and IF statements, and transaction commands such as COMMIT and ROLLBACK. An audit trail is a record of changes made to important data. It can answer who changed a row, what action occurred, and which values changed.
A trigger is useful for auditing because it runs automatically when the table changes. Application code might forget to write the audit row, but a table trigger applies to changes from any client that has permission to modify the table.
Create the Audit Table
The audit table stores the affected employee, the operation, old and new salary values, the database user, and the time of the change.
CREATE TABLE employee_audit (
audit_id NUMBER GENERATED ALWAYS AS IDENTITY,
employee_id NUMBER,
action VARCHAR2(6) NOT NULL,
old_salary NUMBER,
new_salary NUMBER,
changed_by VARCHAR2(128) NOT NULL,
changed_at TIMESTAMP WITH TIME ZONE NOT NULL
);The action column identifies whether the operation was an INSERT, UPDATE, or DELETE. The old value is unavailable during an insert, and the new value is unavailable during a delete. Oracle stores those unavailable values as NULL in this trigger.
Write a Multi-Event Audit Trigger
Use an AFTER row trigger when the audit should be written after the base row operation succeeds. The FOR EACH ROW clause makes the trigger run once for every affected row.
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;
/INSERTING, UPDATING, and DELETING are conditional predicates. They tell the trigger which event caused the current execution. The trigger uses :OLD and :NEW correlation values to read values from the affected row.
The UPDATE OF salary list narrows updates to statements that include the salary column. The trigger does not run for an update that changes only last_name. It may still run when an update assigns the same salary value, because the statement names the column.
The trigger records the session user with SYS_CONTEXT. This identifies the database account, not necessarily the person who initiated a request through a shared application account. Applications that need end-user identity can set a trusted session context before making changes.
Transaction Behavior
A normal trigger shares the transaction of the statement that fired it. Do not use COMMIT or ROLLBACK inside it. If the base change fails or the transaction rolls back, its audit row also rolls back. This keeps the audit trail consistent with the data.
An autonomous transaction can preserve an audit row independently, but it changes those consistency rules and requires careful error handling. Use it only when independent logging is an explicit requirement.
Exercise
What happens to the audit row if an UPDATE fires the trigger and the session later issues ROLLBACK?
Show answer
The audit row is rolled back with the employee update because a normal trigger uses the same transaction.