12.数据库对象----触发器(trigger)

来源:互联网 发布:淘宝系统原始头像图片 编辑:程序博客网 时间:2024/06/01 07:22
1.什么是触发器: 一个事件发生时,触发器自动隐式的执行。是在数据库中独立运行

2.触发器和函数/存储过程的不同
        1.有无参数列表
                触发器一定没有参数列表,而存储过程和函数可以有参数列表
        2.使用方式不同:
                触发器有相应的事件自动触发,而存储过程和函数必须显示调用
3.触发器种类
        DML触发器        增删改
                1.语句级
                2.行级触发器
        系统触发器
                登录....................
        替代触发器
  
4.语法:
        create 【or replace】 trigger trigger_name
        { before | after } { insert | delete | update 【of column 【,column...】】}
        on 【schema.】table_name referencing old as old_name new new_name
      【for each row】
      【when ( condition )】
      【declare variable variable_name; ... 】
       begin
           pl/sql语句
       end;
                                                   大括号表示,必须有其一
                before | after:触发时机, after 后触发, before 前触发

                insert/delete/update
                update之后 使用 of 添加字段列表,指定修改了of 后面的任何一个字段,都会触发
                insert/delete 后不能加 of
                有多个触发事件,用or连接进行关联
                for each row
                指定当前触发器为行级触发器
                如果省略则默认为语句级触发器
                when (condition)   限定触发器体是否执行
  注意:
                不能使用三个谓词
                使用old或者new及其别名时不能加冒号(:)
                when后面的条件要加括号()
                只能用于行级触发器
                      例子: create or replace trigger book_trigger
                                 before update on books
                                 for each row
                                 when (old.id != '00000000-0000-0000-0000-000000000001')
                                 begin
                                        dbms_output.put_line(:old.id||:old.name);
                                        dbms_output.put_line(:new.id||:new.name);
                                end;
 

                                update books set name = '天火大道' where id = '00000000-0000-0000-0000-000000000001';
 
                referencing old as old_name new new_name,为默认的old和new指定别名
      new / old  
        new:数据变动后的
        old: 数据变动前的
                                           可以认为其数据类型为rowtype
                                           如果是语句级,那么如果操作多条数据,不知道返回那个数据
                                          在when后面使用时,不能加冒号(:)
     三个谓词:
        inserting 当插入数据时返回true
        deleting 当删除数据时返回true
        updating 当修改数据时返回true
5.语句级和行级触发器的区别
        语句级触发器在SQL语句执行前或后,触发器只触发一次
        create or replace trigger book_trigger
        after insert or delete or update of name on books
        begin
          if inserting then dbms_output.put_line('插入');
          elsif deleting then dbms_output.put_line('删除');
          elsif updating('name') then dbms_output.put_line('修改');
           end if;
       end;
        行级触发器在SQL语句执行前或后,有多少条数据受到影响,触发器就执行多少次
             create or replace trigger book_trigger
             after insert or delete or update of name
             on books
             for each row
             begin 
                     ifinserting then dbms_output.put_line('插入');
                    elsif deleting then dbms_output.put_line('删除');
                    elsif updating('name') then dbms_output.put_line('修改');
                    end if;
            end;




原创粉丝点击