Fix DB duplicate entries (MySQL bug)

↘锁芯ラ 提交于 2019-12-04 18:18:31
RC.

To list all the anomalies:

SELECT name, count(*) FROM TableA GROUP BY name HAVING count(*) > 1;

There are a few ways to tackle deleting the dups and your path will depend heavily on the number of dups you have.

See this SO question for ways of removing those from your table.

Here is the solution I provided there:

-- Setup for example
create table people (fname varchar(10), lname varchar(10));

insert into people values ('Bob', 'Newhart');
insert into people values ('Bob', 'Newhart');
insert into people values ('Bill', 'Cosby');
insert into people values ('Jim', 'Gaffigan');
insert into people values ('Jim', 'Gaffigan');
insert into people values ('Adam', 'Sandler');

-- Show table with duplicates
select * from people;

-- Create table with one version of each duplicate record
create table dups as 
    select distinct fname, lname, count(*) 
    from people group by fname, lname 
    having count(*) > 1;

-- Delete all matching duplicate records
delete people from people inner join dups 
on people.fname = dups.fname AND 
   people.lname = dups.lname;

-- Insert single record of each dup back into table
insert into people select fname, lname from dups;

-- Show Fixed table
select * from people;

Create a new table, select all rows and group by the unique key (in the example column name) and insert in the new table.

To find out what is that character, do the following query:

SELECT HEX(Name) FROM TableName WHERE Name LIKE 'Hach%'

You will se the ascii code of that 'square'.

If that character is 'x', you could update like this:(but if that column is Unique you will have some errors)

UPDATE TableName SET Name=TRIM(TRAILING 'x' FROM Name);

I'll assume this is a MySQL 4.1 random bug. Somes values are just changing on their own for no particular reason even if they violates some MySQL constraints. MySQL is simply ignoring those violations.

To solve my problem, I will write a prog that tries to resinsert every line of data in the same table (to be precise : another table with the same caracteristics) and log every instance of failures.

I will leave the incident open for a while in case someone gets the same problem and someone else finds a more practical solution.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!