• 18th June 2008 - By Vinu Thomas

    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
  • 17 Comments to “MySQL Error: 1062 Duplicate entry ’0′ for key 1”

    • Afrasyab Bashir on July 11, 2008

      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? ;)

    • vinu on July 12, 2008

      Thanks for pointing that out Afrasyab. So this situation could happen when the auto-increment count goes wrong for some reason.

    • Tony on August 7, 2008

      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/

    • Darren on December 1, 2008

      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

    • Ajay on December 23, 2008

      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.

    • Joe on January 21, 2009

      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

    • Md. Iqbal Hosan on March 10, 2009

      i got the problem. just edit the id field to auto-increment fixed the problem.thnQ 4 the post.

    • dunghoangit on February 4, 2010

      try this

      REPAIR TABLE your_tbl_error QUICK ;

      :)

    • Botnix on April 20, 2010

      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.

    • Fafff on July 23, 2010

      check if your autoincrement field has maxed the datatype for that field.
      (what this mean?i don't understand it)

    • vinuthomas on July 26, 2010

      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

    • Tango on January 28, 2011

      Thanks for that solution.
      It fixed the problem…

    • badr on February 24, 2011

      i got the problem. just edit the id field to auto-increment fixed the problem.thnQ 4 the post.

    • mamta on April 23, 2011

      thanks a lot i ws really fix with this error and i tried the suggestion posted here as set the id for bigint and i got my solution.

    • شات حائل on August 24, 2011

      thanks for sole

    • Mallikarjun Anehosur on September 30, 2011

      Got the solution by going through this blog in an minute …
      error: 1062 was fixed by changing the field value to auto_increment ..could some one please give me this fix details ,why auto_increment …in my case the filed was primary key with int type ,so why the auto_increment.

    • Arpan on February 29, 2012

      This article was useful to me and with help of it I fixed my error.Thanks.

    Leave a Reply