TRIGGER
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.
 A database that has a set of associated triggers
is called an active database.
2
BENEFITS OF A TRIGGER
 Generating some derived column values
automatically
 Enforcing referential integrity
 Event logging and storing information on table
access
 Synchronous replication of tables
 Imposing security authorizations
3
COMPONENTS OF TRIGGER ( E-C-A MODEL )
 Event – SQL statement that causes the trigger to
fire (or activate). This event may be insert, update
or delete operation database table.
 Condition - A condition that must be satisfied for
execution of trigger.
 Action- This is code or statement that is to be
executed when triggering condition is satisfied and
trigger is activated on database table.
4
TRIGGER SYNTAX
CREATE [OR REPLACE] TRIGGER
<trigger_name>
<BEFORE | AFTER>
<INSERT | UPDATE | DELETE>
[OF <column_name_list>]
ON <table_name>
[REFERENCING NEW AS <synonym> OLD AS
<synonym>]
[FOR EACH ROW] [WHEN (<trigger_condition>)]
BEGIN
<trigger_code>
Triggering
stmt
Trigger Constraint
Trigger Body
5
6
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;
/
7
8
Old salary:
New salary: 7500
Salary difference:
UPDATE customers SET salary = salary + 500 WHERE id = 2;
Old salary: 1500
New salary: 2000
Salary difference: 500
TRIGGER TYPES
Row Level Triggers
 A row level trigger is fired each time the table is
affected by the triggering statement.
 For example, if an UPDATE statement updates
multiple rows of a table, a row trigger is fired once for
each row affected by the UPDATE statement.
 If a triggering statement affects no rows, a row
trigger is not run.
 If FOR EACH ROW clause is written that means
trigger is row level trigger
9
Statement Level Triggers
 A statement level trigger is fired once on behalf
of the triggering statement, regardless of the
number of rows in the table that the triggering
statement affects, even if no rows are affected.
 For example, if a DELETE statement deletes
several rows from a table, a statement-level
DELETE trigger is fired only once.
 By Default when FOR EACH ROW clause is not
written in trigger that means trigger is statement
level trigger
10
TRIGGER OPERATIONS
Data Dictionary for Triggers
 Once triggers are created their definitions can be viewed
by selecting it from system tables as shown below
Select *
From user_triggers
Where trigger_name = '<trigger_name>';
 This statement will give you all properties of trigger
including trigger code as well.
11
DROPPING TRIGGERS:
 To remove trigger from database we use command
DROP
Drop trigger <trigger_name>;
12
DISABLING TRIGGERS
 To activate or deactivate trigger temporarily we use
the ALTER trigger command
Alter trigger <trigger_name> {disable | enable};
13

TRIGGERS IN DATABASE MANAGEMENT SYSTEM.ppt

  • 1.
  • 2.
    TRIGGERS  A triggeris 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.  A database that has a set of associated triggers is called an active database. 2
  • 3.
    BENEFITS OF ATRIGGER  Generating some derived column values automatically  Enforcing referential integrity  Event logging and storing information on table access  Synchronous replication of tables  Imposing security authorizations 3
  • 4.
    COMPONENTS OF TRIGGER( E-C-A MODEL )  Event – SQL statement that causes the trigger to fire (or activate). This event may be insert, update or delete operation database table.  Condition - A condition that must be satisfied for execution of trigger.  Action- This is code or statement that is to be executed when triggering condition is satisfied and trigger is activated on database table. 4
  • 5.
    TRIGGER SYNTAX CREATE [ORREPLACE] TRIGGER <trigger_name> <BEFORE | AFTER> <INSERT | UPDATE | DELETE> [OF <column_name_list>] ON <table_name> [REFERENCING NEW AS <synonym> OLD AS <synonym>] [FOR EACH ROW] [WHEN (<trigger_condition>)] BEGIN <trigger_code> Triggering stmt Trigger Constraint Trigger Body 5
  • 6.
    6 CREATE OR REPLACETRIGGER 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; /
  • 7.
  • 8.
    8 Old salary: New salary:7500 Salary difference: UPDATE customers SET salary = salary + 500 WHERE id = 2; Old salary: 1500 New salary: 2000 Salary difference: 500
  • 9.
    TRIGGER TYPES Row LevelTriggers  A row level trigger is fired each time the table is affected by the triggering statement.  For example, if an UPDATE statement updates multiple rows of a table, a row trigger is fired once for each row affected by the UPDATE statement.  If a triggering statement affects no rows, a row trigger is not run.  If FOR EACH ROW clause is written that means trigger is row level trigger 9
  • 10.
    Statement Level Triggers A statement level trigger is fired once on behalf of the triggering statement, regardless of the number of rows in the table that the triggering statement affects, even if no rows are affected.  For example, if a DELETE statement deletes several rows from a table, a statement-level DELETE trigger is fired only once.  By Default when FOR EACH ROW clause is not written in trigger that means trigger is statement level trigger 10
  • 11.
    TRIGGER OPERATIONS Data Dictionaryfor Triggers  Once triggers are created their definitions can be viewed by selecting it from system tables as shown below Select * From user_triggers Where trigger_name = '<trigger_name>';  This statement will give you all properties of trigger including trigger code as well. 11
  • 12.
    DROPPING TRIGGERS:  Toremove trigger from database we use command DROP Drop trigger <trigger_name>; 12
  • 13.
    DISABLING TRIGGERS  Toactivate or deactivate trigger temporarily we use the ALTER trigger command Alter trigger <trigger_name> {disable | enable}; 13