<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    kooyee ‘s blog

    開源軟件, 眾人努力的結晶, 全人類的共同財富
    posts - 103, comments - 55, trackbacks - 0, articles - 66
       :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    [Oracle]each row and table level triggers

    Posted on 2008-04-05 19:36 kooyee 閱讀(667) 評論(0)  編輯  收藏 所屬分類: Database數據庫技術
    row  level triggers : 是每個row改變時,觸發trigger。 比如,2個row update,觸發2次。

    table level triggers: 是table改變時,觸發trigger。無論幾個row改變都沒影響, 比如,1個row update觸發1次 ,2個row update,也觸發1次。

    順便轉個教程

    Before / for each row trigger

    A before trigger is called before because it fires before the new values (:new.field_name) are stored in the table. That means that the new value can be changed in the trigger.
    create table t_update_before_each_row (
    txt varchar2(10)
    );
    create table log (
    txt varchar2(20)
    );
    create trigger update_before_each_row
    before update on t_update_before_each_row
    for each row
    begin
    :new.txt := upper(:new.txt);
    insert into log values ('old: ' || :old.txt);
    insert into log values ('new: ' || :new.txt);
    end update_before_each_row;
    /
    
    insert into t_update_before_each_row values('one');
    insert into t_update_before_each_row values('two');
    insert into t_update_before_each_row values('three');
    insert into t_update_before_each_row values('four');
    
    Updating (that is: concatenating the value with itself) the rows containing two and three:
    update t_update_before_each_row set txt = txt || txt
    where substr(txt,1,1) = 't';
    
    select * from t_update_before_each_row;
    
    As can be seen by the output of the select statement, the trigger changed the values of the new values; they're in uppercase now:
    one
    TWOTWO
    THREETHREE
    four
    
    The log displays the old and new values:
    select * from log;
    
    old: two
    new: TWOTWO
    old: three
    new: THREETHREE
    
    Cleaning up:
    drop table t_update_before_each_row;
    drop table log;
    

    After / for each row trigger

    In contrast to a before trigger, an after trigger does not allow to change :new.field_name because the value is, when the trigger fires, already written to the table.
    If one tries to assign a value to :new.field_name, Oracle throws an ORA-04084: cannot change NEW values for this trigger type.
    create table t_update_after_each_row (
    txt varchar2(10)
    );
    create table log (
    txt varchar2(20)
    );
    create trigger update_after_each_row
    after update on t_update_after_each_row
    for each row
    begin
    -- :new.txt := upper(:old.txt); -- ORA-04084: cannot change NEW values for this trigger type
    insert into log values ('old: ' || :old.txt);
    insert into log values ('new: ' || :new.txt);
    end update_after_each_row;
    /
    
    insert into t_update_after_each_row values('one');
    insert into t_update_after_each_row values('two');
    insert into t_update_after_each_row values('three');
    insert into t_update_after_each_row values('four');
    
    update t_update_after_each_row set txt = txt || txt
    where substr(txt,1,1) = 't';
    
    select * from t_update_after_each_row;
    
    one
    twotwo
    threethree
    four
    
    select * from log;
    
    As the log table shows, it is possible to use :new and :old although it's not possible to assign something to :new.
    old: two
    new: twotwo
    old: three
    new: threethree
    
    Cleaning up:
    drop table t_update_after_each_row;
    drop table log;
    

    Table level trigger

    A table level trigger is a trigger that doesn't fire for each row to be changed. Accordingly, it lacks the for each row. Consequently, both, the :new and :old are not permitted in the trigger's PL/SQL block, otherwise, an ORA-04082: NEW or OLD references not allowed in table level triggers is thrown.
    create table t_update_before (
    txt varchar2(10)
    );
    create table log (
    txt varchar2(20)
    );
    create trigger update_before
    before update on t_update_before
    begin
    -- :new.txt := upper(:old.txt); -- ORA-04082
    insert into log values ('update trigger');
    end update_before;
    /
    
    insert into t_update_before values('one');
    insert into t_update_before values('two');
    insert into t_update_before values('three');
    insert into t_update_before values('four');
    
    update t_update_before set txt = txt || txt
    where substr(txt,1,1) = 't';
    
    select * from t_update_before;
    
    one
    twotwo
    threethree
    four
    
    Although two rows were updated, only one record is found in the log table:
    select * from log;
    
    update trigger
    
    An update statement that doesn't update any row:
    update t_update_before set txt = txt || txt
    where txt = 'no update';
    
    Still, the trigger fires...
    select * from log;
    
    ... which results in another row found in the log table:
    update trigger
    update trigger
    
    Cleaning up:
    drop table t_update_before;
    drop table log;
    

    Order of execution

    Oracle allows to create multiple triggers on the same table. The order of the execution of these triggers is undeterministic (or random, if you want this word) except that all before triggers fire before the after triggers.
    主站蜘蛛池模板: 香蕉免费看一区二区三区| 日本免费大黄在线观看| 女人被男人桶得好爽免费视频| 亚洲第一视频网站| 亚洲沟沟美女亚洲沟沟| 亚洲av无码专区在线观看亚| 欧美在线看片A免费观看| 国产成人精品亚洲日本在线 | 波多野结衣久久高清免费| 亚洲色爱图小说专区| 丝袜捆绑调教视频免费区| 亚洲午夜国产精品无码| 国产一级淫片a免费播放口| 全部免费国产潢色一级| 国产亚洲美女精品久久| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 亚洲人成网77777亚洲色 | 人人揉揉香蕉大免费不卡| 亚洲国产香蕉碰碰人人| 亚洲狠狠婷婷综合久久蜜芽| 日韩免费无砖专区2020狼| 国产午夜亚洲精品不卡电影| av在线亚洲欧洲日产一区二区| WWW免费视频在线观看播放| 亚洲性天天干天天摸| 亚洲精品无码久久久久A片苍井空 亚洲精品无码久久久久YW | 99精品视频免费在线观看| 亚洲国产精品人久久| 4399好看日本在线电影免费| 亚洲AV无码专区在线电影成人 | 99国产精品永久免费视频| 亚洲精品无码专区| 亚洲无码日韩精品第一页| 成人影片一区免费观看| 国产亚洲老熟女视频| 一级一片免费视频播放| 一本色道久久88亚洲综合 | 国产真人无遮挡作爱免费视频| 亚洲av无码一区二区三区观看| 国产一级特黄高清免费大片| 免费a级毛片无码a∨免费软件|