如何在Oracle中存储更改历史记录数据并处理未更改的值

时间:2019-06-19 18:28:52

标签: oracle logging plsql triggers database-trigger

大家好,我有一张叫用户的表: enter image description here

然后我有另一个表,该表包含对上表所做的更改,称为Users_History_Changes: enter image description here

我知道我需要一个触发器,该触发器将在更新一个表并将其插入Users_History_Changes表中时触发。但是,这是我做不到的。当在Users_History_Changes表中创建日志时,仅Last_Name被更新时,其他字段必须保持为空。然后,更改了First_Name,因此该表仅显示该名称。最后,我们更改了年龄,ID为1的用户从 '25岁的劳尔·佩雷斯'更改为 30岁的Pedro Felipes。 Time_Stamp是进行更改的时间。

2 个答案:

答案 0 :(得分:2)

您可以测试每个值是否已更改为历史记录表中插入的一部分:

create trigger users_trigger
before insert or update on users
for each row
begin
  insert into users_history_changes (id, first_name, last_name, age, timestamp_changes)
  values (:new.id,
    case when :old.first_name is null or :new.first_name != :old.first_name then :new.first_name end,
    case when :old.last_name is null or :new.last_name != :old.last_name then :new.last_name end,
    case when :old.age is null or :new.age != :old.age then :new.age end,
    systimestamp);
end;
/

检查值是否已将更改为为null可能没有用,因为这对历史记录不是很有用,所以我只检查了 from null。甚至那些测试也可以/应该扩展到涵盖边缘情况,例如检查某事是否确实已更改。

无论如何,带有以下语句:

insert into users
select 1, 'Raul', 'Peres', 25 from dual
union all select 2, 'Francis', 'Lotters', 40 from dual
union all select 3, 'Maria', 'Lopez', 39 from dual;

update users set last_name = 'Felipes' where id = 1;
update users set first_name = 'Pedro' where id = 1;
update users set age = 30 where id = 1;

update users set first_name = 'Maria', last_name = 'Sanchez', age = 40 where id = 3;

历史记录表的结尾为:

        ID FIRST_NAME LAST_NAME         AGE TIMESTAMP_CHANGES           
---------- ---------- ---------- ---------- ----------------------------
         1 Raul       Peres              25 19-JUN-19 20.02.33.470409000
         2 Francis    Lotters            40 19-JUN-19 20.02.33.473139000
         3 Maria      Lopez              39 19-JUN-19 20.02.33.473183000
         1            Felipes               19-JUN-19 20.02.33.548101000
         1 Pedro                            19-JUN-19 20.02.33.594305000
         1                               30 19-JUN-19 20.02.33.640293000
         3            Sanchez            40 19-JUN-19 20.02.33.688710000

db<>fiddle

或者,如果您希望每个更改的值一行,则可以使用updating子句:

create trigger users_trigger
before insert or update on users
for each row
begin
  if inserting then
    insert into users_history_changes (id, first_name, last_name, age, timestamp_changes)
    values (:new.id, :new.first_name, :new.last_name, :new.age, systimestamp);
  end if;

  if updating ('FIRST_NAME') then
    insert into users_history_changes (id, first_name, timestamp_changes)
    values (:new.id, :new.first_name, systimestamp);
  end if;
  if updating ('LAST_NAME') then
    insert into users_history_changes (id, last_name, timestamp_changes)
    values (:new.id, :new.last_name, systimestamp);
  end if;
  if updating ('AGE') then
    insert into users_history_changes (id, age, timestamp_changes)
    values (:new.id, :new.age, systimestamp);
  end if;
end;
/

,但是这将为“更改”为相同值的值生成一行,除非您添加进一步的逻辑以检查实际更改-updating()检查是update语句在其{ {1}}列表。 db<>fiddle

答案 1 :(得分:0)

您可以比较新旧值以进行更新日志记录

create or replace trigger trg_users on users
after insert or update 
for each row
declare
  v_dml_type varchar2(1);

  procedure pr_upd_log( i_dml_type varchar2, i_col_name varchar2, 
                        i_old_val  varchar2, i_new_val  varchar2 ) is
  begin
    insert into Users_History_Changes( dml_type, col_name, old_val, new_val )
     values( i_dml_type, i_col_name, i_old_val, i_new_val );
  end;
begin

  if updating then v_dml_type := 'U';
  elsif inserting then v_dml_type := 'I'; end if; 

  if ( nvl(:old.user_id,-987) != nvl(:new.user_id,-987) ) then     
     pr_upd_log(v_dml_type,'user_id',to_char(:old.user_id),to_char(:new.user_id));
  end if;

  if ( nvl(:old.age,-987) != nvl(:new.age,-987) ) then     
     pr_upd_log(v_dml_type,'age',to_char(:old.age),to_char(:new.age));
  end if;

  if ( nvl(:old.first_name,'NuLLxYZ') != nvl(:new.first_name,'NuLLxYZ') ) then     
     pr_upd_log(v_dml_type,'first_name',:old.first_name,:new.first_name);
  end if;

  if ( nvl(:old.last_name,'NuLLxYZ') != nvl(:new.last_name,'NuLLxYZ') ) then     
     pr_upd_log(v_dml_type,'last_name',:old.last_name,:new.last_name);
  end if;     
  -- for v_dml_type = 'I', use your former way
end;