Mysql unique constraint allowing single row for a combination

前端 未结 5 1238
囚心锁ツ
囚心锁ツ 2021-01-20 01:25

Is it possible to have a unique constraint such that one particular column has a value only once?

For instance

-----------------------    
name | pri         


        
5条回答
  •  一个人的身影
    2021-01-20 02:01

    I have solved this problem with a strange aproch, using TIMESTAMP as a flag.

    That`s usead as a "deleted" FLAG.

    If IS NULL, then the register it's not deleted - Instead, if is deleted (<> NULL), the unique key will never conflict avoiding duplicate registers for non flaged as deleted registers.

    For example in your case:

    You will never have 2 rows with default: NULL, but you can have N with TIMESTAMP with they exclude time.

提交回复
热议问题