Data history tracking best practice

China☆狼群 提交于 2019-12-04 10:02:26

I have done this multiple times on PostgreSQL using separate "history" schema and triggers.

The tables in the "history" schema are identical to the real tables but with history_id PK and event timestamp added. Tables from "history" schema don't have any constraints. Also you need to create field for action if you need to track deletion as well.

In postgreSQL you can easily create such table using such CREATE statement:

CREATE TABLE history.tbl AS
  (history_id BIGSERIAL PRIMARY KEY, 
  event_time TIMESTAMP DEFAULT NOW(), 
  action CHAR(1), 
  LIKE public.tpl);

After this you should create trigger which will insert into the history table on insert, update, delete.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!