Trigger is same like a stored procedures where we can write
SQL code and save it which gets executed automatically whenever an event takes
place on the associated database. We
cannot call triggers directly. There are 2 steps that are required for writing
Firstly we must mention when the trigger has to
be executed like. Such that this is considered as an event and checks the
trigger and whether it satisfies the given condition for which the trigger has
Now we have to mention the actions that has to
take place when the trigger is executed.
Uses of Triggers:
Triggers are used to maintain the complex
integrity constraints which are not possible at the time of table creation.
Automatically signalling the other programs that
an action needs to be take place when changes are made to a table.
If we want to implement any particular business
rules then triggers are used in that
Types of Triggers:
DML triggers will be executed automatically whenever a data
manipulation operations (insertion, deletion, updating) take place on the table
or view which is mentioned in the trigger. DML triggers are classified into two
INSTEAD OF trigger
After Trigger: These types of triggers are fired only after
the execution of DML operations and before the committing the data into the
database. We cannot apply after trigger
For example: Create an after trigger for insert operation. For table employee which has columns empno,
empfirstname, emplastname, deptno, experience.
CREATE TRIGGER inserttrigeer
set @ID = (select experience from inserted)
if(@ID <5) begin print ' Experience should be greater than 5' rollback end END GO Now if you execute insert statement insert into employee values (2,'abc','kil','p2',8) Then after the execution the after trigger is fired before the insert statement is committed and checks weather the experience is greater than 5 or not else it will print the message and rollback the transaction. INSTEADOF trigger: INSTEAD OF trigger is executed in place of insert, delete, and update. The actual insert, delete and update are not included in the table. For example: suppose if you have INSTEAD of trigger for insert operation. USE testing GO CREATE TRIGGER inserttrigeer ON employee INSTEAD OF INSERT AS declare @ID int set @ID = (select experience from inserted) BEGIN if(@ID >5)
print ‘ Experience is greater than 5′
Then if we give an insert statement
insert into employee values (4,’abc’,’kil’,’p2′,8)
so before the insert
statement is executed the instead of
trigger for insert is raised and it will execute in place of insert operation
and gives output as experience is greater than 5 and when you check the table
the row will not inserted.
Uses of DML Triggers:
DML triggers are same as constraints which
enforce the data integrity.
A normal constraint in the SQL gives a standard
system error message when an issue occurs. In order to customize the message
and to handle more complex error-handling DML triggers are used.
To perform multiple actions for the same event
we can create more than one trigger using DML triggers.
These type of triggers are fired when any of the DDL
statements are executed i.e.; DDL events occurred like when creation,
alteration, granting permission, denning permissions, dropping, Revoke
The scope of the DDL triggers:
We can create the DDL triggers in a particular database or
at the server level. So when you create a DDL trigger we must specify whether
it is database level or server level by ON DATABASE clause or ON ALL SERVER
clause respective. Database level means
when creating a table, procedure, functions and altering a table, procedure and
function in a database. Server level means when creating a login, database,
altering the database, login and dropping the login, granting the server etc.
Unlike the DML triggers DDL triggers doesn’t contain the
INSTEAD OF trigger. DDL trigger will not be executed if an event occurs such
that it has an impact on global temporary tables and procedures.
Uses of DDL Triggers:
By using DDL triggers we can place restrictions
on a user’s who are going to attempt to create, alter, and drop the database
Used to record the changes that take place in
the database schema.
Create a database scope DDL trigger
CREATE TRIGGER droptesting
PRINT ‘table cannot be
Now when we drop the table then the trigger will fire and
print the message table cannot be dropped and then drop command will rollback
and it doesn’t executed.