Triggers
Triggers
1
TRIGGERS
A trigger is a statement that the system
executes automatically as a side effect of a
modification to the database.
Triggers are stored in database as a simple
database objects.
2
BENEFITS OF A TRIGGER
Generating some derived column values
automatically
Enforcing referential integrity
table access
Synchronous replication of tables
3
COMPONENTS OF TRIGGER ( E-C-A
MODEL )
4
TRIGGER SYNTAX
CREATE [OR REPLACE] TRIGGER
<trigger_name>
<BEFORE | AFTER>
Triggering
<INSERT | UPDATE | DELETE> stmt
[OF <column_name_list>]
ON <table_name>
[REFERENCING NEW AS <synonym> OLD AS
<synonym>]
[FOR EACH ROW] [WHEN Trigger Constraint
(<trigger_condition>)]
BEGIN
5
<trigger_code> Trigger Body
CREATE OR REPLACE TRIGGER display_salary_changes
BEFORE DELETE OR INSERT OR UPDATE
ON customers
FOR EACH ROW
WHEN (NEW.ID > 0)
DECLARE sal_diff number;
BEGIN
sal_diff := :NEW.salary - :OLD.salary;
dbms_output.put_line('Old salary: ' || :OLD.salary);
dbms_output.put_line('New salary: ' || :NEW.salary);
dbms_output.put_line('Salary difference: ' || sal_diff);
END;
/
6
7
Old salary:
New salary: 7500
Salary difference:
11
DROPPING TRIGGERS:
To remove trigger from database we use
command DROP
12
DISABLING TRIGGERS
To activate or deactivate trigger temporarily
we use the ALTER trigger command
13