PL/SQL学习笔记-触发器

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

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;


执行如下代码测试post

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;

执行如下代码测试3d

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


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

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

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语句和其余触发器没什么大区别

四:删除触发器get

drop trigger t_xland;