Oracle Tips by
A database trigger is a block of stored PL/SQL that is
associated with a table and is executed by Oracle whenever certain
events are performed on that table. Like other stored PL/SQL objects,
a database trigger is compiled and stored within the data dictionary.
Database triggers can be configured to run at four distinct times
for each of three different events that can modify data. This chapter
discusses the creation of database triggers for several permutations
of these events and also provides an example of designing and creating
a trigger. By the end of the chapter, you will understand the nature
of each type of trigger and the process involved in creating a
Features of Database Triggers
The implementation of database triggers in Oracle7 was a major step
forward in application development. Developers could write code to
enforce complex business rules and be assured that any modification of
data would cause the trigger to fire. By using database triggers, you
can take advantage of several extremely powerful features.
Embedded DML Statements
Database triggers are blocks of PL/SQL code. Like any other block
of PL/SQL code, a database trigger can include embedded SQL
statements. Consider the trigger shown in Listing 7.1.
Listing 7.1 Using a DML statement inside a database trigger.
CREATE OR REPLACE
AFTER INSERT OR UPDATE OF overall_gpa
FOR EACH ROW
IF (:new.overall_gpa = 3.5) THEN
This simple trigger checks the value of the overall_gpa
column in the STUDENTS table. If an overall_gpa value is
equal to 3.5, the student’s social security number is added to the
Restricted SQL Commands
There are some restrictions on SQL commands that can be used inside
a trigger. None of the following statements can be used:
These statements cannot be used because they force a database to
perform actions that can only be performed after a statement has
finished executing. If a trigger is executing, the statement that
fired the trigger has not finished.
This commonly leads to a problem when database triggers call stored
procedures that issue COMMIT or ROLLBACK statements. A
runtime error will occur when attempting to execute a trigger that
uses one of these statements, either directly or indirectly.
A database trigger cannot declare any variables of the long
or long raw datatypes. Attempting to do so will cause
compilation errors. Additionally, the: new and: old specifications
cannot reference columns of these datatypes (these specifications are
discussed later in the chapter).
This is an excerpt from the book "High Performance Oracle
Database Automation" by Jonathan Ingram and Donald K.
Burleson, Series Editor.