如何跟踪数据库表中多列的更改以进行审计?
这个问题已经回答过好几次了:这里,此处和外部链接此处 和 这里。
我了解上述线程中描述的方法,并且我计划使用这种方法。
但我对实施这一点没有什么基本疑问。
就我而言,一行中的多个列可以同时更新,因此要遵循正确的实现方法:
- 找出操作类型(INSERT/UPDATE/DELETE)
- 找出正在更新的列
- 在之前读取整行更新
- 在审计表中为使用旧值和新值(以及其他详细信息)更改的每一列插入一行
- 更新表
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:
- Find out type of operation (INSERT/UPDATE/DELETE)
- Find out columns which are getting updated
- Read the full row before updating
- Insert one row in Audit table for each column being changed with old and new value (along with other details)
- update the table
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您使用的是足够新的 mySQL 版本,我将使用 触发器,就个人而言。
假设它们或多或少与我在其他产品(例如 Oracle)中熟悉的产品一样工作,您的问题会变得更简单,因为您在行上放置了“更新”触发器并使用它来更新每个字段的审计表 可能需要注意的
是:如果您的应用程序仅以一个用户身份登录数据库(例如,如果您使用连接池,则这是一种常见方法),记录实际用户身份可能会很棘手。
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.