1、触发器的概念
每当一个特定的数据库操作语句(Insert、Update、delete)在指定的表上发出时,Oracle自动执行触发器中定义的语句序列。
2、触发器的类型
(1)语句级的触发器
触发的对象为表,在指定的操作语句操作之前或之后执行一次,不管这条语句影响了多少行。
(2)行级触发器(for each row)
触发器作用的对象为每一条记录,在行级触发器中使用:old和:new伪记录变量识别值的状态。
3、定义一个触发器:
4、触发器的应用
(1)安全性检查:
create or replace trigger securityemp
before insert
on emp
begin
if to_char(sysdate,'day') in ('星期六','星期日') or
to_number(to_char(sysdate,'hh24')) not between 9 and 17 then
raise_application_error(-20001,'禁止在非工作时间插入新员工');
end if;
end securityemp;
(2)数据的确认:
create or replace trigger checksalary
before update
on emp
for each row
begin
if :new.sal<:old.sal
then
raise_application_error(-20002,'涨后的薪水不能少于涨前的薪水');
end if;
end checksalary;