|
遇到如题的这么一个场景:需要在MySQL的一张innodb引擎的表(tableA)上添加一个唯一索引(idx_col1_u)。但是表中已经有大量重复数据MySQL 处理重复数据,对于每个key(col1),有的重复2行,有的重复N行。 此时,做数据的手工清理,或者SQL处理无疑是非常耗时的。 1. Alter ignore table come to help 印象中MySQL有一个独有的 alter ignore add unique index的语法。 语法如下: ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name 行为类似于insert ignore,即遇到冲突的unique数据则直接抛弃而不报错。对于加唯一索引的情况来说就是建一张空表,然后加上唯一索引,将老数据用insert ignore语法插入到新表中,遇到冲突则抛弃数据。 IGNOREis a MySQL extension to standard SQL. It controls howALTER TABLEworks if there are duplicates on unique keys in the new table or if warnings occur when strict mode is enabled. IfIGNOREis not specified, the copy is aborted and rolled back if duplicate-key errors occur. IfIGNOREis 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 (编辑:我爱资讯网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|