将表字段更改为 UNIQUE
我想运行以下sql命令:
ALTER TABLE `my_table` ADD UNIQUE ( `ref_id` , `type` );
问题是表中的某些数据会使此命令无效,因此更改表会失败。
MySQL 有没有一种巧妙的方法来删除重复的行?
I want to run the following sql command:
ALTER TABLE `my_table` ADD UNIQUE ( `ref_id` , `type` );
The problem is that some of the data in the table would make this invalid, therefore altering the table fails.
Is there a clever way in MySQL to delete the duplicate rows?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
SQL 充其量只能任意处理这个问题。 换句话说:这是你的问题。
您的数据目前并不唯一。 你想让它独一无二。 您需要决定如何处理重复项。
有多种方法可以处理此问题:
注意:这些都需要用户干预。
您当然可以将表复制到临时表,清空原始表并复制行,忽略那些失败的行,但我希望这不会给您真正想要的结果。
SQL can, at best, handle this arbitrarily. To put it another way: this is your problem.
You have data that currently isn't unique. You want to make it unique. You need to decide how to handle the duplicates.
There are a variety of ways of handling this:
Note: these all require user intervention.
You could of course just copy the table to a temporary table, empty the original and copy in the rows just ignoring those that fail but I expect that won't give you the results that you really want.
如果您不关心哪一行被删除,请使用 IGNORE:
if you don't care which row gets deleted, use IGNORE:
您可以做的就是向表中添加一个临时标识列。 这样,您可以编写查询来识别和删除重复项(您可以稍微修改查询以确保仅保留一组重复行中的一个副本)。
完成此操作后,删除临时列并向原始列添加唯一约束。
希望这可以帮助。
What you can do is add a temporary identity column to your table. With that you can write query to identify and delete the duplicates (you can modify the query little bit to make sure only one copy from the set of duplicate rows are retained).
Once this is done, drop the temporary column and add unique constraint to your original column.
Hope this helps.
我过去所做的是导出唯一的数据集,删除表,使用唯一的列重新创建它并导入数据。
它通常比尝试找出如何删除重复数据要快。
What I've done in the past is export the unique set of data, drop the table, recreate it with the unique columns and import the data.
It is often faster than trying to figure out how to delete the duplicate data.
有一篇很好的知识库文章提供了查找和删除具有重复值的行的分步方法。 它提供了两种方法 - 一种用于查找和删除单行的一次性方法,以及一种在涉及许多行时解决此问题的更广泛的解决方案。
http://support.microsoft.com/kb/139444
There is a good KB article that provides a step-by-step approach to finding and removing rows that have duplicate values. It provides two approaches - a one-off approach for finding and removing a single row and a broader solution to solving this when many rows are involved.
http://support.microsoft.com/kb/139444
这是我用来删除其中一个表中的重复行的片段
Here is a snippet I used to delete duplicate rows in one of the tables
这对我有用:
This worked for me:
您将必须找到其他一些唯一的字段,因为仅删除 ref_id 和 type 就会将它们全部删除。
要获取重复项:
Xarpb 有一些聪明的技巧(也许太聪明了): http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2 /
You will have to find some other field that is unique because deleting on ref_id and type alone will delete them all.
To get the duplicates:
Xarpb has some clever tricks (maybe too clever): http://www.xaprb.com/blog/2007/02/06/how-to-delete-duplicate-rows-with-sql-part-2/