Triggers
Triggers
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.
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:
Types Of Triggers:
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 .