0% found this document useful (0 votes)
13 views15 pages

SQL XLA Batch 43 Dec 21 Session 12 230PM

Triggers are special stored procedures in databases that automatically execute in response to events like INSERT, UPDATE, or DELETE on a table. They can be categorized into DML triggers (after and instead of) and DDL triggers, which respond to schema changes. Triggers utilize special tables, 'inserted' and 'deleted', to manage data changes during their execution.

Uploaded by

Apoorva Arora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views15 pages

SQL XLA Batch 43 Dec 21 Session 12 230PM

Triggers are special stored procedures in databases that automatically execute in response to events like INSERT, UPDATE, or DELETE on a table. They can be categorized into DML triggers (after and instead of) and DDL triggers, which respond to schema changes. Triggers utilize special tables, 'inserted' and 'deleted', to manage data changes during their execution.

Uploaded by

Apoorva Arora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

TRIGGERS

Triggers
Triggers are the special kind of stored procedures

which get automatically executed

whenever the event

INSERT UPDATE DELETE

on the under ling table occurs.


Triggers
A trigger is a database object that is attached to a table. Triggers are often
referred to as a "special kind of stored procedure”. The main difference
between a trigger and a stored procedure is that the trigger is attached
to a table and is only fired when an INSERT, UPDATE or DELETE occurs.
You specify the modification action(s) that fire the trigger when it is
created.
▪ Syntax

CREATE TRIGGER trigger_EID


ON table_EID
FOR INSERT|UPDATE |DELETE
AS
BEGIN
SQL Statements;
END;
Triggers

• A trigger is a special kind of Syntax :


stored procedure that Creating a DML after trigger :
automatically executes when an
event occurs in the database
server create trigger tr_showmessageafterinsert
• DML triggers are fired on dbo.emp after insert
automatically in response to DML as
events(Insert, update and delete) begin
print 'Row/Rows have been inserted'
• DML triggers are of two types
end
• After triggers
• Instead of triggers
Triggers – Inserted/Deleted tables

DML trigger statements use two special


tables: the deleted and inserted tables.
SQL Server automatically creates and Syntax :
manages these tables.
create trigger trg on emp after delete
Deleted Table : as
The deleted table stores copies of the begin
affected rows in the trigger table before select * from deleted
they were changed by a DELETE or end
UPDATE statement (the trigger table is delete from emp where empid=9
the table on which the DML trigger
runs). During the execution of a DELETE
or UPDATE statement, the affected rows
are first copied from the trigger table
and transferred to the deleted table.
Triggers – Inserted/Deleted tables
Deleted Table :

The inserted table stores copies of the Syntax :


new or changed rows after an INSERT or
UPDATE statement. During the execution create trigger trg on emp after insert
of an INSERT or UPDATE statement, the as
new or changed rows in the trigger table begin
are copied to the inserted table. The select * from inserted
rows in the inserted table are copies of end
the new or updated rows in the trigger
insert into emp values(9,'Saurabh','New
table.
Delhi')
ASSIGNMENT
A-1 : Create a trigger for inserting a record in the Fee_Detail table as soon as a
record is inserted into the student table
Student Table

Fee_Detail Table

Fee as per class


XII : 4000
XI:3000
X:2000
IX : 1000
Triggers
Example 1: Trigger to update the stock when product is sold.

CREATE TRIGGER TR_INVENT_UPDATE


ON SALES
FOR INSERT
AS
BEGIN
UPDATE INVENT SET StockQty = StockQty- (SELECT QTY FROM
INSERTED )
WHERE PID = (SELECT PID FROM INSERTED);
END;
DML instead of trigger
• Instead of triggers are
executed in place of insert,
update or delete operations
• Instead of insert trigger Syntax :
• Instead of update trigger To create the trigger
• Instead of delete trigger
• Instead of triggers are create trigger trg_checkbeforeinsert on emp
executed after the creation of instead of insert
inserted and deleted table as
• You can create multiple after begin
triggers however only one print 'You are not allowed to insert'
instead of trigger can be End
created on a table
• A DML trigger can be Alter trigger trg_checkbeforeinsert on emp
encrypted to hide its
definition by using ‘with with encryption
encryption’command instead of insert
as
• The actual insert, delete or
update operation does not begin
occur at all print 'You are not allowed to insert'
end
DDL Trigger

• DDL triggers execute when Syntax :


DDL events like create, Alter To create the trigger
and Drop statements occur in
create trigger trg_demo_create
the database or the server
on database for create_table
• DDL triggers can be used to as
prevent modifications in the begin
database schema (tables, print 'new table created'
views etc) end
• It can be used to execute
some message in response to
a DDL event
• Audit or check the changes
which users are making
DDL Trigger

• Many DDL events are Syntax :


available like create_table, To prevent any changes
alter_table, drop_table,
create_procedure,rename etc
• We can use Rollback alter trigger trg_demo_create
command to prevent DDL on database for create_table
events like creation, alter, as
drop etc begin
rollback
• There are two types of DDL
print 'you cannot create a table'
Triggers
End
• Database Scope
• Server Scope To Disable /enable a trigger:

disable trigger trg_demo_create on


database
enable trigger trg_demo_create on
database
Triggers
Example 2: Trigger to delete the order if the product Is deleted from the
inventory.

CREATE TRIGGER TR_SALE_DELETE


ON INVENT
FOR DELETE
AS
BEGIN
DELETE FROM SALES WHERE PID = (SELECT PID FROM DELETED);
END;
DDL Trigger – Server Scope

Creating a trigger Syntax :


create trigger trg_demo_create
on all server for create_table Altering a trigger
as
begin
print 'you have created a alter trigger trg_demo_create
table' on all server for create_table
End as
begin
To disable : rollback
disable trigger print 'you are not allowed to create a
trg_demo_create on all server table'
To Enable : End
enable trigger trg_demo_create
on all server
Droping a trigger
drop trigger trg_demo_create on all
server
Triggers
Example 4: Trigger to check & update the stock when the order is placed

CREATE TRIGGER TR_INVENT_CHECK


ON SALES
FOR INSERT
AS
BEGIN
DECLARE @QS AS INT;
DECLARE @QR AS INT;
SET @QR= ( SELECT QTY FROM INSERTED);
SET @QS = (SELECT StockQty FROM INVENT WHERE PID=(SELECT PID FROM inserted));
IF @QS >= @QR
Begin
UPDATE INVENT SET StockQty = StockQty- (SELECT QTY FROM INSERTED )
WHERE PID = (SELECT PID FROM INSERTED);
COMMIT;
end
ELSE
ROLLBACK;
END;
Thanks!

You might also like