PL-SQL Triggers
PL-SQL Triggers
AIM
To study and implement the concepts of triggers.
DEFINITION
A trigger is a statement that is executed automatically by the system as a side effect of a
modification to the database. The parts of a trigger are,
Trigger statement: Specifies the DML statements and fires the trigger body. It also specifies
the table to which the trigger is associated.
Trigger body or trigger action: It is a PL/SQL block that is executed when the triggering
statement is used.
Trigger restriction: Restrictions on the trigger can be achieved
TYPES OF TRIGGERS
The various types of triggers are as follows,
Before: It fires the trigger before executing the trigger statement.
After: It fires the trigger after executing the trigger statement.
For each row: It specifies that the trigger fires once per row.
For each statement: This is the default trigger that is invoked. It specifies that the trigger fires
once per statement.
:new
:old
These two variables retain the new and old values of the column updated in the database. The
values in these variables can be used in the database triggers for data manipulation
TRIGGERS - SYNTAX
[OF col_name]
ON table_name
WHEN (condition)
DECLARE
Declaration-statements
BEGIN
Executable-statements
EXCEPTION
Exception-handling-statements
END;
DECLARE
sal_diff number;
BEGIN
END;
Output:
(OLD and NEW references are not available for table-level triggers, rather you can use them for
record-level triggers.)
When a record is updated in the employee table, the above create trigger,
display_salary_changes will be fired and it will display the following result −
Result:
Thus the PL/SQL program on Triggers was executed successfully