How to track changes in multiple columns in database table for auditing purposes?

故事扮演 提交于 2020-01-03 10:44:09

问题


This question has been answered on SO several times : here, here and external links here and here.

I understand the approaches described in above threads and I plan to use this approach.

But I've got few basic doubts in implementing that.

In my case, multiple columns in a row could be updated at the same time, so is following the correct way to implement:

  1. Find out type of operation (INSERT/UPDATE/DELETE)
  2. Find out columns which are getting updated
  3. Read the full row before updating
  4. Insert one row in Audit table for each column being changed with old and new value (along with other details)
  5. update the table

回答1:


Assuming you are using a sufficiently recent version of mySQL I would use triggers, personally.

Assuming they work more or less as the ones I am familiar with in other products (e.g. Oracle) your problem becomes simpler, in the sense that you put an "update" triggers on the row and use it to update the audit table for each field you are interested in.

Possible caveat: if your application logs on the DB as just one user (a common approach if you use connection pooling, for example) it may be tricky to log the actual user identity.



来源:https://stackoverflow.com/questions/5547773/how-to-track-changes-in-multiple-columns-in-database-table-for-auditing-purposes

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