You are on page 1of 10

Triggers

Trigger
• A database trigger is a stored PL/SQL program unit
associated with a specific table.
• Triggers are similar to procedures or functions and must
be stored in a database.
• Triggers implicitly gets fired whenever the table is affected
by any SQL operation.
• Triggers does not accept arguments.
• The act of executing a trigger is known as firing the trigger.
• The trigger event can be a DML operation on a table or
certain kinds of views.
• Triggers can be used for system event such as database
startup or shutdown and certain kinds of DDL.
Purposes
• To generate data automatically
• To enforce complex integrity constraints
• Complex security authorizations
• To maintain replicate tables
• To audit data modifications.
• Automatically signaling other programs that action needs
to take place when changes are made to the table.
• Derive column values automatically

Note: you must have create trigger privileges


• Syntax:
Create or replace trigger <trigger name>
Before | after | instead of
delete | [or] insert | [or] update [of
<col>,<col>]
on <table>
[Referencing clause]
[when <trigger condition>]
[for each row]
trigger body;
Parts
• It has three parts:
Triggering event : It is the SQL statement that causes the
trigger to be fired.It can be INSERT,UPDATE, DELETE
statement of a specific table
Trigger Constraint : It specifies the Boolean expression
that must be true for the trigger to fire. This can be
included in the definition of a row trigger. This is specified
using a WHEN clause.
Trigger action: It is PL/SQL block that contains the SQL
statements. It is executed when a triggering statement is
issued.
Types
• Three Types : DML, instead- of and system
triggers.
• Types based on when they are fired : before ,
after, for each row, for each statement(default)
Before/ after : Specifies the trigger body should
be fired with the triggering statement.
Each Row/ statement : It specifies the trigger fires
once per row or fires for each statement
• Before insert row
• Before insert statement
• Before update row
• Before update statement
• Before delete row
• Before delete statement
• After insert row
• After insert statement
• After delete row
• After delete statement
• After update row
• After update statement
• Instead of Row
• Instead of Statement
Order Of trigger Firing
• Execute the before statement level triggers
• For each row :
Execute the before row level
Execute the statement
Execute the after row level triggers
• Execute the after statement level triggers
Correlation Identifiers
• A row level trigger fires once per row processed by the
triggering statement.
• Inside this , we can access the data in the row that is
currently being processed.
• This is done through two identifiers - :old and :new
• A correlation identifier is a special kind of PL/SQL
variable.
• These are not regular intervals.
• This will treat them as records of type : triggertable
%rowtype
• Insert :
:old – Undefined
:new – values that will be inserted
• Update
:old – Original values before update
:new – new values that will be updated
• Delete
:old – Values before the row is deleted
:new – all fields are null

Raise_application_error : raising user defined error


messages
Raise_application_error(error num, ‘err message’)
Error number ranges from -20000 to -20999

You might also like