Download presentation
Presentation is loading. Please wait.
Published byHugh McCormick Modified over 8 years ago
1
In this session, you will learn to: Implement triggers Implement transactions Objectives
2
Triggers are of the following types: DML triggers DDL triggers Identifying Types of Triggers
3
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
4
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
5
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
6
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
7
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
8
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.)
9
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.)
10
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… Creating Transactions
11
Just a minute Which of the following properties does a transaction NOT posses? 1.Atomicity 2.Consistency 3.Isolation 4.Separation Answer: 4.Separation
12
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… Reverting Transactions
13
Flash presentation: Implementing LocksImplementing 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 Implementing Transactional Integrity (Contd.)
14
Locking is controlled by the following types of isolation levels: READ UNCOMMITTED READ COMMITED REPEATABLE READ SNAPSHOT SERIALIZABLE Implementing Transactional Integrity (Contd.)
15
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… Implementing Transactional Integrity (Contd.)
16
You can resolve deadlocks by: Detecting deadlocks Avoiding deadlocks by using Update locks Resolving Deadlocks
17
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
18
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
19
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
20
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. Demo: Implementing Transactions
21
Solution: To solve the preceding problem, you need to perform the following tasks: 1.Create a transaction. 2.Verify the output. Demo: Implementing Transactions (Contd.)
22
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. Summary
23
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 Summary (Contd.)
24
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. Summary (Contd.)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.