首页 > Linux操作系统 > Linux操作系统 > Oracle之触发器
|
1. 用触发器跟踪对表EMP的DML操作信息,提供审计和日志记录。(提示:使用触发器谓词) n 插入测试: Create table temp(aa varchar2(100));
Insert into emp(empno,ename,job,sal) values(7962,'ROSE', 'CLERK', 2100); 执行结果如下: 已在 EMP表 中插入数据! -- 删除测试: Delete from emp where empno=7962; 执行结果如下: 已删除 EMP表 中的数据! -- 修改测试: Update emp set sal=2000 where empno=7962; 执行结果如下: 已更新 EMP表 中的数据!
Set serverouput on create or replace trigger change_emp before insert or delete or update on emp for each row declare result varchar2(50); begin If inserting then result:=to_char(sysdate,'YY_MON_DD HH24:MI:SS')||'已经向EMP表中插入'||:new.empno||'记录!'; elsif deleting then result:=to_char(sysdate,'YY_MON_DD HH24:MI:SS')||'已经从EMP表中删除'||:old.empno||'记录!'; elsif updating then result:=to_char(sysdate,'YY_MON_DD HH24:MI:SS')||'已经在EMP表中更新'||:old.empno||'记录!'; end If; dbms_output.put_line(result); insert into temp values(result); end;
2. 创建触发器CHECK_SAL,禁示对职务为CLERK的雇员的工资修改值超出1000至2000的范围,即CLERK职务员工的修改后工资值只能在1000~2000之间。要求测试该触发器。
create or replace trigger update_clerk_sal before update on emp for each row declare result varchar2(100); begin if lower(:old.job)='clerk' then if :new.sal>2000 or :new.sal<1000 then raise_application_error(-20000,:old.empno||'的工资修改不符合规定,只能位于1000-2000!'); end if; end if; end;
update emp set sal=sal+1 where empno=7962 update emp set sal=sal-1001 where empno=7962 update emp set sal=sal-1 where empno=7962 |
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/27042095/viewspace-739588/ ,如需转载,请注明出处,否则将追究法律责任。