100% found this document useful (1 vote)
16 views9 pages

Triggers

Uploaded by

TUSHAR BONDE
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
100% found this document useful (1 vote)
16 views9 pages

Triggers

Uploaded by

TUSHAR BONDE
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/ 9

Triggers:

A database trigger is a stored procedure that is fired when insert, update or


delete statement is issued on associated table.

Use of Triggers:
1. To generate data automatically
2. To enforce complex integrity constraints
3. To customize complex security authirization
4. To maintain replicate tables
5. To audit data modifications

Parts Of Triggers:
1. Trigger Statement
2. Trigger Body
3. Trigger Restrictions
Parts Of Trigger:

1. Trigger Statement:
Trigger statements are DML statements like Insert,
update and Delete. It causes trigger (body) to be fired.

2. Trigger Body / Action:


It is a PL/SQL code to be executed when triggering
statement is encountered and any trigger restrictions evaluates to TRUE.

3. Trigger Restrictions:
It specifies boolean expression that must be TRUE for
the trigger to fire. A trigger restriction is specified by using when clause.
Syntax:

create or replace trigger triggername


{before, after}
(Delete, Insert, Update}
on
tablename
[referencing {Old as old, NEW as new} ]
[ for each row [WHEN condition] ]
Declare
variable, constant declarations;
Begin
SQL and PL/SQL statements;
Exception
Exception PL/SQL block;
End;
Types Of Triggers:

Types Of Triggers:

Based on No. of times Based on trigger timing


trigger action to be executed

Row Trigger Statement Trigger Before After


• Based on No. of times trigger action to be executed:
While defining trigger,
the number of times trigger action is to be executed can be specified. This
can be once for every row affected by the triggering statement or once for the
triggering statement, no matter how many rows affected

a) Row Trigger:
It is fired each time a row in the table is affected by the
triggering statement. .

b) Statement Trigger:
A statement trigger is fired once on behalf of the triggering
statement, independent of rows the triggering statement affects.
• Based on trigger timing:
When defining a trigger, it is necessary to
specify the trigger timing, i.e. specifying when the triggering action is to be
executed in relation to triggering statement.

a) Before Trigger:
It executes the triggering action before the triggering
statement.

b) After Trigger:
After trigger executes trigger action after the triggering
statement is executed.
Combinations of Triggers:

1. Before Statement
2. Before Row
3. After Statement
4. After Row

Deleting Trigger:
drop trigger triggername;
Example:
Create a Audit system for a table Client_master. The system must keep
track of the records that are being deleted or updated. The functionality
being when a record is deleted or modified the original record details
and the date of operation are stored in the audit table, then the delete or
update is allowed to go through .

Table Name: client_master


Columns: (client_no, name, addr, city, state, pincode, bal_due)

Table Name: auditClient


Columns: (client_no, name, bal_due, operation, userid, odate)
Create trigger auditRecord
After update or delete
On client_master
Declare
oper varchar2(10);
client_no varchar2(6);
name varchar2(20);
bal_due number(10,2);
Begin
if updating then
oper:=‘Update’ ;
end if
if deleting then
oper:=‘delete’ ;
end if
client_no:= :old.client_no;
name := :old.name;
bal_due := :old.bal_due;
insert into auditClient values(client_no, name, bal_due, oper, user, sysdate);
End;

You might also like