我有一个大约有15个表的数据库。现在,我在下面创建了一个触发器,该触发器将日志保存到辅助审核表中。我将为所有表创建触发器,因为我想监视数据库中用户执行的所有操作。请通过下面的代码帮助我,如果我做错了,请纠正我。我也在寻求有关如何记录表中发生的特定事件的帮助。例如,某个用户修改了某个雇员的薪水,日志应如下所示:Action_done ---更新薪水。我不需要知道确切的员工是谁,只需知道在哪个列上做了什么。最后,我使用一个序列自动填充了审计表的日志ID,我这样做对吗?预先谢谢你!
CREATE OR REPLACE TRIGGER database_level_audit
after insert or delete or update on employee
for each row
WHEN (new.eventlog_id IS NULL)
declare
v_user varchar2;
v_date date;
v_time timestamp;
act_done varchar2(20) := case when updating then 'Update' when deleting then 'Delete' else 'Insert' end;
begin
select user, To_Char(sysdate,'DD/MON/YYY'), To_char(CURRENT_TIMESTAMP,'HH12:MI:SS') into v_user, v_date, v_time from dual;
SELECT eventlog_id_seq.NEXTVAL INTO :new.eventlog_id FROM dual;
if inserting then
insert into eventlogs(eventlog_id, user_name, date_done, time_done, action_done, object_name)
values (:NEW.eventlog_id, v_user, v_date, v_time, act_done , 'customer');
elsif deleting then
insert into eventlogs(eventlog_id, user_name, date_done, time_done, action_done, object_name)
values (:NEW.eventlog_id, v_user, v_date, v_time, act_done,'customer');
elsif updating then
insert into eventlogs(eventlog_id, user_name, date_done, time_done, action_done, object_name)
values (:NEW.eventlog_id, v_user, v_date, v_time, act_done ,'customer');
end if;
end;