Download presentation
Presentation is loading. Please wait.
Published byNeal Dennis Modified over 6 years ago
1
Module 6: Implementing Data Integrity by Using Triggers and XML Schemas
2
Overview Implementing Triggers Implementing XML Schemas
3
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
4
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
5
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
6
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;
7
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;
8
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; int; = COUNT(*) FROM deleted; > BEGIN … END; END;
9
How Nested Triggers Work
INSERT, UPDATE, or DELETE statement 1 3 …and so on… 2 Trigger executes INSERT, UPDATE, or DELETE on another table…
10
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
11
Practice: Creating Triggers
In this practice, you will: Drop existing triggers Create an UPDATE trigger Create an INSTEAD OF trigger
12
Lesson 2: Implementing XML Schemas
What Are XML Schemas? What Is an XML Schema Collection? What Is Typed XML? Practice: Using Typed XML
13
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
14
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
15
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
16
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
17
Lab: Implementing Data Integrity by Using Triggers and XML Schemas
Exercise 1: Creating Triggers Exercise 2: Implementing XML Schemas
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.