Oracle 触发器,trigger

创建触发器:

--每当成功插入新员工后,自动打印“成功插入新员工”

create trigger firsttrigger
after insert
on 表名
declare
begin
  dbms_output.put_line('成功插入新员工');
end;
/

创建触发器(语句级触发器。实现安全性检查的功能):

/*
实施复杂的安全性检查
禁止在非工作时间 插入新员工

1、周末:  to_char(sysdate,'day') in ('星期六','星期日')
2、上班前 下班后:to_number(to_char(sysdate,'hh24')) not between 9 and 17
*/
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 17 then
      --禁止insert
      raise_application_error(-20001,'禁止在非工作时间插入新员工');  --抛出应用层的错误。不能抛出数据库异常(例外)。错误码在-20000到-20999之间。                        
   end if;
  
end securityemp;
/

创建触发器(行级触发器。实现数据确认的功能):

/*
数据的确认
涨后的薪水不能少于涨前的薪水
*/
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 checksalary;
/

猜你喜欢

转载自blog.csdn.net/houyanhua1/article/details/82431581