Home > Database > Mysql Tutorial > body text

**Can You Remove Duplicate Records by Modifying an Existing Index to Unique or Adding a New One?**

Linda Hamilton
Release: 2024-10-28 09:43:29
Original
377 people have browsed it

**Can You Remove Duplicate Records by Modifying an Existing Index to Unique or Adding a New One?**

Removing Duplicates with Unique Index

In the quest to prevent duplicate records, a common approach is to create a unique index on the relevant table columns. However, a recent mistake resulted in the creation of a normal index instead, allowing duplicates to persist within a massive 20 million record table.

Question: Will modifying the existing index to unique or adding a new unique index for those columns remove the duplicate records, or will the operation fail due to existing unique entries?

Answer:

Adding a unique index with the IGNORE modifier will remove duplicate records:

<code class="sql">ALTER IGNORE TABLE mytable ADD UNIQUE INDEX myindex (A, B, C, D);</code>
Copy after login

However, this modifier is deprecated in MySQL 5.7.4 and later, resulting in an error. To circumvent this issue, the following steps can be taken:

  1. Create a temporary table to hold the data from the original table:

    <code class="sql">CREATE TABLE tmp_data SELECT * FROM mytable;</code>
    Copy after login
  2. Truncate the original table:

    <code class="sql">TRUNCATE TABLE mytable;</code>
    Copy after login
  3. Create the unique index on the original table:

    <code class="sql">ALTER TABLE mytable ADD UNIQUE INDEX myindex (A, B, C, D);</code>
    Copy after login
  4. Copy the data back into the original table while ignoring duplicate rows:

    <code class="sql">INSERT IGNORE INTO mytable SELECT * from tmp_data;</code>
    Copy after login
  5. Drop the temporary table:

    <code class="sql">DROP TABLE tmp_data;</code>
    Copy after login

Alternatively, adding a unique index without the IGNORE modifier would cause the query to fail with Error 1062 (duplicate key).

The above is the detailed content of **Can You Remove Duplicate Records by Modifying an Existing Index to Unique or Adding a New One?**. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!