Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 873
understack Avatar asked Apr 05 '11 05:04

understack


People also ask

How do you track changes in a database table?

At the basic database level you can track changes by having a separate table that gets an entry added to it via triggers on INSERT/UPDATE/DELETE statements. Thats the general way of tracking changes to a database table. The other thing you want is to know which user made the change.

What are audit columns in a table?

Creating auditing columnsEvery time a row is added or changed in a table that has an auditing column, the value of the audit column is generated by the database manager. These generated values are maintained for both SQL and native changes to the row.


1 Answers

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.

like image 86
p.marino Avatar answered Sep 28 '22 01:09

p.marino