Enforcing “zero-or-one to one” relationship on SQL database?

后端 未结 4 2064
一整个雨季
一整个雨季 2021-01-06 11:53

I have a Post entity and a FbPost entity.

Post.FbPost is either null or a FbPost, and no two Post en

4条回答
  •  孤街浪徒
    2021-01-06 12:50

    Create a unique filtered index:

    CREATE UNIQUE INDEX Post_Unq_FbPost ON dbo.Post(FbPost) WHERE FbPost IS NOT NULL;
    

    Also create a foreign key, of course.

提交回复
热议问题