Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一张桌子来存储人的电话号码。有一个标志 (IsDefault) 将电话标记为默认电话。每个人应该只有一部手机被标记为默认手机。是否可以创建一个检查约束以仅允许 PersonId 和 IsDefault = True 的一种组合?
使用过滤索引,如下所示:
CREATE TABLE PhoneNumber ( ID int primary key, PhoneNumber nvarchar(20), PersonID int, IsDefault bit ) GO CREATE UNIQUE INDEX UX_Default_PhoneNumber ON PhoneNumber(PersonID) WHERE IsDefault = 1 GO
这仅在 IsDefault 为 true 时强制执行唯一的 PersonID。