|Previous||Table of Contents||Next|
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 trigger.
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.
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 TRIGGER STUDENTS_ARIU AFTER INSERT OR UPDATE OF overall_gpa ON STUDENTS FOR EACH ROW BEGIN IF (:new.overall_gpa = 3.5) THEN INSERT INTO DEANS_LIST_STUDENTS (ssn) VALUES (:new.ssn); END IF; END STUDENTS_ARIU; /
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 students social security number is added to the table DEANS_LIST_STUDENTS.
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).
Database triggers are, by definition, event-driven and fire when the following DML statements are executed:
A trigger can be further defined to fire before or after any or all of these DML statements, and can also be defined to fire at the statement level or the row level. By combining these factors, Table 7.1 can be generated to show the 12 types of database triggers.
|Before DELETE statement level||Before each DELETE statement affects the triggers associated table, no matter how many rows are deleted from the table.|
|Before DELETE row level||For each row affected by a DELETE statement, before each row is deleted.|
|After DELETE row level||For each row affected by a DELETE statement, after each row is deleted.|
|After DELETE statement level||After each DELETE statement that affects the triggers associated table, no matter how many rows are deleted from the table.|
|Before INSERT statement level||Before each INSERT statement affects the triggers associated table, no matter how many rows are inserted into the table.|
|Before INSERT row level||For each row inserted into the table, before each row is inserted.|
|After INSERT row level||For each row inserted into the table, after each row is inserted.|
|After INSERT statement level||After each INSERT statement affects the triggers associated table, no matter how many rows are inserted into the table.|
|Before UPDATE statement level||Before each UPDATE statement affects the triggers associated table, no matter how many rows in the table are updated.|
|Before UPDATE row level||For each row updated in the table, before each row is updated.|
|After UPDATE row level||For each row updated in the table, after each row is updated.|
|After UPDATE statement level||After each UPDATE statement that affects the triggers associated table, no matter how many rows in the table are updated.|
When database triggers were first introduced in Oracle7, no table could have more than one trigger of any given type. If no triggers combined types, a table could have a dozen different associated triggers.
|Previous||Table of Contents||Next|