plsql_triggers_lecture.pdf

Upload: vivek-patel

Post on 14-Apr-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 PLSQL_Triggers_lecture.pdf

    1/10

    PL/SQL TRIGGERS

  • 7/27/2019 PLSQL_Triggers_lecture.pdf

    2/10

  • 7/27/2019 PLSQL_Triggers_lecture.pdf

    3/10

  • 7/27/2019 PLSQL_Triggers_lecture.pdf

    4/10

    Disable/Enable Triggers:

    1. Disable a Trigger

    2. Disable all Triggers on a table3. Enable a Trigger

    4. Enable all Triggers on a table

    INSERT Triggers:

    BEFORE INSERT Trigger and AFTER INSERT Trigger

    Oracle/PLSQL: BEFORE INSERT TriggerRestrictions:

    You can not create a BEFORE trigger on a view.

    You can update the :NEW values.

    You can not update the :OLD values.

    A BEFORE INSERT Trigger means that Oracle will fire this trigger before the INSERT operation isexecuted. The syntax fo r an BEFORE INSERT Trigger i s:

    CREATE or REPLACE TRIGGER trigger_nameBEFORE INSERT

    ON table_name[ FOR EACH ROW ]

    DECLARE-- variable declarations

    BEGIN-- trigger code

    EXCEPTIONWHEN ...

    -- exception handlingEND;

    trigger_name is the name of the trigger to create.

    CREATE TABLE orders( order_id number(5),quantity number(4),cost_per_item number(6,2),total_cost number(8,2),create_date date,created_by varchar2(10)

    );

  • 7/27/2019 PLSQL_Triggers_lecture.pdf

    5/10

    CREATE OR REPLACE TRIGGER orders_before_insertBEFORE INSERT

    ON ordersFOR EACH ROW

    DECLAREv_username varchar2(10);

    BEGIN-- Find username of person performing INSERT into tableSELECT user INTO v_usernameFROM dual;

    -- Update create_date field to current system date:new.create_date := sysdate;

    -- Update created_by field to the username of the person performing the INSERT:new.created_by := v_username;

    END;

    Oracle/PLSQL: AFTER INSERT Trigger

    Restrictions:

    You can not create an AFTER trigger on a view.

    You can not update the :NEW values.

    You can not update the :OLD values.

    The syntax for an AFTER INSERT Trigger is:

    CREATE or REPLACE TRIGGER trigger_nameAFTER INSERT

    ON table_name[ FOR EACH ROW ]

    DECLARE-- variable declarationsBEGIN

    -- trigger codeEXCEPTION

    WHEN ...-- exception handling

    END;

    trigger_name is the name of the trigger to create.

    For example:

    If you had a table created as follows:

    CREATE TABLE orders( order_id number(5),quantity number(4),cost_per_item number(6,2),total_cost number(8,2)

    );

  • 7/27/2019 PLSQL_Triggers_lecture.pdf

    6/10

    We could then create an AFTER INSERT trigger as follows:

    CREATE OR REPLACE TRIGGER orders_after_insertAFTER INSERT

    ON ordersFOR EACH ROW

    DECLAREv_username varchar2(10);

    BEGIN-- Find username of person performing the INSERT into the tableSELECT user INTO v_usernameFROM dual;

    -- Insert record into audit tableINSERT INTO orders_audit( order_id,

    quantity,cost_per_item,total_cost,username )

    VALUES( :new.order_id,:new.quantity,:new.cost_per_item,:new.total_cost,v_username );

    END;

    UPDATE Triggers:

    BEFORE UPDATE Trigger and AFTER UPDATE Trigger

    Oracle/PLSQL: BEFORE UPDATE Trigger

    A BEFORE UPDATE Trigger means that Oracle will fire this trigger before the UPDATE operationis executed.The syntax for an BEFORE UPDATE Trigger is:

    CREATE or REPLACE TRIGGER trigger_nameBEFORE UPDATE

    ON table_name[ FOR EACH ROW ]

    DECLARE-- variable declarations

    BEGIN-- trigger code

    EXCEPTIONWHEN ...-- exception handling

    END;

    trigger_name is the name of the trigger to create.

  • 7/27/2019 PLSQL_Triggers_lecture.pdf

    7/10

    Restrictions:

    You can not create a BEFORE trigger on a view.

    You can update the :NEW values.

    You can not update the :OLD values.

    For example:

    If you had a table created as follows:CREATE TABLE orders( order_id number(5),quantity number(4),cost_per_item number(6,2),total_cost number(8,2),updated_date date,updated_by varchar2(10)

    );

    We could then create a BEFORE UPDATE trigger as follows:

    CREATE OR REPLACE TRIGGER orders_before_updateBEFORE UPDATE

    ON ordersFOR EACH ROW

    DECLAREv_username varchar2(10);

    BEGIN

    -- Find username of person performing UPDATE on the tableSELECT user INTO v_usernameFROM dual;

    -- Update updated_date field to cur rent system date:new.updated_date := sysdate;

    -- Update updated_by field to the username of the person performing the UPDATE:new.updated_by := v_username;

    END;

  • 7/27/2019 PLSQL_Triggers_lecture.pdf

    8/10

    Oracle/PLSQL: AFTER UPDATE Trigger

    An AFTER UPDATE Trigger means that Oracle will fire this trigger after the UPDATE operation isexecuted.The syntax for an AFTER UPDATE Trigger is:

    CREATE or REPLACE TRIGGER trigger_name

    AFTER UPDATEON table_name[ FOR EACH ROW ]

    DECLARE-- variable declarations

    BEGIN-- trigger code

    EXCEPTIONWHEN ...-- exception handling

    END;

    trigger_name is the name of the trigger to create.Restrictions:

    You can not create an AFTER trigger on a view.

    You can not update the :NEW values.

    You can not update the :OLD values.

    For example:If you had a table created as follows:CREATE TABLE orders( order_id number(5),quantity number(4),cost_per_item number(6,2),total_cost number(8,2)

    );

    We could then create an AFTER UPDATE trigger as follows:

    CREATE OR REPLACE TRIGGER orders_after_updateAFTER UPDATE

    ON ordersFOR EACH ROW

    DECLAREv_username varchar2(10);

    BEGIN-- Find username of person performing UPDATE into tableSELECT user INTO v_usernameFROM dual;-- Insert record into audit tableINSERT INTO orders_audit( order_id,quantity_before,quantity_after,username )

    VALUES( :new.order_id,:old.quantity,:new.quantity,v_username );

    END;

  • 7/27/2019 PLSQL_Triggers_lecture.pdf

    9/10

    DELETE Triggers:

    BEFORE DELETE Trigger and AFTER DELTE Trigger

    Oracle/PLSQL: BEFORE DELETE Trigger

    The syntax fo r an BEFORE DELETE Trigger is:

    CREATE or REPLACE TRIGGER trigger_nameBEFORE DELETE

    ON table_name[ FOR EACH ROW ]

    DECLARE-- variable declarations

    BEGIN-- trigger code

    EXCEPTIONWHEN ...-- exception handling

    END;

    Restrictions:

    You can not create a BEFORE trigger on a view.

    You can update the :NEW values.

    You can not update the :OLD values.

    CREATE TABLE orders( order_id number(5),quantity number(4),cost_per_item number(6,2),total_cost number(8,2)

    );CREATE OR REPLACE TRIGGER orders_befo re_deleteBEFORE DELETE

    ON ordersFOR EACH ROW

    DECLAREv_username varchar2(10);

    BEGIN-- Find username of person performing the DELETE on the tableSELECT user INTO v_usernameFROM dual;-- Insert record into audit tableINSERT INTO orders_audit( order_id,quantity,cost_per_item,total_cost,

    delete_date,deleted_by )VALUES( :old.order_id,:old.quantity,:old.cost_per_item,:old.total_cost,sysdate,v_username );

    END;

    WEB REFERENCE:http: //techon thenet.com/oracle/triggers/

  • 7/27/2019 PLSQL_Triggers_lecture.pdf

    10/10

    ALTER TABLE orders DISABLE ALL TRIGGERS;

    ALTER TRIGGER orders_before_insert DISABLE;

    ALTER TRIGGER orders_before_insert ENABLE;

    ALTER TABLE orders ENABLE ALL TRIGGERS;

    DROP TRIGGER orders_before_insert;

    CYCLIC CASCADING in a TRIGGER

    This is an undesirable situation where more than one trigger enter into an infinite loop.

    while creating a trigger we should ensure the such a situtation does not exist.

    The below example shows how Trigger's can enter into cyclic cascading.Let's consider we have two tables 'abc' and 'xyz'. Two triggers are created.

    1) The INSERT Trigger, triggerA on table 'abc' issues an UPDATE on table 'xyz'.

    2) The UPDATE Trigger, triggerB on table 'xyz' issues an INSERT on table 'abc'.

    In such a situation, when there is a row inserted in table 'abc', triggerA fires and willupdate table 'xyz'.

    When the table 'xyz' is updated, triggerB fires and will insert a row in table 'abc'.

    This cyclic situation continues and will enter into a infinite loop, which will crash the

    database