更新时间:2021-12-04 23:12:26
您可以添加计算列,当应该执行FK时,该列将为 0
,否则为 NULL
。然后可以在外键引用中使用它:
You can add a computed column which will be 0
when the FK ought to be enforced and NULL
otherwise. This can then be used in the foreign key reference:
Create Table Account (
AccountId Int,
Uniquifier Int,
NomineeId Int,
NomineeXRef as CASE WHEN Uniqueifier > 0 THEN 0 END persisted
Constraint PK$Account Primary Key (AccountId, Uniquifier),
Constraint FK$SelfReference Foreign Key (NomineeId,NomineeXRef) References Account (AccountId, Uniquifier)
)