位列的唯一约束仅允许1个True(1)值 [英] Unique Constraint for Bit Column Allowing Only 1 True (1) Value

查看:77
本文介绍了位列的唯一约束仅允许1个True(1)值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此表:

CREATE TABLE [tblExample](
    [ExampleID] [int] IDENTITY(1,1) NOT NULL,
    [WordsAndStuff] [nvarchar](max) NOT NULL,
    [Active] [bit] NOT NULL

我希望Active列具有唯一的约束,该约束仅允许一条记录为真(1)。这时,我不需要成为一直都是真实记录,最多只能有一个。

I want the Active column to have a unique constraint that will only allow one record to be true (1). At this point, I don't need there to BE a true record all the time, there just cannot be more than one of them.

我该如何写约束?

推荐答案

表中一次只有一个活动记录吗?您可以将唯一索引与过滤器一起使用:

Just one active record at a time in the table? You can use a unique index with a filter:

create unique nonclustered index uixf_tblExample_Active_filtered
  on tblExample (Active)
    include (ExampleId, WordsAndStuff) -- optional included columns
  where Active=1

这篇关于位列的唯一约束仅允许1个True(1)值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