Session7 Trigger

Embed Size (px)

Citation preview

  • 8/7/2019 Session7 Trigger

    1/23

    Implementing Trigger

    Vu Tuyet [email protected]

    Hanoi University of Technology

    1

  • 8/7/2019 Session7 Trigger

    2/23

    Microsoft

    Microsoft

    Introduction to Triggers

    What Is a Trigger?

    Uses of Triggers

    Considerations for Using Triggers

  • 8/7/2019 Session7 Trigger

    3/23

    Microsoft

    Microsoft

    What Is a Trigger?

    Associated with a Table

    Invoked Automatically

    Cannot Be Called Directly

    Is Part of a Transaction

  • 8/7/2019 Session7 Trigger

    4/23

    Microsoft

    Microsoft

    Outline

    Introduction to Triggers

    Creating, Altering, and Dropping Triggers

    Working with Triggers Uses of Trigger

    Performance Consideration

  • 8/7/2019 Session7 Trigger

    5/23

    Microsoft

    Microsoft

    Creating and Managing Triggers

    Creating Triggers

    Altering and Dropping Triggers

  • 8/7/2019 Session7 Trigger

    6/23

    Microsoft

    Microsoft

    Creating Triggers

    Requires Appropriate Permissions

    Cannot Contain Certain Statements

    Use NorthwindGOCREATE TRIGGER Empl_Delete ON EmployeesFOR DELETEASIF (SELECT COUNT(*) FROM Deleted) > 1BEGIN

    RAISERROR('You cannot delete more than one employee at a time.', 16, 1)

    ROLLBACK TRANSACTIONEND

  • 8/7/2019 Session7 Trigger

    7/23

    MicrosoftMicrosoft

    Altering and Dropping Triggers Altering a Trigger

    Changes the definition without dropping the trigger Can disable or enable a trigger

    Dropping a Trigger

    USE NorthwindGOALTER TRIGGER Empl_Delete ON EmployeesFOR DELETEASIF (SELECT COUNT(*) FROM Deleted) > 6BEGIN

    RAISERROR(

    'You cannot delete more than six employees at a time.', 16, 1)ROLLBACK TRANSACTION

    END

  • 8/7/2019 Session7 Trigger

    8/23

    MicrosoftMicrosoft

    How Triggers Work

    How an INSERT TriggerWorks

    How a DELETE TriggerWorks

    How an UPDATE TriggerWorks How an INSTEAD OF TriggerWorks

  • 8/7/2019 Session7 Trigger

    9/23

  • 8/7/2019 Session7 Trigger

    10/23

    MicrosoftMicrosoft

    Activating Insert Trigger

    Step 1

    INSERT statement to a table with an INSERT triggerdefined

    Step 2

    INSERT Statement Logged

    Step 3

    TriggerActions Executed

  • 8/7/2019 Session7 Trigger

    11/23

  • 8/7/2019 Session7 Trigger

    12/23

    MicrosoftMicrosoft

    Activating UPDATE Trigger

    Step 1:

    DELETE Statement to a Table with a DELETEStatement Defined

    Step 2

    DELETE Statement Logged

    Step 3

    TriggerActions Executed

  • 8/7/2019 Session7 Trigger

    13/23

  • 8/7/2019 Session7 Trigger

    14/23

    MicrosoftMicrosoft

    Activating DELETE Trigger

    Step 1

    UPDATE Statement to a Table with an UPDATE Trigger

    Defined

    Step 2

    UPDATE Statement Logged as INSERT and DELETE

    Statements

    Step 3

    TriggerActions Executed

  • 8/7/2019 Session7 Trigger

    15/23

    MicrosoftMicrosoft

    Outline

    Introduction to Triggers

    Creating, Altering, and Dropping Triggers

    Working with Triggers Uses of Trigger

    Performance Consideration

  • 8/7/2019 Session7 Trigger

    16/23

    MicrosoftMicrosoft

    Uses of Triggers

    Cascade changes through related tables in

    a database

    Enforce more complex data integrity than aCHECK Constraint

    Define custom error messages

    Maintain denormalized data

    Compare before and after states of data undermodification

  • 8/7/2019 Session7 Trigger

    17/23

    MicrosoftMicrosoft

    Considerations for Using Triggers

    Triggers Are Reactive; Constraints Are Proactive

    Constraints Are Checked First

    Tables Can Have Multiple Triggers forAny Action

    Table Owners Can Designate the First and LastTrigger to Fire

    You Must Have Permission to Perform AllStatements That Define Triggers

    Table Owners Cannot Create AFTER Triggers on

    Views or Temporary Tables

  • 8/7/2019 Session7 Trigger

    18/23

    MicrosoftMicrosoft

    Examples of Triggers

    Enforcing Data Integrity

    Enforcing Business Rules

  • 8/7/2019 Session7 Trigger

    19/23

    MicrosoftMicrosoft

    Enforcing Data Integrity

    CREATE TRIGGER BackOrderList_DeleteON Products FOR UPDATEASIF (SELECT BO.ProductID FROM BackOrders AS BO JOIN

    Inserted AS I ON BO.ProductID = I.Product_ID) > 0

    BEGIN

    DELETE BO FROM BackOrders AS BOINNER JOIN Inserted AS ION BO.ProductID = I.ProductID

    END

    ProductsProducts

    ProductIDUnitsInStock

    1

    3

    4

    15

    10

    65

    20

    2 15 Updated

    BackOrdersBackOrders

    ProductID UnitsOnOrder

    1

    12

    3

    15

    10

    65

    2 15Trigger Deletes Row

  • 8/7/2019 Session7 Trigger

    20/23

  • 8/7/2019 Session7 Trigger

    21/23

    MicrosoftMicrosoft

    Performance Considerations

    Triggers Work Quickly Because the Inserted and

    Deleted Tables Are in Cache

    Execution TimeIs Determined by:

    Number of tables that are referenced

    Number of rows that are affected

    Actions Contained in Triggers Implicitly Are Part of

    a Transaction

  • 8/7/2019 Session7 Trigger

    22/23

    MicrosoftMicrosoft

    Summary

    Use Triggers Only When Necessary

    Keep Trigger Definition Statements as Simple as

    Possible

    Include Recursion Termination Check Statements in

    Recursive Trigger Definitions

    Minimize Use ofROLLBACK Statements in Triggers

  • 8/7/2019 Session7 Trigger

    23/23

    MicrosoftMicrosoft