Unit-5 Triggers
Unit-5 Triggers
A trigger is a named PL/SQL block stored in the Oracle Database and executed automatically
when a triggering event takes place.
The event can be any of the following:
• A data manipulation language (DML) statement executed against a table
e.g., INSERT, UPDATE, or DELETE. For example, if you define a trigger that fires
before an INSERT statement on the customers table, the trigger will fire once
before a new row is inserted into the customers table.
• A data definition language (DDL) statement executes
e.g., CREATE or ALTER statement. These triggers are often used for auditing
purposes to record changes of the schema.
• A system event such as startup or shutdown of the Oracle Database.
• A user event such as login or logout.
The act of executing a trigger is also known as firing a trigger. We say that the trigger is
fired.
Benefits of Triggers
Triggers can be written for the following purposes −
• Generating some derived column values automatically
• Enforcing referential integrity
• Event logging and storing information on table access
• Auditing
• Synchronous replication of tables
• Imposing security authorizations
• Preventing invalid transactions
2) Trigger name
Specify the name of the trigger that you want to create after the CREATE OR
REPLACE keywords.
3) BEFORE | AFTER
The BEFORE or AFTER option specifies when the trigger fires, either before or after a
triggering event e.g., INSERT, UPDATE, or DELETE.
4) ON table_name
The table_name is the name of the table associated with the trigger.
6) ENABLE / DISABLE
The ENABLE / DISABLE option specifies whether the trigger is created in
the enabled or disabled state. Note that if a trigger is disabled, it is not fired when the
triggering event occurs.
By default, if you don’t specify the clause ENABLE / DISABLE , the trigger is created with
the enabled state.
AFTER TRIGGERS are used when the triggering statements should completed before
executing the trigger action.
DECLARE
-- variable declarations
BEGIN
-- trigger code
EXCEPTION
WHEN ...
-- exception handling
END;
DECLARE
-- variable declarations
BEGIN
-- trigger code
EXCEPTION
WHEN ...
-- exception handling
END;
Example 1
Let’s create a Trigger which simply display a message before inserting, updating and
deleting a row in table.
Output:
1 row(s) inserted.
Trigger Test!
Example 2
Above Trigger will not be fired while inserting new data if salary is less than 30000.
INSERT INTO employee VALUES(101,'Raaju',29000);
Output:
1 row(s) inserted.
Example – 3
Let’s create a trigger that display’s difference between new salary and old salary of
employee while updating salary of employee.
Output:
1 row(s) updated.
New salary is:39000
Old salary is:34000
Difference is:5000
This table illustrates the availability of :NEW and :OLD variables by the triggering event:
Output:
1 row(s) deleted.
After Trigger Executed!
A statement-level trigger fires only once for each statement. Using the previous example, if
deletion is defined as a triggering event for a particular table, and a single DELETE statement
deletes five rows from that table, the trigger fires once.
For example, if you update 1000 rows in a table, then a statement-level trigger on that table
would only be executed once.
Due to its features, a statement-level trigger is not often used for data-related activities like
auditing the changes of the data in the associated table. It’s typically used to enforce extra
security measures on the kind of transaction that may be performed on a table.
By default, the statement CREATE TRIGGER creates a statement-level trigger when you omit
the FOR EACH ROW clause.
DECLARE
-- variable declarations
BEGIN
-- trigger code
EXCEPTION
WHEN ...
-- exception handling
END;
INSTEAD OF Triggers
INSTEAD OF triggers provide a way to modify views that cannot be modified directly
through DML statements like INSERT, UPDATE and DELETE.
An INSTEAD OF trigger is
1. Always a row-level trigger.
2. Can read OLD and NEW values, but cannot change them.
3. Cannot be conditional. Means we can not add WHEN or IF condition.
If the view has an INSTEAD OF trigger, it will automatically skip the DML statement and
execute other DML statements instead.
Note that an INSTEAD OF trigger is fired for each row of the view that gets modified.
In Oracle, you can create an INSTEAD OF trigger for a view only. You cannot create
an INSTEAD OF trigger for a table.
Now let’s create a view named as vw_employees containing records from employee and
department both tables.
Now, let’s try to insert data in above view, you’ll get following error:
Output:
ORA-01779: cannot modify a column which maps to a non key-preserved table
Now let’s create INSTEAD OF Trigger to insert data in above view as follows:
Now select Employee and Department table you’ll see following output: