SlideShare a Scribd company logo
1 of 8
TRIGGERS: 
1.Trigger is a program used to perform specific task when it is executed triggers are used to give 
conditions or rules on the table or else on database or else on entire sqlserver 
2. To overcome the drawbacks of constraints triggers are used. 
3. Constraints will work on tables and will work only with insert and update command. Triggers will 
work on insert, update, delete,create,alter and drop commands. 
4. Stored procedure will be executed explicitly i.e. user will execute the stored procedure ..Triggers 
will be executed implicitly i.e. sqlserver will execute triggers. 
5. Transactions are the part of triggers 
6. SQL SERVER contains after trigger, instead of triggers and DDL triggers. After trigger means after 
update, after insert and after delete.(where as ORACLE database contains before triggers, after 
triggers…etc.). 
SYNTAX: 
CREATE TRIGGER<TRIGGER_NAME> 
ON[TABLE NAME/INSTEAD OF TRIGGER/DDL TRIGGER] 
FOR[AFTER TRIGGER/INSTEAD OF TRIGGER/DDL TRIGGER] 
AS 
BEGIN 
STATEMENTS 
END 
MAGIC TABLE: 
It contains the columns of the table on which the trigger created. Magic table exists only when the 
trigger is being executed. Trigger execution will take few milliseconds only for few milliseconds 
magic table exist. 
TYPES OF MAGICTABLES: 
1. INSERTED MAGICTABLE: 
The new record given in the table will be stored in inserted magic table. 
2. DELETED MAGICTABLE: 
The records deleted from the table will be stored in deleted magic table.
QUERIES: 
TO MODIFY TRIGGERS IN CURRENT DATABASE 
SELECT * FROM SYSOBJECTS 
WHERE X TYPE=’TR’ 
TO VIEW STATEMENTS IN TRIGGER 
SP_HELPTEXT<TRIGGER_NAME> 
TO DELETE TRIGGER 
DROP TRIGGER<TRIGGER_NAME>[ON DATABASE/ALL SERVER] 
CREATING A TRIGGER WHICH WILL NOT ALLOWS THE USER TO DELETE THE RECORDS FROM 
EMPLOYEE TABLE 
CREATE TRIGGER TRG1 
ON EMPLOYEE 
FOR DELETE 
AS 
BEGIN 
PRINT ’DELETION NOT ALLOWED’ 
ROLLBACK 
END 
NOTE:A table can contain 12 TRIGGER programs 
CREATE A TRIGGER TO DISPLAY RECORD FROM INSERTED MAGICTABLE 
CREATE TRIGGER TRG2 
ON EMPLOYEE 
FOR INSERT 
AS 
BEGIN 
SELECT * FROM INSERTED 
END 
NOTE:
when insert command is executed the values will be stored in employee table 
And inserted magictable and after that trigger program will execute. 
CREATE A TRIGGER TO SHOW RECORDS FROM DELETED MAGIC TABLE 
CREATE TRIGGER TRG3 
ON EMPLOYEE 
FOR DELETE 
AS 
BEGIN 
SELECT * FROM DELETED 
END 
NOTE: 
When delete command is executed the records will be deleted from employee table and deleted 
records will be stored in deleted magic table and after that the trigger program will execute. 
CREATE A TRIGGER WHICH WILL NOT ALLOW THE USER TO ADD,EDIT AND DELETE RECORDS IN 
EMPLOYEE TABLE 
CREATE TRIGGER TRG4 
ON EMPLOYEE 
AS 
BEGIN 
PRINT ‘RECORDS CAN BE VIEWED’ 
ROLLBACK 
END 
CREATE A TRIGGER WHICH WILL NOT ALLOW THE USER TO DELETE MULTIPLE RECORDS ON 
EMPLOYEE TABLE 
CREATE TRIGGER TRG5 
ON EMPLOYEE 
FOR DELETE 
AS 
IF(SELECT COUNT(*)FROM DELETED)>1
AS 
BEGIN 
PRINT ‘MULTIPLE RECORDS CANNOT BE DELETED’ 
ROLLBACK 
END 
CREATE A TRIGGER TO CHECK DUPLICATE ENO,WHILE ADDING RECORD IN EMPLOYEE TABLE 
CREATE TRIGGER TRG6 
ON EMPLOYEE 
FOR INSERT 
AS 
DECLARE @ COUNT INT 
DECLARE @ TENO INT 
SELECT @TENO=ENO FROM INSERTED 
SELECT @TCOUNT=COUNT(*) FROM EMPLOYEE WHERE ENO=@TENO 
IF @TCOUNT>1 
BEGIN 
PRINT ‘ENO ALREADY EXISTS’ 
ROLLBACK 
END 
CREATE A TRIGGER WHICH WILL NOT ALLOW THE USER TO ADD,EDIT AND DELETE RECORDS IN 
EMPLOYEE TABLE BEFORE 9’O CLOCK AND AFTER 5’O CLOCK 
CREATE TRIGGER TRG8 
ON EMPLOYEE 
FOR INSERT,UPDATE,DELETE 
AS 
DECLARE@DT INT 
SELECT @DT=DATEPART(HH,GETDATE()) 
IF @DT NOT BETWEEN 9 AND 17
BEGIN 
PRINT ‘TRANSACTION NOT ALLOWED’ 
ROLLBACK 
END 
CREATE A TRIGGER WHICH WILL NOT ALLOW THE USER TO MODIFY EMPLOYEE NAMES IN 
EMPLOYEE TABLE 
CREATE TRIGGER TRG9 
ON EMPLOYEE 
FOR UPDATE 
AS 
IF UPDATE(ENAME) 
BEGIN 
RAISE ERROR(‘ENAME CANNOT BE MODIFIED’,16,1) 
ROLLBACK 
END 
CREATE A TRIGGERE TO UPDATE VALUES IN ANOTHER TABLE ITEM 
CREATE TRIGGER TRG10 
ON ITEM 
FOR INSERT 
AS 
BEGIN 
DECLARE @TITEMNO INT 
DECLARE @TQTY INT 
SELECT @TITEMNO=ITEMNO,@TQTY=QTY 
FROM INSERTED 
UPDATE ITEMS.SET TOTAL=TOTAL+@TQTY 
WHERE ITEMNO=@TITEMNO 
CREATE A TRIGGER TO DISPLAY NAME FROM EMPLOYEE TABLE
CREATE TRIGGER TRG11 
ON EMPLOYEE 
FOR INSERT 
AS 
DECLARE @TENAME VARCHAR(15) 
SELECT @TENAME=ENAME FROM INSERTED 
PRINT ‘NEW NAME’:+@TENAME 
INSTEAD OF TRIGGERS 
With After Insert, When Insert Command Is Executed The Values Will Go To The Table At The 
Values Will Go To Inserted Magic Table With Instead Of Insert, When Insert Command Is Executed. 
Then Values Will Go Only To Inserted Magic Table. For The Values Will Not Go To The Table On 
Which Trigger Is Created. Instead Of Triggers Can Be Used Only On Views. 
CREATE A TRIGGER TO DISPLAY RECORD FROM INSERETED MAGIC TABLE 
CREATE TRIGGER TRG13 
ON EMPLOYEE 
INSTEAD OF INSERT 
AS BEGIN 
DECLARE @TENO INT 
DECLARE @TENAME VARCHAR(15) 
DECLARE @TSAL INT 
SELECT @TENO=ENO,@TENAME=ENAME FROM INSERTED 
INSERT EMPLOYEE VALUES(@TENO,@TENAME,@TSAL+1000) 
PRINT @TENO 
PRINT @TENAME 
PRINT @TSAL 
END
DDL TRIGGERS: 
IT IS USED TO CREATE TRIGGER ON DATABASE AND ON ENTIRE SQLSERVER AND THESE TRIGGERS 
WILL WORKS ON CREATE,ALTER AND DROP COMMANDS 
CREATE A TRIGGER WHICH WILL NOT ALLOW THE USER TO CREATE AFTER AND DROP TABLE FROM 
CURRENT DATABASE 
CREATE TRIGGER TRG15 
ON DATABASE 
FOR CREATE_TABLE,ALTER_TABLE,DROP_TABLE 
AS 
BEGIN 
PRINT’CREATE,ALTER,DROP WILL NOT WORK’ 
ROLLBACK 
END 
TO DELETE TRIGGER 
DROP TRIGGER TRG14 ON DATABASE 
TO DELETE TRIGGER USING WIZARD 
CLICK ON VIEW OBJECT EXPLORER 
DOUBLE CLICK ON DATABASESPRACICE DATABASEPROGRAMMABILITY 
DATABASE TRIGGERSRIGHT CLICK ON TRG14DELETE 
CREATE A TRIGGER WHICH WILL NOT ALLOW THE USER TO CREATE,ALTER AND DROP TABLE IN 
ANY DATABASE 
CREATE TRIGGER TRG15 
ON ALL SERVER 
FOR CREATE_TABLE,ALTER_TABLE,DROP_TABLE 
AS 
BEGIN 
PRINT ‘CREATE,ALTER.DROP WILLNOT WORK’ 
ROLLBACK
END

