Home > Database > Mysql Tutorial > How to Remove Duplicate Rows in MySQL Using a Unique Index?

How to Remove Duplicate Rows in MySQL Using a Unique Index?

Patricia Arquette
Release: 2025-01-24 02:23:12
Original
647 people have browsed it

How to Remove Duplicate Rows in MySQL Using a Unique Index?

Remove Duplicate Rows in MySQL Using an Index

To eliminate duplicate rows based on specific columns in a MySQL table, a unique index can be created. An index is a data structure that improves query performance by organizing data in a specific order.

Step 1: Create a Unique Index

To create a unique index on the columns that define unique rows (e.g., title, company, and site_id), use the following SQL statement:

ALTER IGNORE TABLE jobs
ADD UNIQUE INDEX idx_name (site_id, title, company);
Copy after login

How it Works

The IGNORE keyword in the SQL statement instructs MySQL to ignore any duplicate rows when creating the index. This means that only the first occurrence of each unique combination of values will be included in the index.

Result

After creating the index, all duplicate rows will be automatically removed from the table. This ensures that the table contains only unique rows based on the specified columns.

Note: This technique is no longer supported in MySQL versions 5.7 and above.

The above is the detailed content of How to Remove Duplicate Rows in MySQL Using a Unique Index?. 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