Inserting new record failed with duplicate key error [duplicate]

15 hours ago 1
ARTICLE AD BOX

You need to remove shares as your PRIMARY KEY OR UNIQUE_KEY

answered Jul 24, 2012 at 20:12

mlishn's user avatar

1 Comment

What I did was i dropped both shares and name and readded them with index of index.

2012-07-24T21:05:30.13Z+00:00

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

chw21's user avatar

chw21

8,1601 gold badge22 silver badges38 bronze badges

answered Feb 4, 2016 at 2:34

Đoàn Nhật Duẩn's user avatar

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

answered Jul 24, 2012 at 20:10

Majid Fouladpour's user avatar

2 Comments

I got this: CREATE TABLE stocks ( name varchar(10) NOT N...

2012-07-24T20:17:43.55Z+00:00

This should show you what the primary key and what the unique key is.

2014-05-27T20:58:20.38Z+00:00

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;

davejal's user avatar

davejal

6,18110 gold badges45 silver badges85 bronze badges

answered Dec 9, 2015 at 2:19

Hegle Leonardo Melgar's user avatar

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

answered Jul 24, 2012 at 20:11

Chris's user avatar

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

FastFarm's user avatar

FastFarm

4291 gold badge6 silver badges21 bronze badges

answered Apr 15, 2013 at 10:15

Kshitiz's user avatar

1 Comment

I got the error Unrecognized statement type. (near "ENGINE" at position 0)

2018-04-10T05:05:57.17Z+00:00

Repair the database by your domain provider cpanel.

Or see if you didnt merged something in the phpMyAdmin

answered Apr 14, 2017 at 20:03

Guy's user avatar

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

answered Oct 30, 2020 at 9:01

ivoroto's user avatar

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.

answered Mar 2, 2023 at 18:26

Al Martins's user avatar

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;

answered Jun 3, 2017 at 19:50

bsautner's user avatar

2 Comments

Where did you read that he's using Java and Spring?

2023-02-04T00:16:38.25Z+00:00

@horvoje - 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!

2023-02-05T15:00:20.433Z+00:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Read Entire Article