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

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

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

    隨筆-314  評(píng)論-209  文章-0  trackbacks-0
    是特定事件出現(xiàn)的時(shí)候,自動(dòng)執(zhí)行的代碼塊。類似于存儲(chǔ)過程,但是用戶不能直接調(diào)用他們。
     
    功能:
    1、 允許/限制對(duì)表的修改
    2、 自動(dòng)生成派生列,比如自增字段
    3、 強(qiáng)制數(shù)據(jù)一致性
    4、 提供審計(jì)和日志記錄
    5、 防止無效的事務(wù)處理
    6、 啟用復(fù)雜的業(yè)務(wù)邏輯
     
    開始
    create trigger biufer_employees_department_id
           before insert or update
                  of department_id
                  on employees
           referencing old as old_value
                           new as new_value
           for each row
           when (new_value.department_id<>80 )
    begin
           :new_value.commission_pct :=0;
    end;
    /
     
    觸發(fā)器的組成部分:
    1、 觸發(fā)器名稱
    2、 觸發(fā)語(yǔ)句
    3、 觸發(fā)器限制
    4、 觸發(fā)操作
     
    1、 觸發(fā)器名稱
    create trigger biufer_employees_department_id
    命名習(xí)慣:
    biufer(before insert update for each row)
    employees 表名
    department_id 列名
     
    2、 觸發(fā)語(yǔ)句
    比如:
    表或視圖上的DML語(yǔ)句
    DDL語(yǔ)句
    數(shù)據(jù)庫(kù)關(guān)閉或啟動(dòng),startup shutdown 等等
    before insert or update
                  of department_id
                  on employees
           referencing old as old_value
                           new as new_value
           for each row
     
    說明:
    1、 無論是否規(guī)定了department_id ,對(duì)employees表進(jìn)行insert的時(shí)候
    2、 對(duì)employees表的department_id列進(jìn)行update的時(shí)候
     
    3、 觸發(fā)器限制
    when (new_value.department_id<>80 )
     
    限制不是必須的。此例表示如果列department_id不等于80的時(shí)候,觸發(fā)器就會(huì)執(zhí)行。
    其中的new_value是代表更新之后的值。
     
    4、 觸發(fā)操作
    是觸發(fā)器的主體
    begin
           :new_value.commission_pct :=0;
    end;
     
    主體很簡(jiǎn)單,就是將更新后的commission_pct列置為0
     
    觸發(fā):
    insert into employees(employee_id,
    last_name,first_name,hire_date,job_id,email,department_id,salary,commission_pct )
    values( 12345,’Chen’,’Donny’, sysdate, 12, ‘donny@hotmail.com’,60,10000,.25);
     
    select commission_pct from employees where employee_id=12345;
     
    觸發(fā)器不會(huì)通知用戶,便改變了用戶的輸入值。
     
     
    觸發(fā)器類型:
    1、 語(yǔ)句觸發(fā)器
    2、 行觸發(fā)器
    3、 INSTEAD OF 觸發(fā)器
    4、 系統(tǒng)條件觸發(fā)器
    5、 用戶事件觸發(fā)器
     
     
     
    1、 語(yǔ)句觸發(fā)器
    是在表上或者某些情況下的視圖上執(zhí)行的特定語(yǔ)句或者語(yǔ)句組上的觸發(fā)器。能夠與INSERT、UPDATE、DELETE或者組合上進(jìn)行關(guān)聯(lián)。但是無論使用什么樣的組合,各個(gè)語(yǔ)句觸發(fā)器都只會(huì)針對(duì)指定語(yǔ)句激活一次。比如,無論update多少行,也只會(huì)調(diào)用一次update語(yǔ)句觸發(fā)器。
     
    例子:
    需要對(duì)在表上進(jìn)行DML操作的用戶進(jìn)行安全檢查,看是否具有合適的特權(quán)。
    Create table foo(a number);
     
    Create trigger biud_foo
           Before insert or update or delete
                  On foo
    Begin
           If user not in (‘DONNY’) then
                  Raise_application_error(-20001, ‘You don’t have access to modify this table.’);
           End if;
    End;
    /
     
    即使SYS,SYSTEM用戶也不能修改foo表
     
    [試驗(yàn)]
    對(duì)修改表的時(shí)間、人物進(jìn)行日志記錄。
     
    1、 建立試驗(yàn)表
    create table employees_copy as select *from hr.employees
     
    2、 建立日志表
    create table employees_log(
            who varchar2(30),
            when date);
     
    3、 在employees_copy表上建立語(yǔ)句觸發(fā)器,在觸發(fā)器中填充employees_log 表。
    Create or replace trigger biud_employee_copy
                  Before insert or update or delete
                         On employees_copy
           Begin
                  Insert into employees_log(
                         Who,when)
                  Values( user, sysdate);
                 
           End;
           /
    4、 測(cè)試
    update employees_copy set salary= salary*1.1;
     
    select *from employess_log;
     
    5、 確定是哪個(gè)語(yǔ)句起作用?
    即是INSERT/UPDATE/DELETE中的哪一個(gè)觸發(fā)了觸發(fā)器?
    可以在觸發(fā)器中使用INSERTING / UPDATING / DELETING 條件謂詞,作判斷:
    begin
            if inserting then
                   -----
            elsif updating then
                   -----
            elsif deleting then
                   ------
            end if;
    end;
     
    if updating(‘COL1’) or updating(‘COL2’) then
            ------
    end if;
     
    [試驗(yàn)]
    1、 修改日志表
    alter table employees_log
            add (action varchar2(20));
     
    2、 修改觸發(fā)器,以便記錄語(yǔ)句類型。
    Create or replace trigger biud_employee_copy
                  Before insert or update or delete
                         On employees_copy
           Declare
                  L_action employees_log.action%type;
           Begin
            if inserting then
                   l_action:=’Insert’;
            elsif updating then
                   l_action:=’Update’;
            elsif deleting then
                   l_action:=’Delete’;
            else
                   raise_application_error(-20001,’You should never ever get this error.’);
     
                  Insert into employees_log(
                         Who,action,when)
                  Values( user, l_action,sysdate);
           End;
           /
     
    3、 測(cè)試
    insert into employees_copy( employee_id, last_name, email, hire_date, job_id)
           values(12345,’Chen’,’Donny@hotmail’,sysdate,12);
     
    select *from employees_log
      

    Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1711633

    posted on 2008-05-27 15:22 xzc 閱讀(897) 評(píng)論(1)  編輯  收藏 所屬分類: Oracle

    評(píng)論:
    # re: Oracle觸發(fā)器 2008-05-28 14:49 | xzc
    語(yǔ)法規(guī)則:
    Create [or replace] trigger [模式.]觸發(fā)器名
    Before| after insert|delete|(update of 列名)

    On 表名

    [for each row]

    When 條件

    PL/SQL塊

    說明:

    For each row的意義是:在一次操作表的語(yǔ)句中,每操作成功一行就會(huì)觸發(fā)一次;不寫的話,表示是表級(jí)觸發(fā)器,則無論操作多少行,都只觸發(fā)一次;

    When條件的出現(xiàn)說明了,在DML操作的時(shí)候也許一定會(huì)觸發(fā)觸發(fā)器,但是觸發(fā)器不一定會(huì)做實(shí)際的工作,比如when 后的條件不為真的時(shí)候,觸發(fā)器只是簡(jiǎn)單地跳過了PL/SQL塊;

    例子:

    sql 代碼
    create or replace trigger wf_tri_user_list before insert or update or delete on user_list
    for each row
    declare
    uid varchar2(10); useq varchar2(10); asql varchar2(200); namea varchar2(200); nameb varchar2(200);
    begin
    namea:=NULL;
    nameb:=NULL;
    if inserting then
    insert into wflow.bpm_org_user(userid,username,diaplayname,seq) values(:NEW.user_id,:NEW.user_name,:NEW.user_realname,:NEW.user_id);
    dbms_output.put_line('insert trigger is chufale .....');

    end if;
    if updating then
    if (:NEW.user_name<>:OLD.user_name) and (:NEW.user_realname<>:OLD.user_realname) then
    namea:=:NEW.user_name;
    nameb:=:NEW.user_realname;
    asql:='update wflow.bpm_org_user set diaplayname=:1 where username=:2';
    execute immediate asql using namea,nameb;
    else
    if :NEW.user_name<>:OLD.user_name then
    namea:=:NEW.user_name;
    asql:='update wflow.bpm_org_user set user_name=:1 where username=:2';
    execute immediate asql using namea;
    else
    if :NEW.user_realname<>:OLD.user_realname then
    nameb:=:NEW.user_realname;
    asql:='update wflow.bpm_org_user set diaplayname=:1 where username=:2';
    execute immediate asql using nameb,:OLD.user_id;
    end if;
    end if;
    end if;
    end if;
    if deleting then
    update wflow.bpm_org_jobusers set userid = 0 where :OLD.user_id =userid and parentid=-1;
    delete from wflow.bpm_org_jobusers where userid = :OLD.user_id;
    delete wflow.bpm_org_user where userid=:OLD.user_id;
    end if;
    commit;
    end;




    關(guān)鍵字:

    :NEW 和:OLD使用方法和意義,new 只出現(xiàn)在insert和update時(shí),old只出現(xiàn)在update和delete時(shí)。在insert時(shí)new表示新插入的行數(shù)據(jù),update時(shí)new表示要替換的新數(shù)據(jù)、old表示要被更改的原來的數(shù)據(jù)行,delete時(shí)old表示要被刪除的數(shù)據(jù)。

    注意:

    在觸發(fā)器中不能使用commit。
      回復(fù)  更多評(píng)論
      
    主站蜘蛛池模板: 女同免费毛片在线播放| 亚洲国产精品无码专区在线观看| 午夜爽爽爽男女免费观看影院 | 久久免费精品一区二区| 亚洲熟妇AV日韩熟妇在线| 亚洲AV日韩精品久久久久久久| 免费二级毛片免费完整视频| 黄页网站免费在线观看| 日韩中文字幕免费视频| 最近免费中文字幕中文高清| 日日狠狠久久偷偷色综合免费| 中国亚洲呦女专区| 亚洲综合激情另类小说区| 国产成人亚洲精品青草天美| 亚洲午夜爱爱香蕉片| 免费国产精品视频| 日韩免费无砖专区2020狼| 免费无码又黄又爽又刺激| 免费观看美女用震蛋喷水的视频| 免费人成网站在线观看不卡| 亚洲精品视频免费观看| 农村寡妇一级毛片免费看视频 | 日本免费的一级v一片| 国产v精品成人免费视频400条| 免费精品无码AV片在线观看| 成全动漫视频在线观看免费高清版下载 | 亚洲中字慕日产2020| 久久精品国产亚洲AV高清热| 亚洲av日韩综合一区在线观看| 国产aⅴ无码专区亚洲av麻豆| 国产美女精品视频免费观看| 成人免费无码大片a毛片| a级毛片无码免费真人| 一二三四影视在线看片免费 | 一二三四在线观看免费高清中文在线观看 | 久久久久久免费视频| 一二三四影视在线看片免费| 欧美a级成人网站免费| 三年片在线观看免费观看高清电影| 曰批视频免费30分钟成人| 好男人www免费高清视频在线|