More Related Content

What's hot

Android Lab Test : Managing the telephone calls (english)
Android Lab Test : Managing the telephone calls (english)Android Lab Test : Managing the telephone calls (english)
Android Lab Test : Managing the telephone calls (english)Bruno Delb
 
Button
ButtonButton
ButtonLwp Xd
 
cafeteria info management system
cafeteria info management systemcafeteria info management system
cafeteria info management systemGaurav Subham
 
CyberLink LabelPrint 2.5 Exploitation Process
CyberLink LabelPrint 2.5 Exploitation ProcessCyberLink LabelPrint 2.5 Exploitation Process
CyberLink LabelPrint 2.5 Exploitation ProcessThomas Gregory
 
Reactive computing
Reactive computingReactive computing
Reactive computingStan Lea
 
Nouveau document texte-_-_
Nouveau document texte-_-_Nouveau document texte-_-_
Nouveau document texte-_-_Mohamed Mlika
 

What's hot (14)

Calculadora
CalculadoraCalculadora
Calculadora
 
Android Lab Test : Managing the telephone calls (english)
Android Lab Test : Managing the telephone calls (english)Android Lab Test : Managing the telephone calls (english)
Android Lab Test : Managing the telephone calls (english)
 
Button
ButtonButton
Button
 
Vs c# lecture7 2
Vs c# lecture7  2Vs c# lecture7  2
Vs c# lecture7 2
 
