SlideShare a Scribd company logo
1 of 24
Querying and Managing Data Using SQL Server 2005
Objectives


               In this session, you will learn to:
                  Implement triggers
                  Implement transactions




    Ver. 1.0                        Session 14       Slide 1 of 24
Querying and Managing Data Using SQL Server 2005
Identifying Types of Triggers


                Triggers are of the following types:
                   DML triggers
                   DDL triggers




     Ver. 1.0                       Session 14         Slide 2 of 24
Querying and Managing Data Using SQL Server 2005
Just a minute


               You want to make changes in another database object
               whenever any new database object is created. Which of the
               following triggers will you use?
                1.   DML Trigger
                2.   Instead Of Trigger
                3.   DDL Trigger
                4.   Nested Trigger




               Answer:
                3. DDL Trigger




    Ver. 1.0                          Session 14                 Slide 3 of 24
Querying and Managing Data Using SQL Server 2005
Creating Triggers


               Triggers:
                  Are created using the CREATE TRIGGER statement
                  Syntax:
                   CREATE TRIGGER trigger_name
                   ON { OBJECT NAME }
                   { FOR | AFTER | INSTEAD OF } { event_type [
                   ,...n ] |
                   DDL_DATABASE_LEVEL_EVENTS }
                   { AS
                   { sql_statement [ ...n ] }
                   }
                  Create two temporary tables called magic tables
               Let’s see how…


    Ver. 1.0                    Session 14                   Slide 4 of 24
Querying and Managing Data Using SQL Server 2005
Managing Triggers


               Involve altering a trigger
                  Syntax:
                   ALTER TRIGGER trigger_name
                   { FOR | AFTER } { event_type [ ,...n ] |
                   DDL_DATABASE_LEVEL_EVENTS }
                   { AS
                   { sql_statement [ ...n ] }
                   }
               Involve deleting a trigger
                  Syntax:
                    DROP TRIGGER { trigger }
               Let’s see how…



    Ver. 1.0                       Session 14             Slide 5 of 24
Querying and Managing Data Using SQL Server 2005
Just a minute


               Name the tables that get created when a trigger is fired in
               response to the INSERT, DELETE or UPDATE statement.




               Answer:
                   Magic tables – Inserted and Deleted




    Ver. 1.0                       Session 14                        Slide 6 of 24
Querying and Managing Data Using SQL Server 2005
Demo: Implementing Triggers


               Problem Statement:
                  In AdventureWorks, Inc., you have created the following view,
                  vwEmployee to view the employee details:
                   Create view vwEmployee as
                   select e.EmployeeID as 'Employee ID',
                   h.FirstName as 'Employee Name', g.Name as 'Department
                   Name', e.HireDate as 'Date of Joining', j.AddressLine1
                   as 'Employee Address'from HumanResources.Employee as e
                   join HumanResources.EmployeeDepartmentHistory as f on
                   e.EmployeeID = f.EmployeeID join
                   HumanResources.Department as g
                   on f.DepartmentID = g.DepartmentID
                   join Person.Contact as h on e.ContactID = h.ContactID
                   join HumanResources.EmployeeAddress as i on
                   e.EmployeeID = i.EmployeeID join Person.Address as j
                   on i.AddressID = j.AddressID




    Ver. 1.0                       Session 14                           Slide 7 of 24
Querying and Managing Data Using SQL Server 2005
Demo: Implementing Triggers (Contd.)


               Problem Statement (Contd.):
               You have identified that you are not able to modify data using this
               view because it is based on multiple tables. How can you make
               the view updateable?




    Ver. 1.0                       Session 14                             Slide 8 of 24
Querying and Managing Data Using SQL Server 2005
Demo: Implementing Triggers (Contd.)


               Solution:
                  To solve the preceding problem, you need to perform the
                  following tasks:
                    1. Create an Instead Of trigger on the view.
                    2. Verify the functionality.




    Ver. 1.0                          Session 14                       Slide 9 of 24
Querying and Managing Data Using SQL Server 2005
Creating Transactions


               Transactions:
                  Execute a number of statements as a single unit
                  Must possess the four properties called ACID
                  Are of the following types:
                      Autocommit transactions
                      Explicit transactions
               Autocommit Transactions:
                – Is the default transaction management mode of SQL Server
               Explicit Transactions:
                  Can be created using the BEGIN TRANSACTION and
                  COMMIT TRANSACTION statements
               Let’s see how…



    Ver. 1.0                        Session 14                       Slide 10 of 24
Querying and Managing Data Using SQL Server 2005
Just a minute


               Which of the following properties does a transaction NOT
               posses?
                1.   Atomicity
                2.   Consistency
                3.   Isolation
                4.   Separation




               Answer:
                4. Separation




    Ver. 1.0                       Session 14                     Slide 11 of 24
Querying and Managing Data Using SQL Server 2005
Reverting Transactions


               Transactions are reverted:
                  When the execution of transaction is in an invalid state
                  To maintain consistency
                  Using the ROLLBACK TRANSACTION and ROLLBACK
                  WORK statements
                  Syntax:
                   ROLLBACK [TRAN[SACTION] [transaction_name
                   |@tran_name_variable
                   |savepoint_name |
                   @savepoint_variable]]
               Let’s see how…




    Ver. 1.0                       Session 14                          Slide 12 of 24
