Triggers - DBMS
Triggers - DBMS
com
Triggers
A trigger is a pl/sql block structure which is fired when a DML statements like Insert, Delete, Update is executed on a database
table. A trigger is triggered automa cally when an associated DML statement is executed.
Syntax:
CREATE [OR REPLACE ] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF }
{INSERT [OR] | UPDATE [OR] | DELETE}
[OF col_name]
ON table_name
[REFERENCING OLD AS o NEW AS n]
[FOR EACH ROW]
WHEN (condition)
BEGIN
--- sql statements
END;
Statement Descrip on
CREATE [OR This clause creates a trigger with the given name or
REPLACE ] overwrites an exis ng trigger with the same name.
TRIGGER
trigger_name
{INSERT [OR] | This clause determines the triggering event. More than
UPDATE [OR] | one triggering events can be used together separated by
DELETE} OR keyword. The trigger gets fired at all the specified
triggering event.
[OF col_name] This clause is used with update triggers. This clause is used
when you want to trigger an event only when a specific
column is updated.
CREATE [OR This clause creates a trigger with the given name or
REPLACE ] overwrites an exis ng trigger with the same name.
TRIGGER
trigger_name
[ON This clause iden fies the name of the table or view to
table_name] which the trigger is associated.
https://tutorialink.com/dbms/triggers.dbms 1/4
11/5/2018 Triggers | DBMS | Tutorialink.com
[REFERENCING This clause is used to reference the old and new values of
OLD AS o NEW the data being changed. By default, you reference the
AS n] values as :old.column_name or :new.column_name. The
reference names can also be changed from old (or new) to
any other user-defined name. You cannot reference old
values when inser ng a record, or new values when
dele ng a record, because they do not exist.
WHEN This clause is valid only for row level triggers. The trigger is
(condi on) fired only for rows that sa sfy the condi on specified.
Example:
Create or replace Trigger np before insert on account for each row
Begin
IF :NEW.bal < 0 THEN
DBMS_OUTPUT.PUT_LINE('BALANCE IS NAGATIVE..');
END IF;
End;
/
Output:
Run SQL Command Line
SQL>start D://t.sql
Trigger created.
Types of Triggers
A trigger's type is defined by the type of triggering transac on and by the level at which the trigger is executed. Oracle has the
following types of triggers depending on the different applica ons.
https://tutorialink.com/dbms/triggers.dbms 2/4
11/5/2018 Triggers | DBMS | Tutorialink.com
Row level triggers execute once for each row in a transac on.
The commands of row level triggers are executed on all rows that are affected by the command that enables the trigger.
For example, if an UPDATE statement updates mul ple rows of a table, a row trigger is fired once for each row affected by the
UPDATE statement.
If the triggering statement affects no rows, the trigger is not executed at all.
Row level triggers are created using the FOR EACH ROW clause in the CREATE TRIGGER command.
Applica on:
Consider a case where our requirement is to prevent upda on of empno 100 record cannot be
updated, then whenever UPDATE statement update records, there must be PL/SQL block that will be fired automa cally by
UPDATE statement to check that it must not be 100, so we have to use Row level Triggers for that type of applica ons.
Applica on:
Consider a case where our requirement is to prevent the DELETE opera on during Sunday. For this whenever
DELETE statement deletes records, there must be PL/SQL block that will be fired only once by DELETE statement to check that
day must not be Sunday by referencing system date, so we have to use Statement level Trigger for which fires only once for
above applica on.
Before Trigger :
Since triggers are executed by events, they may be set to occur immediately before or a er those events.
When a trigger is defined, you can specify whether the trigger must occur before or a er the triggering event i.e. INSERT,
UPDATE, or DELETE commands.
BEFORE trigger execute the trigger ac on before the triggering statement.
These types of triggers are commonly used in the following situa on:
1. BEFORE triggers are used when the trigger ac on should determine whether or not the triggering statement should be
allowed to complete.
2. By using a BEFORE trigger, you can eliminate unnecessary processing of the triggering statement.
3. For example: To prevent dele on on Sunday, for this we have to use Statement level before trigger on DELETE statement.
4. BEFORE triggers are used to derive specific column values before comple ng a triggering INSERT or UPDATE statement.
A er Trigger :
AFTER trigger executes the trigger ac on a er the triggering statement is executed.
AFTER triggers are used when you want the triggering statement to complete before execu ng the trigger ac on.
For example: To perform cascade delete opera on, it means that user delete the record fro one table, but the corresponding
records in other tables are delete automa cally by a trigger which fired a er the execu on of delete statement issued by the
user.
When combining the different types of triggering ac ons, there are mainly 4 possible Valid trigger types available to us.
Dropping Triggers :
Triggers may be dropped via the drop trigger command. In order to drop a trigger, you must either own the trigger or have the
DROP ANY TRIGGER system privilege.
Syntax:
https://tutorialink.com/dbms/triggers.dbms 3/4
11/5/2018 Triggers | DBMS | Tutorialink.com
Example:
Run SQL Command Line
Trigger dropped.
https://tutorialink.com/dbms/triggers.dbms 4/4