SlideShare a Scribd company logo
1 of 12
 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

What's hot

The Ultimate Guide to Oracle solaris 11 installation and configuration essent...
The Ultimate Guide to Oracle solaris 11 installation and configuration essent...The Ultimate Guide to Oracle solaris 11 installation and configuration essent...
The Ultimate Guide to Oracle solaris 11 installation and configuration essent...SoniaSrivastva
 
Trigger and cursor program using sql
Trigger and cursor program using sqlTrigger and cursor program using sql
Trigger and cursor program using sqlSushil Mishra
 
The Ultimate Guide to Oracle web logic server 12c administration i 1z0 133
 The Ultimate Guide to Oracle web logic server 12c  administration i  1z0 133 The Ultimate Guide to Oracle web logic server 12c  administration i  1z0 133
The Ultimate Guide to Oracle web logic server 12c administration i 1z0 133SoniaSrivastva
 
Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, proceduresVaibhav Kathuria
 
Procedures and triggers in SQL
Procedures and triggers in SQLProcedures and triggers in SQL
Procedures and triggers in SQLVikash Sharma
 
The Ultimate Guide to Oracle solaris 11 advanced system administration 1 z0 822
The Ultimate Guide to Oracle solaris 11 advanced system administration  1 z0 822The Ultimate Guide to Oracle solaris 11 advanced system administration  1 z0 822
The Ultimate Guide to Oracle solaris 11 advanced system administration 1 z0 822SoniaSrivastva
 

What's hot (18)

Oracle Database Trigger
Oracle Database TriggerOracle Database Trigger
Oracle Database Trigger
 
trigger dbms
trigger dbmstrigger dbms
trigger dbms
 
PL/SQL TRIGGERS
PL/SQL TRIGGERSPL/SQL TRIGGERS
PL/SQL TRIGGERS
 
Database Triggers
Database TriggersDatabase Triggers
Database Triggers
 
The Ultimate Guide to Oracle solaris 11 installation and configuration essent...
The Ultimate Guide to Oracle solaris 11 installation and configuration essent...The Ultimate Guide to Oracle solaris 11 installation and configuration essent...
The Ultimate Guide to Oracle solaris 11 installation and configuration essent...
 
SQL
SQLSQL
SQL
 
Oracle: Cursors
Oracle: CursorsOracle: Cursors
Oracle: Cursors
 
Introduction to triggers
Introduction to triggersIntroduction to triggers
Introduction to triggers
 
Trigger and cursor program using sql
Trigger and cursor program using sqlTrigger and cursor program using sql
Trigger and cursor program using sql
 
Triggers and active database
Triggers and active databaseTriggers and active database
Triggers and active database
 
The Ultimate Guide to Oracle web logic server 12c administration i 1z0 133
 The Ultimate Guide to Oracle web logic server 12c  administration i  1z0 133 The Ultimate Guide to Oracle web logic server 12c  administration i  1z0 133
The Ultimate Guide to Oracle web logic server 12c administration i 1z0 133
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, procedures
 
MySql: Queries
MySql: QueriesMySql: Queries
MySql: Queries
 
Sql dml & tcl 2
Sql   dml & tcl 2Sql   dml & tcl 2
Sql dml & tcl 2
 
Procedures and triggers in SQL
Procedures and triggers in SQLProcedures and triggers in SQL
Procedures and triggers in SQL
 
The Ultimate Guide to Oracle solaris 11 advanced system administration 1 z0 822
The Ultimate Guide to Oracle solaris 11 advanced system administration  1 z0 822The Ultimate Guide to Oracle solaris 11 advanced system administration  1 z0 822
The Ultimate Guide to Oracle solaris 11 advanced system administration 1 z0 822
 
Les13
Les13Les13
Les13
 

Viewers also liked (12)

Sql create table statement
Sql create table statementSql create table statement
Sql create table statement
 
Sql wksht-7
Sql wksht-7Sql wksht-7
Sql wksht-7
 
Sql commands
Sql commandsSql commands
Sql commands
 
Part 15 triggerr
Part 15  triggerrPart 15  triggerr
Part 15 triggerr
 
Sql update statement
Sql update statementSql update statement
Sql update statement
 
Sql delete, truncate, drop statements
Sql delete, truncate, drop statementsSql delete, truncate, drop statements
Sql delete, truncate, drop statements
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
 
Triggers-Sequences-SQL
Triggers-Sequences-SQLTriggers-Sequences-SQL
Triggers-Sequences-SQL
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introduction
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
Sql ppt
Sql pptSql ppt
Sql ppt
 

Similar to Sql server ___________session_19(triggers)

Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17Thuan Nguyen
 
Lab07_Triggers.pptx
Lab07_Triggers.pptxLab07_Triggers.pptx
Lab07_Triggers.pptxKhngNguyn81
 
10 Creating Triggers
10 Creating Triggers10 Creating Triggers
10 Creating Triggersrehaniltifat
 
SQL interview questions jeetendra mandal - part 5
SQL interview questions jeetendra mandal - part 5SQL interview questions jeetendra mandal - part 5
SQL interview questions jeetendra mandal - part 5jeetendra mandal
 
Les08 (manipulating data)
Les08 (manipulating data)Les08 (manipulating data)
Les08 (manipulating data)Achmad Solichin
 
Subqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsSubqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsmaxpane
 