Querying and Managing Data Using SQL Server 2005
Implementing Transactional Integrity (Contd.)


               Flash presentation: Implementing Locks
               Locks:
                  Help in achieving transactional integrity
                  Help in avoiding:
                   •   Lost updates
                   •   Uncommitted dependency
                   •   Inconsistent analysis (Dirty Read)
                   •   Phantom reads
                  Supported by SQL Server are:
                       Shared locks
                       Exclusive locks
                       Update locks
                       Intent locks
                       Schema locks
                       Bulk update locks

    Ver. 1.0                          Session 14              Slide 13 of 24
Querying and Managing Data Using SQL Server 2005
Implementing Transactional Integrity (Contd.)


               Locking is controlled by the following types of isolation
               levels:
                  READ UNCOMMITTED
                  READ COMMITED
                  REPEATABLE READ
                  SNAPSHOT
                  SERIALIZABLE




    Ver. 1.0                       Session 14                         Slide 14 of 24
Querying and Managing Data Using SQL Server 2005
Implementing Transactional Integrity (Contd.)


               Locks can be implemented using the SET TRANSACTION
               ISOLATION LEVEL statement:
                  Syntax:
                   SET TRANSACTION ISOLATION LEVEL {
                   READ UNCOMMITTED |
                   READ COMMITTED | REPEATABLE
                   READ | SNAPSHOT | SERIALIZABLE } [ ;]
                   BEGIN TRANSACTION
                   ………
                   ………
                   COMMIT TRANSACTION
               Let’s see how…



    Ver. 1.0                    Session 14                  Slide 15 of 24
Querying and Managing Data Using SQL Server 2005
Resolving Deadlocks


               You can resolve deadlocks by:
                  Detecting deadlocks
                  Avoiding deadlocks by using Update locks




    Ver. 1.0                      Session 14                 Slide 16 of 24
Querying and Managing Data Using SQL Server 2005
Just a minute


               Which of the following concurrency problems is also known
               as DIRTY READ?
                1. Uncommitted dependency
                2. Phantom problem
                3. Inconsistence analysis




               Answer:
                1. Uncommitted dependency




    Ver. 1.0                     Session 14                      Slide 17 of 24
Querying and Managing Data Using SQL Server 2005
Just a minute


               Which of the following locks enable others to view the data
               being modified by the current transaction?
                1.   Shared lock
                2.   Exclusive lock
                3.   Update lock
                4.   Intent lock
                5.   Bulk update lock




               Answer:
                1. Shared lock




    Ver. 1.0                            Session 14                  Slide 18 of 24
Querying and Managing Data Using SQL Server 2005
Just a minute


               Which of the following locks prevent your database from
               deadlocks?
                1. Intent lock
                2. Update lock
                3. Shared lock




               Answer:
                2. Update lock




    Ver. 1.0                      Session 14                      Slide 19 of 24
Querying and Managing Data Using SQL Server 2005
Demo: Implementing Transactions


               •   Problem Statement:
                      At AdventureWorks, Inc., an employee named Sidney Higa,
                      who is currently working as Production Technician – WC10 has
                      been promoted as Marketing Manager. The employee ID of
                      Sidney is 13. As a database developer, you need to update his
                      records. This involves updating the title in the Employee table
                      and updating the department history details.
                      You need to ensure that all the changes take effect. In
                      addition, you need to ensure that no other transaction should
                      be able to view the data being modified by the current
                      transaction.




    Ver. 1.0                           Session 14                           Slide 20 of 24
Querying and Managing Data Using SQL Server 2005
Demo: Implementing Transactions (Contd.)


               Solution:
                  To solve the preceding problem, you need to perform the
                  following tasks:
                    1. Create a transaction.
                    2. Verify the output.




    Ver. 1.0                         Session 14                        Slide 21 of 24
Querying and Managing Data Using SQL Server 2005
Summary


              In this session, you learned that:
                 A trigger is a block of code that constitutes a set of T-SQL
                 statements that are activated in response to certain actions.
                 The SQL Server supports following triggers:
                   • DML triggers
                   • DDL triggers
               – A trigger can be altered by using the ALTER TRIGGER
                 statement.
               – A trigger can be deleted by using the DROP TRIGGER
                 statement.
               – Transactions are used to execute a sequence of statements
                 together as a single logical unit of work.




   Ver. 1.0                         Session 14                           Slide 22 of 24
Querying and Managing Data Using SQL Server 2005
Summary (Contd.)


               Every transaction posses the ACID property.
               The SQL Server supports following transactions:
                • Autocommit transaction
                • Explicit transaction
               Locks are used to maintain transactional integrity.
               In the absence of locking, following problems may occur if
               transactions use the same data from a database at the same
               time:
                   Lost updates
                   Uncommitted dependency
                   Inconsistent analysis
                   Phantom reads




    Ver. 1.0                    Session 14                         Slide 23 of 24
Querying and Managing Data Using SQL Server 2005
Summary (Contd.)


               The SQL Server supports following lock modes:
                •   Shared locks
                •   Exclusive locks
                •   Update locks
                •   Intent locks
                •   Schema locks
                •   Bulk Update locks
               A deadlock is a situation in which two users (or transactions)
               have locks on separate objects, and each user wants to
               acquire a lock on the other user’s object.




    Ver. 1.0                      Session 14                           Slide 24 of 24

More Related Content

Similar to 10 qmds2005 session14

09 qmds2005 session13
09 qmds2005 session1309 qmds2005 session13
09 qmds2005 session13
Niit Care
 
08 qmds2005 session11
08 qmds2005 session1108 qmds2005 session11
08 qmds2005 session11
Niit Care
 
03 qmds2005 session03
03 qmds2005 session0303 qmds2005 session03
03 qmds2005 session03
Niit Care
 
