Module 6: Implementing Data Integrity by Using Triggers and XML Schemas
Overview Implementing Triggers Implementing XML Schemas
Lesson 1: Implementing Triggers What Are Triggers? How an INSERT Trigger Works How a DELETE Trigger Works How an UPDATE Trigger Works How an INSTEAD OF Trigger Works How Nested Triggers Work Considerations for Recursive Triggers Practice: Creating Triggers
What Are Triggers? Special stored procedures that execute when INSERT, UPDATE, or DELETE statements modify a table Two categories: AFTER triggers execute after an INSERT, UPDATE, or DELETE statement INSTEAD OF triggers execute instead of an INSERT, UPDATE, or DELETE statement Trigger and the initiating statement are part of a single transaction
How an INSERT Trigger Works 1 INSERT statement executed 2 INSERT statement logged 3 AFTER INSERT trigger statements executed CREATE TRIGGER [insrtWorkOrder] ON [Production].[WorkOrder] AFTER INSERT AS BEGIN SET NOCOUNT ON; INSERT INTO [Production].[TransactionHistory]( [ProductID],[ReferenceOrderID],[TransactionType] ,[TransactionDate],[Quantity],[ActualCost]) SELECT inserted.[ProductID],inserted.[WorkOrderID] ,'W',GETDATE(),inserted.[OrderQty],0 FROM inserted; End
How a DELETE Trigger Works DELETE statement executed 1 2 DELETE statement logged 3 AFTER DELETE trigger statements executed CREATE TRIGGER [delCategory] ON [Categories] AFTER DELETE AS BEGIN UPDATE P SET [Discontinued] = 1 FROM [Products] P INNER JOIN deleted as d ON P.[CategoryID] = d.[CategoryID] END;
How an UPDATE Trigger Works 1 UPDATE statement executed 2 UPDATE statement logged 3 AFTER UPDATE trigger statements executed CREATE TRIGGER [updtProductReview] ON [Production].[ProductReview] AFTER UPDATE NOT FOR REPLICATION AS BEGIN UPDATE [Production].[ProductReview] SET [Production].[ProductReview].[ModifiedDate] = GETDATE() FROM inserted WHERE inserted.[ProductReviewID] = [Production].[ProductReview].[ProductReviewID]; END;
How an INSTEAD OF Trigger Works 1 UPDATE, INSERT, or DELETE statement executed 2 Executed statement does not occur 3 INSTEAD OF trigger statements executed CREATE TRIGGER [delEmployee] ON [HumanResources].[Employee] INSTEAD OF DELETE NOT FOR REPLICATION AS BEGIN SET NOCOUNT ON; DECLARE @DeleteCount int; SELECT @DeleteCount = COUNT(*) FROM deleted; IF @DeleteCount > 0 BEGIN … END; END;
How Nested Triggers Work INSERT, UPDATE, or DELETE statement 1 3 …and so on… 2 Trigger executes INSERT, UPDATE, or DELETE on another table…
Considerations for Recursive Triggers Disabled by default. To enable: Considerations: Can exceed the 32-level nesting limit without careful design and thorough testing Can be difficult to control the order of table updates Can be replaced with nonrecursive logic ALTER DATABASE AdventureWorks SET RECURSIVE_TRIGGERS ON
Practice: Creating Triggers In this practice, you will: Drop existing triggers Create an UPDATE trigger Create an INSTEAD OF trigger
Lesson 2: Implementing XML Schemas What Are XML Schemas? What Is an XML Schema Collection? What Is Typed XML? Practice: Using Typed XML
What Are XML Schemas? Defines the elements and attributes that are valid in an XML document Uses the XML Schema syntax defined by the W3C
What Is an XML Schema Collection? Named collection of one or more XML schemas Associated with columns or variables of xml data type to provide typed XML capability Created by using CREATE XML SCHEMA COLLECTION statement
What Is Typed XML? Columns or variables of the xml data type that are associated with an XML schema collection SQL Server validates the contained XML against XML schemas in the associated XML schema collection
Practice: Using Typed XML In this practice, you will: Create an XML schema collection Create a typed XML column Insert valid and invalid data into a typed XML column
Lab: Implementing Data Integrity by Using Triggers and XML Schemas Exercise 1: Creating Triggers Exercise 2: Implementing XML Schemas