Sie sind auf Seite 1von 27

Implementing Triggers

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

Session Objectives (contd.)


Identify SQL statements that cannot be used in triggers Define nested and cascading triggers Explain Instead Of triggers List the factors that affect the performance of triggers

Triggers

Allen Brian Cathy Greg

Mark Carl Kathy

Derek

Triggers

Allen Brian Cathy Greg Trigger Fired

Mark Carl Kathy Derek

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

Inserted & Deleted Tables

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

Insert Trigger (contd.)

Update Triggers

Column level Update trigger


Example
CREATE TRIGGER NoUpdateMealcode ON Meal FOR UPDATE AS IF UPDATE (Meal_code) BEGIN PRINT 'You cannot modify the meal codes' ROLLBACK TRANSACTION END

Column level Update trigger (cont.)

Table level Update Trigger


Example
CREATE TRIGGER NoUpdateSeats ON Reservation FOR UPDATE AS IF (SELECT no_of_seats FROM inserted) > 5 BEGIN PRINT 'You cannot book more than 5 seats' ROLLBACK TRANSACTION END

Table level Update Trigger (contd.)

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

Delete Trigger (contd.)

Statements not allowed in Triggers

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

INSTEAD OF Trigger on View


CREATE VIEW service_view AS SELECT s.service_code AS scode1, service_name, a.service_code AS scode2, aircode FROM service s JOIN airline_service a ON s.service_code=a.service_code

INSTEAD OF Trigger on view (contd.)

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

Das könnte Ihnen auch gefallen