13 qmds2005 session18
13 qmds2005 session1813 qmds2005 session18
13 qmds2005 session18
Niit Care
 
Database concurrency and transactions - Tal Olier
Database concurrency and transactions - Tal OlierDatabase concurrency and transactions - Tal Olier
Database concurrency and transactions - Tal Olier
sqlserver.co.il
 
05 qmds2005 session07
05 qmds2005 session0705 qmds2005 session07
05 qmds2005 session07
Niit Care
 
Admin Tech Ed Presentation Hardening Sql Server
Admin Tech Ed Presentation   Hardening Sql ServerAdmin Tech Ed Presentation   Hardening Sql Server
Admin Tech Ed Presentation Hardening Sql Server
rsnarayanan
 
Introduction to Threading in .Net
Introduction to Threading in .NetIntroduction to Threading in .Net
Introduction to Threading in .Net
webhostingguy
 
SQLCLR For DBAs and Developers
SQLCLR For DBAs and DevelopersSQLCLR For DBAs and Developers
SQLCLR For DBAs and Developers
webhostingguy
 
Dr. Jekyll and Mr. Hyde
Dr. Jekyll and Mr. HydeDr. Jekyll and Mr. Hyde
Dr. Jekyll and Mr. Hyde
webhostingguy
 
SDC - Programming the CLR in SQL Server 2005.ppt (1.51 MB)
SDC - Programming the CLR in SQL Server 2005.ppt (1.51 MB)SDC - Programming the CLR in SQL Server 2005.ppt (1.51 MB)
SDC - Programming the CLR in SQL Server 2005.ppt (1.51 MB)
webhostingguy
 

Similar to 10 qmds2005 session14 (20)

09 qmds2005 session13
09 qmds2005 session1309 qmds2005 session13
09 qmds2005 session13
 
08 qmds2005 session11
08 qmds2005 session1108 qmds2005 session11
08 qmds2005 session11
 
03 qmds2005 session03
03 qmds2005 session0303 qmds2005 session03
03 qmds2005 session03
 
13 qmds2005 session18
13 qmds2005 session1813 qmds2005 session18
13 qmds2005 session18
 
Database concurrency and transactions - Tal Olier
Database concurrency and transactions - Tal OlierDatabase concurrency and transactions - Tal Olier
Database concurrency and transactions - Tal Olier
 
Welcome to the nightmare of locking, blocking and isolation levels!
Welcome to the nightmare of locking, blocking and isolation levels!Welcome to the nightmare of locking, blocking and isolation levels!
Welcome to the nightmare of locking, blocking and isolation levels!
 
05 qmds2005 session07
05 qmds2005 session0705 qmds2005 session07
05 qmds2005 session07
 
Admin Tech Ed Presentation Hardening Sql Server
Admin Tech Ed Presentation   Hardening Sql ServerAdmin Tech Ed Presentation   Hardening Sql Server
Admin Tech Ed Presentation Hardening Sql Server
 
Database Systems - SQL - DCL Statements (Chapter 3/4)
Database Systems - SQL - DCL Statements (Chapter 3/4)Database Systems - SQL - DCL Statements (Chapter 3/4)
Database Systems - SQL - DCL Statements (Chapter 3/4)
 
Session 2: SQL Server 2012 with Christian Malbeuf
Session 2: SQL Server 2012 with Christian MalbeufSession 2: SQL Server 2012 with Christian Malbeuf
Session 2: SQL Server 2012 with Christian Malbeuf
 
Ordina SOFTC Presentation - SQL CLR
Ordina SOFTC Presentation - SQL CLROrdina SOFTC Presentation - SQL CLR
Ordina SOFTC Presentation - SQL CLR
 
Blackbook microsoft sql server
Blackbook microsoft sql serverBlackbook microsoft sql server
Blackbook microsoft sql server
 
T-SQL & Triggers
T-SQL & TriggersT-SQL & Triggers
T-SQL & Triggers
 
Sql server
Sql serverSql server
Sql server
 
Introduction to Threading in .Net
Introduction to Threading in .NetIntroduction to Threading in .Net
Introduction to Threading in .Net
 
Weblogic 11g admin basic with screencast
Weblogic 11g admin basic with screencastWeblogic 11g admin basic with screencast
Weblogic 11g admin basic with screencast
 
SQLCLR For DBAs and Developers
SQLCLR For DBAs and DevelopersSQLCLR For DBAs and Developers
SQLCLR For DBAs and Developers
 
Dr. Jekyll and Mr. Hyde
Dr. Jekyll and Mr. HydeDr. Jekyll and Mr. Hyde
Dr. Jekyll and Mr. Hyde
 
Summary tables with flexviews
Summary tables with flexviewsSummary tables with flexviews
Summary tables with flexviews
 
SDC - Programming the CLR in SQL Server 2005.ppt (1.51 MB)
SDC - Programming the CLR in SQL Server 2005.ppt (1.51 MB)SDC - Programming the CLR in SQL Server 2005.ppt (1.51 MB)
SDC - Programming the CLR in SQL Server 2005.ppt (1.51 MB)
 

More from Niit Care (20)

Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 b
 
Ajs 4 b
Ajs 4 bAjs 4 b
Ajs 4 b
 
Ajs 4 a
Ajs 4 aAjs 4 a
Ajs 4 a
 
Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 c
 
Ajs 3 b
Ajs 3 bAjs 3 b
Ajs 3 b
 
Ajs 3 a
Ajs 3 aAjs 3 a
Ajs 3 a
 
