DML Triggers
DML Triggers
DML Triggers
Database Triggers
Triggers are named PL/SQL blocks with declarative, executable, and exception handling sections A trigger is executed implicitly whenever the triggering event takes place Triggers do not accept arguments Like packages, triggers are stored database objects and can not be local to a block
A. Anusooya Devi, MIT
DML Triggers
Application:
To impose complex integrity constraints not possible through declarative constraints To audit information in a table To create replica of a table etc. Security reasons
Types of Triggers
DML Triggers
DML Triggers are fired by the execution of DML statements
DDL Triggers
DDL triggers execute every time a DDL statement is executed
Database Triggers
Database triggers are fired during system events like startup/shutdown and user events like logon/logoff.
A. Anusooya Devi, MIT
DML Triggers
DML Trigger Components
Part
Trigger Timing
Description
When the trigger fires in relation to the triggering event Which data manipulation operation on the table or view, causes the trigger to fire How many times the trigger body executes What action the trigger performs
A. Anusooya Devi, MIT
Possible Values
BEFORE AFTER INSERT UPDATE DELETE Statement Row Complete PL/SQL Block
Triggering Event
Trigger Type
Trigger Body
Statement Triggers
The trigger body executes only once for the triggering event. This is the default. Syntax:
CREATE [ OR REPLACE ] TRIGGER trigger_name trigger_timing event1 [ OR event2 OR event3 ] ON table_name PL/SQL Block;
A. Anusooya Devi, MIT
Row Triggers
The trigger body executes once for each row affected by the triggering event. Syntax for creating Row Triggers:
CREATE [ OR REPLACE ] TRIGGER trigger_name trigger_timing event1 [ OR event2 OR event3 ] ON table_name FOR EACH ROW [WHEN condition] PL/SQL Block;
FOR EACH ROW: Designates the trigger to be a row trigger WHEN condition: Specifies the trigger restriction For the UPDATE event there is an optional clause
[OF column_name[,column_name..]]
Old Value
Conditional Predicates
You can combine several triggering events in one trigger To identify which event has raised the trigger, use conditional predicates:
INSERTING UPDATING DELETING
They return a TRUE value depending upon the DML statement executed
A. Anusooya Devi, MIT
CREATE OR REPLACE TRIGGER audit_emp AFTER INSERT OR UPDATE OR DELETE ON emp FOR EACH ROW BEGIN IF INSERTING THEN UPDATE audit_table SET ins = ins + 1 WHERE user_name = USER AND table_name= EMP; ELSIF DELETING THEN UPDATE audit_table SET del = del + 1 WHERE user_name = USER AND table_name= EMP; ELSIF UPDATING THEN UPDATE audit_table SET upd = upd + 1 WHERE user_name = USER AND table_name= EMP; END IF;
END;
WHEN clause
Valid for row triggers only Trigger body executes for those rows that meet the condition Evaluates for each row OLD and NEW variables can be referenced here For OLD and NEW variables we should not use colon (:) in the WHEN condition
A. Anusooya Devi, MIT
WHEN clause
CREATE OR REPLACE TRIGGER TRIG_WHEN AFTER UPDATE OF sal ON emp FOR EACH ROW WHEN (old.sal > new.sal) BEGIN RAISE_APPLICATION_ERROR(-20009, Cannot reduce salary); END;
A. Anusooya Devi, MIT
Restrictions on a Trigger
By default, Transaction Control Language commands like COMMIT or ROLLBACK are not allowed within a trigger body E. g. CREATE TRIGGER trig AFTER INSERT ON emp BEGIN INSERT INTO emp_log VALUES (SYSDATE, 'Insert on emp'); COMMIT; END; INSERT INTO emp(empno) VALUES (1); INSERT INTO emp(empno) VALUES (1) * ERROR at line 1: ORA-04092: cannot COMMIT in a trigger ORA-06512: at "SCOTT.TAB1_TRIG", line 3 ORA-04088: error during execution of trigger 'SCOTT.TAB1_TRIG'
emp');
Managing Triggers
Disable / Enable trigger: ALTER TRIGGER trigger_name ENABLE/DISABLE; Disable / Enable all triggers for a table: ALTER TABLE table_name DISABLE/ENABLE ALL TRIGGERS; Dropping Triggers: DROP TRIGGER trigger_name; USER_TRIGGERS data dictionary view: To query database trigger details stored in a database dictionary
Laboratory Exercise
Q)Create a trigger named TR_CHECK_COMM to implement the following business rule. In EMP table, employee having job as Salesman should receive a commission. A Salesman must receive a commission of not less than Rs. 100. Employees who are not sales persons are not entitled to get commission (comm value should be NULL). Test it by inserting a record in the emp table.
A. Anusooya Devi, MIT
THANK YOU