The need and the usage
Enforce business rules
Execute business rules.
triggers do not accept parameters
or arguments .
triggers cannot perform commit or
rollback operations .
triggers are normally slow
Type of trigger
Row Triggers and Statement Trigger
• A row trigger is fired
each time the table
is affected by the
• A statement trigger is
fired once on behalf
of the triggering
BEFORE and AFTER Triggers
BEFORE triggers run the trigger
action before the triggering
statement is run.
To eliminate unnecessary
To derive specific column values.
• AFTER triggers run the trigger
action after the triggering statement
Triggers on System Events and User
Exp. DBMS_AQ Package database level
Parts of a Trigger
THE TRIGGERING EVENT OR STATMENT
A CREATE, ALTER, or DROP statement
on any schema object
An INSERT, UPDATE, or DELETE
statement on a specific table
A database startup or instance
A specific error message or any error
A user logon or logoff
Depends of true and false
of trigger restriction.
A trigger action is the procedure (PL/SQL block, Java
program, or C callout) that contains the SQL
statements and code to be run when the following
• An enabled trigger runs its trigger action
if a triggering statement is issued and the
trigger restriction (if any) evaluates to
• A disabled trigger does not run its trigger
action, even if a triggering statement is
issued and the trigger restriction (if any)
would evaluate to false.
ALTER TRIGGER <Trigger Name> DISABLE|ENABLE
Creating a Database Trigger
Name of the trigger
Table to be associated with
When trigger is to be fired - before or
Command that invokes the trigger UPDATE, DELETE, or INSERT
Whether row-level trigger or not
Condition to filter rows.
PL/SQL block that is to be executed
when trigger is fired.
CREATE [OR REPLACE]
BEFORE (or AFTER)
INSERT OR UPDATE [OF COLUMNS] OR DELETE
[FOR EACH ROW [WHEN (condition)]]
CREATE TRIGGER <triggerName>
[OF <columnList>] ON <tableName>|<viewName>
[REFERENCING [OLD AS <oldName>] [NEW AS
[FOR EACH ROW] (default is “FOR EACH
CREATE OR REPLACE TRIGGER
DBMS_OUTPUT.PUT_LINE(’BEFORE INSERT OF
’ || :NEW.NAME);
CREATE OR REPLACE TRIGGER Print_salary_changes
BEFORE DELETE OR INSERT OR UPDATE ON emp
WHEN (EMPNO > 0)
sal_diff := :NEW.SAL - :OLD.SAL;
dbms_output.put_line('Old salary: '
dbms_output.put_line('New salary: ' ||
dbms_output.put_line(' Difference ' ||