MySQL Error: 1062 Duplicate entry ‘0′ for key 1
-
Here’s a problem we had come across today. Whenever we tried inserting data into a certain table, MySQL kept throwing up the following error:mySQL error: 1062
Duplicate entry '0' for key 1After scratching our heads over this one, we tried to alter the table to set the autoincrement field to the next number and even that failed to fix the problem. Then hunting around the web, we found the cause of this problem. It seems that Mysql throws this error because the field type of the auto increment field is not large enough to hold the next value, so it tries to wraps the count back to ‘0′ where the is already a record with that value.
We changed the field from int to unsigned bigint which fixed the problem for us. So if you face the same problem, check if your autoincrement field has maxed the datatype for that field.

























July 11th, 2008 at 12:12 am
Above normally means that there already exists an entry in the database that already has the value which autoincrement is trying to initialise with. So if duplicate entry ‘0′ is there then probably auto_increment value is trying to initialise with 0. I received the error duplicate entry ‘1′ for key 1, which strangely meant that the column already have a value 0 which the auto_increment index was trying to initialise. I had to change this value to the last value then changed the column to cater for auto_increment then re-initalise the counter to the last value and then revert the last value of my column back to it’s beginning value i.e. 0. (am i confusing you?
July 12th, 2008 at 12:03 am
Thanks for pointing that out Afrasyab. So this situation could happen when the auto-increment count goes wrong for some reason.
August 7th, 2008 at 8:45 pm
I have the same problem, out of the blue got this error”
:Duplicate entry ‘4853′ for key 1
and I am using “int” as the data type. changing it to bigint may resolve things by resetting the autoincrement for the time being, but I expect it will surface again sometime. There is some other cause for this
Also, it seems not a good idea to use bigint because extra disk space use ref: http://ronaldbradford.com/blog/bigint-v-int-is-there-a-big-deal-2008-07-18/