在sql中跟踪记录更新的一些最佳实践是什么?(What are some of the best practices for tracking updates to a record over time in sql? [closed])
我有一个带有主键@productid(bigint),产品号(int)和版本(int)的产品表
每当有人仅对产品记录进行更改时,我计划在数据库中插入一个具有相同产品编号和版本号+1的新行。这将为我提供记录所需的历史跟踪,因为我可以看到版本随时间变化。
/* Selecting the current version is simple */ Select top 1 * from products where productnumber = @productnumber order by version desc但是,我的问题来自外键一对多或多对多关系表。 此表指向还需要跟踪的许多其他(即具有日期范围,产品类别等的产品定价)。
/* Product categories, pricing */ /* Should I use @productnumber here? How do I track changes to these records? */ select name from productcategories where productid = @productid select price from productpricing where productid = @productid and StartDate > @StartDate and EndDate <@Enddate所以现在任何时候都有版本更改,我打算重新插入新的类别和定价记录以及生成的新主键产品ID。这将导致大量重复,特别是如果没有更改对这些记录做了。
此问题也随之而来 - 如果删除某个类别但产品记录没有变化会怎样? 我想看看谁删除了这个类别。 基本上,每个表都需要进行全面审核。
我已经看到了一些不同的例子,但是大多数例子似乎只处理一个表中的记录而不是一对多或多对多关系的记录。 我希望这可以在不需要额外表格的情况下完成。
有没有更好的方法或做法? 这会成为一场表演噩梦吗?
I have a product table with a primary key @productid (bigint), a product number (int), and a version (int)
Any time someone makes changes to the product record OLY, I plan on inserting a new row in the database with the same product number and version number + 1. This will provide me with the historical tracking I need for the record because I can see the version changes throughout time.
/* Selecting the current version is simple */ Select top 1 * from products where productnumber = @productnumber order by version descHowever, my problem comes in with the Foreign key one-to-many or many-to-many relatihip tables. This table points to many others ( product pricing with date ranges, product categories, etc.) which also need to be tracked.
/* Product categories, pricing */ /* Should I use @productnumber here? How do I track changes to these records? */ select name from productcategories where productid = @productid select price from productpricing where productid = @productid and StartDate > @StartDate and EndDate <@EnddateSo now any time there is a version change, I plan to to re-insert new category and pricing records with the new Primary Key product id that was generated..This is going to lead to a ton of duplicates, especially if no changes were made to these records.
Also the issue comes in with - what happens if a category is removed but there were no changes to the product record? I would want to see who removed the category. Essentially, a full audit is needed on each table.
I have seen some different examples but most of them only seem to deal with a record in one table and not a record that is a part of one-to-many or many-to-many relatihips. I was hoping this could be done without the need of additional tables.
Are there any better methods or practices? Is this going to be a performance nightmare?
最满意答案
如果您使用的是较新版本的SQL Server,则可以查看时态表,因为这可能是您的最佳选择。
如果你需要支持旧版本,我首选的方法是让历史表包含一个新的PK列,更改标志(I,U,D),修改日期,进行更改的用户以及来自的所有列主表。 然后,我将与非历史记录表的PK相关的列编入索引。 如果不在其中放置逻辑,触发器不会对性能产生太大影响。 示例(伪代码):
Table: Car Column: CarID IT IDETITY(1,1) Primary Key Column: ame varchar Table: Car_hist Column: Car_histID IT IDETITY(1,1) Primary Key Column: Change char(1) Column: DateOfChange DateTime2 Column: ChangedByUser (varchar or int) Column: CarID <-add a unique non-clustered index Column: ame varchar您可以在SQL中编写生成器来生成脚本以创建历史表,索引等。如果您有一致的表设计实践,它会有所帮助。
现在的原因是:我很少查询历史表,但是当我这样做时,几乎总是只有一条记录才能看到发生了什么以及谁更改了它。 此方法允许您快速从父表的PK值中选择历史记录,并轻松地将其读取为历史更改日志(谁更改了什么以及何时更改)。 我不知道你在设计中如何做到这一点。 如果你真的很光滑,你可以到或写一个为你区分行的网格,你可以很快看到改变了什么。
If you are using a newer version of SQL Server as you are, you can should look into temporal tables as this might be your best option.
If you need to support older versi, my preferred method is to have a history table with a new PK column, change flag (I,U,D), a date modified, user that made the change, and all of the columns from the primary table. I then index the column related to the PK of the non-history table. Triggers don't impact performance too much if you don't put logic in them. Example (pseudocode):
Table: Car Column: CarID IT IDETITY(1,1) Primary Key Column: ame varchar Table: Car_hist Column: Car_histID IT IDETITY(1,1) Primary Key Column: Change char(1) Column: DateOfChange DateTime2 Column: ChangedByUser (varchar or int) Column: CarID <-add a unique non-clustered index Column: ame varcharYou can write a generator in SQL that generates the script to create the history table, indexes, etc. It helps if you have a cistent table design practice.
ow the reason: I rarely have to query history tables, but when I do, it is almost always for a single record to see what happened and who changed it. This method allows you to select from the history on the parent table's PK value quickly and read it as a historical change log easily (who changed what and when). I don't see how you can do that in your design. If you are really slick, you can find or write a grid that diffs rows for you and you can quickly see what changed.
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
推荐阅读
留言与评论(共有 19 条评论) |
本站网友 竹山路二手房 | 15分钟前 发表 |
U | |
本站网友 怀孕初期症状 | 27分钟前 发表 |
如果不在其中放置逻辑 | |
本站网友 拆借公司 | 26分钟前 发表 |
ame varchar Table | |
本站网友 三轨推拉门 | 2分钟前 发表 |
etc.) which also need to be tracked. /* Product categories | |
本站网友 北京烤肉自助餐 | 11分钟前 发表 |
it is almost always for a single record to see what happened and who changed it. This method allows you to select from the history on the parent table's PK value quickly and read it as a historical change log easily (who changed what and when). I don't see how you can do that in your design. If you are really slick | |
本站网友 hah | 4分钟前 发表 |
Change char(1) Column | |
本站网友 asp教程 | 20分钟前 发表 |
每个表都需要进行全面审核 | |
本站网友 番茄的作用 | 17分钟前 发表 |
每个表都需要进行全面审核 | |
本站网友 泪囊炎 | 15分钟前 发表 |
Change char(1) Column | |
本站网友 辽宁省分析科学研究院 | 4分钟前 发表 |
ChangedByUser (varchar or int) Column | |
本站网友 宝利沥青 | 24分钟前 发表 |
这将为我提供记录所需的历史跟踪 | |
本站网友 zhonguancun | 13分钟前 发表 |
indexes | |
本站网友 雷军老婆 | 29分钟前 发表 |
它会有所帮助 | |
本站网友 曹子里 | 17分钟前 发表 |
此表指向还需要跟踪的许多其他(即具有日期范围 | |
本站网友 测算预产期 | 9分钟前 发表 |
a product number (int) | |
本站网友 男人整容 | 19分钟前 发表 |
产品号(int)和版本(int)的产品表 每当有人仅对产品记录进行更改时 | |
本站网友 男友忘不了前女友 | 25分钟前 发表 |
此问题也随之而来 - 如果删除某个类别但产品记录没有变化会怎样? 我想看看谁删除了这个类别 | |
本站网友 香港东亚银行 | 20分钟前 发表 |
产品类别等的产品定价) |