문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top