#1062 - Duplicate entry for key 'PRIMARY'

275,667

Solution 1

You need to remove shares as your PRIMARY KEY OR UNIQUE_KEY

Solution 2

Use SHOW CREATE TABLE your-table-name to see what column is your primary key.

Solution 3

  1. Make sure PRIMARY KEY was selected AUTO_INCREMENT.
  2. Just enable Auto increment by :
    ALTER TABLE [table name] AUTO_INCREMENT = 1
  3. When you execute the insert command you have to skip this key.

Solution 4

I solved it by changing the "lock" property from "shared" to "exclusive":

ALTER TABLE `table` 
CHANGE COLUMN `ID` `ID` INT(11) NOT NULL AUTO_INCREMENT COMMENT '' , LOCK = EXCLUSIVE;

Solution 5

What is the exact error message? #1062 means duplicate entry violating a primary key constraint for a column -- which boils down to the point that you cannot have two of the same values in the column. The error message should tell you which of your columns is constrained, I'm guessing "shares".

Share:
275,667
irosenb
Author by

irosenb

I am 15 years old, and i love to program. However, I only know Scratch right now, but hopefully, after going to a program this summer (at brown), I will learn how to program. I am interested in Creative Commons, play drums, read the newspaper, and a ton more. I also participate in Kids Walk for Kids with Cancer, a walk in New York and New Jersey. You can check it out here: http://kidswalkforkidswithcancer.org/ I also have a youtube channel, http://www.youtube.com/user/undacovabroda45. For now, I am just uploading cc-licensed short videos. Just give me credit if u use them.

Updated on August 05, 2022

Comments

  • irosenb
    irosenb almost 2 years

    So my MySQL database is behaving a little bit wierd. This is my table:

    Name shares id  price   indvprc
    cat   2     4   81      0
    goog  4     4   20      20
    fb    4     9   20      20
    

    I'm getting this #1062 error when I try to insert into the table. So I looked into it further and realized that when I try to insert values into the table, in which the name and shares values are the same, it will return the #1062 error. For example, If i inserted:

    fb    4      6     20   20 
    

    It would return an error. But if i changed the shares number to 6, it would run fine. Is it because of one of my columns that could be unique, or is it just something with mysql?