#1062 - Duplicate entry for key 'PRIMARY'
Asked Answered
D

10

30

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?

Dettmer answered 24/7, 2012 at 20:5 Comment(5)
what is the primary key on your table? You cannot have duplicate values in a primary key field. A primary key can also consist of multiple fields so what fields are in your key?Faddist
Don't know for sure, but could your database have formed a relationship between shares and id ? take a look in phpMyAdmin and see what you can find....Only
Most likely you primary key is composed by the columns Name and shares. That means that the combination of both columns must be unique (in your example, the combination fb, 4 already exists)Embezzle
well in my phpmyadmin I cannot click on the primary key for both name and shares. Does that mean something?Dettmer
Show your schema please.Ulphiah
M
17

You need to remove shares as your PRIMARY KEY OR UNIQUE_KEY

Metonym answered 24/7, 2012 at 20:12 Comment(1)
What I did was i dropped both shares and name and readded them with index of index.Dettmer
W
9

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

Warmup answered 24/7, 2012 at 20:10 Comment(2)
I got this: CREATE TABLE stocks ( name varchar(10) NOT N...Dettmer
This should show you what the primary key and what the unique key is.Zygophyte
A
9
  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.
Arv answered 4/2, 2016 at 2:34 Comment(0)
S
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;
Sternum answered 9/12, 2015 at 2:19 Comment(0)
L
2

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".

Leisaleiser answered 24/7, 2012 at 20:11 Comment(0)
P
2

Probably this is not the best of solution but doing the following will solve the problem

Step 1: Take a database dump using the following command

mysqldump -u root -p databaseName > databaseName.db

find the line

ENGINE=InnoDB AUTO_INCREMENT="*****" DEFAULT CHARSET=utf8;

Step 2: Change ******* to max id of your mysql table id. Save this value.

Step 3: again use

mysql -u root -p databaseName < databaseName.db

In my case i got this error when i added a manual entry to use to enter data into some other table. some how we have to set the value AUTO_INCREMENT to max id using mysql command

Portfolio answered 15/4, 2013 at 10:15 Comment(1)
I got the error Unrecognized statement type. (near "ENGINE" at position 0)Ketcham
M
0

Repair the database by your domain provider cpanel.

Or see if you didnt merged something in the phpMyAdmin

Mercurialize answered 14/4, 2017 at 20:3 Comment(0)
F
0

The DB I was importing had a conflict during the import due to the presence of a column both autoincrement and primary key.

The problem was that in the .sql file the table was chopped into multiple "INSERT INTO" and during the import these queries were executed all together.

MY SOLUTION was to deselect the "Run multiple queries in each execution" on Navicat and it worked perfectly

Freeloader answered 30/10, 2020 at 9:1 Comment(0)
H
0

Do not mix SQLAlchemy commands with Python's Try-Except. SQL will keep trying regardless, and the error will only show later in your program flow.

Heinrich answered 2/3, 2023 at 18:26 Comment(0)
A
-2

I had this error from mySQL using DataNucleus and a database created with UTF8 - the error was being caused by this annotation for a primary key:

@PrimaryKey
@Unique
@Persistent(valueStrategy = IdGeneratorStrategy.UUIDSTRING)
protected String id;

dropping the table and regenerating it with this fixed it.

@PrimaryKey
@Unique
@Persistent(valueStrategy = IdGeneratorStrategy.UUIDHEX)
protected String id;
Attest answered 3/6, 2017 at 19:50 Comment(2)
Where did you read that he's using Java and Spring?Amagasaki
@Amagasaki - The OP, asked over a decade ago, had the exact same error message that I had. So when I googled it, 5 years ago, I found this SO question and shared my fix should any one else encounter the exact same error. Thanks for the memory!Attest

© 2022 - 2024 — McMap. All rights reserved.