如果您希望经常按 id 选择,您可能需要在 id 列上添加索引。此外,如果您可以添加有关谁进行更改的信息,可能会有所帮助。也许您也想考虑记录插入和删除?除此之外,我认为这是一个完全合理的方法。
You might want to add an index on the id column if you expect to select by id often. Furthermore, it would probably be helpful if you could add information about who made the change. And maybe you want to consider logging inserts and deletes too? Other than that I think it is a perfectly reasonable approach.
Create a record in that table every time user creates a new record or update one;
This table should have:
the name of the table that was updated,
the number of the record,
the name or id of the user that made the change,
the action taken, like 'updated address' or 'new client created',
the timestamp of the moment that update was made
You have to insert data to this "Audit" table in the software, not from any trigger in the database or you will need to copy the trigger for each table. Programming you could do one method to handle every table once.
考虑将旧值和新值都存储在审计表中(即非 id 列数量的两倍),因为当仅记录当前值时,搜索值更改的时间很困难,但是当您记录这两个值时,这很容易
where old_column1 != new_column1
:使查找特定时间的值变得容易:
where someDateTime between old_last_updated and new_last_updated
我已经成功地使用了这种方法。
It's basically a good idea. You probably want to add triggers for all events, ie insert, update and delete. With delete you'll have to insert the current timestamp into the audit table manually, as delete wont update the timestamp column.
Consider storing both old and new values in the audit table (ie double the number of non-id columns) because searching for when a value changed is difficult when only the current value is logged, but when you log both, it's easy:
where old_column1 != new_column1
It also makes finding the value at a particular time easy:
where someDateTime between old_last_updated and new_last_updated
发布评论
评论(3)
如果您希望经常按
id
选择,您可能需要在id
列上添加索引。此外,如果您可以添加有关谁进行更改的信息,可能会有所帮助。也许您也想考虑记录插入和删除?除此之外,我认为这是一个完全合理的方法。You might want to add an index on the
id
column if you expect to select byid
often. Furthermore, it would probably be helpful if you could add information about who made the change. And maybe you want to consider logging inserts and deletes too? Other than that I think it is a perfectly reasonable approach.您应该考虑:
该表应具有:
时间戳
您必须将数据插入到软件中的此“审核”表中,而不是从数据库中的任何触发器中插入数据,否则您需要为每个触发器复制触发器桌子。
通过编程,您可以使用一种方法来处理每个表一次。
You should consider:
This table should have:
timestamp
of the moment that update was madeYou have to insert data to this "Audit" table in the software, not from any trigger in the database or you will need to copy the trigger for each table.
Programming you could do one method to handle every table once.
这基本上是个好主意。您可能想为所有事件添加触发器,即插入、更新和删除。使用删除时,您必须手动将当前时间戳插入审核表中,因为删除不会更新时间戳列。
考虑将旧值和新值都存储在审计表中(即非 id 列数量的两倍),因为当仅记录当前值时,搜索值更改的时间很困难,但是当您记录这两个值时,这很容易
:使查找特定时间的值变得容易:
我已经成功地使用了这种方法。
It's basically a good idea. You probably want to add triggers for all events, ie insert, update and delete. With delete you'll have to insert the current timestamp into the audit table manually, as delete wont update the timestamp column.
Consider storing both old and new values in the audit table (ie double the number of non-id columns) because searching for when a value changed is difficult when only the current value is logged, but when you log both, it's easy:
It also makes finding the value at a particular time easy:
I have successfully used exactly this approach.