Triggers Flashcards
Oracle Trigger Syntax
CREATE TRIGGER
TriggerNameMode Event {OR Event }ON TargetTable
[[REFERENCING ReferenceName]
FOR EACH ROW
[WHEN Predicate]]
PL/SQL Block
Oracle Execution Algorithm
- Before statements
- For each tuple affected by the triggering statement:
- Before row triggers are executed
- Triggering statement is executed + integrity constraints checked on tuples
- After row triggers - Integrity constraints on tables are checked
- After triggers are checked
Oracle Trigger Execution Order
Same event triggers execution order is non-deterministic
Oracle: When Error Occurs
Rollback of all operations performed by the trigger
Rollback of the triggering transaction
Oracle: Cascade Trigger Execution
Limit after which error is thrown: 32 triggers
Oracle: Mutating Table Access
Cannot be touched with ROW Triggers, but can be with statement triggers.
Oracle: Conditional Execution
Condition can be specified only for row level triggers
DB2 Syntax
CREATE TRIGGER TriggerName
{BEFORE, AFTER} {INSERT, DELETE, UPDATE}
ON TargetTable
[REFERENCING ReferenceName]
FOR EACH {ROW, STATEMENT}
WHEN Predicate
Procedural SQL Statements
DB2 Limitations
Only one event (UPDATE, INSERT, DELETE) is allowed for each trigger.
DB2 Execution algorithm
Transaction T contains a statement S which generates event E.
- T’s execution is suspended and its state is saved into a stack
- Old and new values of E are computed
- Before triggers on E are executed
- New values are applied to the DB (modification due to E is actually performed)
- constraints are checks to avoid recurive invocation on the same procedure.
- After triggers due to E are executed.
- If any trigger contains an action which triggers other triggers, those are executed in a recurive fashion.
- The execution state of T is extracted from the stack and T is resumed.
Oracle VS. DB2
Reference to Old_Table and New_table in statement triggers
Oracle: NO
DB2: Yes
Oracle VS. DB2
When clause in statement triggers
Oracle: no
DB2: yes
Oracle VS. DB2
Execution order between row and statement triggers with same mode
Oracle: Specified
DB2: Arbitrary
Oracle VS. DB2
Execution order between triggers with same event, mode, granularity
Oracle: Unspecified
DB2: Creation order
Oracle VS. DB2
More than one triggering event allowed
Oracle: Yes
DB2: No