SlideShare a Scribd company logo
Sql server  ___________session_19(triggers)
 Microsoft SQL Server provides two primary
mechanisms for enforcing business rules and
data integrity: constraints and triggers. A
trigger is a special type of stored procedure
that automatically takes effect when a
language event executes. SQL Server
includes three general types of triggers: DML
triggers, DDL triggers, and logon triggers.
 DDL triggers are invoked when a data definition language
(DDL) event takes place in the server or database.
 DML triggers are invoked when a data manipulation
language (DML) event takes place in the database. DML
events include INSERT, UPDATE, or DELETE statements
that modify data in a specified table or view. A DML
trigger can query other tables and can include complex
Transact-SQL statements.The trigger and the statement
that fires it are treated as a single transaction, which can
be rolled back from within the trigger. If a severe error is
detected (for example, insufficient disk space), the entire
transaction automatically rolls back.
 They can cascade changes through related tables in the database;
however, these changes can be executed more efficiently using
cascading referential integrity constraints.
 They can guard against malicious or incorrect INSERT, UPDATE,
and DELETE operations and enforce other restrictions that are
more complex than those defined with CHECK constraints.
 Unlike CHECK constraints, DML triggers can reference columns in
other tables. For example, a trigger can use a SELECT from
another table to compare to the inserted or updated data and to
perform additional actions, such as modify the data or display a
user-defined error message.
 They can evaluate the state of a table before and after a data
modification and take actions based on that difference.
 Multiple DML triggers of the same type (INSERT, UPDATE, or
DELETE) on a table allow multiple, different actions to take place
in response to the same modification statement.
 AFTERTriggers
 AFTER triggers are executed after the action of the
INSERT, UPDATE, or DELETE statement is performed.
Specifying AFTER is the same as specifying FOR,
which is the only option available in earlier versions of
Microsoft SQL Server.AFTER triggers can be specified
only on tables.
 INSTEAD OFTriggers
 INSTEAD OF triggers are executed in place of the
usual triggering action. INSTEAD OF triggers can also
be defined on views with one or more base tables,
where they can extend the types of updates a view
can support.
TRIGGERS
1. when you create a trigger
you have to identify event
and action of your trigger.
2. trigger is run automatically
if the event is occurred.
3. within a trigger you can call
specific s.p.
STORED PROCEDURES
1. when you create s.p you
don't identify event and
action.
2. s.p don't run automatically
but you have to run it
manually.
3. within a s.p you cannot call
a trigger.
CREATETABLE Employee_Test
(
Emp_ID INT Identity,
Emp_nameVarchar(100),
Emp_Sal Decimal (10,2)
)
 INSERT INTO Employee_TestVALUES ('Anees',1000);
 INSERT INTO Employee_TestVALUES ('Rick',1200);
 INSERT INTO Employee_TestVALUES ('John',1100);
 INSERT INTO Employee_TestVALUES
('Stephen',1300);
 INSERT INTO Employee_TestVALUES ('Maria',1400);
 CREATETABLE Employee_Test_Audit
 (
 Emp_ID int,
 Emp_name varchar(100),
 Emp_Sal decimal (10,2),
 Audit_Action varchar(100),
 Audit_Timestamp datetime
 )
 CREATETRIGGER trgAfterInsert ON Employee_Test FOR INSERT
 AS declare @empid int;
 declare @empname varchar(100);
 declare @empsal decimal(10,2);
 declare @audit_action varchar(100);
 select @empid=i.Emp_ID from inserted i;
 select @empname=i.Emp_Name from inserted i;
 select @empsal=i.Emp_Sal from inserted i;
 set @audit_action='Inserted Record -- After InsertTrigger.';
 insert into Employee_Test_Audit
(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)
values(@empid,@empname,@empsal,@audit_action,getdate())
; PRINT 'AFTER INSERT trigger fired.‘
 CREATETRIGGER trgAfterUpdate ON Employee_Test
 FOR UPDATE AS declare @empid int;
 declare @empname varchar(100);
 declare @empsal decimal(10,2);
 declare @audit_action varchar(100);
 select @empid=i.Emp_ID from inserted i;
 if update(Emp_Name)
 select @empname=i.Emp_Name from inserted i;
 set @audit_action='Updated Record -- After UpdateTrigger.';
 if update(Emp_Sal)
 select @empsal=i.Emp_Sal from inserted i;
 set @audit_action='Updated Record -- After UpdateTrigger.';
 insert into Employee_Test_Audit
 values(@empid,@empname,@empsal,@audit_action,getdate())