triggeroracle-eryk-130621201822-phpapp01.pdf
triggeroracle-eryk-130621201822-phpapp01.pdftriggeroracle-eryk-130621201822-phpapp01.pdf
triggeroracle-eryk-130621201822-phpapp01.pdfsaikumar580678
 

Similar to Sql server ___________session_19(triggers) (20)

Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16
 
Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17
 
triggers.pptx
triggers.pptxtriggers.pptx
triggers.pptx
 
Lab07_Triggers.pptx
Lab07_Triggers.pptxLab07_Triggers.pptx
Lab07_Triggers.pptx
 
Sql
SqlSql
Sql
 
Sql
SqlSql
Sql
 
Oracle sql material
Oracle sql materialOracle sql material
Oracle sql material
 
10 Creating Triggers
10 Creating Triggers10 Creating Triggers
10 Creating Triggers
 
Trigger
TriggerTrigger
Trigger
 
SQL interview questions jeetendra mandal - part 5
SQL interview questions jeetendra mandal - part 5SQL interview questions jeetendra mandal - part 5
SQL interview questions jeetendra mandal - part 5
 
Assignment#08
Assignment#08Assignment#08
Assignment#08
 
Les08
Les08Les08
Les08
 
Les08 (manipulating data)
Les08 (manipulating data)Les08 (manipulating data)
Les08 (manipulating data)
 
Subqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsSubqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactions
 
DOODB_LAB.pptx
DOODB_LAB.pptxDOODB_LAB.pptx
DOODB_LAB.pptx
 
T-SQL & Triggers
T-SQL & TriggersT-SQL & Triggers
T-SQL & Triggers
 
triggeroracle-eryk-130621201822-phpapp01.pdf
triggeroracle-eryk-130621201822-phpapp01.pdftriggeroracle-eryk-130621201822-phpapp01.pdf
triggeroracle-eryk-130621201822-phpapp01.pdf
 
Less09 Data
Less09 DataLess09 Data
Less09 Data
 
Les21
Les21Les21
Les21
 
4 trigger
4  trigger4  trigger
4 trigger
 

More from Ehtisham Ali

Sql server ___________session_20(ddl triggers)
Sql server  ___________session_20(ddl triggers)Sql server  ___________session_20(ddl triggers)
Sql server ___________session_20(ddl triggers)Ehtisham Ali
 
Sql server ___________session3-normailzation
Sql server  ___________session3-normailzationSql server  ___________session3-normailzation
Sql server ___________session3-normailzationEhtisham Ali
 
Sql server ___________session2-data_modeling
Sql server  ___________session2-data_modelingSql server  ___________session2-data_modeling
Sql server ___________session2-data_modelingEhtisham Ali
 
Sql server ___________session_18(stored procedures)
Sql server  ___________session_18(stored procedures)Sql server  ___________session_18(stored procedures)
Sql server ___________session_18(stored procedures)Ehtisham Ali
 
Sql server ___________session_17(indexes)
Sql server  ___________session_17(indexes)Sql server  ___________session_17(indexes)
Sql server ___________session_17(indexes)Ehtisham Ali
 
Sql server ___________session_16(views)
Sql server  ___________session_16(views)Sql server  ___________session_16(views)
Sql server ___________session_16(views)Ehtisham Ali
 
Sql server ___________session_15(data integrity)
Sql server  ___________session_15(data integrity)Sql server  ___________session_15(data integrity)
Sql server ___________session_15(data integrity)Ehtisham Ali
 
Sql server ___________session_11-12(joins)
Sql server  ___________session_11-12(joins)Sql server  ___________session_11-12(joins)
Sql server ___________session_11-12(joins)Ehtisham Ali
 
Sql server ___________session_10(group by clause)
Sql server  ___________session_10(group by clause)Sql server  ___________session_10(group by clause)
Sql server ___________session_10(group by clause)Ehtisham Ali
 
Sql server ___________session_1-intro
Sql server  ___________session_1-introSql server  ___________session_1-intro
Sql server ___________session_1-introEhtisham Ali
 
Sql server ___________session 3(sql 2008)
Sql server  ___________session 3(sql 2008)Sql server  ___________session 3(sql 2008)
Sql server ___________session 3(sql 2008)Ehtisham Ali
 
Sql server ___________session 2(sql 2008)
Sql server  ___________session 2(sql 2008)Sql server  ___________session 2(sql 2008)
Sql server ___________session 2(sql 2008)Ehtisham Ali
 
Sql server ___________session 1(sql 2008)
Sql server  ___________session 1(sql 2008)Sql server  ___________session 1(sql 2008)
Sql server ___________session 1(sql 2008)Ehtisham Ali
 
Sql server ___________data type of sql server
Sql server  ___________data type of sql serverSql server  ___________data type of sql server
Sql server ___________data type of sql serverEhtisham Ali
 
Sql server ___________data control language
Sql server  ___________data control languageSql server  ___________data control language
Sql server ___________data control languageEhtisham Ali
 
Sql server ___________ (advance sql)
Sql server  ___________  (advance sql)Sql server  ___________  (advance sql)
Sql server ___________ (advance sql)Ehtisham Ali
 

More from Ehtisham Ali (17)

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

Recently uploaded

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 

Recently uploaded (20)

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 

Sql server ___________session_19(triggers)

  • 1.
  • 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