2. Definition
• A Trigger is a statement that the system
executes automatically as a side effect of a
modification to the database.
3. To Design a trigger mechanism, we
must meet two requirements:
1. Specify when a trigger is to be executed. This
a broken up into an event that causes the
trigger to be checked and a condition that
must be satisfied for trigger execution to
proceed.
2. Specify the actions to be taken when the
trigger executes.
The above model of triggers is referred to as the
event-condition-action-model for triggers.
4. • The database stores triggers just as if they
were regular data, so that they are persistent
and are accessible to all database operations.
• Once we enter a trigger into the database, the
database system takes on the responsibility to
executing it whenever the specified event
occurs and the corresponding condition is
satisfied.
5. Database Triggers Vs. Procedures
• The oracle engine allows the user to define procedures that
are implicitly executed, when an INSERT,UPDATE od
DELETE is issued against a table from SQL*Plus or through
an application. These procedures are called database
Triggers.
• There are very few differences between these database
triggers and procedures
1. Triggers do not accept parameters whereas Procedures
can.
2. A Trigger is executed implicitly by the Oracle engine itself
upon modification of an associated table or its data. To
execute a Procedure, it has to be explicitly called by the
user.
6. How to Apply Database Triggers
• A Trigger has three basic parts:
1.A triggering event or statement.
2.A Trigger restriction.
3.A Trigger action.
7. A triggering event or statement.
• It is a SQL statement that causes a trigger to
be fired. It can be INSERT, UPDATE or DELETE
statement for a specific table.
8. Trigger Restriction
• A Trigger restriction specifies a Boolean
(Logical) expression that must be TRUE for
trigger to fire. It is an option available for
triggers that are fired for each row.
• Its function is to conditionally control the
execution of a trigger.
• A trigger restriction is specified using a WHEN
clause.
9. Trigger Actions
• A trigger action is the PL/SQL code to be
executed when a triggering statement is
encountered and any trigger restriction
evaluates to TRUE.
10. Types of Triggers
• While defining a trigger, the number of times
the trigger action is to be executed can be
specified.
1.Row Triggers
2.Statement Triggers
3.Before Vs. After Trigger.
4.Combinations Trigger.
11. Row Triggers
• A row trigger is fired each time a row in the table
is affected by the triggering statement.
• For e.g., if an UPDATE statement updates
multiple rows of a table, a row trigger is fired
once for each row effected by the UPDATE
statement. If the triggering statement affects no
rows, the trigger is not executed at all.
• Row triggers should be used when some
processing is required whenever a triggering
statement affects a single row in a table.
12. Statement Triggers
• A statement trigger is fired once on behalf of
the triggering statement, independent of the
number of rows the triggering statement
affects(even if no rows are affected).
• Statement triggers should be used when a
triggering statement affects rows in a table
but the processing required is completely
independent of the number of rows affected.
13. Before Vs. After Triggers
• 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 the triggering statement.
• BEFORE and AFTER apply to both row and the
statement triggers.
14. Before Trigger
• BEFORE triggers execute the trigger action before the
triggering statement. These types of triggers are
commonly used in the following situations:
1. BEFORE triggers are used when the trigger action
should determine whether or not the triggering
statement should be allowed to complete. By using a
BEFORE trigger, you can eliminate unnecessary
processing of the triggering statement.
2. BEFORE triggers are used to derive specific column
values before completing a triggering INSERT or
UPDATE statement.
15. After Triggers
• After triggers executes the trigger action after
the triggering statement is executed. These types
of triggers are commonly used in the following
situations:
1. AFTER triggers are used when you want the
triggering statement to complete before
executing the trigger action.
2. If a BEFORE trigger is already present , an AFTER
trigger can perform different actions on the same
triggering statement.
16. Combinations Trigger.
• Using the above options, four types of triggers could be created.
BEFORE Statement Trigger: Before executing the triggering
statement, the trigger action is executed
BEFORE Row Trigger: Before modifying each row affected by the
triggering statement and before appropriate integrity constraints,
the trigger is execute if the trigger restriction either evaluates to
TRUE or was not included.
AFTER Statement Trigger: After executing the triggering statement
and applying any integrity constraint, the trigger action is executed.
AFTER Row Trigger: After modifying each row affected by the
triggering statement and possibly applying appropriate integrity
constraints, the trigger action is executed for the current row, if the
trigger restriction either evaluates to TRUE or was not included.
Unlike BEFORE row trigger, AFTER row trigger have rows locked.
17. The Generic PL/SQL Block
• The minimum sections of a PL/SQL blocks are:
• The Declare section
• The Master Begin and END section that contains the Exception
section.
1. The Declare Section: Code block starts with a declaration section, in
which memory variable & other oracle objects can be declared, & if
required intialized. Once declared they can be used in the SQL
statements for data manipulation.
2. The Begin Section: It consist of a set of SQL and PL/SQL statements,
which describe processes that have to be applied to the data.
Actual data manipulation, retrieval, looping & branching constructs
are specified in this section.
3. The Exception Section: This section deals with handling of errors
that arise during execution of the data manipulation statements.
Errors can arise due to syntax, logic and/or validation rule violation.
4. The End Section: This marks the end of a PL/SQL block.
19. How To Create A Trigger?
• Syntax for Creating a Trigger:
CREATE OR REPLACE TIRGGER triggername
{BEFORE, AFTER}
{DELETE,INSERT,UPDATE [OF column,…]}
ON tablename
[REFERENCING {OLD AS old NEW AS new}]
[FOR EACH ROW [WHEN condition]]
DECLARE
Variable declarations;
Constant declarations;
BEGIN
PL/SQL subprogram body;
EXCEPTION
Exception PL/SQL block;
END;
21. Example
• Create a transparent 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
22. Table: Client_master
Column Name
Data Type
Size
Attributes
Client_no
Varchar2
6
Primary Key/First letter must start with ‘C’
Name
Varchar2
20
Not Null
Address
Varchar2
30
City
Varchar2
30
State
Varchar2
15
Pincode
Number
6
Bal_Due
Number
10,2
23. Table: Auditclient
Column Name
Data Type
Size
Client_no
Varchar2
6
Name
Varchar2
20
Bal_Due
Number
10,2
Operation
Varchar2
8
Userid
Varchar2
20
Odate
Date
Where:
Operation
Odate
Userid
Attributes
: The operation performed on the client_master table.
: The date when the operation was performed.
: Then name of the user performing the operation.
24. CREATE TRIGGER audit_trail
AFTER UPDATE OR DELETE ON client_master
FOR EACH ROW
DECLARE
Oper varchar2(8);
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;
Clent_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;