Loading, please wait...

DML Triggers

How to use DML Triggers in SQL

 

Introduction:
In this article, you will learn types of DML triggers in SQL.

 

DML triggers are divided into two main types:

 

  • After Triggers
  • Instead Of Triggers

 

 

After Triggers

The SQL After trigger is the one, where our statement execution is successfully completed, Alter trigger fires. They are not supported for views.

 

After Triggers can be divided into three parts as:

 

  • After Insert Trigger
  • After Update Trigger
  • After Delete Trigger

 

First, we will create a new database and a new table.

 

 

 

  • After Insert Trigger

Step 1: After creating a table, we will create a trigger to insert the data.

 

 

 

Step 2: Insert some data in this table.

 

 

 

 

Step 3: The output is:

 

 

 

  • After Update Trigger

Step 1: Now, we will create a trigger to update the data.

 

 

 

Step 2: Here, we can see our command ran successfully.

 

 

 

Step 3: Thus, we will update the rows in this table.

 

 

 

 

  • After Delete Trigger

Step 1: Now, we will create a trigger to delete the data.

 

 

 

Step 2: Here, we can see our command ran successfully.

 

 

 

Instead Of Triggers

SQL Instead Of Trigger is the one, when our query starts execution, Instead Of Trigger fires. When we will perform Insert and Delete operation on Instead Of Trigger, it runs successfully but does not delete the data in the actual table.

 

Instead Of Triggers can be divided into three parts as:

 

  • Instead Of Insert Trigger
  • Instead Of Update Trigger
  • Instead Of Delete Trigger

 

 

  • Instead Of Insert Trigger

Step 1: After creating a table, we will create a trigger to insert the data.

 

 

 

Step 2: Here, we can see our command ran successfully.

 

 

 

Step 3: Now, we will insert the data into this table.

 

 

 

Step 4: The output is:

 

 

 

  • Instead Of Update Trigger

Step 1: Now, we will create a trigger to update the data.

 

 

 

Step 2: Here, we can see our command ran successfully.

 

 

 

Step 3: Now, we update the rows in this table.

 

 

 

  • Instead Of Delete Trigger

Step 1: Now, we will create a trigger to delete the data.

 

 

 

Step 2: Here, we can see our command ran successfully.

 

 

 

Summary:

Thus, we learned the use of After Tiggers and Instead Of Triggers in SQL.