SQL Server bit column constraint, 1 row = 1, all others 0

前端 未结 6 1904
北海茫月
北海茫月 2020-12-06 00:09

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.

6条回答
  •  佛祖请我去吃肉
    2020-12-06 00:24

    All versions:

    • Trigger
    • Indexed view
    • Stored proc (eg test on write)

    SQL Server 2008: a filtered index

    CREATE UNIQUE INDEX IX_foo ON bar (MyBitCol) WHERE MyBitCol = 1
    

提交回复
热议问题