MySQL PHPMyAdmin Error #1062 - Duplicate entry '0' for key 'PRIMARY'

本秂侑毒 提交于 2020-01-15 05:17:07

问题


Now I want to add a primary key id column but it throws the error:

#1062 - Duplicate entry '0' for key 'PRIMARY'

I already tried this: Add primary key to existing table


回答1:


When you creates a new column, a default value is asigned (in your case will be 0), so you need to specify wich values will it have (besides you can tell it to the column to be autoincremental, and it will do the work for you for the new entries of rows). You have to change all the values to be differents between them, the id key MUST be unique

To change all your ids, in mysql you can do:

SET @new_id=0;
UPDATE your_table
SET id = @new_id := @new_id + 1
where id = 0



回答2:


First please change ids of the table with below query

SET @counter = 1
UPDATE #tablename
SET @counter = id = @counter + 1

And then Apply primary key.



来源:https://stackoverflow.com/questions/43892841/mysql-phpmyadmin-error-1062-duplicate-entry-0-for-key-primary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!