enabled | An enabled trigger executes its trigger action if a triggering statement is issued and the trigger restriction (if any) evaluates to TRUE. |
disabled | A disabled trigger does not execute its trigger action, even if a triggering statement is issued and the trigger restriction (if any) would evaluate to TRUE. |
Oracle uses the following execution model to maintain the proper firing sequence of multiple triggers and constraint checking:
1. Execute all BEFORE statement triggers that apply to the statement.
2. Loop for each row affected by the SQL statement.
a. Execute all BEFORE row triggers that apply to the
statement.
b. Lock and change row, and perform integrity constraint
checking (The lock is not released until the
transaction is committed.)
c. Execute all AFTER row triggers that apply to the
statement.
3. Complete deferred integrity constraint checking.
4. Execute all AFTER statement triggers that apply to the statement.
The definition of the execution model is recursive. For example, a given SQL statement can cause a BEFORE row trigger to be fired and an integrity constraint to be checked. That BEFORE row trigger, in turn, might perform an update that causes an integrity constraint to be checked and an AFTER statement trigger to be fired. The AFTER statement trigger causes an integrity constraint to be checked. In this case, the execution model executes the steps recursively, as follows:
1. Original SQL statement issued.
2. BEFORE row triggers fired.
3. AFTER statement triggers fired by UPDATE in
BEFORE row trigger.
4. Statements of AFTER statement triggers
executed.
5. Integrity constraint on tables changed by
AFTER statement triggers checked.
6. Statements of BEFORE row triggers executed.
7. Integrity constraint on tables changed by
BEFORE row triggers checked.
8. SQL statement executed.
9. Integrity constraint from SQL statement checked.
For example, in the previously outlined scenario, suppose that Steps 1 through 8 succeed; however, in Step 9 the integrity constraint is violated. As a result of this violation, all changes made by the SQL statement (in Step 8), the fired BEFORE row trigger (in Step 6), and the fired AFTER statement trigger (in Step 4) are rolled back.
Note: Be aware that triggers of different types are fired in a specific order. However, triggers of the same type for the same statement are not guaranteed to fire in any specific order. For example, all BEFORE ROW triggers for a single UPDATE statement may not always fire in the same order. Design your applications not to rely on the firing order of multiple triggers of the same type.
Example
Assume that the SALARY_CHECK trigger (body) includes the following SELECT statement:
SELECT minsal, maxsal INTO minsal, maxsal
FROM salgrade
WHERE job_classification = :new.job_classification;
For this example, assume that transaction T1 includes an update to the MAXSAL column of the SALGRADE table. At this point, the SALARY_CHECK trigger is fired by a statement in transaction T2. The SELECT statement within the fired trigger (originating from T2) does not see the update by the uncommitted transaction T1, and the query in the trigger returns the old MAXSAL value as of the read-consistent point for transaction T2.
Example
Assume the following definition of the TOTAL_SALARY trigger, a trigger to maintain a derived column that stores the total salary of all members in a department:
CREATE TRIGGER total_salary
AFTER DELETE OR INSERT OR UPDATE OF deptno, sal ON emp
FOR EACH ROW BEGIN
/* assume that DEPTNO and SAL are non-null fields */
IF DELETING OR (UPDATING AND :old.deptno != :new.deptno)
THEN UPDATE dept
SET total_sal = total_sal - :old.sal
WHERE deptno = :old.deptno;
END IF;
IF INSERTING OR (UPDATING AND :old.deptno != :new.deptno)
THEN UPDATE dept
SET total_sal = total_sal + :new.sal
WHERE deptno = :new.deptno;
END IF;
IF (UPDATING AND :old.deptno = :new.deptno AND
:old.sal != :new.sal )
THEN UPDATE dept
SET total_sal = total_sal - :old.sal + :new.sal
WHERE deptno = :new.deptno;
END IF;
END;
For this example, suppose that one user's uncommitted transaction includes an update to the TOTAL_SAL column of a row in the DEPT table. At this point, the TOTAL_SALARY trigger is fired by a second user's SQL statement. Because the uncommitted transaction of the first user contains an update to a pertinent value in the TOTAL_SAL column (in other words, a row lock is being held), the updates performed by the TOTAL_SALARY trigger are not executed until the transaction holding the row lock is committed or rolled back. Therefore, the second user waits until the commit or rollback point of the first user's transaction.