• 数据库触发器应用场景


    一、实施复杂的安全性检查

    create or replace trigger mytrigger 
    before insert
    on emp
    begin
       if to_char(sysdate,'day') in ('星期六','星期日')
          or
             to_number(to_char(sysdate,'hh24')) not between 9 and 18
       then 
       raise_application_error(-20001,'禁止插入新的员工');
      
       end if;
    end;
    /
    

    二、数据的确认

    create or replace trigger checksal 
    before update
    on emp
    for each row
    begin
       if :new.sal < :old.sal 
       then 
       raise_application_error(-20002,'涨薪后的薪水不能低于原来的薪水,涨薪后薪水:'||:new.sal||' 原来薪水:'||:old.sal);
      
       end if;
    end;
    /
    

    三、数据库基于值的审计

    --创建表保存审计后的数据
    create table empinfo(
           info varchar2(300)
    )
    --创建触发器
    create or replace trigger audits
    after update 
    on emp
    for each row
    begin
        if :new.sal > 6000 then 
           insert into empinfo values(:new.empno||'  '||:new.ename||'  '||:new.sal);
        end if;
    end;
    /
    

    四、数据的备份

    --直接创建emp表的备份表emp_back
    
    create table emp_back as select * from emp;
    
    
    create or replace trigger sync_sal
    after update 
    on emp
    for each row
    begin
       update emp_back set sal = :new.sal where empno = :new.empno; 
    end;
    /
    

    --慕课网学习小结

  • 相关阅读:
    [POJ3635]Full Tank? 题解
    洪水题解
    [HNOI2009]最小圈 题解
    Grazing on the Run 题解
    [BZOJ4237]稻草人 题解
    [POJ3783]Balls 题解
    [POI2005]Bank notes 题解
    字符串题解
    pyinstaller利用spec文件打包的使用模板
    Pycharm2020 永久激活
  • 原文地址:https://www.cnblogs.com/dylq/p/9878968.html
Copyright © 2020-2023  润新知