将表字段更改为 UNIQUE

发布于 2024-07-24 16:12:29 字数 160 浏览 9 评论 0原文

我想运行以下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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

温柔戏命师 2024-07-31 16:12:29

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:

  • Modifying or deleting duplicate rows by hand if the numbers are sufficiently small;
  • Running statements to update or delete duplicate that meet certain criteria to get to a point where the exceptions can be dealt with on an individual basis;
  • Copying the data to a temporary table, emptying the original and using queries to repopulate the table; and
  • so on.

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.

饮湿 2024-07-31 16:12:29

如果您不关心哪一行被删除,请使用 IGNORE:

ALTER IGNORE TABLE `my_table` ADD UNIQUE (
`ref_id` ,
`type`
);

if you don't care which row gets deleted, use IGNORE:

ALTER IGNORE TABLE `my_table` ADD UNIQUE (
`ref_id` ,
`type`
);
为你拒绝所有暧昧 2024-07-31 16:12:29

您可以做的就是向表中添加一个临时标识列。 这样,您可以编写查询来识别和删除重复项(您可以稍微修改查询以确保仅保留一组重复行中的一个副本)。

完成此操作后,删除临时列并向原始列添加唯一约束。

希望这可以帮助。

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.

夜巴黎 2024-07-31 16:12:29

我过去所做的是导出唯一的数据集,删除表,使用唯一的列重新创建它并导入数据。

它通常比尝试找出如何删除重复数据要快。

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.

山有枢 2024-07-31 16:12:29

有一篇很好的知识库文章提供了查找和删除具有重复值的行的分步方法。 它提供了两种方法 - 一种用于查找和删除单行的一次性方法,以及一种在涉及许多行时解决此问题的更广泛的解决方案。

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

巴黎盛开的樱花 2024-07-31 16:12:29

这是我用来删除其中一个表中的重复行的片段

BEGIN TRANSACTION
Select *,
rank() over (Partition by PolicyId, PlanSeqNum, BaseProductSeqNum, 
        CoInsrTypeCd, SupplierTypeSeqNum
            order by CoInsrAmt desc) as  MyRank
into #tmpTable
from PlanCoInsr 

select distinct PolicyId,PlanSeqNum,BaseProductSeqNum,
        SupplierTypeSeqNum, CoInsrTypeCd, CoInsrAmt 
into #tmpTable2
from #tmpTable where MyRank=1

truncate table PlanCoInsr

insert into PlanCoInsr
    select * from #tmpTable2

drop table #tmpTable
drop table #tmpTable2

COMMIT

Here is a snippet I used to delete duplicate rows in one of the tables

BEGIN TRANSACTION
Select *,
rank() over (Partition by PolicyId, PlanSeqNum, BaseProductSeqNum, 
        CoInsrTypeCd, SupplierTypeSeqNum
            order by CoInsrAmt desc) as  MyRank
into #tmpTable
from PlanCoInsr 

select distinct PolicyId,PlanSeqNum,BaseProductSeqNum,
        SupplierTypeSeqNum, CoInsrTypeCd, CoInsrAmt 
into #tmpTable2
from #tmpTable where MyRank=1

truncate table PlanCoInsr

insert into PlanCoInsr
    select * from #tmpTable2

drop table #tmpTable
drop table #tmpTable2

COMMIT
孤蝉 2024-07-31 16:12:29

这对我有用:

ALTER TABLE table_name ADD UNIQUE KEY field_name (field_name) 
  

This worked for me:

ALTER TABLE table_name ADD UNIQUE KEY field_name (field_name)
夏雨凉 2024-07-31 16:12:29

您将必须找到其他一些唯一的字段,因为仅删除 ref_id 和 type 就会将它们全部删除。

要获取重复项:

select ref_id, type from my_table group by ref_id, type having count(*)>1

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:

select ref_id, type from my_table group by ref_id, type having count(*)>1

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文