; PRINT 'AFTER UPDATETrigger fired.'
 CREATETRIGGER trgAfterDelete ON Employee_Test AFTER
DELETE AS declare
 @empid int;
 declare @empname varchar(100);
 declare @empsal decimal(10,2);
 declare @audit_action varchar(100);
 select @empid=d.Emp_ID from deleted d;
 select @empname=d.Emp_Name from deleted d;
 select @empsal=d.Emp_Sal from deleted d;
 set @audit_action='Deleted -- After DeleteTrigger.';
 insert into Employee_Test_Audit
(Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp)
values(@empid,@empname,@empsal,@audit_action,getdate())
; PRINT 'AFTER DELETETRIGGER fired.'
 ALTERTABLE Employee_Test
{ENABLE|DISBALE}TRIGGER ALL
 ALTERTABLE Employee_Test DISABLE
TRIGGER trgAfterDelete

More Related Content

PPT
Lecture 4. MS SQL. DML Triggers
PPT
Review of SQL
PPTX
Triggers
PPTX
Using triggers in my sql database
PPTX
PDF
Trigger in DBMS
PPT
Database Triggers
PPT
Mca ii-dbms-u-v-transaction management
Lecture 4. MS SQL. DML Triggers
Review of SQL
Triggers
Using triggers in my sql database
Trigger in DBMS
Database Triggers
Mca ii-dbms-u-v-transaction management

What's hot (18)

PPT
Oracle Database Trigger
PPTX
trigger dbms
PDF
PL/SQL TRIGGERS
PPTX
Database Triggers
PDF
The Ultimate Guide to Oracle solaris 11 installation and configuration essent...
PPTX
Oracle: Cursors
ODP
Introduction to triggers
RTF
Trigger and cursor program using sql
PDF
Triggers and active database
PDF
The Ultimate Guide to Oracle web logic server 12c administration i 1z0 133
PPTX
Sql tutorial
PPTX
Cursors, triggers, procedures
PPTX
MySql: Queries
PPT
Sql dml & tcl 2
PPTX
Procedures and triggers in SQL
PDF
The Ultimate Guide to Oracle solaris 11 advanced system administration 1 z0 822
PPT
Oracle Database Trigger
trigger dbms
PL/SQL TRIGGERS
Database Triggers
The Ultimate Guide to Oracle solaris 11 installation and configuration essent...
Oracle: Cursors
Introduction to triggers
Trigger and cursor program using sql
Triggers and active database
The Ultimate Guide to Oracle web logic server 12c administration i 1z0 133
Sql tutorial
Cursors, triggers, procedures
MySql: Queries
Sql dml & tcl 2
Procedures and triggers in SQL
The Ultimate Guide to Oracle solaris 11 advanced system administration 1 z0 822
Ad

Viewers also liked (12)

PDF
Sql create table statement
PDF
Sql wksht-7
ODP
PDF
Part 15 triggerr
PDF
Sql update statement
PDF
Sql delete, truncate, drop statements
PPT
Introduction to-sql
RTF
Triggers-Sequences-SQL
PPTX
SQL - Structured query language introduction
PPTX
SQL Basics
PPT
Sql ppt
Sql create table statement
Sql wksht-7
Part 15 triggerr
Sql update statement
Sql delete, truncate, drop statements
Introduction to-sql
Triggers-Sequences-SQL
SQL - Structured query language introduction
SQL Basics
Sql ppt
Ad

Similar to Sql server ___________session_19(triggers) (20)

