Self-referencing a partial key of a table

☆樱花仙子☆ 提交于 2019-12-08 06:44:28

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)     
)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!