a need
Prohibit inserting new employee into employee table when not working
two code
--触发器应用场景1:实施复制的安全性检查
--禁止在非工作时间插入新员工
/*
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 9and18then
--禁止insert新员工
raise_application_error(-20001,'禁止在非工作时间插入新员工');
endif;
end;
/
Three verification
SQL> insert into emp(empno,ename,sal,deptno) values(1003,'Tome','3000',10);
insert into emp(empno,ename,sal,deptno) values(1003,'Tome','3000',10)
*
Error on line 1:
ORA-20001: insertion of new employee during non-working hours is prohibited
ORA-06512: 在 "SCOTT.SECURITYEMP", line 5
ORA-04088: error during execution of trigger 'SCOTT.SECURITYEMP'