简体   繁体   English

mysql 插入错误 1062

[英]mysql insert error 1062

SQL query: SQL查询:

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

MySQL said: MySQL 说:

#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) ).即使第 1062 行没有条目,它也会显示此重复条目错误。(ID 是主键,并且是唯一的(ID_Category,Keyword))。 Can u help me in this?...你能帮我解决这个问题吗?...

You already have a row in your database with the values '18' and 'free mail'.您的数据库中已经有一行值为“18”和“免费邮件”。 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' .删除原始行并再次尝试插入: 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.使用INSERT IGNORE忽略错误。
  • Use REPLACE instead of INSERT to replace the old row with the new row.使用REPLACE而不是INSERT用新行替换旧行。
  • Attempt the INSERT knowing that the client-side will be alerted of the error.尝试INSERT知道客户端将收到错误警报。

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.从错误消息中,我想说这对数据已经存在(18,'免费邮件') - 你说这被限制为唯一的。

The row number is not an indication, because it doesn't correspond to the key.行号不是指示,因为它不对应于键。

That is MySQL Error number 1062, not row number.那是 MySQL 错误号 1062,而不是行号。 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 和 ID_Category 中插入 NULL 和 '18' 两次,因此第二次执行时会抛出此错误。 ID_Category is very likely the name of your index. ID_Category很可能是您的索引名称。 You can do a你可以做一个

show index from website_categorization.category_keyword

to see the index name.查看索引名称。

您的 ID_category 键被声明为唯一的,因此您不能有两个具有相同值的条目。

If your ID field is truly a primary key, it is mostly likely (or should be) auto-incremented.如果您的ID字段确实是主键,则它很可能(或应该)自动递增。 So leave that field out of the INSERT query.因此,请将该字段从 INSERT 查询中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM