0% found this document useful (0 votes)
11 views13 pages

Triggers

Uploaded by

Neha Mendjoge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views13 pages

Triggers

Uploaded by

Neha Mendjoge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

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>
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:

UPDATE customers SET salary = salary + 500 WHERE id = 2;

Old salary: 1500


New salary: 2000
Salary difference: 500
8
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.
9

 If FOR EACH ROW clause is written that means


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

You might also like