Introduction to Triggers in SQL Server

Introduction

Tiggers are commonly used in SQL Server to automate data updates. This introduction article explains the basics of triggers in SQL, the types of triggers, and how to implement triggers in SQL Server.

What is Trigger in SQL Server?

A SQL trigger is a database object which fires when an event occurs in a database. For example, we can execute a SQL query that will "do something" in a database when a change occurs on a database table, such as when a record is inserted, updated, or deleted. For example, a trigger can be set on a record insert in a database table. For example, if you want to increase the blogs count in the Reports table when a new record is inserted in the Blogs table, we can create a trigger on the Blogs table on INSERT and update the Reports table by increasing the blog count to 1. 

Types of Triggers in SQL Server

There are two types of triggers:

  1. DDL Trigger
  2. DML Trigger

DDL Triggers in SQL Server 

The DDL triggers are fired in response to DDL (Data Definition Language) command events that start with Create, Alter, and Drop, such as Create_table, Create_view, drop_table, Drop_view, and Alter_table.

Code of a DDL Trigger

create trigger saftey  
on database  
for  
create_table,alter_table,drop_table  
as  
print'you can not create ,drop and alter table in this database'  
rollback;

When we create, alter, or drop any table in a database, then the following message appears,

DDl-trigers-in-sql.jpg

DML Triggers in SQL Server

The DML triggers are fired in response to DML (Data Manipulation Language) command events that start with Insert, Update, and Delete. Like insert_table, Update_view and Delete_table.

create trigger deep  
on emp  
for  
insert,update,delete  
as  
print'you can not insert,update and delete this table i'  
rollback;

When we insert, update, or delete a table in a database, then the following message appears,

dml-triggers-in-sql.jpg

Types of DML triggers

There are two types of DML Triggers in SQL Server.

AFTER Triggers

AFTER triggers are executed after the action of an INSERT, UPDATE, or DELETE statement.

create trigger insertt  
on emp  
after insert  
as  
begin  
insert into empstatus values('active')  
end 

aftertrigger-in-sql.jpg

INSTEAD Of Triggers

It will tell the database engine to execute the trigger instead of the statement. For example, an insert trigger executes when an event occurs instead of the statement that would insert the values in the table. 

CREATE TRIGGER instoftr  
ON v11  
INSTEAD OF INSERT  
AS  
BEGIN  
INSERT INTO emp  
SELECT I.id, I.names  
FROM INSERTED I  
   
INSERT INTO emp1values  
SELECT I.id1, I.name1  
FROM INSERTED I  
END

When we insert data into a view by the following query, it inserts values in both tables.

insert into v11 values(1,'d','dd') 

You can see both tables by the following query.

select * from emp  
select * from emp1values

insteedofcorsor-in-sql.jpg

Summary

In this article, you learn about triggers in SQL and how to implement triggers in SQL Server. 

 

 

Similar Articles