PPTX
PLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrj
PDF
Triggers in PL introduction yo database s
PPTX
triggers.pptx
PPTX
Relational Database Management System-- vivek singh
PPTX
triggersandactivedatabasesindatabases.pptx
PPTX
Lab07_Triggers.pptx
DOC
DOC
DOC
Oracle sql material
PPT
10 Creating Triggers
DOCX
PPTX
SQL interview questions jeetendra mandal - part 5
PDF
Assignment#08
PPT
PPT
Les08 (manipulating data)
PPTX
Block Programming - MySQL Triggers - adv topic
DOC
Subqueries views stored procedures_triggers_transactions
PPTX
DOODB_LAB.pptx
PPTX
DBMS: Week 12 - Triggers in MySQL Database Server
PPTX
T-SQL & Triggers
PLSQL.pptxokokokoo9oooodjdjfjfjfjrjejrjrrjrj
Triggers in PL introduction yo database s
triggers.pptx
Relational Database Management System-- vivek singh
triggersandactivedatabasesindatabases.pptx
Lab07_Triggers.pptx
Oracle sql material
10 Creating Triggers
SQL interview questions jeetendra mandal - part 5
Assignment#08
Les08 (manipulating data)
Block Programming - MySQL Triggers - adv topic
Subqueries views stored procedures_triggers_transactions
DOODB_LAB.pptx
DBMS: Week 12 - Triggers in MySQL Database Server
T-SQL & Triggers

More from Ehtisham Ali (17)

PPT
Android tutorial
PPTX
Sql server ___________session_20(ddl triggers)
PPTX
Sql server ___________session3-normailzation
PPTX
Sql server ___________session2-data_modeling
PPTX
Sql server ___________session_18(stored procedures)
PPTX
Sql server ___________session_17(indexes)
PPTX
Sql server ___________session_16(views)
PPTX
Sql server ___________session_15(data integrity)
PPTX
Sql server ___________session_11-12(joins)
PPTX
Sql server ___________session_10(group by clause)
PPT
Sql server ___________session_1-intro
PPT
Sql server ___________session 3(sql 2008)
PPT
Sql server ___________session 2(sql 2008)
PPT
Sql server ___________session 1(sql 2008)
PPTX
Sql server ___________data type of sql server
PPTX
Sql server ___________data control language
PPTX
Sql server ___________ (advance sql)
Android tutorial
Sql server ___________session_20(ddl triggers)
Sql server ___________session3-normailzation
Sql server ___________session2-data_modeling
Sql server ___________session_18(stored procedures)
Sql server ___________session_17(indexes)
Sql server ___________session_16(views)
Sql server ___________session_15(data integrity)
Sql server ___________session_11-12(joins)
Sql server ___________session_10(group by clause)
Sql server ___________session_1-intro
Sql server ___________session 3(sql 2008)
Sql server ___________session 2(sql 2008)
Sql server ___________session 1(sql 2008)
Sql server ___________data type of sql server
Sql server ___________data control language
Sql server ___________ (advance sql)

Recently uploaded (20)

PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Insiders guide to clinical Medicine.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Institutional Correction lecture only . . .
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Lesson notes of climatology university.
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
01-Introduction-to-Information-Management.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Cell Types and Its function , kingdom of life
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
102 student loan defaulters named and shamed – Is someone you know on the list?
Insiders guide to clinical Medicine.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Microbial diseases, their pathogenesis and prophylaxis
Institutional Correction lecture only . . .
Renaissance Architecture: A Journey from Faith to Humanism
Lesson notes of climatology university.
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
01-Introduction-to-Information-Management.pdf
Anesthesia in Laparoscopic Surgery in India
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Pharma ospi slides which help in ospi learning
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Basic Mud Logging Guide for educational purpose
Cell Types and Its function , kingdom of life
Abdominal Access Techniques with Prof. Dr. R K Mishra

