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 1
After 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.




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?
Thanks for pointing that out Afrasyab. So this situation could happen when the auto-increment count goes wrong for some reason.
I have the same problem, out of the blue got this error”
uplicate 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/
I had the same problem when using the INSERT function to enter data into an already populated table, I just had to set the primary key as Id, and set it to auto_increment,
Another reason why this could happen is the data type, check if you’re using TINYINT or INT
Hi,
I found your site very helpful. While inserting data into a table I again and again got this error “Duplicate entry ‘0′ for key 1″ and I was surprised where the error is ? When I searched this problem on google, I found solution on your site and fixed the error within seconds.
Thanks a lot for posting solutions.
Thanks for posting this — I was really puzzled with this error. I changed the data type to BIGINT and reloaded the data and it worked. -Joe
i got the problem. just edit the id field to auto-increment fixed the problem.thnQ 4 the post.
try this
REPAIR TABLE your_tbl_error QUICK ;
I also solved this error by editing the id field to auto increment, in phpmyadmin there is a check box A_I which can be checked to make sure the id increments.
check if your autoincrement field has maxed the datatype for that field.
(what this mean?i don't understand it)
What I meant was, if you're using for example, an INT field, make sure that the maximum value of an int (2147483647) hasn't been reached, or 4294967295 for unsigned INT. If the value in the field has reached the maximum the field can support, it'll reset back to 0