Mysql unique constraint allowing single row for a combination

前端 未结 5 1235
囚心锁ツ
囚心锁ツ 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:03

    You could make a trigger that checks if there allready is a field with the 'TRUE' value, and if so take action.

    Note that you cannot easily "reject" the update. (see e.g. : How to abort INSERT operation in MySql trigger? ).

    You could for instance just insert it with false, and save your error somehow, by setting a flag.

提交回复
热议问题