Instructor Inputs - Session 13 pptx

24 122 0
Instructor Inputs - Session 13 pptx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Instructor Inputs Session 13 ¤NIIT Instructor Inputs 13.3 This session includes Chapter 8 of the Student Guide. Slide 1 Slide 1 of 24Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement triggers Implement transactions Objectives Start the session by sharing the objectives with the students. In this session, the students will learn the importance of triggers and how to implement them. In addition, they will also learn how to implement transactions to maintain data integrity. Session Overview 13.4 Instructor Inputs ¤NIIT Slide 2 Slide 2 of 24Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 Triggers are of the following types: DML triggers DDL triggers Identifying Types of Triggers In this topic, you need to explain the triggers and various types of triggers. Tell the importance of using triggers. You can tell 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. Also mention that the overhead involved with a trigger is very high, but the functionality provided is also very useful. You can also discuss the cascade delete, restrict delete, and nullify delete rules. 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. To explain the utility of an insteadof trigger, tell 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. ¤NIIT Instructor Inputs 13.5 Mention that DDL triggers are used by database administrators. Slide 3 Slide 3 of 24Session 13 Ver. 1.0 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 Reiterate the learning by asking the given question. Slide 4 Slide 4 of 24Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 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… Creating Triggers In this topic, you need to explain how to create a trigger using the CREATE TRIGGER statement. 13.6 Instructor Inputs ¤NIIT For demonstration of this example, you can use the create_trDepartment.sql file from the Datafiles_for_faculty\ QMDS2005\chapter 08\Instep_Demo 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. In this topic, you need to demonstrate how to create different types of triggers. You can use the examples given in the Student Guide. For each example, the code to create trigger is given in datafiles, shown in the following table. Example Data file Handling Inputs Creating an insert trigger trgInsertShift.sql During the insertion operation, the record will not be inserted if the modified date is not the current date. Creating a delete trigger trgDeleteDepartment.sql You can test by inserting a new record in department table and then trying to delete that record. You will notice that the tigger is executed and the record will not be deleted. Creating an update trigger trgUpdateEmployeePayHistory.sql You can test by updating the Rate for all the employees. You will notice that the tigger is executed and the record will not be deleted. Creating an after trigger trgDeleteShift.sql To verify the trigger, you need to first insert a record in the shift table and then delete it. After deletion the trigger will display the message. ¤NIIT Instructor Inputs 13.7 Note Example Data file Handling Inputs To set trigger order setTrigger.sql To verify the trigger, you need to first create the trgDeleteShift1 trigger on the Shift table and execute the sp_settriggerorder statement to set the order. Next, you can insert a new record in the shift table and then delete it. After deletion, the trigger will display the messages displayed by both the triggers. Note that the message displayed by the trgDeleteShift1 trigger appears first. Data Files to be Used The ROLLBACK TRANSACTION statement is used to roll back transactions. The ROLLBACK TRANSACTION statement in the trgInsertShift trigger is used to undo the insert operation. Mention that triggers cannot be created 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. 13.8 Instructor Inputs ¤NIIT To enable recursive triggers for a particular database, issue the following command: sp_dboption <databasename>, ‘recursive triggers’, True. Slide 5 Slide 5 of 24Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 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… Managing Triggers In this topic, you need to explain managing the triggers to the students. State that managing trigger includes altering the trigger and deleting a trigger. Explain the syntax and usage of the ALTER TRIGGER and DROP TRIGGER statements. For demonstration use the alter_trgInsertShift.sql file in the Datafiles_for_faculty\ QMDS2005\chapter 08\Instep_Demo folder in the TIRM CD. ¤NIIT Instructor Inputs 13.9 Slide 6 Slide 6 of 24Session 13 Ver. 1.0 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 Reiterate the learning by asking the given question. Slide 7 Slide 7 of 24Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 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 Demo: Implementing Triggers At the end of the demo, the students will be able to create and implement triggers. You can use the codes given in the Demo1.sql data file in the Datafiles_for_faculty\ QMDS2005\chapter 08\Instep_Demo folder in the TIRM CD. 13.10 Instructor Inputs ¤NIIT Slide 8 Slide 8 of 24Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 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? Demo: Implementing Triggers (Contd.) For demonstration purpose, you can use the codes given in the Demo1.sql data file in the Datafiles_for_faculty\QMDS2005\Chapter 08\Activity folder in the TIRM CD. Slide 9 Slide 9 of 24Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 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. Demo: Implementing Triggers (Contd.) [...]... | @savepoint_variable]] Let’s see how… Ver 1.0 Session 13 Slide 12 of 24 In this topic, you need to explain how to revert the changes done by a transaction to the students You can use the codes given in the create_transaction_tr1.sql data file in the datafiles_for_faculty\chapter 08\Instep_Demo folder in the TIRM CD NIIT Instructor Inputs 13. 13 Slide 13 Querying and Managing Data Using SQL Server 2005... folder in the TIRM CD 13. 20 Instructor Inputs NIIT Slide 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 Session 13 Ver 1.0 Slide 21 of 24 Slide 22 Querying and Managing Data Using SQL Server 2005 Summary In this session, you learned... those locks are compared instead of comparing locks in the lower level 5 If there exists a trigger and a rule, which will get executed first? Ans: The rule will get executed first NIIT Instructor Inputs 13. 23 13. 24 Instructor Inputs NIIT ... Session 13 Slide 18 of 24 Reiterate the learning by asking the given question NIIT Instructor Inputs 13. 19 Slide 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 13 Slide 19 of 24 Reiterate the learning by asking the given question Slide 20... transactions, a distributed transaction is processed on more than one database server 13. 12 Instructor Inputs NIIT Slide 11 Querying and Managing Data Using SQL Server 2005 Just a minute Which of the following properties does a transaction NOT posses? 1 2 3 4 Atomicity Consistency Isolation Separation Answer: 4 Separation Session 13 Ver 1.0 Slide 11 of 24 Reiterate the learning by asking the given question... 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 13. 22 Instructor Inputs Session 13 Slide 24 of 24 NIIT FAQs 1 Is it possible to create multiple triggers for a DML operation? Ans: Multiple triggers for a DML operation can be created on the same table For instance, in... read committed This disables other transaction to read the uncommitted updates in the tables NIIT Instructor Inputs 13. 17 Slide 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 13 Slide 16 of 24 In this topic, you need to explain the concept of deadlocks and how to resolve... 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... 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 13 Slide 22 of 24 Summarize the session NIIT Instructor Inputs 13. 21 Slide 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... Uncommitted dependency Session 13 Ver 1.0 Slide 17 of 24 Reiterate the learning by asking the given question Slide 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 2 3 4 5 Shared lock Exclusive lock Update lock Intent lock Bulk update lock Answer: 1 Shared lock Ver 1.0 Session 13 Slide 18 . Instructor Inputs Session 13 ¤NIIT Instructor Inputs 13. 3 This session includes Chapter 8 of the Student Guide. Slide 1 Slide 1 of 2 4Session 13 Ver. 1.0 Querying and. datafiles_for_facultychapter 08Instep_Demo folder in the TIRM CD. 13. 14 Instructor Inputs ¤NIIT Slide 13 Slide 13 of 2 4Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 Flash. transaction is processed on more than one database server. ¤NIIT Instructor Inputs 13. 13 Slide 11 Slide 11 of 2 4Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 Just a

Ngày đăng: 31/07/2014, 15:20

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan