mysql insert error 1062

戏子无情 提交于 2019-11-26 18:40:03

问题


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


回答1:


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.



回答2:


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.




回答3:


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




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/5775400/mysql-insert-error-1062

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!