Delete Duplicates in MYSQL using a text column for comparison
-
26-07-2021 - |
سؤال
We've got a database table that need a multiple column unique key. However, one of those columns is TEXT and has lengths as long as 1000 chars (so varchar won't work either). Because of the TEXT column, I can't actually have a unique key for those columns. What's a good way to remove duplicates? Of course, fast would be nice.
المحلول
The best way is to use a UNIQUE INDEX
to avoid duplicate.
Creating a new unique key on the over columns you need to have as uniques will automatically clean the table of any duplicates.
ALTER IGNORE TABLE `table_name`
ADD UNIQUE KEY `key_name`(`column_1`,`column_2`);
The IGNORE
part does not allow the script to terminate after the first error occurs. And the default behavior is to delete the duplicates.
نصائح أخرى
Add a unique
constraint as below:
ALTER IGNORE TABLE table1
ADD UNIQUE unique_name(column1, comlumn1, column3 ... Text);
Here IGNORE
will help in removing the duplicates while creating the constraint.