×

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

File Organization in DBMS

File Organization in DBMS: A database contains a huge amount of data, which is stored is in the physical memory in the form of files. A file is a set...

9 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.

Evaluating DBMS

There are various database management systems available in the market. Each type has its features and can be used for varied purposes. The large number of DBMS makes it difficult...

4 minutes read.

Schedule in DBMS

Schedule A sequence of statements that specify the sequential order in which the statements of concurrent transactions are executed. The transaction will get committed when it executes the instructions successfully without any...

4 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.

B-Tree Insertion in DBMS

A B-tree is a special type of m-way tree, commonly used for disk access. A B-tree of order m can have at most m-1 keys and m descendants. B-trees are...

4 minutes read.

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.

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.

What is Advanced Database Management System

Before diving into the Advance Database management system, we need to learn about the database management system and its usage. Database management system Database Management System or DBMS in short implies the...

3 minutes read.

Components of an ER Diagram

An ER Diagram consists of the following components: Entity AttributesRelationships 1. Entity An entity may be an object, place, person, or an event which stores data in the database. In an entity-relationship diagram, an entity...

4 minutes read.

ER Diagram for Banking System in DBMS

Entity Relationship Diagram Entity Relationship Diagrams, or ER Diagrams for short, are diagrams that show the relationships among entity sets that are stored in databases. Alternatively said, ER diagrams assist in...

3 minutes read.

Specialization in DBMS

Specialization in DBMS In the database management system, specialization breaks the higher-level entity into two or more than two lower entities. The main motive of this concept is to share the...

2 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 Tree in DBMS

How to perform Insertion operation in B Tree A new value is inserted at the leaf node. Same like in a binary search tree we traverse from starting root node to...

10 minutes read.

Codds Rule of DBMS

Codd’s Rule of DBMS Database having certain constraints and tables, need not to be a relational database system always. For perfect database management system, there are certain rules for the database,...

3 minutes read.

Applications of DBMS

There are various fields where a database management system is used. Following are some applications which make use of the database management system: 1. Railway Reservation System: In the railway reservation...

7 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.

Levels of Abstraction in DBMS

Data abstraction is a way to hide unwanted or irrelevant information from the end user in DBMS. It helps in enhancing the security of data, and simplifies database design using...

4 minutes read.

DBMS Architecture

This is the first step when creating a database management system. The architecture of a database management system plays an important role in determining the actual design and layout of...

4 minutes read.

Advantages of RDBMS

The relational database management system collects related data stored in tabular format. The data is stored in rows and columns, where rows usually represent the individual entity of the collected...

6 minutes read.