SQL Server:检查布尔值只能出现一次为真 [英] SQL Server : check boolean can only occur once as true
本文介绍了SQL Server:检查布尔值只能出现一次为真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 SQL Server 2008 R2 中,我有一个包含 [Id]
、[Name]
和 [bDefault]
的表.所有列不可为空.
In SQL Server 2008 R2 I have a table with [Id]
, [Name]
and [bDefault]
. All the columns are not nullable.
是否有可能检查 bDefault
只能对 一个 记录为真?将其设为唯一索引是行不通的,因为只有最多 1 条记录为真,其余均为假.
Is it possible with a check that bDefault
can only be true for one record? Making it a unique index won't work, since only max 1 record will be true, all the rest will be false.
推荐答案
使用过滤的唯一索引:
CREATE UNIQUE INDEX IX_... ON [Table](bDefault) WHERE bDefault = 1
这篇关于SQL Server:检查布尔值只能出现一次为真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文