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

前端 未结 6 1915
北海茫月
北海茫月 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:16

    The accepted answer to the below question is both interesting and relevant:

    Constraint for only one record marked as default

    "But the serious relational folks will tell you this information should just be in another table."

    Have a separate 1 row table that tells you which record is 'default'. Anon touched on this in his comment.

    I think this is the best approach - simple, clean & doesn't require a 'clever' esoteric solution prone to errors or later misunderstanding. You can even drop the IsDefualt column.

提交回复
热议问题