Sql server ___________session_19(triggers)

  • 2.  Microsoft SQL Server provides two primary mechanisms for enforcing business rules and data integrity: constraints and triggers. A trigger is a special type of stored procedure that automatically takes effect when a language event executes. SQL Server includes three general types of triggers: DML triggers, DDL triggers, and logon triggers.
  • 3.  DDL triggers are invoked when a data definition language (DDL) event takes place in the server or database.  DML triggers are invoked when a data manipulation language (DML) event takes place in the database. DML events include INSERT, UPDATE, or DELETE statements that modify data in a specified table or view. A DML trigger can query other tables and can include complex Transact-SQL statements.The trigger and the statement that fires it are treated as a single transaction, which can be rolled back from within the trigger. If a severe error is detected (for example, insufficient disk space), the entire transaction automatically rolls back.
  • 4.  They can cascade changes through related tables in the database; however, these changes can be executed more efficiently using cascading referential integrity constraints.  They can guard against malicious or incorrect INSERT, UPDATE, and DELETE operations and enforce other restrictions that are more complex than those defined with CHECK constraints.  Unlike CHECK constraints, DML triggers can reference columns in other tables. For example, a trigger can use a SELECT from another table to compare to the inserted or updated data and to perform additional actions, such as modify the data or display a user-defined error message.  They can evaluate the state of a table before and after a data modification and take actions based on that difference.  Multiple DML triggers of the same type (INSERT, UPDATE, or DELETE) on a table allow multiple, different actions to take place in response to the same modification statement.
  • 5.  AFTERTriggers  AFTER triggers are executed after the action of the INSERT, UPDATE, or DELETE statement is performed. Specifying AFTER is the same as specifying FOR, which is the only option available in earlier versions of Microsoft SQL Server.AFTER triggers can be specified only on tables.  INSTEAD OFTriggers  INSTEAD OF triggers are executed in place of the usual triggering action. INSTEAD OF triggers can also be defined on views with one or more base tables, where they can extend the types of updates a view can support.
  • 6. TRIGGERS 1. when you create a trigger you have to identify event and action of your trigger. 2. trigger is run automatically if the event is occurred. 3. within a trigger you can call specific s.p. STORED PROCEDURES 1. when you create s.p you don't identify event and action. 2. s.p don't run automatically but you have to run it manually. 3. within a s.p you cannot call a trigger.
  • 7. CREATETABLE Employee_Test ( Emp_ID INT Identity, Emp_nameVarchar(100), Emp_Sal Decimal (10,2) )  INSERT INTO Employee_TestVALUES ('Anees',1000);  INSERT INTO Employee_TestVALUES ('Rick',1200);  INSERT INTO Employee_TestVALUES ('John',1100);  INSERT INTO Employee_TestVALUES ('Stephen',1300);  INSERT INTO Employee_TestVALUES ('Maria',1400);
  • 8.  CREATETABLE Employee_Test_Audit  (  Emp_ID int,  Emp_name varchar(100),  Emp_Sal decimal (10,2),  Audit_Action varchar(100),  Audit_Timestamp datetime  )
  • 9.  CREATETRIGGER trgAfterInsert ON Employee_Test FOR INSERT  AS declare @empid int;  declare @empname varchar(100);  declare @empsal decimal(10,2);  declare @audit_action varchar(100);  select @empid=i.Emp_ID from inserted i;  select @empname=i.Emp_Name from inserted i;  select @empsal=i.Emp_Sal from inserted i;  set @audit_action='Inserted Record -- After InsertTrigger.';  insert into Employee_Test_Audit (Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp) values(@empid,@empname,@empsal,@audit_action,getdate()) ; PRINT 'AFTER INSERT trigger fired.‘
  • 10.  CREATETRIGGER trgAfterUpdate ON Employee_Test  FOR UPDATE AS declare @empid int;  declare @empname varchar(100);  declare @empsal decimal(10,2);  declare @audit_action varchar(100);  select @empid=i.Emp_ID from inserted i;  if update(Emp_Name)  select @empname=i.Emp_Name from inserted i;  set @audit_action='Updated Record -- After UpdateTrigger.';  if update(Emp_Sal)  select @empsal=i.Emp_Sal from inserted i;  set @audit_action='Updated Record -- After UpdateTrigger.';  insert into Employee_Test_Audit  values(@empid,@empname,@empsal,@audit_action,getdate()) ; PRINT 'AFTER UPDATETrigger fired.'
  • 11.  CREATETRIGGER trgAfterDelete ON Employee_Test AFTER DELETE AS declare  @empid int;  declare @empname varchar(100);  declare @empsal decimal(10,2);  declare @audit_action varchar(100);  select @empid=d.Emp_ID from deleted d;  select @empname=d.Emp_Name from deleted d;  select @empsal=d.Emp_Sal from deleted d;  set @audit_action='Deleted -- After DeleteTrigger.';  insert into Employee_Test_Audit (Emp_ID,Emp_Name,Emp_Sal,Audit_Action,Audit_Timestamp) values(@empid,@empname,@empsal,@audit_action,getdate()) ; PRINT 'AFTER DELETETRIGGER fired.'
  • 12.  ALTERTABLE Employee_Test {ENABLE|DISBALE}TRIGGER ALL  ALTERTABLE Employee_Test DISABLE TRIGGER trgAfterDelete