Professional Documents
Culture Documents
11/18/21 DBS_Trigger 2
Overview of triggers
A triggers is always defined on a table and to
be fired whenever the data in the underlying
table is affected by any of the Data
Manipulation Language (DML) – e.g.
INSERT, UPDATE, or DELETE statements.
Trigger helps in maintaining consistency,
reliability, and correctness of the data. It
enables complex actions to be performed and
cascade the actions to other dependent
tables.
11/18/21 DBS_Trigger 3
Characteristics of trigger (1)
11/18/21 DBS_Trigger 4
Characteristics of trigger (2)
11/18/21 DBS_Trigger 5
Creating trigger
Create
create trigger trigger_name on table_name
{[for] after | instead of} {insert, update,
delete}
as
statement_block
Drop
drop trigger trigger_name
11/18/21 DBS_Trigger 6
Creating Trigger: trigger class types
(1)
Trigger class:
AFTER
After trigger is the default trigger class. This
trigger gets executed automatically after the
statement that triggered it completes. The AFTER
clause is synonym with FOR clause. You cannot
used both clause.
E.g. AFTER UPDATE or
FOR UPDATE
11/18/21 DBS_Trigger 7
Execution order after triggers
2. delete/insert/update is executed
11/18/21 DBS_Trigger 8
When is an AFTER Trigger Fired?
11/18/21 DBS_Trigger 9
Creating Trigger: trigger class types
(2)
Trigger class:
INSTEAD OF
Is the trigger which is executed in place of the
triggering statement. For example, a delete action
to a table containing INSTEAD OF DELETE
trigger will run the trigger rather than the delete
statement.
create trigger trigger_name on
table_name
instead of delete
as
statement_block
11/18/21 DBS_Trigger 10
Creating trigger: trigger types
INSERT
An insert trigger is a trigger which is fired in
response to the insert-row action made in the table
where the trigger belongs.
UPDATE
An update trigger is a trigger which is fired
whenever an attempt is made to update row(s) in
the triggered table.
DELETE
A delete trigger is a trigger which is fired whenever
an attempt is made to delete row(s) from the
triggered table.
11/18/21 DBS_Trigger 11
Trigger restriction
Trigger cannot be created on system table or
temporary table, although the transact-SQL
language within the trigger can reference temporary
tables or system tables.
Only INSTEAD OF trigger can be created on VIEW
INSTEAD OF DELETE and INSTEAD OF UPDATE
triggers cannot be defined on tables that have
corresponding ON DELETE or ON UPDATE
cascading referential integrity defined.
11/18/21 DBS_Trigger 12
Triggers
Used for:
Auditing
Controlling denormalization
Drawbacks of triggers:
Non standard
Complex coding
reaction
11/18/21 DBS_Trigger 13
The inserted and deleted pseudo table
When an INSERT, UPDATE, or DELETE
trigger fired, the event creates one or more
pseudo tables (also known as logical table).
These logical tables can thought of as the
transaction logs of the event.
There are two types of logical tables: the
INSERTED and DELETED table.
An insert or update creates INSERTED Table
An update or delete creates DELETED tables
11/18/21 DBS_Trigger 14
Inserted and Deleted
DML after trigger has read-access on the before and after images of
the data. Trigger can make comparisons, calculations, and (if
necessary) undo the changes (rollback).
• identical in structure to the table on which the trigger is defined
• deleted holds the before versions of the data
• inserted holds the after versions of the data
11/18/21 DBS_Trigger 16
The inserted and deleted pseudo table:
example (2)
11/18/21 DBS_Trigger 17
After Trigger Example
create trigger emp_del_trigger
on employee
after delete
as
begin
declare @rowc int
set @rowc = @@rowcount
if @rowc != 1 begin rollback tran return end
11/18/21 DBS_Trigger 18
After Trigger Example
create trigger emp_upd_trigger
on employee
after update, insert
as
begin
declare @rowc int
set @rowc = @@rowcount
if @rowc != 1 begin rollback tran return end
11/18/21 DBS_Trigger 19
INSERT trigger: example
create trigger trgInsertCity
on City
for insert
as
if (select count(*) from country, Inserted
where country.countryid = inserted.countryid) !=@@rowcount
/* Cancel the insert and print a message.*/
begin
rollback transaction
print 'Cannot add data, referential integrity abused
titles. No such country id in Country'
end
/* Otherwise, allow it. */
else
print 'Added!'
11/18/21 DBS_Trigger 20
Insert Trigger
Insert-Trigger is fired once after every
INSERT:
CREATE TRIGGER trg_test_ins
ON test
AFTER INSERT
AS
BEGIN
PRINT 'Insert of 0, 1 or many records identified'
END
11/18/21 DBS_Trigger 21
@@ROWCOUNT
System function @@ROWCOUNT
Number of rows processed by immediately preceding
command
Try:
select * from sys.objects
select @@rowcount
go
select * from sys.objects
if @@rowcount > 0
print 'Then-part: rowcount = ' + cast(@@rowcount
as varchar(10))
else
Test for modification at the beginning of a trigger useful
print 'Else-part: rowcount = 0'
for performance reasons
11/18/21 DBS_Trigger 22
UPDATE trigger: example
CREATE TRIGGER trgUpdateOrderID
ON Orders
FOR UPDATE
AS
IF UPDATE(OrderId)
BEGIN
RAISERROR('OrderID cannot be modified',12,2)
ROLLBACK TRAN
END
11/18/21 DBS_Trigger 23
DELETE trigger: example
USE PUBS
11/18/21 DBS_Trigger 24
INSTEAD OF TRIGGER: example
Use Test
create trigger trgIOD_Test1
on Test1
instead of delete
as
BEGIN
print 'you cannot delete data from table 1'
END
INSTEAD OF DELETE triggers cannot be defined on a table that has a
foreign key defined with a DELETE action.
11/18/21 DBS_Trigger 25
Altering trigger
Altering trigger
ALTER TRIGGER trigger_name
ON table_name
[FOR] trigger_class && trigger type(s)
[WITH ENCRYPTION]
AS
sql_statement
11/18/21 DBS_Trigger 26
Trigger in MySQL
The general syntax of CREATE TRIGGER is :
CREATE TRIGGER trigger_name
trigger_time trigger_event
ON tbl_name
FOR EACH ROW
trigger_statement
11/18/21 DBS_Trigger 27