Sie sind auf Seite 1von 7

DataBase Triggers

22/01/2009

DataBase Triggers

Components of DataBase Triggers

DML Statement Triggers

DML Row Triggers

Managing Triggers

Removing Triggers

MANUFACTURING/ EAI

LEKSHMI N
lekshmi.n@tcs.com
DataBase Triggers

Introduction to this document

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.

Components of DataBase Trigger:

A triggering statement contains:

• Trigger timing tells when the trigger has to be fired in relation to the triggering
event.

Trigger Timing: when should the trigger fire.

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.

Triggering event: which DML statement causes the trigger to execute.


INSERT
DELETE
UPDATE
While specifying the triggering event as an UPDATE statement, there is an option
to include the column-list which tells what all columns must be changed to invoke
the trigger. This option is not available with INSERT and DELETE since they affect
the entire rows.
DataBase Triggers

• Table name

Table name: trigger is defined on which table.


It can be either a table name or name of the view.

• 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.

Statement trigger-This is fired once on behalf of the triggering event even if no


rows are being affected at all.
Row trigger-This is fired as many number of times the table is affected by the
triggering event. If no row is being affected by the triggering event then row
trigger won’t be fired.

• WHEN clause

When: it contains the restricting condition

• Trigger body tells what the trigger body performs.

Trigger body: what action the trigger should perform.


The trigger body is a PL/SQL block or a call to procedure.

Syntax for creating DML Statement Triggers:

CREATE [OR REPLACE] TRIGGER trigger_name


timing
event1 [event2 or event3]
ON table_name
trigger_body

trigger_name should be unique within the schema.


timing indicates when the trigger fires in relation to triggering event.
event1 identifies the data manipulation operation that causes to fire the trigger.
table_name identifies the table name or view name to which trigger is associated.
trigger_body defines the action to be performed by the trigger.

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:

CREATE TRIGGER emp_table_after_insertion


after insert on emp_table
begin
IF (emp_id NOT BETWEEN ' 01' and ' 100 ')
then
RAISE_APPLICATION_ERROR (-20500,' Emp_id being inserted should fall within 01-
100 ');
ELSIF (emp_id NOT BETWEEN ' 01' and ' 100 ')
then
RAISE_APPLICATION_ERROR (-20510,' Emp_division being inserted should fall
within 001-010 ');
END IF;
END IF;
END;

Syntax for creating DML Row Triggers:

CREATE [OR REPLACE] TRIGGER trigger_name


timing
event1 [event2 or event3]
ON table_name
[REFERENCING OLD as old and NEW as new]
FOR EACH ROW
[WHEN (CONDITION)]
trigger_body

trigger_name should be unique within the schema.


timing indicates when the trigger fires in relation to triggering event.
event1 identifies the data manipulation operation that causes to fire the trigger.
table_name identifies the table name or view name to which trigger is associated.
REFERENCING specifies correlation names for the old and new values of current row. The
default values are OLD and NEW.
FOR EACH ROW designates that the trigger is a row trigger.
WHEN specifies the trigger restriction.
trigger_body defines the action to be performed by the trigger.

The above example with the trigger emp_table_after_insertion can be made row trigger as
given below:

CREATE TRIGGER emp_table_after_insertion


DataBase Triggers

after insert on emp_table


for each row
begin
IF (:NEW.emp_id NOT BETWEEN ' 01' and ' 100 ') AND (:NEW.emp_id NOT BETWEEN
' 01' and ' 100 ')
then
RAISE_APPLICATION_ERROR (-20510,' Check the permissible range for emp_id and
emp_division');
END IF;
END;
If the above row trigger has to be restricted on the basis of emp_address then it can be
done as follows:

CREATE TRIGGER emp_table_after_insertion


after insert on emp_table
for each row
when (:NEW.emp_address = ‘ Bangalore‘)
begin
IF (:NEW.emp_id NOT BETWEEN ' 01' and ' 100 ') AND (:NEW.emp_id NOT BETWEEN
' 01' and ' 100 ')
then
RAISE_APPLICATION_ERROR (-20510,' Check the permissible range for emp_id and
emp_division');
END IF;
END;

Managing Triggers:

Disable or enable an already enabled trigger using the following syntax:

ALTER TRIGGER trigger_name DISABLE|ENABLE

Disable or enable already enabled all triggers using the following syntax:

ALTER TABLE table_name DISABLE|ENABLE ALL TRIGGERS

Recompile a trigger for a table:

ALTER TRIGGER trigger_name COMPILE

Removing Triggers:

To remove a trigger from the database use the DROP trigger syntax:

DROP TRIGGER trigger_name;


DataBase Triggers

When a table is dropped all the triggers in the table are automatically dropped.

Das könnte Ihnen auch gefallen