Beruflich Dokumente
Kultur Dokumente
Session Objectives
Define triggers List the uses of triggers List the guidelines for creating triggers Explain the CREATE TRIGGER T-SQL statement List the types of triggers Explain INSERT triggers Explain UPDATE triggers Explain DELETE triggers
Triggers
Derek
Triggers
Derek
Inserted
Uses of Triggers
Compare versions of data Read data from other tables in other databases Cascade changes or deletes throughout related tables in a database Reverse invalid changes Enforce more complex restrictions than CHECK constraints Execute local and remote stored procedures
Creating Triggers
Syntax
CREATE TRIGGER Trigger_name ON table FOR {[DELETE] [,] [INSERT][,][UPDATE]} [WITH ENCRYPTION] AS Sql_statement
Logical tables that are accessible to triggers Contain images of data prior to and after the updation Schema identical to the table being updated
Considerations
A trigger can be associated with three actions performed on a table: INSERT, UPDATE, and DELETE. A trigger applies to a single table. The WITH ENCRYPTION option can hide a trigger definition. An encrypted trigger can, however, not be decrypted. A trigger can reference a view or a temporary table, but cannot be associated with it.
Considerations (contd.)
A trigger can include any number of SQL statements. By default, the permission to create the trigger is available only to the owner of the database. This permission is not transferable. A trigger can be created only in the current database. However, it can refer to the objects of another database.
Types of Triggers
Insert Trigger
Example
CREATE TRIGGER CheckFare ON flight_details FOR INSERT AS IF (SELECT fare FROM INSERTED AS i JOIN flight AS f ON i.aircraft_code = f.aircraft_code JOIN airlines_master AS am ON f.aircode = am.aircode WHERE i.class_code = 'FC' AND am.airline_name = 'Indian Airlines') > 8000 BEGIN PRINT 'CheckFareTrigger: Fare for FC class of Indian Airlines flights cannot exceed 8000' PRINT 'Change the fare to a value less than 8000' ROLLBACK TRANSACTION END
Update Triggers
Delete Trigger
Example
CREATE TRIGGER NoDeleteBA01 ON Flight FOR DELETE AS IF (SELECT aircraft_code FROM deleted) = BA01 BEGIN PRINT You cannot delete the details of aircraft code BA01 ROLLBACK TRANSACTION END
Nested Triggers
A trigger initiates another trigger Nesting is permitted upto 32 levels The @@nestlevel system global variable indicates the current nesting level sp_configure enables or disables nesting of triggers
Cascading Triggers
Enforce referential integrity Modify data in related tables if a change occurs in one table Executed after constraints are tested
INSTEAD OF Triggers
Contain code that replaces the original data manipulation statement Useful for data modifications through nonupdateable views Can only be based on one data modification action
CREATE TRIGGER del_service ON service_view INSTEAD OF DELETE AS DELETE service WHERE service_code IN (SELECT scode1 FROM DELETED) DELETE airline_service WHERE service_code IN (SELECT scode2 FROM DELETED)
Performance of Triggers
Overhead associated with triggers is usually low A significant amount of time is spent in referencing tables other than logical tables Deleted and inserted logical tables are always present in memory