search

Home  >  Q&A  >  body text

mysql UNIQUE的条件

ALTER TABLE  `table` ADD  `unionid` VARCHAR( 255 ) NOT NULL

然后设置唯一索引

ALTER TABLE  `table` ADD UNIQUE (
`unionid`
)

提示

1062 - Duplicate entry '' for key 'unionid'

请问这是为什么呢?

伊谢尔伦伊谢尔伦2770 days ago712

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 15:34:49

    The first SQL sentence: You added a table column to the unionid table, and it is a non-empty string, so the initial value is an empty string '';

    Second SQL sentence: You establish a unique constraint on the table column of the unionid table, but the premise of the unique constraint is that the values ​​of the unionid column are not repeated. However, all values ​​of the unionid column are null characters. string '', so it will report Duplicate entry '' for key 'unionid'.

    Solution: First correct the value of unionid to be non-repeating and then add a unique constraint to the column.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 15:34:49

    Go directly to the database to operate. When you add an index, there will be a command prompt, so you will know what is wrong

    reply
    0
  • Cancelreply