Vp lecture 9 ararat
Vp lecture 9 araratVp lecture 9 ararat
Vp lecture 9 ararat
 
cafeteria info management system
cafeteria info management systemcafeteria info management system
cafeteria info management system
 
Demanding scripting
Demanding scriptingDemanding scripting
Demanding scripting
 
ICP - Lecture 9
ICP - Lecture 9ICP - Lecture 9
ICP - Lecture 9
 
CyberLink LabelPrint 2.5 Exploitation Process
CyberLink LabelPrint 2.5 Exploitation ProcessCyberLink LabelPrint 2.5 Exploitation Process
CyberLink LabelPrint 2.5 Exploitation Process
 
DS- Stack ADT
DS- Stack ADTDS- Stack ADT
DS- Stack ADT
 
Android databases
Android databasesAndroid databases
Android databases
 
Reactive computing
Reactive computingReactive computing
Reactive computing
 
Nouveau document texte-_-_
Nouveau document texte-_-_Nouveau document texte-_-_
Nouveau document texte-_-_
 
PLSQL.docx
PLSQL.docxPLSQL.docx
PLSQL.docx
 

Similar to Triggers

Intro to tsql unit 15
Intro to tsql   unit 15Intro to tsql   unit 15
Intro to tsql unit 15Syed Asrarali
 
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
 
Lecture 4. MS SQL. DML Triggers
Lecture 4. MS SQL. DML TriggersLecture 4. MS SQL. DML Triggers
Lecture 4. MS SQL. DML TriggersAlexey Furmanov
 
Mca ii-dbms-u-v-transaction management
Mca ii-dbms-u-v-transaction managementMca ii-dbms-u-v-transaction management
Mca ii-dbms-u-v-transaction managementRai University
 
Top MNC'S Interview questions and answers
Top MNC'S Interview questions and answersTop MNC'S Interview questions and answers
Top MNC'S Interview questions and answersMadhu Dhare
 
Oracle trigger
Oracle triggerOracle trigger
Oracle triggernasrul28
 
Lab07_Triggers.pptx
Lab07_Triggers.pptxLab07_Triggers.pptx
Lab07_Triggers.pptxKhngNguyn81
 
Final ProjectBe sure to follow the instructions for each step as.docx
Final ProjectBe sure to follow the instructions for each step as.docxFinal ProjectBe sure to follow the instructions for each step as.docx
Final ProjectBe sure to follow the instructions for each step as.docxvoversbyobersby
 
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
 
