TABLE clause. However, you can do
so by defining an INSTEAD OF trigger on the nested table column of the view. The triggers on the nested tab
les fire if a nested table element is updated, inserted, or deleted and handle the actual modifications to the underlying tables.
|
See Also:
|
<
p>See Also:
|
A tr igger is in either of two distinct modes:
| Trigger Mod e | Definition |
|---|---|
| Enabled | An enabled trigger runs its trigger action if
a triggering statement is issued and the trigger restriction (if any) evaluates to true. |
| Disabled | A disabled tri
gger does not run its trigger action, even if a triggering statement is issued and the trigger restriction (if any) would evaluate to
true. |
For enabled triggers, Oracle automatically performs the following actions:
Oracle runs triggers of each type in a planned firing sequence when more than one trigger is fired by a single SQL statement. First, statement level triggers are fired, and then row level triggers are fired.
Oracle performs integrity constraint checking at a set point in time with respect to the different types of triggers and guarantees that triggers cannot compromise integrity constraints.
Oracle provides read-consistent views for queries and constraints.
Oracle manages the dependencies among triggers and schema objects referenced in the code of the trigger action
Oracle uses two-phase commit if a trigger updates remote tables in a distributed database.
Oracle fires multiple triggers in an unspecified, random order, if more than one trigger of the same type exists for a given stat ement; that is, triggers of the same type for the same statement are not guaranteed to fire in any specific order.
A single SQL statement can potentially fire up to four types of triggers:
BEFORE row triggers
BEFORE statement triggers
AFTER row
triggers
AFTER statement triggers
A tri ggering statement or a statement within a trigger can cause one or more integrity constraints to be checked. Also, triggers can conta in statements that cause other triggers to fire (cascading triggers).
Oracle uses the following execution model to maintain th e proper firing sequence of multiple triggers and constraint checking:
Run all BEFORE statement triggers that apply to the statement.
Loop for each row affected by the SQL statem ent.
Run all BEFORE row triggers that apply to the statement.
Lock and change row, and perform integrity constraint checking. (The lock is not released until the transaction is committed.)
Run all AFTER row triggers that apply to the statement.
Complete deferred integrity constraint checking.
Run all AFTER statement trigg
ers that apply to the statement.
The definition of the execution model is recursive. For example, a given SQL statem
ent can cause a BEFORE row trigger to be fired and an integrity constraint to be checked. That row trigger, in turn, might perform an update that causes an integrity constraint to be checke
d 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 runs the steps recursively, as follows:
Original SQL statement issued.
BEFORE row triggers fired.
AFTER statement triggers fired by UPDATE in BEFORE
code> row trigger.
i. Statements of AFTER statement triggers run.
ii. Integrity constraint checked on tables changed by AFTER statement triggers.
Statements of BEFORE row triggers run.
Integrity constraint checked on tables
changed by BEFORE row triggers.
SQL statement run.
Int egrity constraint from SQL statement checked.
There are two exceptions to this recursion:
When a triggering statement modifies one table in a referential constraint (either the primary key or foreign key table), and a tr iggered statement modifies the other, only the triggering statement will check the integrity constraint. This allows row triggers to enhance referential integrity.
Statement triggers fired due to DELETE CASCADE
and DELETE SET NULL are fired before and after the user DELETE statement, not bef
ore and after the individual enforcement statements. This prevents those statement triggers from encountering mutating errors.
An important property of the execution m odel is that all actions and checks done as a result of a SQL statement must succeed. If an exception is raised within a trigger, and the exception is not explicitly handled, all actions performed as a result of the original SQL statement, including the actions perf ormed by fired triggers, are rolled back. Thus, integrity constraints cannot be compromised by triggers. The execution model takes in to account integrity constraints and disallows triggers that violate declarative integrity constraints.
For example, in the pr
eviously outlined scenario, suppose that the integrity constraint is violated. As a result of this violation, all changes made by the
SQL statement, the fired BEFORE row trigger, and the fired AFTER statement
trigger are rolled back.
|
Note: strong> Although triggers of different types are fired in a specific order, triggers of the same type for the same stateme nt are not guaranteed to fire in any specific order. For example, allBEFORE row triggers for a si
ngle UPDATE statement may not always fire in the same order. Design your applications so they do not rely on the firing
order of multiple triggers of the same type. |
When a trigger i s fired, the tables referenced in the trigger action might be currently undergoing changes by SQL statements in other users' transact ions. In all cases, the SQL statements run within triggers follow the common rules used for standalone SQL statements. In particular, if an uncommitted transaction has modified values that a trigger being fired either needs to read (query) or write (update), then th e SQL statements in the body of the trigger being fired use the following guidelines:
Queries see the cu rrent read-consistent materialized view of referenced tables and any data changed within the same transaction.
Updates wait for existing data locks to be released before proceeding.
Oracle stores PL/SQL triggers in compile
d form, just like stored procedures. When a CREATE TRIGGER stateme
nt commits, the compiled PL/SQL code, called P code (for pseudocode), is stored in the database and the source code of the trigger is
flushed from the shared pool.
|
See Also : PL/SQL User's Guide and Reference for more information about compiling and storing PL/SQL code |
Oracle runs a trigger internally using the same steps used for procedure execution. The only subtle difference is that a user has the right to fire a trigger if he or she has the privilege to run the triggering statement. Other than this, triggers are va lidated and run the same way as stored procedures.
Like procedures, triggers depend on referenced objects. Oracle automatically manages the dependencies of a trigger on the schema objects referenced in its trigger action. The dependency issues fo r triggers are the same as those for stored procedures. Triggers are treated like stored procedures. They are inserted into the data dictionary.