这是维护 mysql 表更改的日志/审核表的合理方法吗?

发布于 2024-10-02 04:25:34 字数 1432 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋风の叶未落 2024-10-09 04:25:34

如果您希望经常按 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.

街角迷惘 2024-10-09 04:25:34

您应该考虑:

  1. 创建一个名为“Audit”的表;
  2. 每次用户创建新记录或更新记录时,都会在该表中创建一条记录;
  3. 该表应具有:

    • 已更新的表的名称,
    • 记录的数量,
    • 进行更改的用户的姓名或 ID,
    • 采取的操作,例如“更新地址”或“创建新客户”,
    • 更新时的时间戳

您必须将数据插入到软件中的此“审核”表中,而不是从数据库中的任何触发器中插入数据,否则您需要为每个触发器复制触发器桌子。
通过编程,您可以使用一种方法来处理每个表一次。

You should consider:

  1. Create a table called "Audit";
  2. Create a record in that table every time user creates a new record or update one;
  3. 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.

删除会话 2024-10-09 04:25:34

这基本上是个好主意。您可能想为所有事件添加触发器,即插入、更新和删除。使用删除时,您必须手动将当前时间戳插入审核表中,因为删除不会更新时间戳列。

考虑将旧值和新值都存储在审计表中(即非 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

I have successfully used exactly this approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文