gks
gks

Reputation: 65

mysql insert error 1062

SQL query:

INSERT INTO  `website_categorization`.`category_keyword` (
`ID` ,
`ID_Category` ,
`Keyword` ,
`Score`)
VALUES (
NULL ,  '18',  'free mail',  ''
), (
NULL ,  '18',  'web email free',  ''
)  

MySQL said:

#1062 - Duplicate entry '18-free mail' for key 'ID_Category'

It shows this duplicate entry error even though there is no entry at row no 1062. ( ID is primary key, and unique(ID_Category,Keyword) ). Can u help me in this?...

Upvotes: 0

Views: 1256

Answers (5)

Mark Byers
Mark Byers

Reputation: 838216

You already have a row in your database with the values '18' and 'free mail'. You can't have two such rows because of the unique constraint. You have some choices:

  • Remove the original row and try your insert again: DELETE FROM yourtable WHERE ID_Category = '18' AND Keyword = 'free mail'.
  • Remove the unique constraint to allow both rows to exist.
  • Use INSERT IGNORE to ignore the error.
  • Use REPLACE instead of INSERT to replace the old row with the new row.
  • Attempt the INSERT knowing that the client-side will be alerted of the error.

Upvotes: 4

boisvert
boisvert

Reputation: 3729

Well, it means that the data you are inserting breaks the unique constraints. From the error messasge I'd say some data already exists with the pair (18, 'free mail') - you say that is constrained to be unique.

The row number is not an indication, because it doesn't correspond to the key.

Upvotes: 2

lobster1234
lobster1234

Reputation: 7779

That is MySQL Error number 1062, not row number. The error means duplicate entry. You are inserting NULL and '18' twice in ID and ID_Category respectively, so it will throw this error the 2nd time you do it. ID_Category is very likely the name of your index. You can do a

show index from website_categorization.category_keyword

to see the index name.

Upvotes: 1

Tieson T.
Tieson T.

Reputation: 21191

If your ID field is truly a primary key, it is mostly likely (or should be) auto-incremented. So leave that field out of the INSERT query.

Upvotes: 1

Spyros
Spyros

Reputation: 48626

Your ID_category key is declared as unique and thus you cannot have two entries with the same value.

Upvotes: 1

Related Questions