Triggers in plsql
Triggers in plsqlTriggers in plsql
Triggers in plsqlArun Sial
 
Sql Queries
Sql QueriesSql Queries
Sql Querieswebicon
 
triggeroracle-eryk-130621201822-phpapp01.pdf
triggeroracle-eryk-130621201822-phpapp01.pdftriggeroracle-eryk-130621201822-phpapp01.pdf
triggeroracle-eryk-130621201822-phpapp01.pdfsaikumar580678
 
5 Cool Things About PLSQL
5 Cool Things About PLSQL5 Cool Things About PLSQL
5 Cool Things About PLSQLConnor McDonald
 

Similar to Triggers (20)

Module06
Module06Module06
Module06
 
Trigger
TriggerTrigger
Trigger
 
Trigger
TriggerTrigger
Trigger
 
Intro to tsql unit 15
Intro to tsql   unit 15Intro to tsql   unit 15
Intro to tsql unit 15
 
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
 
Lecture 4. MS SQL. DML Triggers
Lecture 4. MS SQL. DML TriggersLecture 4. MS SQL. DML Triggers
Lecture 4. MS SQL. DML Triggers
 
Database Triggers
Database TriggersDatabase Triggers
Database Triggers
 
Triggers
TriggersTriggers
Triggers
 
Mca ii-dbms-u-v-transaction management
Mca ii-dbms-u-v-transaction managementMca ii-dbms-u-v-transaction management
Mca ii-dbms-u-v-transaction management
 
Top MNC'S Interview questions and answers
Top MNC'S Interview questions and answersTop MNC'S Interview questions and answers
Top MNC'S Interview questions and answers
 
Oracle trigger
Oracle triggerOracle trigger
Oracle trigger
 
Sql triggers
Sql triggersSql triggers
Sql triggers
 
Lab07_Triggers.pptx
Lab07_Triggers.pptxLab07_Triggers.pptx
Lab07_Triggers.pptx
 
Final ProjectBe sure to follow the instructions for each step as.docx
Final ProjectBe sure to follow the instructions for each step as.docxFinal ProjectBe sure to follow the instructions for each step as.docx
Final ProjectBe sure to follow the instructions for each step as.docx
 
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 in plsql
Triggers in plsqlTriggers in plsql
Triggers in plsql
 
Sql Queries
Sql QueriesSql Queries
Sql Queries
 
triggeroracle-eryk-130621201822-phpapp01.pdf
triggeroracle-eryk-130621201822-phpapp01.pdftriggeroracle-eryk-130621201822-phpapp01.pdf
triggeroracle-eryk-130621201822-phpapp01.pdf
 
Oracle Database Trigger
Oracle Database TriggerOracle Database Trigger
Oracle Database Trigger
 
5 Cool Things About PLSQL
5 Cool Things About PLSQL5 Cool Things About PLSQL
5 Cool Things About PLSQL
 

More from Yaswanth Babu Gummadivelli (20)

Presentation on BA
Presentation on BAPresentation on BA
Presentation on BA
 
ERP
ERPERP
ERP
 
Ba -content
Ba -contentBa -content
Ba -content
 
E commerce use case documentation.
E commerce use case documentation.E commerce use case documentation.
E commerce use case documentation.
 
MOM on activity diagram
MOM on activity diagramMOM on activity diagram
MOM on activity diagram
 
UML Diagrams
UML DiagramsUML Diagrams
UML Diagrams
 
Business analyst ppt
Business analyst pptBusiness analyst ppt
Business analyst ppt
 
MOM on BA
MOM on BAMOM on BA
MOM on BA
 
exception handling
 exception handling exception handling
exception handling
 
collections
 collections collections
collections
 
Constructors
Constructors Constructors
Constructors
 
array
array array
array
 
Use case for atm
Use case for atmUse case for atm
Use case for atm
 
use case diagramHospital managment system
use case diagramHospital managment systemuse case diagramHospital managment system
use case diagramHospital managment system
 
Activity diagram for ticket vending machine
Activity diagram for ticket vending machineActivity diagram for ticket vending machine
Activity diagram for ticket vending machine
 
Extreme programming
Extreme programmingExtreme programming
Extreme programming
 
Agile model
Agile model Agile model
Agile model
 
Business Analyst
Business AnalystBusiness Analyst
Business Analyst
 
