Oracle Database Triggers Complex Security Checks

a need

Prohibit inserting new employee into employee table when not working
two code
  1. --触发器应用场景1:实施复制的安全性检查
  2. --禁止在非工作时间插入新员工
  3. /*
  4. 1、周末:to_char(sysdate,'day') in ('星期六','星期日')
  5. 2、上班前和下班后:to_number(to_char(sysdate,'hh24')) not between 9 and 18
  6. */
  7. create or replace trigger securityemp
  8. before insert
  9. on emp
  10. begin
  11. if to_char(sysdate,'day')in('星期六','星期日')or
  12. to_number(to_char(sysdate,'hh24'))not between 9and18then
  13. --禁止insert新员工
  14. raise_application_error(-20001,'禁止在非工作时间插入新员工');
  15. endif;
  16. end;
  17. /
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'

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326149014&siteId=291194637