Oracle常用命令11(触发器)
触发器
CREATE [OR REPLACE] TRIGGER trigger_name
AFTER | BEFORE | INSTEAD OF
[INSERT] [[OR] UPDATE [OF column_list]]
[[OR] DELETE]
ON table_or_view_name
[REFERENCING {OLD [AS] old / NEW [AS] new}]
[FOR EACH ROW]
[WHEN (condition)]
pl/sql_block;
启用和禁用触发器
ALTER TRIGGER 触发器名 DISABLE/ENABLE;
删除触发器
DROP TRIGGER 触发器名;
显示有关触发器的信息
select trigger_name from dba_triggers/user_triggers;
显示某个表中的触发器的信息
select trigger_name from dba_triggers/user_triggers where table_name = '表名';
例1:当订单明细商品数量、价格改变时,订单中的金额随之改变
create or replace trigger tri_product
after update of D_QUANTITY,D_PRICE on orderdetail
for each row
declare
updateSum number;
begin
--得到订单明细的商品数量改变后的订单总金额,将订单表中的金额进行修改
updateSum := :new.D_QUANTITY * :new.D_PRICE - :old.D_QUANTITY * :old.D_PRICE;
dbms_output.put_line(:old.o_id);
update orderTab set O_MONEY = O_MONEY + updateSum where O_ID = :old.O_ID;
end;
--测试触发器:订单明细商品数量、价格改变时,订单中的金额随之改变
update orderdetail set d_quantity = 3 where d_id=2;