zoukankan      html  css  js  c++  java
  • PL/SQL学习笔记触发器

    一:语句级触发器
    语句级触发器是指当执行DML操作时,以语句为单位执行的触发器
    (注意与下面提到的行级触发器比较)
    先看代码

    create or replace trigger xland_trigger
    before insert
    or update
    or delete
    on labor.xland
    begin
    if(to_char(sysdate,'DAY') in ('星期六','星期日'))
    or (to_char(sysdate,'HH24') not between 8 and 18) then
    raise_application_error(-20001,'不是上班时间');
    end if;
    end;


    执行以下代码测试

    insert into labor.xland (xland.title,xland.content,xland.state) values ('123','234',3);

    ORACLE抛出异常
    image 

    二:行级触发器
    行级触发器是指执行DML操作时,以数据行为单位执行的触发器,每一行都执行一次触发器
    先看代码:

    create or replace trigger xland_trigger
    before insert 
    on labor.xland
    for each row
    begin
       if :new.title = 'xland' then
          insert into labor.xland (xland.title,xland.content,xland.state) values ('123','cha2',3);
       end if;
    end;

    执行以下代码测试

    insert into labor.xland (xland.title,xland.content,xland.state) values ('xland','cha1',3);


    结果:
    image 
    在行级触发器中可以对列的值进行访问(很重要!)
    列名前加   :old.    表示变化前的值
    列名前加   :new.    表示变化后的值
    在when子句中不用冒号。

    三:instead of 触发器(视图上的触发器)
    先看代码

    create or replace trigger t_xland
    instead of insert
    on v_xland
    for each row
    begin
    insert into xland (title,content,state) values ('1','1',1);
    end;


    其实就是取代了insert语句和其他触发器没什么大区别

    四:删除触发器

    drop trigger t_xland;
  • 相关阅读:
    Win7下安装iMac系统
    Windows平台cocos2d-x 3.0 android开发环境
    iOS Dev (50)用代码实现图片加圆角
    内部消息 微软中国云计算 内測Azure免费账号 赶紧申请 错过不再有
    android锁屏软件制作
    CF1019E Raining season
    各数据库系统独有函数
    其他函数
    日期时间函数
    字符串函数
  • 原文地址:https://www.cnblogs.com/liulun/p/1541517.html
Copyright © 2011-2022 走看看