简体   繁体   English

如何向已包含数据的MariaDB表添加密钥?

[英]How can I add a key to MariaDB table that already contains data?

I'm trying to alter my table with MySQL but it will show an error on ALTER: 我正在尝试用MySQL改变我的表,但它会在ALTER上显示错误:

#1062 - Duplicate entry '5009-daring-1' for key 'doctermitem' #1062 - 关键'doctermitem'重复输入'5009-daring-1'

SQL query: SQL查询:

ALTER TABLE `wpi4_asp_index`
  ADD UNIQUE KEY `doctermitem` (`doc`,`term`,`blogid`),
  ADD KEY `term_ptype_bid_lang` (`term`(20),`post_type`(20),`blogid`,`lang`(10)),
  ADD KEY `rterm_ptype_bid_lang` (`term_reverse`(20),`post_type`(20),`blogid`,`lang`(10))

How can I solve this error? 我该如何解决这个错误?

You are trying create doctermitem as UNIQUE KEY , but already there are duplicate entry exist in that combination (as you mentioned '5009-daring-1'), so it is not allowing to add the doctermitem as UNIQUE KEY . 您正在尝试将doctermitem创建为UNIQUE KEY ,但已经存在该组合中存在重复条目(正如您提到的'5009-daring-1'),因此它不允许将doctermitem添加为UNIQUE KEY

You need to manually remove those duplicate combination values, then it will allow to create the UNIQUE KEY 您需要手动删除那些重复的组合值,然后它将允许创建UNIQUE KEY

The columns you're targeting for a unique key are not unique - there is at least one row which is duplicated, there may be more. 您为唯一键定位的列不是唯一的 - 至少有一行是重复的,可能还有更多。

Find them through a SQL statement: 通过SQL语句找到它们:

select doc,
      term,
      blogid,
      count(*)
from wpi4_asp_index
group by doc,
      term,
      blogid
having count(*) > 1

You will then have to decide what the problem is and how to fix it. 然后,您必须确定问题所在以及如何解决问题。 Broadly speaking, there are two likely causes: 从广义上讲,有两个可能的原因:

  • You have incorrect data. 您的数据不正确。 Fix the data, eg by deleting the duplicates. 修复数据,例如删除重复项。
  • Your assumptions about uniqueness are wrong - you may need to add an additional column to your unique key. 您对唯一性的假设是错误的 - 您可能需要在唯一键中添加其他列。

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

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