设为首页 收藏本站
查看: 1000|回复: 0

[经验分享] oracle 触发器1

[复制链接]

尚未签到

发表于 2016-7-19 12:35:58 | 显示全部楼层 |阅读模式
  存放在数据库中,并被隐含执行的存储过程。在Oracle8i之前,只允许给予表或者视图的的DML的操作,而从Oracle8i开始,不仅可以支持DML触发器,也允许给予系统事件和DDL的操作。
  
  --Before语句触发器
--禁止工作人员在休息日改变雇员信息
create or replace trigger tr_src_emp
  before insert or update or delete on emp
  begin
    if to_char(sysdate,'DY','nls_date_language=AMERICAN') in ('SAT','SUN') then
      raise_application_error(-20001,'can not modify user information in weekend');
    end if;
  end;
/
  --使用条件谓语
create or replace trigger tr_src_emp
  before insert or update or delete on emp
  begin
    if to_char(sysdate,'DY') in ('星期六','星期天') then
      case
        when inserting then
          raise_application_error(-20001,'fail to insert');
        when updating then
          raise_application_error(-20001,'fail to updating');
        when deleting then
          raise_application_error(-20001,'fail to delete');
      end case;
    end if;
  end;
/
  
  --after语句触发器
--例如:为了统计在EMP表上的增、删、改的次数。先建一张表
create table audit_table(
    Name varchar2(20),
    ins int,
    upd int,
    del int,
    starttime date,
    endtime date
  );
  
  create or replace trigger tr_audit_emp
after insert or update or delete on emp
declare
  v_temp int;
  begin
    select count(*) into v_temp from audit_table
    where name='EMP';
    if v_temp=0 then
      insert into audit_table values('EMP',0,0,0,sysdate,null);
    end if;
    case
      when inserting then
        update audit_table set ins=ins+1,endtime=sysdate where name='EMP';
      when updating then
        update audit_table set upd=upd+1,endtime=sysdate where name='EMP';
      when deleting then
        update audit_table set del=del+1,endtime=sysdate where name='EMP';
    end case;
  end;
/
  
  -- 行触发器 执行DML操作时,每作用一行就触发一次触发器。
-- Before行触发器
-- 例如:确保员工工资不能低于原有工资
create or replace trigger tr_emp_sal
before update of sal on emp
for each row
  begin
    if :new.sal<:old.sal then
      raise_application_error(-20010,'sal should not be less');
    end if;
  end;
/
  
  -- after行触发器
-- 例如:统计员工工资变化
  Create table audit_emp_change(
    Name varchar2(10),
    Oldsal number(6,2),
    Newsal number(6,2),
    Time date
   );
  
  create or replace trigger tr_sal_sal
after update of sal on emp
for each row
  declare
    v_temp int;
    begin
      select count(*) into v_temp from audit_emp_change where name=:old.ename;
      if v_temp=0 then
        insert into audit_emp_change values(:old.ename,:old.sal,:new.sal,sysdate);
      else
        update audit_emp_change set oldsal=:old.sal,newsal=:new.sal,time=sysdate where name=:old.ename;
      end if;
   end;
/
  
  -- 限制行触发器
create or replace trigger tr_sal_sal
after update of sal on emp
for each row
   when (old.job='SALESMAN')
     declare
       v_temp int;
       begin
         select count(*) into v_temp from audit_emp_change where name=:old.ename;
         if v_temp=0 then
            insert into audit_emp_change values(:old.ename,:old.sal,:new.sal,sysdate);
         else
            update audit_emp_change set oldsal=:old.sal,newsal=:new.sal,time=sysdate where name=:old.ename;
        end if;
    end;
/
  
  
  -- 注意事项
  编写DML触发器的时,触发器代码不能从触发器所对应的基表中读取数据。
例如:如果要基于EMP表建立触发器。那么该触发器的执行代码不能包含对EMP表的查询操作。
Create or replace trigger tr_emp_sal
  Before update of sal on emp
  For each row
   declare
     Maxsal number(6,2);
     Begin
      select max(sal) into maxsal from emp;
      If :new.sal>maxsal then
        Raise_application_error(-21000,'error');
      End if;
   End;
/
  创建的时候不会报错。但是一旦执行就报错了 update emp set sal=sal*1.1 where deptno=30
  
  -- 触发器的主要用途
  控制数据安全  例如:在非工作时间不能对EMP表做操作
  create or replace trigger tr_emp_time
before insert or update or delete on emp
begin
  if to_char(sysdate,'HH24') not between '9' AND '17' THEN
    raise_application_error(-20101,'not work time');
  end if;
end;
/
  
  实现数据统计 例如:上面提到的记载员工的工资变化等
  
  实现数据的完整性
  例如:如果只是限制员工的工资不能低于800,可以选用check约束
Alter table emp add constraint ck_sal check(sal>=800);
但如果是限定新工资不能低于其原来工资,也不能高于20%。则通过约束是无法实现的。这时可通过触发器来实现.
  Create or replace trigger tr_check_sal
Before update of sal on emp
For each row
  When(new.sal<old.sal or new.sal>1.2*old.sal)
  Begin
    Raise_application_error(-20931,'ddd');
  End;
/
  
  实现参照完整性 约束可实现级联删除,却不能实现级联更新.可通过触发器实现.
  Create or replace trigger tr_update_cascade
after update of deptno on dept
for each row
begin
   update emp set deptno=:new.deptno where deptno=:old.deptno;
end;
/

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-246373-1-1.html 上篇帖子: Oracle建分区表 下篇帖子: ORACLE 的if else
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表