`
noblemoon
  • 浏览: 24867 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

触发器简介

 
阅读更多
触发器(trigger)是个特殊的存储过程,它的执行不是由程序调用,也不是手工启动,而是由事件来触发,不能带参数,比如当对一个表进行操作( insert,delete, update)时就会激活它执行。触发器经常用于加强数据的完整性约束和业务规则等。 触发器可以从 DBA_TRIGGERS ,USER_TRIGGERS 数据字典中查到。
      Oracle触发器语法
触发器是特定事件出现的时候,自动执行的代码块。类似于存储过程,触发器与存储过程的区别在于:存储过程是由用户或应用程序显式调用的,而触发器是不能被直接调用的。
功能:
1、 允许/限制对表的修改
2、 自动生成派生列,比如自增字段
3、 强制数据一致性
4、 提供审计和日志记录
5、 防止无效的事务处理
6、 启用复杂的业务逻辑
     触发器触发时间有两种:after和before。
1、触发器的语法:
CREATE [OR REPLACE] TIGGER触发器名 触发时间 触发事件
ON表名
[FOR EACH ROW]
BEGIN
pl/sql语句
END
其中:
触发器名:触发器对象的名称。
触发时间:指明触发器何时执行,该值可取:
before---表示在数据库动作之前触发器执行;
after---表示在数据库动作之后出发器执行。
触发事件:指明哪些数据库动作会触发此触发器:                       
insert:数据库插入会触发此触发器;
update:数据库修改会触发此触发器;
delete:数据库删除会触发此触发器。
表 名:数据库触发器所在的表。
for each row:对表的每一行触发器执行一次。如果没有这一选项,则只对整个表执行一次。

事务的特性:
事务具有ACID四大特性:
某种意义上说,原子性是手段,一致性是目的,通过原子性的手段达到一致性的目的。
A:原子性: 做都做 不做都不做-------手段
C:一致性: 做事务操作的前后,数据保持一致性-----目的
I: 隔离性: 隔离性越强,并发性越弱
                 ---------用for update 来实现事务的隔离性 通过锁的特性:
                                     |
                            是一个加锁过程
D:永久性:同一个事务在commit 之后不能rollback


oracle中 DML语言 发生时 oracle 只操作内存,并不能自动提交
行触发器的工作原理是 俩个重要的内存表
                   old                 new
insert          ----                    ok
update         ok                   ok
delete           ok                     ----
:new --为一个引用最新的列值;
:old --为一个引用以前的列值; 这两个变量只有在使用了关键字 "FOR EACH ROW"时才存在.且update语句两个都有,而insert只有:new ,delect 只有:old;
实例

--drop trigger trig_account_spittor
create or replace trigger trig_account_spittor    
       after insert or update  
       of spittor_id,amount,status
       on spittor_account  
       for each row   
       begin
          if :new.status=1 then
           if inserting then
                update spittor set balance=(balance+:new.amount) where id=:new.spittor_id;
           end if;
          end if;
          commit;
       end;
--drop trigger trig_spittor_order
create or replace trigger trig_spittor_order    
       after insert or update   
       of id,spittor_id,name,total_price,type,status
       on spittor_order    
       for each row   
       begin
          if :new.status=1 then
           if inserting then
              if :new.type=1 then
                 insert into spittor_account(id,order_id,spittor_id,amount,fiinsh_date,status)values(seq_spittoraccount.nextval,:new.id,:new.spittor_id,:new.total_price,sysdate,:new.status);
              elsif :new.type=2 then
                  insert into spittor_account(id,order_id,spittor_id,amount,fiinsh_date,status)values(seq_spittoraccount.nextval,:new.id,:new.spittor_id,-:new.total_price,sysdate,:new.status);
              end if;
           elsif updating then
               ifld.status=1 then
                  dbms_output.put_line('warning:this order is also done it....');
               else
                   if :new.type=1 then
                      insert into spittor_account(id,order_id,spittor_id,amount,fiinsh_date,status)values(seq_spittoraccount.nextval,:new.id,:new.spittor_id,:new.total_price,sysdate,:new.status);
                  elsif :new.type=2 then
                      insert into spittor_account(id,order_id,spittor_id,amount,fiinsh_date,status)values(seq_spittoraccount.nextval,:new.id,:new.spittor_id,-:new.total_price,sysdate,:new.status);
                  end if;                  
               end if;
           end if;
          end if;
       end;
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics