保留用户更改的旧数据

发布于 2024-08-06 22:55:00 字数 655 浏览 5 评论 0原文

我有一个 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 in users with the new. If I want to find all changes made by this user, I would search the changes table by his userid. Problem with this is that I'm mixing all data changes (of all fields) into one table and so the fieldcontent field in changes 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 技术交流群。

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

发布评论

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

评论(3

薆情海 2024-08-13 22:55:00

无论你做什么,都不要做第一个。

更改表是一种更好的方法。它也称为审计表或历史表。不过,我不会记录键值对的历史记录。相反,为每个相关表创建历史记录。您可以在应用程序代码中或通过数据库触发器来执行此操作。基本上,每当发生插入、更新或删除时,您都会记录发生的情况以及更改的数据。

表 user:

  • id
  • 用户名
  • 电子邮件地址
  • 电话
  • 地址

表 user_history:

  • id
  • change_type(I、U 或 D 表示插入、更新或删除)
  • user_id(FK user.id)
  • 电子邮件地址
  • 电话
  • 地址
  • 更改日期/时间
  • (可选),还存储更改者记录

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:

  • id
  • username
  • email address
  • phone
  • address

Table user_history:

  • id
  • change_type (I, U or D for insert, update or delete)
  • user_id (FK user.id)
  • email address
  • phone
  • address
  • date/time of change
  • optionally, also store who changed the record
梦里人 2024-08-13 22:55:00

我们用来跟踪此类更改的一种非常简单的方法是:

users_history` 
    userid 
    changenumber smallint not null
    changedate datetime not null
    changeaddr varchar(32) not null
    phone NULL,
    address NULL

    primary key on (userid, linenumber)

每次在 users 表中插入或更新一条记录时,只需在 users_history 表中插入一条新记录即可。 changenumber 从 1 开始并从那里递增。 changedatechangeaddr 可用于跟踪时间和地点。

如果字段值未更改,请随意将 NULL 放入相应的 users_history 表字段中。

归根结底,您的应用不需要在 users 表中更改或存储大量历史数据,但一切都触手可及。

编辑:

这确实保留了旧数据。请参阅以下示例,其中用户一开始使用给定的地址和电话,4 天后更新了地址,5 天后更新了电话。 您拥有一切。

当前用户记录:

100                            |  234-567-8901   |   123 Sesame Street

历史记录表示例

100   |  1  | 2009-10-01 12:00 |  123-456-7890   |   555 Johnson Street
100   |  2  | 2009-10-05 13:00 |  NULL           |   123 Sesame Street
100   |  3  | 2009-10-10 15:00 |  234-567-8901   |   NULL

A very simple way that we have used to track such changes is this:

users_history` 
    userid 
    changenumber smallint not null
    changedate datetime not null
    changeaddr varchar(32) not null
    phone NULL,
    address NULL

    primary key on (userid, linenumber)

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 and changeaddr 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:

100                            |  234-567-8901   |   123 Sesame Street

Sample History Table

100   |  1  | 2009-10-01 12:00 |  123-456-7890   |   555 Johnson Street
100   |  2  | 2009-10-05 13:00 |  NULL           |   123 Sesame Street
100   |  3  | 2009-10-10 15:00 |  234-567-8901   |   NULL
顾铮苏瑾 2024-08-13 22:55:00

实现这一点的最简单方法是使用另一个仅用于历史目的的表,即快照。您不需要镜像所有字段,只需

change_id // row id (just for easy management later on if you need to delete specific row, otherwise its not really necessary)
user_id // Original user id
change_time // time of change
data // serialized data before change.

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

change_id // row id (just for easy management later on if you need to delete specific row, otherwise its not really necessary)
user_id // Original user id
change_time // time of change
data // serialized data before change.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文