MySQL:ALTER IGNORE TABLE给出了“违反完整性约束" [英] MySQL: ALTER IGNORE TABLE gives "Integrity constraint violation"

查看:113
本文介绍了MySQL:ALTER IGNORE TABLE给出了“违反完整性约束"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ALTER IGNORE TABLE + UNIQUE KEY从MySQL表中删除重复项. MySQL文档说:

I'm trying to remove duplicates from a MySQL table using ALTER IGNORE TABLE + an UNIQUE KEY. The MySQL documentation says:

IGNORE是对标准SQL的MySQL扩展.如果新表中的唯一键上有重复项,或者在启用严格模式时出现警告,则它控制ALTER TABLE的工作方式.如果未指定IGNORE,则在发生重复键错误的情况下,副本将中止并回滚.如果指定了IGNORE,则只有第一行用于唯一键重复的行.其他冲突的行将被删除.不正确的值将被截断为最接近的匹配可接受值.

IGNORE is a MySQL extension to standard SQL. It controls how ALTER TABLE works if there are duplicates on unique keys in the new table or if warnings occur when strict mode is enabled. If IGNORE is not specified, the copy is aborted and rolled back if duplicate-key errors occur. If IGNORE is specified, only the first row is used of rows with duplicates on a unique key. The other conflicting rows are deleted. Incorrect values are truncated to the closest matching acceptable value.

当我运行查询时...

When I run the query ...

ALTER IGNORE TABLE table ADD UNIQUE INDEX dupidx (field)

...我仍然收到错误#1062-键"dupidx"的条目"blabla"重复.

... I still get the error #1062 - Duplicate entry 'blabla' for key 'dupidx'.

推荐答案

MySQL的IGNORE关键字扩展似乎具有

The IGNORE keyword extension to MySQL seems to have a bug in the InnoDB version on some version of MySQL.

您总是可以将其转换为MyISAM,对索引进行IGNORE-ADD,然后再转换回InnoDB

You could always, convert to MyISAM, IGNORE-ADD the index and then convert back to InnoDB

ALTER TABLE table ENGINE MyISAM;
ALTER IGNORE TABLE table ADD UNIQUE INDEX dupidx (field);
ALTER TABLE table ENGINE InnoDB;

请注意,如果您有外键约束,这将无法正常工作,则必须先删除这些约束,然后再将其重新添加.

Note, if you have Foreign Key constraints this will not work, you will have to remove those first, and add them back later.

这篇关于MySQL:ALTER IGNORE TABLE给出了“违反完整性约束"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