×

Trigger in DBMS

Trigger in DBMS: A trigger is a procedure of SQL statements, which is automatically fired when the DML statements are executed on the table of the database. Triggers are the event-driven procedures, which are managed and stored by the database management system.

Trigger helps in maintaining data integrity by changing the database data in a systematic way. Triggers are always associated with the insert or update or delete command of the database table.

Syntax of Trigger:

CREATE [OR REPLACE ] TRIGGER trigger_name
{ BEFORE | AFTER | INSTEAD OF }
{ INSERT [ OR ] | UPDATE [ OR ] | DELETE}
[OF name_of_column]
ON name_of_the_table 
REFERENCING [ OLD AS old | NEW AS new ] 
[ FOR EACH ROW | FOR EACH STATEMENT ]  
WHEN (condition)
DECLARE
[ declaration_section ]  
 BEGIN   
  --- sql statements 
  END;

Explanation of each statement is described below:

CREATE [OR REPLACE ] TRIGGER trigger_name:

This statement in the syntax creates a trigger in the database with the specified name. It also overwrites the trigger, which exists with the same name.

{ BEFORE | AFTER | INSTEAD OF }:

BEFORE or AFTER statement in the syntax indicates at what time the trigger is fired on the table.

For example, users can use Trigger Before or After inserting data into the table, Before or After updating the data of the table, Before or After deleting the data from the table. INSTEAD OF is a statement, which is used for creating the Trigger on the view. 

{ INSERT [ OR ] | UPDATE [ OR ] | DELETE }:

This statement in the syntax specifies the DML operations. 

[OF col_name] :

This statement in the syntax is only used when the update operation is triggered.

ON table_name  :

This statement in the syntax specifies the table name on which DML operation is to be applied.

REFERENCING [ OLD AS old | NEW AS new ]  :

It is a statement that allows the user to provide the new value by replacing the old value. By default, we have to reference the values in the following form:

:old.column_name

:new.column_name

[ FOR EACH ROW  ]:

This statement in the syntax specifies the row-level triggered.

WHEN (condition):

It is a clause that specifies the condition to be applied to the database data. It is triggered only for those rows which satisfy the condition.

Declare, Begin Body :

Both statements contain the queries and SQL statements to be executed when the trigger is called.

Types of Trigger

The trigger can be categorized into the following three types:

1. Statement Level Trigger

2. Row Level Trigger

3. Before Trigger

4. After Trigger

Row Level Trigger

Row-level Trigger is that trigger that executes once for each table row. This type of trigger always uses the FOR EACH ROW clause in the trigger procedure.

Statement Level Trigger

Statement Level Trigger is that trigger that executes only one time for each statement. In this type of trigger, FOR EACH ROW clause is not present, i.e., it is omitted.

Before Trigger

'Before Trigger' is called before the execution of the DML operations on the database table.

After Trigger

'After Trigger' is called or fired after the execution of DML operations on the database table.


Related Topics

ACID Properties in DBMS

A transaction in a database has the following four properties, known as ACID properties. These properties are used to maintain the consistency of the database in the case of system failure and concurrent...

4 minutes read.

Checkpoint in DBMS

A checkpoint in DBMS is used to define a point of the transaction that is in a consistent state and then all the transactions will be in the committed state...

4 minutes read.

Query processing in DBMS

Operations on DBMSs are made much simpler, more organized, and methodical with SQL. These are not only simpler for people to comprehend, but after gaining a basic comprehension of them, they...

4 minutes read.

DBMS Tutorial | Database Management System

Database Management System (DBMS) tutorial is all about managing and maintaining the data effectively. Our DBMS tutorial is designed for beginners as well as professionals. What is Data? Data is a real-world...

5 minutes read.

Advantages of Database Management System in DBMS

A database management system (DBMS) is a software tool that provides an interface for managing data stored in a database. Some advantages of using a DBMS include: data integration, data...

3 minutes read.

Types of Data Abstraction in DBMS

What Is Data Abstraction? To ship an email, you want to know the address. But to send the email, you don't need to see where the email is physically stored. You...

6 minutes read.

Query optimization in DBMS

Choosing an effective execution strategy to execute a query is a process known as query optimization. After the decision-making process of query parsing, which determines how many distinct ways a given...

5 minutes read.

Redundancy in Database Management System

Redundancy in a database management system (DBMS) refers to the duplication of data within the database. This duplication can occur in multiple ways, such as having multiple copies of the...

13 minutes read.

Relational DBMS Concepts

What is RDBMS? RDBMS stands for Relational Database Management System.The relational database management system is a type of DBMS that stores information in the form of related tables and uses a...

3 minutes read.

Relational Algebra in DBMS

Relational Algebra is a widely used procedural query language, which takes instances of one or more relation as an input and generates a new relation as an output. It uses a different...

6 minutes read.

Network model in DBMS

Network model: The many-to-many relationship between the database constraints is represented hierarchically by the Network Model in DBMS. It is a straightforward and straightforward database model. Due to the Network Model...

4 minutes read.

Data Abstraction in DBMS

Introduction Data abstraction is a fundamental concept in database management systems (DBMS). It refers to the process of hiding the details of how data is stored and retrieved from the user,...

3 minutes read.

Candidate Key in DBMS

Candidate key in DBMS: The candidate key is a single column or the set of columns that uniquely identifies the rows of data in the database table. It is a...

2 minutes read.

Aggregate Functions in DBMS

Aggregate Functions in DBMS: Aggregate functions are those functions in the DBMS which takes the values of multiple rows of a single column and then form a single value by...

2 minutes read.

Functional Dependencies

Functional Dependencies (FD) in the relational database management system occurs when one attribute in a relation uniquely determines other attribute in that relation. It describes the relation between the attributes. The term functional...

2 minutes read.

Primary Key in DBMS

Primary Key in DBMS: A primary key is the minimal set of columns in the database table, which uniquely identifies each row or tuple in that table. Any table in...

2 minutes read.

Three Schema Architecture of DBMS

The three schema architecture describes how the data is represented or viewed by the user in the database. This architecture is also known as three-level architecture and is sometimes called...

3 minutes read.

Disavantages of RDBMS

RDBMS offers many features when handling the data, but it also has certain limitations that can be overcome by choosing an alternative data model. The limitations of the Relational Data...

3 minutes read.

B+ (Plus) Tree in DBMS

What is a B+ tree? The B+ tree is known as a balanced binary search tree. in this tree, we can store the data in the form of nodes. B+ tree...

9 minutes read.

How to draw ER-Diagram in DBMS?

Entity Relationship Diagrams, or ERDs, are diagrams that assist you in visualizing your database design. An ERD, also called anER diagram or ER model,describes data and how parts of the data interact. ERDs are crucial in database architecture and projects that call for...

9 minutes read.