12.數據庫對象----觸發器(trigger)

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 
                     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;




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章