SQL Server 位列约束,1 行 = 1,所有其他 0 [英] SQL Server bit column constraint, 1 row = 1, all others 0
本文介绍了SQL Server 位列约束,1 行 = 1,所有其他 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 bit IsDefault
列.表中只有一行数据可以将此位列设置为1
,所有其他数据必须为0
.
I have a bit IsDefault
column. Only one row of data within the table may have this bit column set to 1
, all the others must be 0
.
我如何强制执行此操作?
How can I enforce this?
推荐答案
所有版本:
- 触发
- 索引视图
- 存储过程(例如写入测试)
SQL Server 2008:过滤索引
SQL Server 2008: a filtered index
CREATE UNIQUE INDEX IX_foo ON bar (MyBitCol) WHERE MyBitCol = 1
这篇关于SQL Server 位列约束,1 行 = 1,所有其他 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文