gpt4 book ai didi

sql-server - 具有可为空列的复合外键

转载 作者:行者123 更新时间:2023-12-02 11:22:31 26 4
gpt4 key购买 nike

在下表中,是否有办法确保 PreviousID 始终引用具有匹配 ParentID 的行中的 ID,或者,如果 ParentID 为空,则它在引用的行中也为空?

CREATE TABLE MyTable (
ID int not null identity(1,1) primary key,
ParentID int null foreign key references MyTable (ID),
PreviousID int null foreign key reference MyTable (ID),
foreign key (ParentID, PreviousID) references MyTable (ParentID, ID)
)

一个例子:

+-ID-+-ParentID-+-PreviousID-+
| 1 |空 |空 |
| 2 | 1 |空 |
| 3 |空 | 2 | <-- 应该不可能,应该引用 ParentID 为 null 的 ID
+----+------------+------------+

有没有办法强制执行?

更新 :对于那些想知道的人,由于以下原因,复合外键不会强制执行此操作(从 MSDN 复制):

A FOREIGN KEY constraint can contain null values; however, if any column of a composite FOREIGN KEY constraint contains null values, verification of all values that make up the FOREIGN KEY constraint is skipped. To make sure that all values of a composite FOREIGN KEY constraint are verified, specify NOT NULL on all the participating columns.



更新 :如果它有助于可视化所表示的数据结构,它是一个 B 树,其中具有相同父节点的节点组成一个链表。我试图强制链表中的每个“前一个”指针指向另一个具有相同父节点的节点。在父节点为空的情况下,它应该指向另一个父节点也为空的节点。

最佳答案

尝试这个:

CREATE TABLE MyTable ( 
ID int not null identity(1,1) primary key,
ParentID int null foreign key references MyTable (ID),
PreviousID int null foreign key references MyTable (ID),
foreign key (ParentID, PreviousID) references MyTable (ParentID, ID),
unique (ParentID, ID), /* Required for foreign key */
check (PreviousID is null or ParentID is not null) /* enforeces requested constraint */
)

结果:
insert into MyTable (ParentID, PreviousID) values (null, null) /* (1 row(s) affected) */
insert into MyTable (ParentID, PreviousID) values (1, null) /* (1 row(s) affected) */
insert into MyTable (ParentID, PreviousID) values (null, 2) /* The INSERT statement conflicted with the CHECK constraint.
The statement has been terminated. */

关于sql-server - 具有可为空列的复合外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3918897/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com