Sub topic 10
Firing Order and the FOLLOWS Clause
Prerequisites: you should know basic CREATE TRIGGER syntax, row-level triggers, BEFORE and AFTER timing, and the :OLD and :NEW correlation values.
Why Firing Order Matters
Oracle can have several triggers for the same table, event, and timing point. For example, two BEFORE INSERT FOR EACH ROW triggers might assign an employee number and normalize a last name.
When multiple triggers share a timing point, do not rely on the order in which Oracle happens to execute them. Without an explicit relationship, the firing order is not guaranteed. A later database change can therefore expose a hidden dependency.
The FOLLOWS clause declares that one trigger must run after another compatible trigger. It controls order among triggers at the same timing point. It does not change whether a trigger fires or turn a row trigger into a statement trigger.
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;
/
CREATE OR REPLACE TRIGGER employees_bi_second
BEFORE INSERT ON employees
FOR EACH ROW
FOLLOWS employees_bi
BEGIN
:NEW.last_name := UPPER(:NEW.last_name);
END employees_bi_second;
/employees_bi_second runs after employees_bi for the same BEFORE INSERT row event. The second trigger sees the values produced by the first trigger through :NEW.
Building an Order Dependency
Use FOLLOWS when one trigger needs another trigger's changes or when the order itself is part of the business rule. For example, a trigger that supplies a missing identifier should run before a trigger that writes that identifier to another structure.
Keep the dependency chain short. If trigger A follows trigger B and trigger B follows trigger C, understanding one insert requires inspecting all three triggers. A single trigger with clearly ordered sections may be easier to maintain.
When creating or replacing a trigger, the referenced trigger must be compatible with the firing point. If the referenced trigger has been renamed, dropped, or does not match the event and timing requirements, compilation can fail. Check compilation errors after changing trigger definitions.
Understanding the Result
The order applies separately at each firing point. A BEFORE INSERT trigger does not become ordered relative to an AFTER INSERT trigger through FOLLOWS; those triggers already belong to different timing points.
Practical Rules
- Do not depend on undocumented order.
- Use
FOLLOWSonly for a real dependency. - Prefer independent triggers when their work does not interact.
- Test the final
:NEWand:OLDvalues, not only whether each trigger fired. - Inspect compilation errors after creating or replacing an ordered trigger.
Exercise
Two BEFORE INSERT row triggers both modify :NEW.last_name, and the second must see the first trigger's value. What should you add?
Show answer
Add FOLLOWS followed by the first trigger's name to the second trigger, and verify that both triggers use the same compatible firing point.