Oracle/PLSQL Topics: Triggers

BEFORE INSERT Trigger
None.gif A BEFORE INSERT Trigger means that Oracle will fire this trigger before the INSERT operation is executed.
None.gif
None.gifThe syntax for an BEFORE INSERT Trigger is:
None.gif
None.gifCREATE or REPLACE TRIGGER trigger_name
None.gifBEFORE INSERT
None.gif    ON table_name
None.gif    [ FOR EACH ROW ]
None.gifDECLARE
None.gif    -- variable declarations
None.gifBEGIN
None.gif    -- trigger code
None.gifEXCEPTION
None.gif    WHEN dot.gif
None.gif    -- exception handling
None.gifEND;
None.gif
None.giftrigger_name is the name of the trigger to create.
None.gif
None.gifRestrictions:
None.gif
None.gifYou can not create a BEFORE trigger on a view. 
None.gifYou can update the :NEW values. 
None.gifYou can not update the :OLD values. 
None.gif
None.gif
None.gifFor example:
None.gif
None.gifIf you had a table created as follows:
None.gif
None.gifCREATE TABLE orders 
None.gif( order_id number(5), 
None.gif quantity number(4), 
None.gif cost_per_item number(6,2), 
None.gif total_cost number(8,2), 
None.gif create_date date, 
None.gif created_by varchar2(10) 
None.gif); 
None.gif
None.gif
None.gif
None.gifWe could then create a BEFORE INSERT trigger as follows:
None.gif
None.gifCREATE OR REPLACE TRIGGER orders_before_insert
None.gifBEFORE INSERT
None.gif    ON orders
None.gif    FOR EACH ROW
None.gif
None.gifDECLARE
None.gif    v_username varchar2(10);
None.gif
None.gifBEGIN
None.gif
None.gif    -- Find username of person performing INSERT into table
None.gif    SELECT user INTO v_username
None.gif    FROM dual;
None.gif
None.gif    -- Update create_date field to current system date
None.gif    :new.create_date := sysdate;
None.gif
None.gif    -- Update created_by field to the username of the person performing the INSERT
None.gif    :new.created_by := v_username;
None.gif
None.gifEND;
None.gif
AFTER INSERT Trigger
None.gif An AFTER INSERT Trigger means that Oracle will fire this trigger after the INSERT operation is executed.
None.gif
None.gifThe syntax for an AFTER INSERT Trigger is:
None.gif
None.gifCREATE or REPLACE TRIGGER trigger_name
None.gifAFTER INSERT
None.gif    ON table_name
None.gif    [ FOR EACH ROW ]
None.gifDECLARE
None.gif    -- variable declarations
None.gifBEGIN
None.gif    -- trigger code
None.gifEXCEPTION
None.gif    WHEN dot.gif
None.gif    -- exception handling
None.gifEND;
None.gif
None.giftrigger_name is the name of the trigger to create.
None.gif
None.gifRestrictions:
None.gif
None.gifYou can not create an AFTER trigger on a view. 
None.gifYou can not update the :NEW values. 
None.gifYou can not update the :OLD values. 
None.gif
None.gif
None.gifFor example:
None.gif
None.gifIf you had a table created as follows:
None.gif
None.gifCREATE TABLE orders 
None.gif( order_id number(5), 
None.gif quantity number(4), 
None.gif cost_per_item number(6,2), 
None.gif total_cost number(8,2) 
None.gif); 
None.gif
None.gif
None.gifWe could then create an AFTER INSERT trigger as follows:
None.gif
None.gifCREATE OR REPLACE TRIGGER orders_after_insert
None.gifAFTER INSERT
None.gif    ON orders
None.gif    FOR EACH ROW
None.gif
None.gifDECLARE
None.gif    v_username varchar2(10);
None.gif
None.gifBEGIN
None.gif    -- Find username of person performing the INSERT into the table
None.gif    SELECT user INTO v_username
None.gif    FROM dual;
None.gif
None.gif    -- Insert record into audit table
None.gif    INSERT INTO orders_audit
None.gif     ( order_id,
None.gif       quantity,
None.gif       cost_per_item,
None.gif       total_cost,
None.gif       username )
None.gif    VALUES
None.gif     ( :new.order_id,
None.gif       :new.quantity,
None.gif       :new.cost_per_item,
None.gif       :new.total_cost,
None.gif       v_username );
None.gif
None.gifEND;
None.gif
BEFORE UPDATE Trigger
None.gif A BEFORE UPDATE Trigger means that Oracle will fire this trigger before the UPDATE operation is executed.
None.gif
None.gifThe syntax for an BEFORE UPDATE Trigger is:
None.gif
None.gifCREATE or REPLACE TRIGGER trigger_name
None.gifBEFORE UPDATE
None.gif    ON table_name
None.gif    [ FOR EACH ROW ]
None.gifDECLARE
None.gif    -- variable declarations
None.gifBEGIN
None.gif    -- trigger code
None.gifEXCEPTION
None.gif    WHEN dot.gif
None.gif    -- exception handling
None.gifEND;
None.gif
None.giftrigger_name is the name of the trigger to create.
None.gif
None.gifRestrictions:
None.gif
None.gifYou can not create a BEFORE trigger on a view. 
None.gifYou can update the :NEW values. 
None.gifYou can not update the :OLD values. 
None.gif
None.gif
None.gifFor example:
None.gif
None.gifIf you had a table created as follows:
None.gif
None.gifCREATE TABLE orders 
None.gif( order_id number(5), 
None.gif quantity number(4), 
None.gif cost_per_item number(6,2), 
None.gif total_cost number(8,2), 
None.gif updated_date date, 
None.gif updated_by varchar2(10) 
None.gif); 
None.gif
None.gif
None.gifWe could then create a BEFORE UPDATE trigger as follows:
None.gif
None.gifCREATE OR REPLACE TRIGGER orders_before_update
None.gifBEFORE UPDATE
None.gif    ON orders
None.gif    FOR EACH ROW
None.gif
None.gifDECLARE
None.gif    v_username varchar2(10);
None.gif
None.gifBEGIN
None.gif
None.gif    -- Find username of person performing UPDATE on the table
None.gif    SELECT user INTO v_username
None.gif    FROM dual;
None.gif
None.gif    -- Update updated_date field to current system date
None.gif    :new.updated_date := sysdate;
None.gif
None.gif    -- Update updated_by field to the username of the person performing the UPDATE
None.gif    :new.updated_by := v_username;
None.gif
None.gifEND;
None.gif
AFTER UPDATE Trigger
None.gif An AFTER UPDATE Trigger means that Oracle will fire this trigger after the UPDATE operation is executed.
None.gif
None.gifThe syntax for an AFTER UPDATE Trigger is:
None.gif
None.gifCREATE or REPLACE TRIGGER trigger_name
None.gifAFTER UPDATE
None.gif    ON table_name
None.gif    [ FOR EACH ROW ]
None.gifDECLARE
None.gif    -- variable declarations
None.gifBEGIN
None.gif    -- trigger code
None.gifEXCEPTION
None.gif    WHEN dot.gif
None.gif    -- exception handling
None.gifEND;
None.gif
None.giftrigger_name is the name of the trigger to create.
None.gif
None.gifRestrictions:
None.gif
None.gifYou can not create an AFTER trigger on a view. 
None.gifYou can not update the :NEW values. 
None.gifYou can not update the :OLD values. 
None.gif
None.gif
None.gifFor example:
None.gif
None.gifIf you had a table created as follows:
None.gif
None.gifCREATE TABLE orders 
None.gif( order_id number(5), 
None.gif quantity number(4), 
None.gif cost_per_item number(6,2), 
None.gif total_cost number(8,2) 
None.gif); 
None.gif
None.gif
None.gifWe could then create an AFTER UPDATE trigger as follows:
None.gif
None.gifCREATE OR REPLACE TRIGGER orders_after_update
None.gifAFTER UPDATE
None.gif    ON orders
None.gif    FOR EACH ROW
None.gif
None.gifDECLARE
None.gif    v_username varchar2(10);
None.gif
None.gifBEGIN
None.gif
None.gif    -- Find username of person performing UPDATE into table
None.gif    SELECT user INTO v_username
None.gif    FROM dual;
None.gif
None.gif    -- Insert record into audit table
None.gif    INSERT INTO orders_audit
None.gif     ( order_id,
None.gif       quantity_before,
None.gif       quantity_after,
None.gif       username )
None.gif    VALUES
None.gif     ( :new.order_id,
None.gif       :old.quantity,
None.gif       :new.quantity,
None.gif       v_username );
None.gif
None.gifEND;
None.gif
BEFORE DELETE Trigger
None.gif A BEFORE DELETE Trigger means that Oracle will fire this trigger before the DELETE operation is executed.
None.gif
None.gifThe syntax for an BEFORE DELETE Trigger is:
None.gif
None.gifCREATE or REPLACE TRIGGER trigger_name
None.gifBEFORE DELETE
None.gif    ON table_name
None.gif    [ FOR EACH ROW ]
None.gifDECLARE
None.gif    -- variable declarations
None.gifBEGIN
None.gif    -- trigger code
None.gifEXCEPTION
None.gif    WHEN dot.gif
None.gif    -- exception handling
None.gifEND;
None.gif
None.giftrigger_name is the name of the trigger to create.
None.gif
None.gifRestrictions:
None.gif
None.gifYou can not create a BEFORE trigger on a view. 
None.gifYou can update the :NEW values. 
None.gifYou can not update the :OLD values. 
None.gif
None.gif
None.gifFor example:
None.gif
None.gifIf you had a table created as follows:
None.gif
None.gifCREATE TABLE orders 
None.gif( order_id number(5), 
None.gif quantity number(4), 
None.gif cost_per_item number(6,2), 
None.gif total_cost number(8,2) 
None.gif); 
None.gif
None.gifWe could then create a BEFORE DELETE trigger as follows:
None.gif
None.gifCREATE OR REPLACE TRIGGER orders_before_delete
None.gifBEFORE DELETE
None.gif    ON orders
None.gif    FOR EACH ROW
None.gif
None.gifDECLARE
None.gif    v_username varchar2(10);
None.gif
None.gifBEGIN
None.gif
None.gif    -- Find username of person performing the DELETE on the table
None.gif    SELECT user INTO v_username
None.gif    FROM dual;
None.gif
None.gif    -- Insert record into audit table
None.gif    INSERT INTO orders_audit
None.gif     ( order_id,
None.gif       quantity,
None.gif       cost_per_item,
None.gif       total_cost,
None.gif       delete_date,
None.gif       deleted_by )
None.gif    VALUES
None.gif     ( :old.order_id,
None.gif       :old.quantity,
None.gif       :old.cost_per_item,
None.gif       :old.total_cost,
None.gif       sysdate,
None.gif       v_username );
None.gif
None.gifEND;
None.gif
AFTER DELETE Trigger
None.gif An AFTER DELETE Trigger means that Oracle will fire this trigger after the DELETE operation is executed.
None.gif
None.gifThe syntax for an AFTER DELETE Trigger is:
None.gif
None.gifCREATE or REPLACE TRIGGER trigger_name
None.gifAFTER DELETE
None.gif    ON table_name
None.gif    [ FOR EACH ROW ]
None.gifDECLARE
None.gif    -- variable declarations
None.gifBEGIN
None.gif    -- trigger code
None.gifEXCEPTION
None.gif    WHEN dot.gif
None.gif    -- exception handling
None.gifEND;
None.gif
None.giftrigger_name is the name of the trigger to create.
None.gif
None.gifRestrictions:
None.gif
None.gifYou can not create an AFTER trigger on a view. 
None.gifYou can not update the :NEW values. 
None.gifYou can not update the :OLD values. 
None.gif
None.gif
None.gifFor example:
None.gif
None.gifIf you had a table created as follows:
None.gif
None.gifCREATE TABLE orders 
None.gif( order_id number(5), 
None.gif quantity number(4), 
None.gif cost_per_item number(6,2), 
None.gif total_cost number(8,2) 
None.gif); 
None.gif
None.gif
None.gifWe could then create an DELETE UPDATE trigger as follows:
None.gif
None.gifCREATE OR REPLACE TRIGGER orders_after_delete
None.gifAFTER DELETE
None.gif    ON orders
None.gif    FOR EACH ROW
None.gif
None.gifDECLARE
None.gif    v_username varchar2(10);
None.gif
None.gifBEGIN
None.gif
None.gif    -- Find username of person performing the DELETE on the table
None.gif    SELECT user INTO v_username
None.gif    FROM dual;
None.gif
None.gif    -- Insert record into audit table
None.gif    INSERT INTO orders_audit
None.gif     ( order_id,
None.gif       quantity,
None.gif       cost_per_item,
None.gif       total_cost,
None.gif       delete_date,
None.gif       deleted_by)
None.gif    VALUES
None.gif     ( :old.order_id,
None.gif       :old.quantity,
None.gif       :old.cost_per_item,
None.gif       :old.total_cost,
None.gif       sysdate,
None.gif       v_username );
None.gif
None.gifEND;
None.gif
Drop a Trigger
None.gif DROP TRIGGER trigger_name;

转载于:https://www.cnblogs.com/timsoft/articles/412715.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值