触发器
触发器与表关联,每当执行一个特定的增删改操作时,oracle会自动执行触发器中定义的语句。
语法:
create (or replace) trigger 触发器名称
(before/after) (insert/delete/update of 列名) on 表名
begin
程序块
end 触发器名;
触发器分为两类:
1.语句级触发器
在指定sql语句操作之前或之后执行一次,无论这条sql影响了多少行。
例:双休日禁止给员工表写入数据
create or replace trigger addCheck
before insert on employee
begin
if to_char(sysdate,‘day’) in (‘星期六’,‘星期日’)
raise_applocation_error(-20001,‘周末禁止添加员工’);
end if;
end addCheck;
2.行级触发器
对指定sql语句影响到的每一行都会执行一次,行级触发器中用new和old来记录变量,识别值的状态。
例:校验员工涨薪后工资大于原来工资
create or replace trigger addSalary
before update of sal on employee
for each row
begin
if :new.sal <= :old.sal then
raise_application_error(-20002,‘涨薪后工资不能比以前低’);
end if;
end addSalary;