Ajs 3 c
Ajs 3 cAjs 3 c
Ajs 3 c
 
Ajs 2 b
Ajs 2 bAjs 2 b
Ajs 2 b
 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 a
 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 c
 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 a
 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 c
 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-b
 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-c
 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-b
 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cf
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
 
Design and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data ScienceDesign and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data Science
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational Performance
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governance
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 

10 qmds2005 session14

  • 1. Querying and Managing Data Using SQL Server 2005 Objectives In this session, you will learn to: Implement triggers Implement transactions Ver. 1.0 Session 14 Slide 1 of 24
  • 2. Querying and Managing Data Using SQL Server 2005 Identifying Types of Triggers Triggers are of the following types: DML triggers DDL triggers Ver. 1.0 Session 14 Slide 2 of 24
  • 3. Querying and Managing Data Using SQL Server 2005 Just a minute You want to make changes in another database object whenever any new database object is created. Which of the following triggers will you use? 1. DML Trigger 2. Instead Of Trigger 3. DDL Trigger 4. Nested Trigger Answer: 3. DDL Trigger Ver. 1.0 Session 14 Slide 3 of 24
  • 4. Querying and Managing Data Using SQL Server 2005 Creating Triggers Triggers: Are created using the CREATE TRIGGER statement Syntax: CREATE TRIGGER trigger_name ON { OBJECT NAME } { FOR | AFTER | INSTEAD OF } { event_type [ ,...n ] | DDL_DATABASE_LEVEL_EVENTS } { AS { sql_statement [ ...n ] } } Create two temporary tables called magic tables Let’s see how… Ver. 1.0 Session 14 Slide 4 of 24
  • 5. Querying and Managing Data Using SQL Server 2005 Managing Triggers Involve altering a trigger Syntax: ALTER TRIGGER trigger_name { FOR | AFTER } { event_type [ ,...n ] | DDL_DATABASE_LEVEL_EVENTS } { AS { sql_statement [ ...n ] } } Involve deleting a trigger Syntax: DROP TRIGGER { trigger } Let’s see how… Ver. 1.0 Session 14 Slide 5 of 24
  • 6. Querying and Managing Data Using SQL Server 2005 Just a minute Name the tables that get created when a trigger is fired in response to the INSERT, DELETE or UPDATE statement. Answer: Magic tables – Inserted and Deleted Ver. 1.0 Session 14 Slide 6 of 24
  • 7. Querying and Managing Data Using SQL Server 2005 Demo: Implementing Triggers Problem Statement: In AdventureWorks, Inc., you have created the following view, vwEmployee to view the employee details: Create view vwEmployee as select e.EmployeeID as 'Employee ID', h.FirstName as 'Employee Name', g.Name as 'Department Name', e.HireDate as 'Date of Joining', j.AddressLine1 as 'Employee Address'from HumanResources.Employee as e join HumanResources.EmployeeDepartmentHistory as f on e.EmployeeID = f.EmployeeID join HumanResources.Department as g on f.DepartmentID = g.DepartmentID join Person.Contact as h on e.ContactID = h.ContactID join HumanResources.EmployeeAddress as i on e.EmployeeID = i.EmployeeID join Person.Address as j on i.AddressID = j.AddressID Ver. 1.0 Session 14 Slide 7 of 24
  • 8. Querying and Managing Data Using SQL Server 2005 Demo: Implementing Triggers (Contd.) Problem Statement (Contd.): You have identified that you are not able to modify data using this view because it is based on multiple tables. How can you make the view updateable? Ver. 1.0 Session 14 Slide 8 of 24
  • 9. Querying and Managing Data Using SQL Server 2005 Demo: Implementing Triggers (Contd.) Solution: To solve the preceding problem, you need to perform the following tasks: 1. Create an Instead Of trigger on the view. 2. Verify the functionality. Ver. 1.0 Session 14 Slide 9 of 24
  • 10. Querying and Managing Data Using SQL Server 2005 Creating Transactions Transactions: Execute a number of statements as a single unit Must possess the four properties called ACID Are of the following types: Autocommit transactions Explicit transactions Autocommit Transactions: – Is the default transaction management mode of SQL Server Explicit Transactions: Can be created using the BEGIN TRANSACTION and COMMIT TRANSACTION statements Let’s see how… Ver. 1.0 Session 14 Slide 10 of 24
  • 11. Querying and Managing Data Using SQL Server 2005 Just a minute Which of the following properties does a transaction NOT posses? 1. Atomicity 2. Consistency 3. Isolation 4. Separation Answer: 4. Separation Ver. 1.0 Session 14 Slide 11 of 24
  • 12. Querying and Managing Data Using SQL Server 2005 Reverting Transactions Transactions are reverted: When the execution of transaction is in an invalid state To maintain consistency Using the ROLLBACK TRANSACTION and ROLLBACK WORK statements Syntax: ROLLBACK [TRAN[SACTION] [transaction_name |@tran_name_variable |savepoint_name | @savepoint_variable]] Let’s see how… Ver. 1.0 Session 14 Slide 12 of 24
  • 13. Querying and Managing Data Using SQL Server 2005 Implementing Transactional Integrity (Contd.) Flash presentation: Implementing Locks Locks: Help in achieving transactional integrity Help in avoiding: • Lost updates • Uncommitted dependency • Inconsistent analysis (Dirty Read) • Phantom reads Supported by SQL Server are: Shared locks Exclusive locks Update locks Intent locks Schema locks Bulk update locks Ver. 1.0 Session 14 Slide 13 of 24
  • 14. Querying and Managing Data Using SQL Server 2005 Implementing Transactional Integrity (Contd.) Locking is controlled by the following types of isolation levels: READ UNCOMMITTED READ COMMITED REPEATABLE READ SNAPSHOT SERIALIZABLE Ver. 1.0 Session 14 Slide 14 of 24
  • 15. Querying and Managing Data Using SQL Server 2005 Implementing Transactional Integrity (Contd.) Locks can be implemented using the SET TRANSACTION ISOLATION LEVEL statement: Syntax: SET TRANSACTION ISOLATION LEVEL { READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SNAPSHOT | SERIALIZABLE } [ ;] BEGIN TRANSACTION ……… ……… COMMIT TRANSACTION Let’s see how… Ver. 1.0 Session 14 Slide 15 of 24
  • 16. Querying and Managing Data Using SQL Server 2005 Resolving Deadlocks You can resolve deadlocks by: Detecting deadlocks Avoiding deadlocks by using Update locks Ver. 1.0 Session 14 Slide 16 of 24
  • 17. Querying and Managing Data Using SQL Server 2005 Just a minute Which of the following concurrency problems is also known as DIRTY READ? 1. Uncommitted dependency 2. Phantom problem 3. Inconsistence analysis Answer: 1. Uncommitted dependency Ver. 1.0 Session 14 Slide 17 of 24
  • 18. Querying and Managing Data Using SQL Server 2005 Just a minute Which of the following locks enable others to view the data being modified by the current transaction? 1. Shared lock 2. Exclusive lock 3. Update lock 4. Intent lock 5. Bulk update lock Answer: 1. Shared lock Ver. 1.0 Session 14 Slide 18 of 24
  • 19. Querying and Managing Data Using SQL Server 2005 Just a minute Which of the following locks prevent your database from deadlocks? 1. Intent lock 2. Update lock 3. Shared lock Answer: 2. Update lock Ver. 1.0 Session 14 Slide 19 of 24
  • 20. Querying and Managing Data Using SQL Server 2005 Demo: Implementing Transactions • Problem Statement: At AdventureWorks, Inc., an employee named Sidney Higa, who is currently working as Production Technician – WC10 has been promoted as Marketing Manager. The employee ID of Sidney is 13. As a database developer, you need to update his records. This involves updating the title in the Employee table and updating the department history details. You need to ensure that all the changes take effect. In addition, you need to ensure that no other transaction should be able to view the data being modified by the current transaction. Ver. 1.0 Session 14 Slide 20 of 24
  • 21. Querying and Managing Data Using SQL Server 2005 Demo: Implementing Transactions (Contd.) Solution: To solve the preceding problem, you need to perform the following tasks: 1. Create a transaction. 2. Verify the output. Ver. 1.0 Session 14 Slide 21 of 24
  • 22. Querying and Managing Data Using SQL Server 2005 Summary In this session, you learned that: A trigger is a block of code that constitutes a set of T-SQL statements that are activated in response to certain actions. The SQL Server supports following triggers: • DML triggers • DDL triggers – A trigger can be altered by using the ALTER TRIGGER statement. – A trigger can be deleted by using the DROP TRIGGER statement. – Transactions are used to execute a sequence of statements together as a single logical unit of work. Ver. 1.0 Session 14 Slide 22 of 24
  • 23. Querying and Managing Data Using SQL Server 2005 Summary (Contd.) Every transaction posses the ACID property. The SQL Server supports following transactions: • Autocommit transaction • Explicit transaction Locks are used to maintain transactional integrity. In the absence of locking, following problems may occur if transactions use the same data from a database at the same time: Lost updates Uncommitted dependency Inconsistent analysis Phantom reads Ver. 1.0 Session 14 Slide 23 of 24
  • 24. Querying and Managing Data Using SQL Server 2005 Summary (Contd.) The SQL Server supports following lock modes: • Shared locks • Exclusive locks • Update locks • Intent locks • Schema locks • Bulk Update locks A deadlock is a situation in which two users (or transactions) have locks on separate objects, and each user wants to acquire a lock on the other user’s object. Ver. 1.0 Session 14 Slide 24 of 24

