對於某個業務表邏輯比較複雜,修改資料頻繁,這時候為了方便開發定位問題,常常對該錶的每次修改的資料都保持。我們可以使用另外一張表來記錄資訊, 一般開發人員會稱呼這些表為歷史表。
下面舉例,私人圖書館的-圖書預約記錄歷史表(這只是舉例,因為-圖書預約記錄不需要歷史表的,哈哈)
--圖書預約記錄歷史表【booking_record_history】
然後建立觸發器,為了方便以後維護,可以加乙個判斷,當不需要記錄歷史表時,則跳過該過程。就像乙個開掛一樣來控制,比如,通過表booking_tr_control來控制,當表中查不到對應的觸發器的記錄時,則不記錄到歷史表。create table sys.booking_record_history
(
id_booking_record_history varchar2(32) not null,
id_booking_record varchar2(32),
created_by varchar2(100),
date_created date,
updated_by varchar2(100),
date_updated date,
date_updated_msel timestamp(6)
);
-- add comments to the table
comment on table sys.booking_rrcord_history is '歷史表';
-- add comments to the columns
comment on column sys.booking_rrcord_history.id_booking_rrcord_history
is '歷史表主鍵';
comment on column sys.booking_rrcord_history.id_booking_rrcord
is 'booking_rrcord基礎表主鍵';
comment on column sys.booking_rrcord_history.created_by
is '建立人';
comment on column sys.booking_rrcord_history.date_created
is '建立時間';
comment on column sys.booking_rrcord_history.updated_by
is '建立人';
comment on column sys.booking_rrcord_history.date_updated
is '更新時間';
comment on column sys.booking_rrcord_history.date_updated_msel
is '修改時間精確到毫秒';
-- create index
create unique index sys.pk_booking_rrcord_history_id on sys.booking_rrcord_history (id_booking_rrcord_history);
-- create/recreate primary, unique and foreign key constraints
alter table sys.booking_rrcord_history
add constraint pk_booking_rrcord_history_id primary key (id_booking_rrcord_history) using index pk_booking_rrcord_history_id;
delete from booking_tr_control hu where hu.trigger_name = 'booking_rrcord_history_au' and hu.switch_for = 'booking_rrcord_hi_au';
insert into booking_tr_control values('booking_rrcord_history_au','booking_rrcord_hi_au','1');
create or replace trigger mytrg.booking_rrcord_history_au
after insert or update on sys.booking_rrcord
for each row
declare
v_sqlerrm varchar2(800);
v_sqlcode varchar2(6);
v_error_comment varchar2(300);
v_trigger_user varchar2(30);
v_count number;
begin
select user into v_trigger_user from dual;
select count(0)
into v_count
from booking_tr_control
where trigger_name = 'booking_rrcord_history_au'
and switch_for = 'booking_rrcord_hi_au'
and status = '1';
-- trigger 開關
if v_count > 0 then
insert into booking_rrcord_history
(id_booking_rrcord_history,
id_booking_rrcord,
created_by,
date_created,
updated_by,
date_updated,
date_updated_msel)
values
(sys_guid(),
:new.id_booking_rrcord,
:new.created_by,
:new.date_created,
:new.updated_by,
:new.date_updated,
systimestamp);
end if;
exception
when others then
v_sqlcode := sqlcode;
v_sqlerrm := substr(sqlerrm, 1, 200);
insert into tr_error_log
(error_no, --系統錯誤**
error_message, --系統錯誤資訊
trigger_name, --出錯的trigger
trigger_user, --出錯的使用者
trigger_date, --出錯的時間
error_comment --出錯詳細資訊
)values
(v_sqlcode,
v_sqlerrm,
'booking_rrcord_hi_au',
v_trigger_user,
sysdate,
v_error_comment);
end;
/
開獎歷史表
第2009065期 08,12,20,22,30,33 02 第2009066期 02,15,19,24,31,32 04 第2009067期 04,10,16,23,28,30 05 第2009068期 06,11,18,20,25,30 05 第2009069期 03,05,12,18,21,2...
將歷史記錄移到歷史表中
用一配製表存需要轉移資料的大表名稱 每次都查表名與tab name相匹配且is used 1的 說明是當前的歷史表 create table config table configid int,id tab name varchar 64 需要轉移資料大表名稱 condition col varch...
mysql歷史資料 mysql 歷史資料表遷移方案
當業務執行一段時間後,會出現有些表資料量很大,可能對系統效能產生不良的影響,常見的如訂單表 登入log表等,這些資料很有時效性,比如我們一般很少去查上個月的訂單,最多也就是報表統計會涉及到。在我們的資料庫中,使用者登入表就是這種型別的表,一般而言,表中的資料是不可逆的,只有插入操作沒有刪除或者修改操...