Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL duplicate entry error even though there is no duplicate entry

I am using MySQL 5.1.56, MyISAM. My table looks like this:

CREATE TABLE IF NOT EXISTS `my_table` (   `number` int(11) NOT NULL,   `name` varchar(50) NOT NULL,   `money` int(11) NOT NULL,   PRIMARY KEY (`number`,`name`) ) ENGINE=MyISAM; 

It contains these two rows:

INSERT INTO `my_table` (`number`, `name`, `money`) VALUES (1, 'S. Name', 150), (2, 'Another Name', 284); 

Now I am trying to insert another row:

INSERT INTO `my_table` (`number`, `name`, `money`) VALUES (2, 'S. Name', 240); 

And MySQL just won't insert it while telling me this:

#1062 - Duplicate entry '2-S. Name' for key 'PRIMARY' 

I really don't understand it. The primary key is on the first two columns (both of them), so the row I am trying to insert HAS a unique primary key, doesn't it?

I tried to repair the table, I tried to optimize the table, all to no avail. Also please note that I cannot change from MyISAM to InnoDB.

Am I missing something or is this a bug of MySQL or MyISAM? Thanks.

To summarize and point out where I think is the problem (even though there shouldn't be): Table has primary key on two columns. I am trying to insert a row with a new combination of values in these two columns, but value in column one is already in some row and value in column two is already in another row. But they are not anywhere combined, so I believe this is supposed to work and I am very confused to see that it doesn't.

like image 976
user1763581 Avatar asked Oct 30 '12 04:10

user1763581


People also ask

How do I fix a duplicate entry in MySQL replication?

Try to locate duplicate entry and delete that entry from slave DB. Once you have deleted the old entry then execute stop slave and then start slave on slave DB. Most probably replication will start again and come back to normal. If it gets stuck again for same error for some other record then repeat same steps.

How do I find non duplicate records in MySQL?

Preventing Duplicates from Occurring in a Table. You can use a PRIMARY KEY or a UNIQUE Index on a table with the appropriate fields to stop duplicate records. Let us take an example – The following table contains no such index or primary key, so it would allow duplicate records for first_name and last_name.

How do I fix error 1062 in MySQL?

1062 - Duplicate Entry To solve this, Set the primary key column as AUTO_INCREMENT . And when you are trying to insert a new row, ignore the primary key column or insert NULL value to primary key.

What does duplicate entry mean in MySQL?

When creating a primary key or unique constraint after loading the data, you can get a “Duplicate entry for key 'PRIMARY'” error. If the data in the source database is valid and there are no any duplicates you should check which collation is used in your MySQL database.


2 Answers

Your code and schema are OK. You probably trying on previous version of table.

http://sqlfiddle.com/#!2/9dc64/1/0

Your table even has no UNIQUE, so that error is impossible on that table.

Backup data from that table, drop it and re-create.

Maybe you tried to run that CREATE TABLE IF NOT EXIST. It was not created, you have old version, but there was no error because of IF NOT EXIST.

You may run SQL like this to see current table structure:

DESCRIBE my_table; 

Edit - added later:

Try to run this:

DROP TABLE `my_table`; --make backup - it deletes table  CREATE TABLE `my_table` (   `number` int(11) NOT NULL,   `name` varchar(50) NOT NULL,   `money` int(11) NOT NULL,   PRIMARY KEY (`number`,`name`),   UNIQUE (`number`, `name`) --added unique on 2 rows ) ENGINE=MyISAM; 
like image 193
Kamil Avatar answered Sep 25 '22 08:09

Kamil


I know this wasn't the problem in this case, but I had a similar issue of "Duplicate Entry" when creating a composite primary key:

ALTER TABLE table ADD PRIMARY KEY(fieldA,fieldB);  

The error was something like:

#1062 Duplicate entry 'valueA-valueB' for key 'PRIMARY' 

So I searched:

select * from table where fieldA='valueA' and fieldB='valueB' 

And the output showed just 1 row, no duplicate!

After some time I found out that if you have NULL values in these field you receive these errors. In the end the error message was kind of misleading me.

like image 24
carla Avatar answered Sep 25 '22 08:09

carla