--触发器基本语法
--成功插入新员工后,自动打印“成功插入新员工”create or replace trigger saynewemp
after insert
on emp
declare
begindbms_output.put_line('成功插入新员工');
end;
/
/*
触发器应用一:实施复杂的安全性检查
禁止在非工作时间插入新员工1. 周末:to_char(sysdate,'day') in ('星期六','星期日')
2. 上班前 下班后:to_number(to_char(sysdate,'hh24')) not between 9 and 18
*/
create or replace trigger securityemp
before insert
on emp
beginif to_char(sysdate,'day') in ('星期六','星期日','星期五') or to_number(to_char(sysdate,'hh24')) not between 9 and 18 then--禁止insertraise_application_error(-20001,'禁止在非工作时间插入新员工');end if;
end;
/
/*
数据的 确认: 涨后的工资不能少于涨前的工资
*/
create or replace trigger checksalary
before update
on emp
for each row
begin--if 涨后的薪水 < 涨前的薪水 thenif :new.sal < :old.sal thenraise_application_error(-20002,'涨后的工资不能少于涨前的工资。涨前:'||:old.sal||' 涨后:'||:new.sal);end if;end;
/