Introduction
In databases, a trigger is a stored procedure that is automatically invoked whenever a particular event occurs in the Database. A trigger, for example, can be invoked when a row is inserted into a specified table or when certain table columns are updated, and so forth.
Wonder how?
Let us get started with triggers in PL/SQL right away:

In this article, we will discuss the overview of the triggers, how they can be implemented, and their uses in databases.
Recommended topics, Coalesce in SQL and Tcl Commands in SQL
What are the Triggers in SQL?
In Sql, triggers are database objects that allow the automatic execution of a predefined set of actions or operations when specific events occur within a database. These events generally involve changes to data, like INSERT, DELETE, or UPDATE operations. Triggers provide a method to respond to these events without requiring manual intervention. Triggers are essential in enforcing data integrity, automating tasks, and implementing complex business rules within a database system.
Events can be any of the following –
-
Database manipulation (DML): statement like - INSERT, DELETE, or UPDATE
-
Database definition ( DDL ) : statement like - CREATE, ALTER, or DROP
-
Database operation : statement like - LOGON, STARTUP, or SHUTDOWN
Unlike a stored procedure, a trigger can be enabled and disabled but not explicitly invoked. By explicitly invoked means, whenever we want to fire something after/before an event, we need to define or store the trigger beforehand in the database.
When a trigger is enabled, the database will automatically invoke it—that is, the trigger will fire—when its triggering event occurs. When a trigger is disabled, it does not fire.
Moreover, the trigger is said to be created or defined on the item, which could be a table, a view, a schema, or the database. We can also specify the timing point, which determines whether the trigger fires before or after the triggering statement runs and whether it fires for each row that the triggering statement affects. By default, a trigger is created in the enabled state.
Usages of Triggers
Triggers can be used for the following purposes -
-
To gain strong Control over Security.
-
Enforcing referential integrity - To put it simply, it is a concept of foreign key constraint which depicts that you cannot add value to the child table if the master table is empty.
-
Event logging and storing information on table access.
-
Auditing.
-
Synchronous replication of tables.
-
Preventing invalid transactions.
-
Accumulates information on table usage.
- It monitors the critical information.
Types of Triggers
There are three types of Triggers:
- Level Triggers
- Event Triggers
-
Timing Triggers
Let us discuss each one of them:
LEVEL TRIGGERS
Level triggers are divided into two parts:
ROW LEVEL TRIGGERS
- It fires for each record that was affected by the execution of DML statements such as INSERT, UPDATE, DELETE, and so on.
- A FOR EACH ROW clause is always used in a triggering statement.
STATEMENT LEVEL TRIGGERS
- It fires once for each executed statement.
EVENT TRIGGERS
Event triggers are divided into three parts:
DDL EVENT TRIGGER
- It is triggered by the execution of every DDL statement (CREATE, ALTER, DROP, TRUNCATE).
DML EVENT TRIGGER
- It is triggered by the execution of every DML statement (INSERT, UPDATE, DELETE).
DATABASE EVENT TRIGGER
- It is triggered by the execution of any database operation, such as LOGON, LOGOFF, SHUTDOWN, SERVERERROR, and so on.
TIMING TRIGGERS
Timing triggers are divided into two parts:
BEFORE TRIGGER
- It fires before the DML statement is executed.
AFTER TRIGGER
- It fires after the DML statement has been executed.
To understand the above methodology, we need to consider the syntax: