Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

MySQL Table

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

like image 919
Sugumar Venkatesan Avatar asked Jun 07 '26 20:06

Sugumar Venkatesan


1 Answers

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
like image 53
A Monad is a Monoid Avatar answered Jun 10 '26 09:06

A Monad is a Monoid