CRM and ERP
CRM and ERPCRM and ERP
CRM and ERP
 
Reflection
ReflectionReflection
Reflection
 

Recently uploaded

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
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
 
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
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
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
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 

Recently uploaded (20)

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
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...
 
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
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
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
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
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
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 

Triggers

  • 1. TRIGGERS: 1.Trigger is a program used to perform specific task when it is executed triggers are used to give conditions or rules on the table or else on database or else on entire sqlserver 2. To overcome the drawbacks of constraints triggers are used. 3. Constraints will work on tables and will work only with insert and update command. Triggers will work on insert, update, delete,create,alter and drop commands. 4. Stored procedure will be executed explicitly i.e. user will execute the stored procedure ..Triggers will be executed implicitly i.e. sqlserver will execute triggers. 5. Transactions are the part of triggers 6. SQL SERVER contains after trigger, instead of triggers and DDL triggers. After trigger means after update, after insert and after delete.(where as ORACLE database contains before triggers, after triggers…etc.). SYNTAX: CREATE TRIGGER<TRIGGER_NAME> ON[TABLE NAME/INSTEAD OF TRIGGER/DDL TRIGGER] FOR[AFTER TRIGGER/INSTEAD OF TRIGGER/DDL TRIGGER] AS BEGIN STATEMENTS END MAGIC TABLE: It contains the columns of the table on which the trigger created. Magic table exists only when the trigger is being executed. Trigger execution will take few milliseconds only for few milliseconds magic table exist. TYPES OF MAGICTABLES: 1. INSERTED MAGICTABLE: The new record given in the table will be stored in inserted magic table. 2. DELETED MAGICTABLE: The records deleted from the table will be stored in deleted magic table.
  • 2. QUERIES: TO MODIFY TRIGGERS IN CURRENT DATABASE SELECT * FROM SYSOBJECTS WHERE X TYPE=’TR’ TO VIEW STATEMENTS IN TRIGGER SP_HELPTEXT<TRIGGER_NAME> TO DELETE TRIGGER DROP TRIGGER<TRIGGER_NAME>[ON DATABASE/ALL SERVER] CREATING A TRIGGER WHICH WILL NOT ALLOWS THE USER TO DELETE THE RECORDS FROM EMPLOYEE TABLE CREATE TRIGGER TRG1 ON EMPLOYEE FOR DELETE AS BEGIN PRINT ’DELETION NOT ALLOWED’ ROLLBACK END NOTE:A table can contain 12 TRIGGER programs CREATE A TRIGGER TO DISPLAY RECORD FROM INSERTED MAGICTABLE CREATE TRIGGER TRG2 ON EMPLOYEE FOR INSERT AS BEGIN SELECT * FROM INSERTED END NOTE:
  • 3. when insert command is executed the values will be stored in employee table And inserted magictable and after that trigger program will execute. CREATE A TRIGGER TO SHOW RECORDS FROM DELETED MAGIC TABLE CREATE TRIGGER TRG3 ON EMPLOYEE FOR DELETE AS BEGIN SELECT * FROM DELETED END NOTE: When delete command is executed the records will be deleted from employee table and deleted records will be stored in deleted magic table and after that the trigger program will execute. CREATE A TRIGGER WHICH WILL NOT ALLOW THE USER TO ADD,EDIT AND DELETE RECORDS IN EMPLOYEE TABLE CREATE TRIGGER TRG4 ON EMPLOYEE AS BEGIN PRINT ‘RECORDS CAN BE VIEWED’ ROLLBACK END CREATE A TRIGGER WHICH WILL NOT ALLOW THE USER TO DELETE MULTIPLE RECORDS ON EMPLOYEE TABLE CREATE TRIGGER TRG5 ON EMPLOYEE FOR DELETE AS IF(SELECT COUNT(*)FROM DELETED)>1
  • 4. AS BEGIN PRINT ‘MULTIPLE RECORDS CANNOT BE DELETED’ ROLLBACK END CREATE A TRIGGER TO CHECK DUPLICATE ENO,WHILE ADDING RECORD IN EMPLOYEE TABLE CREATE TRIGGER TRG6 ON EMPLOYEE FOR INSERT AS DECLARE @ COUNT INT DECLARE @ TENO INT SELECT @TENO=ENO FROM INSERTED SELECT @TCOUNT=COUNT(*) FROM EMPLOYEE WHERE ENO=@TENO IF @TCOUNT>1 BEGIN PRINT ‘ENO ALREADY EXISTS’ ROLLBACK END CREATE A TRIGGER WHICH WILL NOT ALLOW THE USER TO ADD,EDIT AND DELETE RECORDS IN EMPLOYEE TABLE BEFORE 9’O CLOCK AND AFTER 5’O CLOCK CREATE TRIGGER TRG8 ON EMPLOYEE FOR INSERT,UPDATE,DELETE AS DECLARE@DT INT SELECT @DT=DATEPART(HH,GETDATE()) IF @DT NOT BETWEEN 9 AND 17
  • 5. BEGIN PRINT ‘TRANSACTION NOT ALLOWED’ ROLLBACK END CREATE A TRIGGER WHICH WILL NOT ALLOW THE USER TO MODIFY EMPLOYEE NAMES IN EMPLOYEE TABLE CREATE TRIGGER TRG9 ON EMPLOYEE FOR UPDATE AS IF UPDATE(ENAME) BEGIN RAISE ERROR(‘ENAME CANNOT BE MODIFIED’,16,1) ROLLBACK END CREATE A TRIGGERE TO UPDATE VALUES IN ANOTHER TABLE ITEM CREATE TRIGGER TRG10 ON ITEM FOR INSERT AS BEGIN DECLARE @TITEMNO INT DECLARE @TQTY INT SELECT @TITEMNO=ITEMNO,@TQTY=QTY FROM INSERTED UPDATE ITEMS.SET TOTAL=TOTAL+@TQTY WHERE ITEMNO=@TITEMNO CREATE A TRIGGER TO DISPLAY NAME FROM EMPLOYEE TABLE
  • 6. CREATE TRIGGER TRG11 ON EMPLOYEE FOR INSERT AS DECLARE @TENAME VARCHAR(15) SELECT @TENAME=ENAME FROM INSERTED PRINT ‘NEW NAME’:+@TENAME INSTEAD OF TRIGGERS With After Insert, When Insert Command Is Executed The Values Will Go To The Table At The Values Will Go To Inserted Magic Table With Instead Of Insert, When Insert Command Is Executed. Then Values Will Go Only To Inserted Magic Table. For The Values Will Not Go To The Table On Which Trigger Is Created. Instead Of Triggers Can Be Used Only On Views. CREATE A TRIGGER TO DISPLAY RECORD FROM INSERETED MAGIC TABLE CREATE TRIGGER TRG13 ON EMPLOYEE INSTEAD OF INSERT AS BEGIN DECLARE @TENO INT DECLARE @TENAME VARCHAR(15) DECLARE @TSAL INT SELECT @TENO=ENO,@TENAME=ENAME FROM INSERTED INSERT EMPLOYEE VALUES(@TENO,@TENAME,@TSAL+1000) PRINT @TENO PRINT @TENAME PRINT @TSAL END
  • 7. DDL TRIGGERS: IT IS USED TO CREATE TRIGGER ON DATABASE AND ON ENTIRE SQLSERVER AND THESE TRIGGERS WILL WORKS ON CREATE,ALTER AND DROP COMMANDS CREATE A TRIGGER WHICH WILL NOT ALLOW THE USER TO CREATE AFTER AND DROP TABLE FROM CURRENT DATABASE CREATE TRIGGER TRG15 ON DATABASE FOR CREATE_TABLE,ALTER_TABLE,DROP_TABLE AS BEGIN PRINT’CREATE,ALTER,DROP WILL NOT WORK’ ROLLBACK END TO DELETE TRIGGER DROP TRIGGER TRG14 ON DATABASE TO DELETE TRIGGER USING WIZARD CLICK ON VIEW OBJECT EXPLORER DOUBLE CLICK ON DATABASESPRACICE DATABASEPROGRAMMABILITY DATABASE TRIGGERSRIGHT CLICK ON TRG14DELETE CREATE A TRIGGER WHICH WILL NOT ALLOW THE USER TO CREATE,ALTER AND DROP TABLE IN ANY DATABASE CREATE TRIGGER TRG15 ON ALL SERVER FOR CREATE_TABLE,ALTER_TABLE,DROP_TABLE AS BEGIN PRINT ‘CREATE,ALTER.DROP WILLNOT WORK’ ROLLBACK
  • 8. END