When I’m trying to enable auto_increment on a column in phpmyadmin, but I always get this error:
#1062 - Duplicate entry '1' for key 1
The column in question is the PK and an INT. It already has several rows in it. Because of this, I tried the following:
ALTER TABLE Persons AUTO_INCREMENT=7
This should have set the auto incrementing to start at 7, and so skipping the 0 to 6 that already exist in that row… but it didn’t work. Or that was the theory.
What am I doing wrong?
Update:
I have tried removing the PK index on the column and running a query setting the column to NULL. Everything was set to NULL. I tried to then add auto increment and I got the following error: #1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key. I try to add a PK index again and get the following #1062 - Duplicate entry '0' for key 1. Argh.
When trying to add an
auto_incrementto an existing table, you need to make sure all of the key values already in the table are all unique.Setting the column value to
NULLshould work. mySQL will then auto-fill the values with auto increment ones.