我遇到一种情况,我需要对一组列强制执行唯一约束,但仅对列的一个值执行。假设有一个名为Person的表,且列“ Name”和“ isActive” 以下应该是结果
INSERT INTO Person (name,isActive) VALUES ('testUser1',0); --Success
INSERT INTO Person (name,isActive) VALUES ('testUser1',1); --Success
INSERT INTO Person (name,isActive) VALUES ('testUser1',0); --Success
INSERT INTO Person (name,isActive) VALUES ('testUser1',1); --Failure
INSERT INTO Person (name,isActive) VALUES ('testUser1',0); --Success
INSERT INTO Person (name,isActive) VALUES ('testUser2',0); --Success
INSERT INTO Person (name,isActive) VALUES ('testUser2',1); --Success
INSERT INTO Person (name,isActive) VALUES ('testUser2',0); --Success
INSERT INTO Person (name,isActive) VALUES ('testUser2',1); --Failure
INSERT INTO Person (name,isActive) VALUES ('testUser2',0); --Success
基本上我需要的是相同的名称和active = 1行,不应重复。
答案 0 :(得分:3)
create unique index index_name on Person (name) where isActive = 1