Database Trigger
Database Trigger
Database Trigger
Database Triggers are procedures that are stored in the database and implicitly run, or fired, when
something happens.
Traditionally, triggers supported the execution of a PL/SQL block when an INSERT, UPDATE, or DELETE
occurred on a table or view. Starting with Oracle8i, triggers support system and other data events on
DATABASE and SCHEMA.
A trigger:
Is a PL/SQL block or a PL/SQL procedure associated with a table, view, schema, or the database
Executes implicitly whenever a particular event takes place
1
Dynamic SQL
By Muhammad Raheem [email protected]
TRIGGER TIMING
When should the trigger fire?
BEFORE: Execute the trigger body before the triggering DML event on a table.
AFTER: Execute the trigger body after the triggering DML event on a table.
INSTEAD OF: Execute the trigger body instead of the triggering statement. This is used for views
that are not otherwise modifiable.
TRIGGER TYPE
Should the trigger body execute for each row the statement affects or only once?
You can specify that the trigger will be executed once for every row affected by the triggering statement
(such as a multiple row update) or once for the triggering statement, no matter how many rows it affects.
Statement: The trigger body executes once for the triggering event. This is the default. A statement
trigger fires once, even if no rows are affected at all.
Row: The trigger body executes once for each row affected by the triggering event. A row
trigger is not executed if the triggering event affects no rows.
TRIGGER BODY
What action should the trigger perform?
The trigger body is a PL/SQL block or a call to a procedure.
FIRING SEQUENCE
Use the following firing sequence for a trigger on a table, when a single row is manipulated:
DML statement
1 row created.
Triggering action
2
Dynamic SQL
By Muhammad Raheem [email protected]
FIRING SEQUENCE
Use the following firing sequence for a trigger on a table, when many rows are manipulated:
UPDATE emp
SET sal = sal * 1.1
WHERE deptno = 10
3 rows updated.