Beruflich Dokumente
Kultur Dokumente
22/01/2009
DataBase Triggers
Managing Triggers
Removing Triggers
MANUFACTURING/ EAI
LEKSHMI N
lekshmi.n@tcs.com
DataBase Triggers
The main purpose of this document is to give a detailed description of triggers in PL/SQL.
The database triggers explained in this document are very useful in real-time environment.
This document covers DML row triggers and DML statement database triggers in detail,
which will be very useful for the audience in the future. This document will help the
audience to ease their work, to improve performance and also to have a clear
understanding of database triggers.
This document is mainly intended for the audience who is working in Oracle environment
and for those who is having some idea on Pl/SQL.
DataBase Triggers
DataBase Triggers
A trigger is a Pl/SQL block or PL/SQL procedure associated with a table, view, schema or
the database. Use triggers to guarantee that when a specific operation is performed,
related actions are performed. A database trigger is implicitly executed when a data event
(includes DML statements like insert, update or delete) or system event (shutdown or
logon) occurs on the schema or database. A trigger consists of sequence of PL/SQL
statements to be executed as a unit and trigger can be used to invoke procedures. Triggers
are similar to stored procedures. There is a basic difference between the way in which
procedures and triggers are executed. Procedures have to be executed explicitly by a user,
application or trigger but a trigger will be implicitly executed by Oracle irrespective of the
user and application. Triggers will be stored in the database separately from the tables.
DataBase triggers are also invoked when some user actions (logging on to the database) or
database system action (DBA shuts down the database) occur.
Database triggers can be defined on tables and views. If a DML operation is issued on a
view, the INSTEAD OF trigger defines what actions take place. If these actions include DML
operations on table then any triggers on the base table are fired. That simply means
triggers on the base table of a view are fired if INSERT, UPDATE or DELETE statement is
issued against a view.
• Trigger timing tells when the trigger has to be fired in relation to the triggering
event.
BEFORE-Execute the trigger body before the triggering DML event on the table.
AFTER-Execute the trigger body after the triggering DML event on the table.
INSTEAD OF-This option is used only for views. It executes the trigger statement
instead of the trigger body.
• Triggering event tells which data manipulation operation on the table or view fires
the trigger.
• Table name
• Trigger type tells how many times the trigger body executes.
Trigger type: should the trigger body executes for each the statement affects or
only once.
Here we can specify whether the trigger should be executed once for each of the
row being affected by the triggering event or only once even if multiple rows are
being affected by the triggering event.
• WHEN clause
For example consider a table emp_table with the details emp_name, emp_id,
DataBase Triggers
emp_division and emp_address.The values for emp_id should fall with in 01-100 and value
for emp_division should fall with 001-010.If the value inserted is not falling within the
designed range then fire a database trigger with the name emp_table_after_insertion
which will raise an exception. The trigger emp_table_after_insertion can be created as
follows:
The above example with the trigger emp_table_after_insertion can be made row trigger as
given below:
Managing Triggers:
Disable or enable already enabled all triggers using the following syntax:
Removing Triggers:
To remove a trigger from the database use the DROP trigger syntax:
When a table is dropped all the triggers in the table are automatically dropped.