SQL - 表设计 - DateCreated 和 DateUpdated 列

发布于 2024-07-08 02:23:37 字数 243 浏览 10 评论 0原文

对于我的应用程序,有多个实体类,用户、客户、帖子等,

我即将设计数据库,并且我想存储创建和更新实体的日期。 这就是棘手的地方。 当然,一种选择是为每个实体表添加created_timestamp和update_timestamp列,但这不是多余的吗?

另一种可能性是创建一个存储此信息的日志表,并且可以使其包含对任何实体的更新的跟踪。

有什么想法吗? 我倾向于实施后者。

For my application there are several entity classes, User, Customer, Post, and so on

I'm about to design the database and I want to store the date when the entities were created and updated. This is where it gets tricky. Sure one option is to add created_timestamp and update_timestamp columns for each of the entity tables but that isn't that redudant?

Another possibility could be to create a log table that stores this information, and it could be made to contain keep track of updates for any entity.

Any thoughts? I'm leaning on implementing the latter.

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

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

发布评论

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

评论(5

生生漫 2024-07-15 02:23:37

我能想到的单日志表适用于所有表的方法有两个主要问题:

  1. 日志表的设计(可能)会限制所有其他表的设计。 日志表很可能有一个名为 TableName 的列,然后是另一个名为 PKValue 的列(它将存储您正在记录的记录的主键值)。 如果您的某些表具有复合主键(即多于一列),则日志表的设计必须考虑到这一点(可能通过具有 PKValue1、PKValue2 等列)。
  2. 如果这是某种类型的 Web 应用程序,那么从触发器中可用的用户身份将是应用程序的帐户,而不是 Web 应用程序用户的 ID(这很可能是您真正想要存储在 CreatedBy 中的内容)场地)。 这只会帮助您区分由 Web 应用程序代码创建的记录和以其他方式创建的记录。

CreatedDate 和 ModifiedDate 列并不是多余的,因为它们是在每个表中定义的。 我会坚持这种方法,并在每个表上放置插入和更新触发器来填充这些列。 如果我还需要记录进行更改的最终用户,我将跳过触发器并从应用程序代码填充时间戳和用户字段。

The single-log-table-for-all-tables approach has two main problems that I can think of:

  1. The design of the log table will (probably) constrain the design of all the other tables. Most likely the log table would have one column named TableName and then another column named PKValue (which would store the primary key value for the record you're logging). If some of your tables have compound primary keys (i.e. more than one column), then the design of your log table would have to account for this (probably by having columns like PKValue1, PKValue2 etc.).
  2. If this is a web application of some sort, then the user identity that would be available from a trigger would be the application's account, instead of the ID of the web app user (which is most likely what you really want to store in your CreatedBy field). This would only help you distinguish between records created by your web app code and records created otherwise.

CreatedDate and ModifiedDate columns aren't redundant just because they're defined in each table. I would stick with that approach and put insert and update triggers on each table to populate those columns. If I also needed to record the end-user who made the change, I would skip the triggers and populate the timestamp and user fields from my application code.

在巴黎塔顶看东京樱花 2024-07-15 02:23:37

我使用“日志”或“事件”表来执行后者。 根据我的经验,“更新”时间戳很快就会变得令人沮丧,因为很多时候您发现自己处于修复状态,而您不仅仅需要最新的更新时间。

I do the latter, with a "log" or "events" table. In my experience, the "updated" timestamp becomes frustrating pretty quick, because a lot of the time you find yourself in a fix where you want not just the very latest update time.

寂寞清仓 2024-07-15 02:23:37

您需要多久将创建/更新的时间戳包含在表示层中? 如果答案不仅仅是“很久一次”,我认为在每个表中包含这些列会更好。

How often will you need to include the created/updated timestamps in your presentation layer? If the answer is anything more than "once in a great great while", I think you would be better served by having those columns in each table.

孤君无依 2024-07-15 02:23:37

在我几年前参与的一个项目中,我们实现了触发器来更新我们所谓的审计表(它存储有关所做更改的基本信息,每个表一个审计表)。 这包括修改日期(和最后修改日期)。

它们仅应用于关键表(不适用于联接或引用数据表)。

这消除了许多通常需要考虑 LastCreated 和 LastCreated 的挫败感。 LastModified 字段,但引入了保持触发器最新的烦恼。

最后,触发器/审核表设计运行良好,我们只需记住在 ETL 之前删除并重新应用触发器(!)。

On a project I worked on a couple of years ago, we implemented triggers which updated what we called an audit table (it stored basic information about the changes being made, one audit table per table). This included modified date (and last modified).

They were only applied to key tables (not joins or reference data tables).

This removed a lot of the normal frustration of having to account for LastCreated & LastModified fields, but introduced the annoyance of keeping the triggers up to date.

In the end the trigger/audit table design worked well and all we had to remember was to remove and reapply the triggers before ETL(!).

情泪▽动烟 2024-07-15 02:23:37

它适用于我正在开发的基于网络的 CMS。 创建和上次更新日期将显示在大多数页面上,并且将列出上次创建(和更新)页面。 管理界面也将使用此信息。

It's for a web based CMS I work on. The creation and last updated dates will be displayed on most pages and there will be lists for the last created (and updated) pages. The admin interface will also use this information.

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