【发布时间】:2020-06-04 02:04:01
【问题描述】:
我有一个表,它有一个名为“Flag”的位列,它不允许 NULL。这里的要求是始终为下表中的每个 Id 设置Flag=1,但每个唯一的 Id 和 Flag 列组合只有 1 次。同时,如果有超过 1 个条目,则所有其他行可以多次设置 Flag=0。
基本上按 ID 分组的 Flag 的总和应始终为 1。
我虽然对 Id 和 Flag 字段有唯一约束,但由于 Flag=0 可以为同一组合设置多次,因此不能使用。
有什么建议吗?
-- 当前数据集
drop table if exists #test;
go
create table #Test (Id_pk int identity(1,1) not null, id int not null, Flag bit not null)
Insert into #Test (id, Flag)
values (12, 0), (12,0), (12, 0), (12,0), (12, 1), (12,1), (13,0), (13, 0), (14,1), (14,1), (20,1), (20,0), (30,1), (40,0)
select * from #Test
-- 期望的结果
drop table if exists #test;
go
create table #Test (Id_pk int identity(1,1) not null, id int not null, Flag bit not null)
Insert into #Test (id, Flag)
values (12, 0), (12,0), (12, 0), (12,0), (12, 0), (12,1), (13,0), (13, 1), (14,0), (14,1), (20,1), (20,0), (30,1), (40,1)
select * from #Test
【问题讨论】:
标签: sql sql-server unique-constraint check-constraints