MySQL Error: 1062 Duplicate entry ‘0′ for key 1

MySQL LogoHere’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.

Zemanta Pixie
  • i got the problem. just edit the id field to auto-increment fixed the problem.thnQ 4 the post.
  • Joe
    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
  • Ajay
    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.
  • 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
  • Tony
    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-...
blog comments powered by Disqus