技术开发 频道

ORACLE PL/SQL编程之八:把触发器说透

  8.6 数据库触发器的应用实例

  用户可以使用数据库触发器实现各种功能:

  复杂的审计功能;

  例:将EMP 表的变化情况记录到AUDIT_TABLE和AUDIT_TABLE_VALUES中。

CREATE TABLE audit_table(

  Audit_id NUMBER,

  User_name VARCHAR2(
20),

  Now_time
DATE,

  Terminal_name VARCHAR2(
10),

  Table_name VARCHAR2(
10),

  Action_name VARCHAR2(
10),

  Emp_id NUMBER(
4));

  CREATE TABLE audit_table_val(

  Audit_id NUMBER,

  Column_name VARCHAR2(
10),

  Old_val NUMBER(
7,2),

  New_val NUMBER(
7,2));

  CREATE SEQUENCE audit_seq

  START
WITH 1000

  INCREMENT BY
1

  NOMAXVALUE

  NOCYCLE NOCACHE;

  CREATE
OR REPLACE TRIGGER audit_emp

  AFTER INSERT
OR UPDATE OR DELETE ON emp

  
FOR EACH ROW

  DECLARE

  Time_now
DATE;

  Terminal CHAR(
10);

  BEGIN

  Time_now:
=sysdate;

  Terminal:
=USERENV('TERMINAL');

  
IF INSERTING THEN

  INSERT INTO audit_table

  VALUES(audit_seq.NEXTVAL, user, time_now,

  terminal,
'EMP', 'INSERT', :new.empno);

  ELSIF DELETING
THEN

  INSERT INTO audit_table

  VALUES(audit_seq.NEXTVAL, user, time_now,

  terminal,
'EMP', 'DELETE', :old.empno);

  
ELSE

  INSERT INTO audit_table

  VALUES(audit_seq.NEXTVAL, user, time_now,

  terminal,
'EMP', 'UPDATE', :old.empno);

  
IF UPDATING('SAL') THEN

  INSERT INTO audit_table_val

  VALUES(audit_seq.CURRVAL,
'SAL', :old.sal, :new.sal);

  
ELSE UPDATING('DEPTNO')

  INSERT INTO audit_table_val

  VALUES(audit_seq.CURRVAL,
'DEPTNO', :old.deptno, :new.deptno);

  
END IF;

  
END IF;

  
END;
0
相关文章