--觸發器基本語法
--成功插入新員工後,自動打印“成功插入新員工”
create or replace trigger saynewemp
after insert
on emp
declare
begin
dbms_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
begin
if to_char(sysdate,'day') in ('星期六','星期日','星期五') or
to_number(to_char(sysdate,'hh24')) not between 9 and 18 then
--禁止insert
raise_application_error(-20001,'禁止在非工作時間插入新員工');
end if;
end;
/
/*
數據的 確認: 漲後的工資不能少於漲前的工資
*/
create or replace trigger checksalary
before update
on emp
for each row
begin
--if 漲後的薪水 < 漲前的薪水 then
if :new.sal < :old.sal then
raise_application_error(-20002,'漲後的工資不能少於漲前的工資。漲前:'||:old.sal||' 漲後:'||:new.sal);
end if;
end;
/