You are on page 1of 55

SQL Triggers

Prepared by:
Rahim Suwal (29)
Shyam Rajak (40)
Triggers
● Triggers are the special types of logic that are ran
automatically by the database whenever a event
occurs.
● The events are insert, update and delete etc.

● It is a result of some events, they can’t be run just by


themselves. It can be after or before.
An Analogy for Triggers

The event(insert, update etc. ) is a


rubber ball that you throw. The trigger
is a dog that chases after a thrown ball.
Use of Triggers
● Log database activity.

● To access both old and changed values on insert,


update, delete operation.
● Enforce referential integrity. Example: When you
delete a customer, you can use a trigger to delete
corresponding rows in the orders table. Etc.
Types of Triggers
There are mainly two types of triggers, they are:
1. Data Manipulation Language (DML) triggers

2. Data Definition Language (DDL) triggers


DML Triggers
A DML trigger is used when the tables are affected by the DML
statement, as INSERT, UPDATE and DELETE. They help user in
maintaining the consistent, reliable, and proper data in the tables.
Two temporary tables are created which are known as magic tables.
They are known as inserted or deleted. The structure of these tables
is similar to the database tables.
Cont.…

INSERT trigger – The trigger is fired when the attempt is made to


insert a row in the trigger table. After the INSERT statement is
executed the new row is added to the inserted table.
DELETE trigger – It is fired when the attempt is made to delete
the row in the trigger table. The deleted rows are added to the
deleted table.
Cont.…

UPDATE trigger – It is fired when the UPDATE statement is


executed in the trigger table. Two logic tables are used for the
operations performed by the triggers. The deleted table contains the
original rows and the inserted table contains the new table.
DDL Triggers
DDL triggers run when events such as creating,
altering or dropping and object occurs on the server.
Used for data base administration tasks such as
auditing and controlling object access.
Triggers can get fired in TWO MODES
1. For/After Trigger:
• It gets fired only after the SQL server completes all
actions successfully on a specified table.
• Can rollback all the earlier transactions by issuing
ROLLBACK
• For example on inserting a row a table, the trigger defined
on the INSERT operation fires only after the row gets
successfully inserted and if the insert fails SQL does not
execute the trigger.
Cont.…
2. Instead of Trigger:
• It causes the code present in the trigger to execute
instead of the operation that caused the trigger to
fire.
• If we defined INSTEAD OF Trigger on the above
mentioned table, instead would not happen but the
trigger gets fired.
Syntax of Trigger
DML Trigger
CREATE TRIGGER trigger_name
ON { Table_name}
Before/after (update/delete/insert) on table name
Begin
<code>
End;
Syntax of Trigger
DDL Trigger
CREATE TRIGGER trigger_name
ON { ALL SERVER|DATABASE }
FOR { event_type | event_group }
AS
Begin
Code Trigger body
End
Drawbacks of Triggers
● It is easy to view table relationships , constraints, indexes, stored procedure in
database but triggers are difficult to view.
● Triggers execute invisible to client-application application. They are not
visible or can be traced in debugging code.
● It is hard to follow their logic as it they can be fired before or after the
database insert/update happens.
● It is easy to forget about triggers and if there is no documentation it will be
difficult to figure out for new developers for their existence.
● Triggers run every time when the database fields are updated and it is
overhead on system. It makes system run slower.
Basic Example
use of Trigger for log activity

Demo 1
GUI based
Demo 2 GUI Based
Use of Inserted and deleted tables
Summary of Demo 2
● Triggers are nothing but logic, simple SQL
codes which are fired after/before some events.
● 2 temporary table: Inserted table and deleted
table.
● Inserted table have new value and deleted
have old.
Disable Triggers

Syntax

DISABLE TRIGGER trigger_name


ON Table name
Enable Triggers

Syntax

ENABLE TRIGGER trigger_name


ON Table name
Drop Triggers

For DML triggers


Syntax

DROP TRIGGER [if exists] trigger_name


Drop Triggers

For DDL triggers


Syntax

DROP TRIGGER [if exists] trigger_name


ON {Database}
Reference

https://www.sqlservertutorial.net/sql-server-triggers/
https://www.geeksforgeeks.org/sql-trigger-student-database/
https://docs.microsoft.com/en-us/sql/t-sql/statements/create-trig
ger-transact-sql?view=sql-server-ver15

https://www.essentialsql.com/what-is-a-database-trigger/
Thank you

You might also like