简体   繁体   English

跟踪数据变化

[英]Tracking data changes

I work on a market research database centric website, developed in PHP and MySQL. 我在以PHP和MySQL开发的市场研究数据库中心网站上工作。 It consists of two big parts – one in which users insert and update own data (let say one table T with an user_id field) and another in which an website administrator can insert new or update existing records (same table). 它由两个重要部分组成 - 一个是用户插入和更新自己的数据(比如一个表T带有user_id字段),另一个是网站管理员可以插入新的或更新现有记录(同一个表)。

Obviously, in some cases end users will have their data overridden by the administrator while in other cases, administrator entered data is updated by end users (it is fine both ways). 显然,在某些情况下,最终用户的数据会被管理员覆盖,而在其他情况下,管理员输入的数据会被最终用户更新(两种方式都很好)。

The requirement is to highlight the view/edit forms with (let's say) blue if end user was the last to update a certain field or red if the administrator is to “blame”. 如果最终用户是最后一个更新某个字段,则要求突出显示视图/编辑表单(如果说是蓝色),如果管理员要“责备”,则突出显示红色。

I am looking into an efficient and consistent method to implement this. 我正在寻找一种有效且一致的方法来实现它。

So far, I have the following options: 到目前为止,我有以下选择:

  • For each record in table T, add another one ( char(1) ) in which write 'U' if end user inserted/updated the field or 'A' if the administrator did so. 对于表T中的每个记录,添加另一个(char(1)),如果最终用户插入/更新了字段,则写入“U”,如果管理员这样做,则添加“A”。 When the view/edit form is rendered, use this information to highlight each field accordingly. 呈现视图/编辑表单时,使用此信息相应地突出显示每个字段。

  • Create a new table H storing an edit history containing something like user_id, field_name, last_update_user_id. 创建一个新表H,其中存储包含user_id,field_name,last_update_user_id之类的编辑历史记录。 Keep table H up-to-date when fields are updated in main table T. When the view/edit form is rendered, use this information to highlight each form field accordingly. 在主表T中更新字段时,使表H保持最新。在呈现视图/编辑表单时,使用此信息相应地突出显示每个表单字段。

What are the pros/cons of these options; 这些选择的优缺点是什么? can you suggest others? 你能推荐别人吗?

I suppose it just depends how forward-looking you want to be. 我想这只取决于你想要的前瞻性。

Your first approach has the advantage of being very simple to implement, is very straightforward to update and utilize, and also will only increase your storage requirements very slightly, but it's also the extreme minimum in terms of the amount of information you're storing. 您的第一种方法的优点是实现起来非常简单,更新和使用非常简单,而且只会略微增加您的存储要求,但就您存储的信息量而言,它也是极端最小的。

If you go with the second approach and store a more complete history, if you need to add an "edit history" in the future, you'll already have things set up for that, and a lot of data waiting around. 如果您使用第二种方法并存储更完整的历史记录,如果您将来需要添加“编辑历史记录”,那么您已经为此设置了一些内容,并且等待了大量数据。 But if you end up never needing this data, it's a bit of a waste. 但如果你最终不需要这些数据,那就有点浪费了。

Or if you want the best of both worlds, you could combine them. 或者,如果你想要两全其美,你可以将它们结合起来。 Keep a full edit history but also update the single-character flag in the main record. 保留完整的编辑历史记录,但也更新主记录中的单字符标志。 That way you don't have to do any processing of the history to find the most recent edit, just look at the flag. 这样你就不必对历史进行任何处理来查找最新的编辑,只需查看标志即可。 But if you ever do need the full history, it's available. 但如果您确实需要完整的历史记录,那么它就可以使用。

Personally, I prefer keeping more information than I think I'll need at the time. 就个人而言,我更喜欢保留比当时我想要的更多的信息。 Storage space is very cheap, and you never know when it's going to come in handy. 存储空间非常便宜,你永远不知道什么时候它会派上用场。 I'd probably go even further than what you proposed, and also make it so the edit history keeps track of what they changed, and the before/after values. 我可能会走得更远比你什么建议,也使它这样的编辑历史保存了他们改变什么曲目,和前/后的值。 That can be very handy for debugging, and could be useful in the future depending on the project's exact needs. 这对于调试非常方便,并且可能在将来有用,具体取决于项目的确切需求。

Yes, implement an audit table that holds copies of the historical data, by/from whom &c. 是的,实施一个审核表,其中包含历史数据的副本,由/来自谁。 I work on a system currently that keeps it simple and writes the value changes as simple name-value string pairs along with date and by whom. 我正在处理一个目前保持简单的系统,并将值更改写为简单的名称 - 值字符串对以及日期和由谁。 It requires mandatory master record adjustment, but works well for tracking. 它需要强制主记录调整,但适用于跟踪。 You could implement this easily with a trigger. 您可以使用触发器轻松实现此功能。

The best way to audit data changes is through a trigger on the database table. 审计数据更改的最佳方法是通过数据库表上的触发器。 In your case you may want to just update the last person to make the change. 在您的情况下,您可能只想更新最后一个人进行更改。 Or you may want a full auditing solution where you store the previous values making it easy to restore them if they were made in error. 或者,您可能需要一个完整的审核解决方案,您可以在其中存储以前的值,以便在出错时轻松恢复它们。 But the key to this is to do this on the database and not through the application. 但关键是要在数据库上而不是通过应用程序执行此操作。 Database changes are often made through sources other than the application and you will want to know if this happened as well. 数据库更改通常通过应用程序以外的来源进行,您将想知道是否也发生了这种情况。 Suppose someone hacked into the database and updated the data, wouldn't you like to be able to find the old data easily or know who did it even if he or she did it through a query window and not through the application? 假设某人入侵数据库并更新了数据,您是不是希望能够轻松找到旧数据,或者知道是谁通过查询窗口而不是通过应用程序执行此操作? You might also need to know if the data was changed through a data import if you ever have to get large amounts of data at one time. 如果您一次需要获取大量数据,则可能还需要知道数据是否已通过数据导入进行了更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM