保留用户更改的旧数据
我有一个 users
表,其中包含以下字段:用户 ID、电话和地址
。由于这是用户数据,我让用户随时更改它们。问题是我想跟踪这些更改并保留旧数据。以下是我考虑的一些想法:
将新数据附加到旧数据并使用像管道这样的分隔符。检索字段时,我会检查该分隔符是否存在,如果存在,则获取其后面的字符作为新数据。 (感觉很麻烦,感觉不太对)
设置一个不同的
changes
表,其中包含以下字段:userid、fieldname、fieldcontent
。当/如果用户更改数据(任何数据)时,我会在用户的用户 ID 下的这个单独的表中记录事件,以及字段的名称/ID 和字段的旧内容,然后我现在可以覆盖他的旧数据在users
中使用新的。如果我想查找该用户所做的所有更改,我将通过他的用户 ID 搜索changes
表。问题是我将(所有字段的)所有数据更改混合到一个表中,因此changes
中的fieldcontent
字段必须是文本以适应不同的字段类型。这看起来仍然比第一个想法更好,但仍然不确定我是否在做正确的事情。
还有哪些其他想法或已知的最佳实践来保留旧数据?
提前致谢
I have a users
table that has the following fields: userid, phone, and address
. Since this is user data, I'm letting the user change them whenever he wants. Problem is I'd like to keep track of those changes and preserve the old data too. Here's some of the ideas I considered:
appending the new data to the old data and using a separator like a pipe. When retrieving the field, I would check for the existence of that separator and if exists, get the chars after it as the new data. (feels cumbersome and doesn't feel right)
setting up a different
changes
table with the following fields:userid, fieldname, fieldcontent
. When/if a user changes data (any data), I would log the event in this separate table under the user's userid, and the name/id of the field and the old content of the field, then I can now overwrite his old data inusers
with the new. If I want to find all changes made by this user, I would search thechanges
table by his userid. Problem with this is that I'm mixing all data changes (of all fields) into one table and so thefieldcontent
field inchanges
has to be text to accommodate the varying field types. This still seems better than the first idea, but still not sure if I'm doing the right thing.
What other ideas are there or known best practices to keep old data?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
无论你做什么,都不要做第一个。
更改表是一种更好的方法。它也称为审计表或历史表。不过,我不会记录键值对的历史记录。相反,为每个相关表创建历史记录。您可以在应用程序代码中或通过数据库触发器来执行此操作。基本上,每当发生插入、更新或删除时,您都会记录发生的情况以及更改的数据。
表 user:
表 user_history:
Whatever you do don't do the first one.
The changes table is a better approach. It's also called an audit or history table. I wouldn't do a history of key-value pairs however. Instead do a history per relevant table. You can do this in application code or via database triggers. Basically whenever an insert, update or delete happens you record which happened and what data was changed.
Table user:
Table user_history:
我们用来跟踪此类更改的一种非常简单的方法是:
每次在
users
表中插入或更新一条记录时,只需在 users_history 表中插入一条新记录即可。changenumber
从 1 开始并从那里递增。changedate
和changeaddr
可用于跟踪时间和地点。如果字段值未更改,请随意将 NULL 放入相应的
users_history
表字段中。归根结底,您的应用不需要在
users
表中更改或存储大量历史数据,但一切都触手可及。编辑:
这确实保留了旧数据。请参阅以下示例,其中用户一开始使用给定的地址和电话,4 天后更新了地址,5 天后更新了电话。 您拥有一切。
当前用户记录:
历史记录表示例
A very simple way that we have used to track such changes is this:
Each time you INSERT or UPDATE a record in the
users
table, simply INSERT a new record in the users_history table.changenumber
starts at 1 and increments from there.changedate
andchangeaddr
could be used to track when and where.If a field value has not changed, feel free to put NULL in the respective
users_history
table field.At the end of the day, your app does not need to change or store bulky history data in the
users
table, but you have all if it at your fingertips.Edit:
This does preserve the old data. See the following example where the user started with a given address and phone, and then 4 days later updated the address, and 5 days later updated the phone. You have everything.
Current users record:
Sample History Table
实现这一点的最简单方法是使用另一个仅用于历史目的的表,即快照。您不需要镜像所有字段,只需
The simplest way to implement this will be have another table just for history purpose, a snapshot. You don't need to mirror all the fields, just