Trigger and Procedure in
DBMS
Lecture 9
What are DML Triggers?
• Triggersare special stored procedures that
execute when events occur. DML triggers fire on
INSERT, UPDATE, DELETE.
Triggers
• A trigger is a special kind of procedure that executes only
when some triggering event such as INSERT, UPDATE, or
DELETE operations occur in a table.
• Advantages of Triggers:
• Protection of data
• Inhibits transactions that are not valid
• It also keeps the different tables in sync.
• Referential integrity is enforced with the use of triggers.
• Triggers can also be used in event logging and auditing.
AFTER Triggers vs. INSTEAD OF Triggers
• AFTER triggers
Fire after the event that they relate to
Are treated as part of the same transaction as the statement
that triggered them
Can roll back the statement that triggered them (and any
transaction that statement was part of)
• INSTEAD OF triggers
Allow executing alternate code – unlike a BEFORE trigger in
other database engines
Are often used to create updatable views with more than one
base table
• Both can be implemented in managed code or T-SQL
Inserted and Deleted Virtual Tables
• inserted and deleted Virtual Tables
Allow us to access the state of the data before and after the
modification began
Virtual tables are often joined to the modified table data
Available in both AFTER and INSTEAD OF triggers
Statement inserted deleted
INSERT rows just inserted
DELETE rows just deleted
UPDATE modified row contents original row contents
AFTER INSERT Triggers
• INSERT statement is executed
• AFTER INSERT trigger then fires
• Ensure multi-row INSERTs are supported
CREATE
CREATE TRIGGER
TRIGGER TR_Opportunity_Insert
TR_Opportunity_Insert
ON
ON Sales.Opportunity
Sales.Opportunity
AFTER
AFTER INSERT
INSERT AS AS
BEGIN
BEGIN
SET
SET NOCOUNT
NOCOUNT ON; ON;
INSERT
INSERT INTO
INTO Sales.OpportunityAudit
Sales.OpportunityAudit
(OpportunityID,
(OpportunityID, ActionPerformed,
ActionPerformed, ActionOccurredAt)
ActionOccurredAt)
SELECT
SELECT i.OpportunityID,
i.OpportunityID,
'I',
'I',
SYSDATETIME()
SYSDATETIME()
FROM
FROM inserted
inserted ASAS i;
i;
END;
END;
AFTER DELETE Triggers
• DELETE statement is executed
• AFTER DELETE trigger then fires
CREATE
CREATE TRIGGER
TRIGGER TR_Category_Delete
TR_Category_Delete
ON
ON Product.Category
Product.Category
AFTER
AFTER DELETE
DELETE AS
AS
BEGIN
BEGIN
SET
SET NOCOUNT
NOCOUNT ON;
ON;
UPDATE
UPDATE p p SET
SET p.Discontinued
p.Discontinued =
=11
FROM
FROM Product.Product
Product.Product ASAS p
p
INNER
INNER JOIN
JOIN deleted
deleted as
as d
d
ON
ON p.CategoryID
p.CategoryID == d.CategoryID;
d.CategoryID;
END;
END;
GO
GO
AFTER UPDATE Triggers
• UPDATE statement is executed
• AFTER UPDATE trigger then fires
CREATE
CREATE TRIGGER
TRIGGER TR_ProductReview_Update
TR_ProductReview_Update
ON
ON Product.ProductReview
Product.ProductReview
AFTER
AFTER UPDATE
UPDATE AS AS
BEGIN
BEGIN
SET
SET NOCOUNT
NOCOUNT ON;ON;
UPDATE
UPDATE pr pr
SET
SET Product.ProductReview.ModifiedDate
Product.ProductReview.ModifiedDate = = SYSDATETIME()
SYSDATETIME()
FROM
FROM Product.ProductReview
Product.ProductReview AS AS pr
pr
INNER
INNER JOIN
JOIN inserted
inserted AS
AS ii
ON
ON i.ProductReviewID
i.ProductReviewID = = pr.ProductReviewID;
pr.ProductReviewID;
END;
END;
How Nested Triggers Work
INSERT, UPDATE, or
1 DELETE statement
3…and so on…
2
Trigger executes
INSERT, UPDATE,
or DELETE on
another table…
Procedures
• A procedure is a combination of SQL statements written to
perform specified tasks. It helps in code re-usability and
saves time and lines of code.
• Advantages of Procedures:
• A Stored Procedure can be used as modular programming,
which means that it can be created once, stored, and
called multiple times as needed. This allows for speedier
execution.
• Reduces network traffic
• Improving data security
• Easy to maintain because the stored procedure scripts are
all in one place and hence, it is easy to update and track
dependencies when schema changes occur.
• Testing can be carried out independent of the application.
Triggers & Stored procedures
No. Key Triggers Stored procedures
1 Basic trigger is a stored procedure Stored procedures are a pieces
that runs automatically of the code in written in PL/SQL
when various events happen to do some specific task
(eg update, insert, delete)
2 Running It can execute automatically It can be invoked explicitly by
Methodology based on the events the user
3 Parameter It can not take input as It can take input as a
parameter parameter
4 Transaction we can't use transaction We can use transaction
statements statements inside a trigger statements like begin
transaction, commit transaction,
and rollback inside a stored
procedure
5 Return Triggers can not return Stored procedures can return
values values
Triggers & Stored Procedures
• Stored procedures are a pieces of the code in written in PL/SQL to do
some specific task. Stored procedures can be invoked explicitly by the
user. It's like a java program , it can take some input as a parameter then
can do some processing and can return values.
• Trigger is a stored procedure that runs automatically when various events
happen (eg update, insert, delete). Triggers are more like an event handler
they run at the specific event. Trigger can not take input and they can’t
return values.