Triggers Basic Sql3
Triggers Basic Sql3
Event-Condition-Action (ECA)
+ procedures All data actions performed by the trigger execute within the same transaction in which the trigger fires, Cannot contain transaction control statements (COMMIT, SAVEPOINT, ROLLBACK)
2
Available in most enterprise DBMSs (Oracle, IBM DB2, MS SQL server) and some public domain DBMSs (Postgres)
but not present in smaller desktop (Oracle Lite) and public domain DBMS (MySQL)
Some vendor DBMS permit native extensions to SQL for specifying the triggers
e.g. C/C++ in Poet, Java in Oracle, C#/VB in SQL Server for example also to views as in Oracle
3
How many times should the trigger body execute when the triggering event takes place? Per statement: the trigger body executes once for the triggering event. This is the default. For each row: the trigger body executes once for each row affected by the triggering event. When the trigger can be fired Relative to the execution of an SQL DML statement (before or after or instead of it) Exactly in a situation depending on specific system resources (e.g. signal from system clock)
4
Execute only once even if multiple rows affected Example 2: Monitoring Row Events
SQL> UPDATE emp 2 SET sal = sal * 1.1 3 WHERE deptno = 30;
ENAME
KING BLAKE SMITH
DEPTNO
30 30 30 BEFORE row trigger AFTER row trigger BEFORE row trigger AFTER row trigger BEFORE row trigger AFTER row trigger AFTER statement trigger
6
increase_salary_trg BEFORE executing the statement UPDATE of sal column emp table INSERT values INTO sal_hist table
9
Trigger name: Timing: Triggering event: Filtering condition: Target: Trigger parameters: Trigger action:
derive_commission_trg BEFORE executing the statement UPDATE of sal column job = SALESMAN emp table old, new calculate the new commission to be updated
11
Table Operations
Tracking
Protecting
Maintenance
of Semantic Integrity
e.g. when the factory is closed, all employees should become unemployed
Storing
e.g. the number of items in the trolley should correspond to the current session selection e.g. checking user privileges when accessing sensitive information14
Security
continuation
MAX_INS 5 5 MAX_UPD 5 5 0 MAX_DEL 5 1
15
16
12-SEP-04
10-AUG-04
continuation
OLD_TITL E NULL CLERK NEW_TITLE OLD_SALARY NULL ANALYST SALESMAN 1100 NEW_SALARY 3500 1100
17
Whenever some details for an employee are deleted or updated, both the previous and new details are recorded in an audit table to allow tracing the history of changes. An insert operation cannot be recorded with this trigger as old.empno has no value.
18
19
Problem: impossible to determine certain values during execution of a sequence of operations belonging to one and the same transaction Mutating tables: contain rows which change their values after certain operation and which are used again before the current transaction commits Preventing table mutation: Should not contain rows which are constrained by rows from other changing tables Should not contain rows which are updated and read in one and the same operation Should not contain rows which are updated and read via other operations during the same transaction
20
ERROR at line 1: ORA-04091: table CGMA2.EMP is mutating, trigger/ function may not see it
21
1: Do not change data in the primary key, foreign key, or unique key columns of any table Rule 2: Do not update records in the same table you read during the same transaction Rule 3: Do not aggregate over the same table you are updating Rule 4: Do not read data from a table which is updated during the same transaction
23
24