用于指示更改的 SQL 列名称

发布于 2024-07-14 14:17:00 字数 437 浏览 4 评论 0原文

我真的不知道如何制定标题。 但这是我的问题。 我们使用 SqlCacheDependency 来更新缓存对象。 但这背后的查询是“复杂的”,我们希望让它变得更容易一些。

我们的第一个想法是添加一个像 DateChanged 这样的字段名,并使用 DateChanged 来检查对象是否确实发生了更改,而不是加载表的所有列。

SELECT DateChanged FROM Table1

但我希望

SELECT Id, Title, DateChanged, Description FROM Table1

有人能告诉我是否还有其他方法可以做到这一点,或者是否有命名指示更改的列的标准。 像“Entity Framework”或“NHibernate”这样的框架如何处理这个问题?

I didn't really know how to formulate the title.
But this is my problem.
We are using SqlCacheDependencies to update out cache objects. But the query behind this is "complicated" that we would like to make it somewhat easier.

Our first thought was to add a fieldname like DateChanged and use the DateChanged to check if an object has actually changed in stead of loading all the columns of a table.

SELECT DateChanged FROM Table1

in stead of

SELECT Id, Title, DateChanged, Description FROM Table1

But I was hoping someone could tell me if there are other ways to do this or if there are standards for naming the column that indicates a change.
How are frameworks like "Entity framework" or "NHibernate" handle this?

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

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

发布评论

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

评论(3

九局 2024-07-21 14:17:00

它通常被称为“时间戳”,

  • LLBLGEN
  • < a href="http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2008/07/01/10557.aspx" rel="nofollow noreferrer">LINQTOSQL

It's usually called " timestamp"

ie in

子栖 2024-07-21 14:17:00

这些是我在“可审核”实体上使用的列:

version: hibernate's optimistic concurrency control
createdBy: FK to users
modifiedBy: FK to users
createdAt: timestamp
modifiedAt: timestamp

These are the columns I use on "auditable" entities:

version: hibernate's optimistic concurrency control
createdBy: FK to users
modifiedBy: FK to users
createdAt: timestamp
modifiedAt: timestamp
葮薆情 2024-07-21 14:17:00

如果您使用 SQL Server,请注意时间戳列的命名有些误导性。 虽然它可用于跟踪列的更改,但其值与日期或时间无关。 它可以被认为是数据库中的唯一ID,仅此而已。 http://msdn.microsoft.com/en-us/library/aa260631。 aspx

为了知道是否发生了更改,您需要两个时间戳,就像 cherouvim 的答案一样。 一个是在创建对象时设置的,另一个是在任何修改时设置的,可能是通过更新触发器设置的。 如果它们不同,则该对象已被编辑,但您无法准确判断何时编辑。

在 SQL Server 上,您必须使用相同的模式,但使用日期时间列。 然后,您可以通过从修改日期减去创建日期来计算该行的日期。

If you're on SQL Server be aware a timestamp column is somewhat misleadingly named. Whilst it can be used to track changes to a column, its value has nothing to do with the date or time. It can be thought of as an ID uniqiue within the database, nothing more. http://msdn.microsoft.com/en-us/library/aa260631.aspx

In order to know if a change has taken place you need two timstamps, like in cherouvim's answer. One is set when an object is created, the other on any modification, probably via update trigger. If they differ then the object has been edited, but you can't tell exactly when.

On SQL Server you have to use the same pattern, but with datetime columns. You can then calculate the date of the row by subtracting the creation date from the mod date.

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