Editor's Notes

  1. Start the session by sharing the objectives with the students.
  2. In this slide, you need to explain the triggers and various types of triggers to the students. Stress on the fact that triggers are used when complex business rules have to be implemented. While constraints can be used to maintain referential integrity, triggers can also be used if required. Mention that triggers are a special type of stored procedure, but cannot be executed explicitly. Mention that the overhead involved with a trigger is very high, but the functionality provided is also very good. You also need to touch upon the cascade delete, restrict delete, and nullify delete rules in your class. If a record is deleted from the master table, then the corresponding records from the transaction table also get deleted. This is the cascade delete rule. In the restrict delete rule, if an open transaction exists in the transaction table, then the corresponding records in the parent table cannot be deleted. In the nullify delete rule, if a record is deleted from a parent table, then the corresponding values in the foreign key column of the child tables is replaced by NULL. Discuss the concept of nesting levels of triggers. Follow naming conventions for triggers. Prefix a trigger name with ‘trg’. Multiple Triggers Tell the students that multiple triggers for a DML operation can be created on the same table. For instance, in a table called TAB1 you can create two triggers TRIG1 and TRIG2, both for update operation. Also, mention that this facility was not available in the earlier versions of SQL Server. The benefit of using multiple triggers is that you can implement multiple business rules using different triggers. However, you can incorporate all the rules in a single trigger. Having multiple triggers helps in maintenance, readability, and documentation. The triggers are executed in the order they have been created. Instead Of Triggers Tell the students that in SQL Server, you have a new variation of the database object trigger. This type of trigger is used to update the base tables of a view when a view is created on multiple tables. This type of trigger is particularly useful for validating insert values before inserting in the base tables. The instead of triggers can be created on tables or views. In case a table contains primary key or foreign key constraints that implement with cascade delete or cascade update functionality, then the instead of delete and instead of update triggers cannot be defined on them.
  3. Reiterate the learning by asking the given question.
  4. In this slide, you need to explain creating triggers to the students. Example: CREATE TRIGGER [HumanResources].[trgDepartment] ON [HumanResources].[Department] AFTER UPDATE AS BEGIN UPDATE [HumanResources].[Department] SET [HumanResources].[Department].[ModifiedDate] = GETDATE() FROM inserted WHERE inserted.[DepartmentID] = [HumanResources].[Department].[DepartmentID]; END; CG NOTE: For demonstration of this example, you can use the create_trDepartment.sql file from the Datafiles_for_faculty\\Chapter 8 folder in the TIRM CD. In this file, you will find the code to create the trigger as well as to create the trgMagic trigger and the update statement to verify the trigger. FAQ: Q: When does a trigger get executed? After a DML (update, insert, or delete) transaction. Q: If there exists a trigger and a rule, which will get executed first? The rule will get executed first. Additional Inputs The maximum nesting level for triggers is 32. You cannot create triggers on system tables. Triggers unlike stored procedures do not return values or result sets. If multiple business rules need to be applied when a DML operation is underway use multiple triggers for implementing the same. For example, if three columns are being updated and different business rules have to be applied for each, use three different update triggers for each business rule. SQL Server allows recursive triggers. Recursion occurs when the same trigger gets executed again and again. There are two types of recursion, direct and indirect. For example, if an application updates table T3, the trigger TRIG3 defined on the table for update gets executed. This trigger again does an updation on the table T3, thereby, re-executing the trigger TRIG3. This is an example of direct recursion. If an application updates table T3, the trigger TRIG3 defined on the table for update gets executed. This trigger updates another table T4, this executes trigger TRIG4 defined for update on the table. TRIG4 updates table T3 thereby executing TRIG3. This is an example of indirect recursion. To enable recursive triggers for a particular database, issue the following command: sp_dboption <databasename>, ‘recursive triggers’, True.
  5. In this slide, you need to explain managing the triggers to the students. State that managing trigger includes altering the trigger and deleting a trigger. Example: ALTER TRIGGER HumanResources.trgInsertShift ON HumanResources.Shift FOR INSERT AS DECLARE @ModifiedDate datetime SELECT @ModifiedDate = ModifiedDate FROM Inserted IF (@ModifiedDate != getdate()) BEGIN RAISERROR (’The modified date is not the current date. The transaction cannot be processed.',10, 1) ROLLBACK TRANSACTION END RETURN CG Input: For demonstration use the alter_trgInsertShift.sql file in the datafiles_for_faculty\\chapter 8 folder in the TIRM CD. Example: DROP TRIGGER HumanResources.trgMagic
  6. Reiterate the learning by asking the given question.
  7. You need to ensure that after the end of the demo, the students are able to create and implement triggers. CG Input: You can use the codes given in the Demo1.sql data file in the datafiles_for_faculty\\chapter 8 folder in the TIRM CD.
  8. CG Input: You can use the codes given in the Demo1.sql data file in the datafiles_for_faculty\\chapter 8 folder in the TIRM CD.
  9. In this slide, you need to explain the concept of transactions to the students. In addition, also explain the ACID properties of a transaction. BEGIN TRAN myTran SELECT * FROM HumanResources.Department COMMIT TRAN myTran FAQ: To switch between the implicit and the explicit modes, use the SET IMPLICIT_TRANSACTIONS {ON | OFF} statement In the implicit mode the following statements trigger off a transaction: ALTER TABLE, INSERT, OPEN, CREATE, DELETE, REVOKE, DROP, SELECT, FETCH, TRUNCATE TABLE, GRANT, UPDATE. The number of open transactions per connection is stored in the system variable @@TRANCOUNT. Every new transaction i.e. every BEGIN TRANSACTION increments the value of this system variable by one and every COMMIT TRANSACTION or ROLLBACK TRANSACTION decrements the value by one. In the implicit mode, every issue of the above mentioned commands automatically generates a BEGIN TRANSACTION. Experiences: Transaction Stress on the fact that a transaction is an atomic unit of work, which either happens completely or does not happen at all. If an insert operation in one table and two update operations in two different tables constitute a logical unit of work, then the three operations can be termed as a transaction. If only one insert happens and the other two updates do not happen, the transaction is not complete and may result in inconsistency of data. Hence, it is essential that all the operations happen or none of them happens at all. Explicit statements like BEGIN TRANSACTION and COMMIT TRANSACTION ensure that all statements in a transaction are completed successfully or do not take place at all in case there is a system crash while the transaction is running. By default SQL Server uses a row level lock. Tell the students that the transactions should be as short as possible, and table level locks should be avoided as this locks the entire table. If multiple transactions refer to the same tables, then refer them in a specific order to minimize deadlocks. While creating a transaction, follow the naming conventions. Prefix the transaction name with a ‘trn’. Transaction Log Tell the students that the transaction log is like a huge ‘security register’ where any activity on the database gets recorded. The log is used to roll forward or rollback transactions in case of a system failure. Tell them that the transaction log plays a big role in transaction management. Transaction Modes There are two types of transaction modes, explicit and implicit. The default is implicit, this can be changed by using BEGIN TRANSACTION, COMMIT TRANSACTION, COMMIT WORK, ROLLBACK TRANSACTION, and ROLLBACK WORK. SAVE TRANSACTION can be used to save transactions to a certain point. Tell the students when SAVE TRANSACTION is used, and if we rollback a transaction then the transaction rolls back only up till the save point. Distributed Transactions Unlike normal transactions, a distributed transaction is processed on more than one database server.
  10. Reiterate the learning by asking the given question.
  11. In this slide, you need to explain to revert the changes done by a transaction to the students. Example: BEGIN TRANSACTION TR BEGIN TRY UPDATE Person.Contact SET EmailAddress='jolyn@yahoo.com' WHERE ContactID = 1070 UPDATE HumanResources.EmployeeAddress SET AddressID = 32533 WHERE EmployeeID = 1 COMMIT TRANSACTION TR SELECT 'Transaction Executed' END TRY BEGIN CATCH ROLLBACK TRANSACTION TR SELECT 'Transaction Rollbacked' END CATCH CG Input: You can use the codes given in the create_transaction_tr1.sql data file in the datafiles_for_faculty\\chapter 8 folder in the TIRM CD.
  12. In this slide, you need to explain transactional integrity, problems that occur due to transactional integrity, and implementing transactional integrity in your transactions. In addition, you also need to talk about Concurrency Problems Find below the examples for each of the concurrency problems: Lost updates Lost updates occur when two or more transactions select the same row and then update the row based on the value originally selected. Each transaction is unaware of the other transaction. The last update overwrite updates made by the other transaction, which results in lost data. Let us assume that both Sam and Anne are simultaneously trying to update the price of all the “Business” books in the Titles table. Sam is trying to update the price by 10% while Anne is trying to update the price by 15%. Now, the table will get updated by the changes of the query that will get completed last. That means, if Sam’s query is executed later than Anne’s query, then the price column in titles table will get increased by 10% and the changes made by Anne’s query will be lost. Uncommitted dependency Uncommitted dependency occurs when a second transaction selects a row that is being updated by another transaction. The second transaction is reading data that has not been committed yet and may be changed by the transaction updating the row. Let us assume User A and B are working on titles table. User A had increased the price of title_id ‘BU1032’ by Rs. 10. But user A does not commit the transaction. Now User B tries to execute a query on title_id ‘BU1032’. User B is accessing old record as the transaction handle by user A is not yet complete. Therefore user B also updates the price of title_id ‘BU1032’ by Rs. 5. These transactions will update the record by Rs 15. Such kind of problems leads to inconsistency in the table. Inconsistent Analysis Inconsistent analysis occurs when a second transaction accesses the same row several times and reads different data each time. Inconsistent analysis is similar to uncommitted dependency in that another transaction is changing the data that a second transaction is reading. However, in inconsistent analysis, the data read by the second transaction was committed by the transaction that made the change. Also, inconsistent analysis involves multiple reads (two or more) of the same row and each time the information is changed by another transaction; thus, the term nonrepeatable read. For example: Let us assume that you are accessing the online reservation system to check the status of your ticket. The site showed the status as ‘Waiting’. Just a little later, when you refreshed the page, you found that the status is ‘confirmed’. This shows that while you were browsing thru the information, some procedure was updating the record information. Phantom reads Phantom reads occur when an insert or delete action is performed against a row that belongs to a range of rows being read by a transaction. The transaction's first read of the range of rows shows a row that no longer exists in the second or succeeding read, as a result of a deletion by a different transaction. Similarly, as the result of an insert by a different transaction, the transaction's second or succeeding read shows a row that did not exist in the original read. For Example: You are accessing online catalog of a book store. You found a book name “You can win” in your initial search where you are looking for titles having “win” in their title name. But subsequent search of the same query did not show “You can win” in the output. The reason can be that some procedure might have deleted the title from the table. Note: Though these scenarios will help you to explain the concept, please clarify to the students that these situations occur in remote multi-user environment. They will not find similar scenario while working in the MR. LOCKS Discuss the types of locks in detail. Also, discuss when each of these locks are used by SQL Server. SQL Server uses row lock by default. The concept of deadlock needs to be explained to the students. Tell the students that if the deadlock priority is set low for a transaction, then the transaction has a higher probability of becoming the deadlock victim in a deadlock situation. Mention that lock_timeout is used on a transaction if you do not want a transaction to wait for an indefinite period. Using lock_timeout you can mention in milliseconds how long a transaction will wait for a lock to open. Tell the students that whenever an ad hoc DML statement is executed, SQL Server, by default, treats the statement as a transaction and commits the transaction. This mode is called the auto commit mode. Tell the students that although SQL Server 2000 uses dynamic locking, it is still very important for you to differentiate between the different locking modes. The different types of locks used by SQL Server 2000 are shared, update, exclusive, intent, schema, and bulk-update.
  13. In this slide, you need to explain the concept of Isolation Level. Example: SET TRANSACTION ISOLATION LEVEL READ COMMITTED BEGIN TRANSACTION TR BEGIN TRY UPDATE Person.Contact SET EmailAddress='jolyn@yahoo.com' WHERE ContactID = 1070 UPDATE HumanResources.EmployeeAddress SET AddressID = 32533 WHERE EmployeeID = 1 COMMIT TRANSACTION TR SELECT 'Transaction Executed' END TRY BEGIN CATCH ROLLBACK TRANSACTION TR SELECT 'Transaction Rollbacked' END CATCH Additional Inputs Timeouts can be used to prevent deadlocks. For multiple transactions running simultaneously on a SQL Server, you can define their isolation level to balance between concurrency and data integrity. By choosing the right transaction, isolation level can improve performance of the SQL Server queries. There are four transaction isolation levels: Read Committed  This is the default isolation level. Read Uncommitted  The restriction in this isolation level is the least as there are no shared or exclusive locks. This allows data updates before the transaction is over. Repeatable Read  In this isolation level, rows can be added but existing data cannot be updated. Serializable  Data integrity is the highest in this isolation level but concurrency between transactions is very low. Data involved in this transaction isolation level is locked. Transactions with this isolation level execute one by one. The syntax for setting transaction isolation levels is shown below: SET TRANSACTION ISOLATION LEVEL {READ COMMITTED|READ UNCOMMITTED|REPEATABLE READ|SERIALIZABLE} When an INSENSETIVE cursor is created SQL Server stores the result set of the cursor in a temporary table in the tempdb database. This result set does not get updated with changes made to the base table(s). Apart from this the cursor itself is a read-only cursor i.e. it cannot be updated. The following code declares an INSENSETIVE cursor: DECLARE curPublishers CURSOR FOR SELECT * FROM publishers OPEN curPublishers FETCH NEXT FROM curPublishers <<<example needs to be changed>>>
  14. Example: SET TRANSACTION ISOLATION LEVEL READ COMMITTED BEGIN TRANSACTION TR BEGIN TRY UPDATE Person.Contact SET EmailAddress='jolyn@yahoo.com' WHERE ContactID = 1070 UPDATE HumanResources.EmployeeAddress SET AddressID = 32533 WHERE EmployeeID = 1 COMMIT TRANSACTION TR SELECT 'Transaction Executed' END TRY BEGIN CATCH ROLLBACK TRANSACTION TR SELECT 'Transaction Rollbacked' END CATCH Additional Inputs Timeouts can be used to prevent deadlocks. For multiple transactions running simultaneously on a SQL Server, you can define their isolation level to balance between concurrency and data integrity. By choosing the right transaction, isolation level can improve performance of the SQL Server queries. There are four transaction isolation levels: Read Committed  This is the default isolation level. Read Uncommitted  The restriction in this isolation level is the least as there are no shared or exclusive locks. This allows data updates before the transaction is over. Repeatable Read  In this isolation level, rows can be added but existing data cannot be updated. Serializable  Data integrity is the highest in this isolation level but concurrency between transactions is very low. Data involved in this transaction isolation level is locked. Transactions with this isolation level execute one by one. The syntax for setting transaction isolation levels is shown below: SET TRANSACTION ISOLATION LEVEL {READ COMMITTED|READ UNCOMMITTED|REPEATABLE READ|SERIALIZABLE} When an INSENSETIVE cursor is created SQL Server stores the result set of the cursor in a temporary table in the tempdb database. This result set does not get updated with changes made to the base table(s). Apart from this the cursor itself is a read-only cursor i.e. it cannot be updated. The following code declares an INSENSETIVE cursor: DECLARE curPublishers CURSOR FOR SELECT * FROM publishers OPEN curPublishers FETCH NEXT FROM curPublishers <<<example needs to be changed>>>
  15. In this slide, you need to explain the concept of deadlocks and how to resolve them. Do not execute any of the statements in the class, instead focus on clearing the concept. Deadlock Suppose a delete trigger called Trigger1 has been defined on a table called Table1 and there is a delete trigger called Trigger2 on another table called Table2. Trigger1 deletes a row on Table2 and Trigger2 deletes a row on Table1. If you delete a row in Table1, then Trigger1 will try to obtain an exclusive lock on Table2 and Trigger2, which will be fired now, will try to acquire an exclusive lock on Table1 that is already locked. Both these transactions wait for the other to release the locks imposed by them. This causes a stalemate because neither application can release its locks and finish its session while waiting for the other application to release its locks. SQL Server automatically fixes this by choosing one application, forcing it to release the lock and allowing the other session to continue. By setting the DEADLOCK PRIORITY, you can decide which session is more likely to be the next deadlock looser. SQL Server will release the lock of the session that has lower priority. Set the DEADLOCK PRIORITY using the following syntax: SET DEADLOCK_PRIORITY {LOW | NORMAL}. FAQ: Q.: How does the transaction log help in transaction management? Every transaction is recorded in the transaction log to maintain database consistency and aid in recovery. Q: When will SQL Server use an update lock? When updating rows, SQL Server first searches for the records and uses a shared lock in the process. Once the records are located, the shared lock is upgraded to an exclusive lock. If another transaction has applied a shared lock on the resource, the shared lock imposed while searching for the records cannot be upgraded. To avoid this SQL Server uses an update lock while updating records. Q: In case there are a lot of transactions, which lock will you request for while updating a row in a table? You should request for an intent lock. Intent locks impose locks higher up in the hierarchy and only those locks are compared instead of comparing locks in the lower level.
  16. Reiterate the learning by asking the given question.
  17. Reiterate the learning by asking the given question.
  18. Reiterate the learning by asking the given question.
  19. You need to ensure that after the end of this demo, the students are able to implement Transactions.
  20. You can summarize the session by running through the summary given in SG. In addition, you can also ask students summarize what they have learnt in this session.
  21. You can summarize the session by running through the summary given in SG. In addition, you can also ask students summarize what they have learnt in this session.
  22. You can summarize the session by running through the summary given in SG. In addition, you can also ask students summarize what they have learnt in this session.