我必须创建一个数据库,并确保我们可以加载特定日期的数据,所以我决定使用 元组版本控制。
假设我们有以下两个表:
CREATE TABLE Author
(
Id UNIQUEIDENTIFIER NOT NULL,
Firstname VARCHAR(100) NOT NULL,
Surname VARCHAR(200) NOT NULL,
ValidFrom DATETIME NOT NULL,
ValidUntil DATETIME NULL,
PRIMARY KEY (ID, ValidFrom)
)
CREATE TABLE Book
(
Id UNIQUEIDENTIFIER NOT NULL,
Title VARCHAR(100) NOT NULL,
ISBN VARCHAR(100) NOT NULL,
AuthorId UNIQUEIDENTIFIER NOT NULL,
ValidFrom DATETIME NOT NULL,
ValidUntil DATETIME NULL,
PRIMARY KEY (Id, ValidFrom)
)
当我第一次输入新作者时,我将生成一个新的 GUID。我也在图书表中使用这个 GUID 来作为作者的参考。
如果作者有更新,我将创建具有相同 GUID 的新记录,但将当前日期定义为“ValidFrom”,并将原始记录中的“ValidUntil”设置为当前日期。
我不必更改图书表,因为 Author.Id 没有更改。
我现在面临的问题是我想在 Book.AuthorId = Author.Id 上添加外键约束,
不幸的是这不起作用,因为我使用复合主键。我不想将 Author.ValidFrom 添加到我的 Book 表中,因为我只想引用最新的版本而不是特定版本。
我知道如何解决这个问题吗?我想我可以添加一个触发器,确保如果已经记录了一本书,则无法删除作者,但我没有允许级联删除的解决方案。
我很感激每一个提示或建议。
I have to create a database and have to make sure that we can load the data as it was at a specific date, so I decided to use tuple versioning.
Let's say we have the following two tables:
CREATE TABLE Author
(
Id UNIQUEIDENTIFIER NOT NULL,
Firstname VARCHAR(100) NOT NULL,
Surname VARCHAR(200) NOT NULL,
ValidFrom DATETIME NOT NULL,
ValidUntil DATETIME NULL,
PRIMARY KEY (ID, ValidFrom)
)
CREATE TABLE Book
(
Id UNIQUEIDENTIFIER NOT NULL,
Title VARCHAR(100) NOT NULL,
ISBN VARCHAR(100) NOT NULL,
AuthorId UNIQUEIDENTIFIER NOT NULL,
ValidFrom DATETIME NOT NULL,
ValidUntil DATETIME NULL,
PRIMARY KEY (Id, ValidFrom)
)
The first time when I enter a new author I will generate a new GUID. I use this GUID in the book table as well to make a reference to the author.
If there is an update on the author, I create a new record with the same GUID, but define the current date as "ValidFrom" and also set the "ValidUntil" from the original record to the current date.
I don't have to change the book table because Author.Id did not change.
The problem I'm facing now is that I would like to add a foreign key constraint on Book.AuthorId = Author.Id
Unfortunately this does not work because I use a composite primary key. I do not want to add the Author.ValidFrom to my Book table because I just want to reference the most recent one and not a specific version.
Any idea on how I can solve this? I think I could add a trigger that makes sure that you can't delete an author if there is already a book recorded, but I have no solution to allow cascade delete.
I'm grateful for every hint or advise.
发布评论
评论(1)
这适用于 2008 年(依赖于使用 MERGE 语句以原子方式更改 Book 引用的行)。它确实引入了新列,您可能希望将它们隐藏在视图后面:
对于 2008 年之前的解决方案,我认为您无法比触发器做得更好。您可以引入第二个 Author 表,该表只有 Id 列(唯一),您可以从 Book 中 FK 列,然后从该表到 Book 进行级联删除。然后,您只需要在作者上有一个删除触发器,这样,如果您要从作者中删除特定作者 ID 的最后一行,则可以从这个新表中删除该行
This works on 2008 (relies on using a MERGE statement to change which row is being referenced by Book atomically). It does introduce new columns, you might want to hide them behind a view:
For a pre-2008 solution, I don't think you can do better than triggers. You can introduce a second Author table that does just have the Id column (uniquely), which you can FK against from Book, and cascade delete from that table to Book. Then you just need a delete trigger on Author, such that if you're removing the final row from Author for a particular Author Id, you delete the row from this new table