原载:http://blog.sina.com.cn/s/blog_84ee61a90100zhst.html
触发器是个特殊的存储过程,它的执行不是由程序调用,也不是手工启动,而是由事件来触发,下文中就为大家详细讲解Oracle触发器。
Oracle触发器基本格式:
create or replace trigger trigger_name
触发时间 触发类型 on 表名
[for each row] 每更新一行 就触发一次
begin
end
说明:
触发时间:选择值为before 或者 after
触发类型:选择值为insert update delete ,如果选择两者以上用or连接
[for each row]:可选值,选择后表示每更新一行就触发一次
示例:
create or replace trigger tri_update_emp_bak
after update
on emp_bak
for each row 每更新一行 就触发一次
begin
Oracle 里面对触发器也提供了特殊的对象 :NEW :OLD 来访问更新前后的数据
dbms_output.put_line('更新后' || :NEW.sal);
dbms_output.put_line('更新前' || :OLD.sal);
end;
创建触发器(行级触发器)(前置)
create or replace trigger tri3_update_emp_bak
before update
on emp_bak
for each row 每更新一行 就触发一次
begin
oracle 里面 对触发器 也提供了特殊的对象 :NEW :OLD 来访问 更新前后的数据
dbms_output.put_line('更新后' || :NEW.sal);
dbms_output.put_line('更新前' || :OLD.sal);
end;
创建触发器(表级触发器)
//表级别触发器里面 不允许使用 :NEW :OLD 变量
create or replace trigger tri2_update_emp_bak
after update
on emp_bak
begin
dbms_output.put_line('更新后' || :NEW.sal);
dbms_output.put_line('更新前' || :OLD.sal);
end;