Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

What is a trigger in PL/SQL?

TRIGGER is a data base object which can be enabled or executed implicitly for any dml operation on the
table.

1. To maintain the business data uniformly


2. Generally it can be used to take a audit information of table data

Trigger contains 3 parts:

i) Trigger event: Any dml (insert or update or delete) operation is known as trigger event

ii) Trigger restriction: It controls the trigger execution that is before or after dml operation.

iii) Trigger action: It implements or represents the logic or functionality of the trigger that is what it is
doing for dml operation.

Types of triggers: 12

At row level

FOR EACH ROW--It enable the trigger execution for each record inside the table. The following are
record level triggers

Before insert, after insert, before update, after update, before delete, after delete

At statement level--Trigger is executed only once for any number affected records for a dml operation

Before insert, after insert, before update, after update, before delete, after delete

:NEW--It represents the new values into a column[ insert & After update ]

:OLD--It represents the old values from a column [ Before update & before delete ]

What is the maximum number of triggers, you can apply on a


single table?
12 triggers.
How many types of triggers exist in PL/SQL?
There are 12 types of triggers in PL/SQL that contains the combination of BEFORE,
AFTER, ROW, TABLE, INSERT, UPDATE, DELETE and ALL keywords.

o BEFORE ALL ROW INSERT


o AFTER ALL ROW INSERT
o BEFORE INSERT
o AFTER INSERT etc.

Syntax for creating trigger:


Here,

o CREATE [OR REPLACE] TRIGGER trigger_name: It creates or replaces an existing


trigger with the trigger_name.
o {BEFORE | AFTER | INSTEAD OF} : This specifies when the trigger would be
executed. The INSTEAD OF clause is used for creating trigger on a view.
o {INSERT [OR] | UPDATE [OR] | DELETE}: This specifies the DML operation.
o [OF col_name]: This specifies the column name that would be updated.
o [ON table_name]: This specifies the name of the table associated with the trigger.
o [REFERENCING OLD AS o NEW AS n]: This allows you to refer new and old values
for various DML statements, like INSERT, UPDATE, and DELETE.
o [FOR EACH ROW]: This specifies a row level trigger, i.e., the trigger would be
executed for each row being affected. Otherwise the trigger will execute just once
when the SQL statement is executed, which is called a table level trigger.
o WHEN (condition): This provides a condition for rows for which the trigger would
fire. This clause is valid only for row level triggers.

You might also like