2

我正在设计一个关系数据库。它有几个表,并且在应用程序级别有多个用户。我需要知道对某个表的某条记录进行了更改,由哪个用户、哪个时间以及实际更改了什么。有一个用于保存用户信息的表,该表也包含在此行为中。

我应该如何在 SQL 数据库设计中做到这一点,以便让用户看到他们中的哪一个进行了这些更改?

4

2 回答 2

0

您想要的是类似 Wiki 的版本控制。基本上,对于您要保留版本的每个表,您至少需要创建该表的副本,其中包含您提到的添加的字段(用户 ID,添加时)。只要您只需要跟踪更改,这可能就是它的全部内容。然后,在进行编辑时,您只需在该复制表中创建当前行的备份,并将新行放入实际表中。通过这种方式,您可以(希望)添加版本控制,而无需触及现有的演示代码。

如果您需要记录其他操作,例如创建新行和删除,它会变得更加棘手。

如果您需要代码示例,只需查看一些 Wiki 的引擎盖,例如https://mediawiki.org/

于 2012-12-25T18:43:15.297 回答
0

For starters you can look at sql server version tracking mechanisms (row versioning or row changes). After that you can look at sql server audit features. I think sql server audit would be the best for your needs.

On the other hand, if you want to make ad-hok versioning then YOU MUST NOT go to triggers. Imagine, you must create triggers for all tables for inserts, updates and deletes. This IS bad practice.

I think ad-hoc versioning should be avoided (degradation in performance and difficult to support) but in case it cannot be avoided, I would surely use CONTEXT_INFO in order to track current user and then I would try to create something that would read the schema of the table, I would get changes by using sql server change tracking mechanisms and store that in a tablename, changeduser, changedtime, column, prevValue, newValue style. I would not replicate each and every table for the changes.

于 2012-12-26T00:04:30.063 回答