MySQL Handling Duplicate Data
Some MySQL data tables may contain duplicate records. In some cases we allow the existence of duplicate data, but sometimes we need to delete these duplicate records.
In this chapter we will introduce how to prevent duplicate data in a table and how to delete duplicate data from a table.
Preventing Duplicate Data in a Table
You can set specified fields in a MySQL data table as
PRIMARY KEY (primary key)
or
UNIQUE (unique)
indexes to ensure data uniqueness.
Let's try an example: the following table has no index or primary key, so it allows multiple duplicate records.
CREATE TABLE person_tbl
(
first_name CHAR(20),
last_name CHAR(20),
sex CHAR(10)
);
If you want to make the fields first_name and last_name non-duplicable, you can set a double primary key to ensure data uniqueness. If you set a double primary key, then the default value of that key cannot be NULL and can be set to NOT NULL. As shown below:
CREATE TABLE person_tbl
(
first_name CHAR(20) NOT NULL,
last_name CHAR(20) NOT NULL,
sex CHAR(10),
PRIMARY KEY (last_name, first_name)
);
If we set a unique index, then when inserting duplicate data, the SQL statement will fail to execute and throw an error.
The difference between INSERT IGNORE INTO and INSERT INTO is that INSERT IGNORE INTO ignores data that already exists in the database: if the database has no data, it inserts the new data; if there is data, it skips this record. This preserves the data already in the database, achieving the purpose of inserting data in the gaps.
The following example uses INSERT IGNORE INTO; no error will occur after execution, and no duplicate data will be inserted into the table:
mysql> INSERT IGNORE INTO person_tbl (last_name, first_name)
-> VALUES( 'Jay', 'Thomas');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT IGNORE INTO person_tbl (last_name, first_name)
-> VALUES( 'Jay', 'Thomas');
Query OK, 0 rows affected (0.00 sec)
INSERT IGNORE INTO, when inserting data, after record uniqueness is set, if duplicate data is inserted, it will not return an error but only a warning. REPLACE INTO, if there is a record with the same primary or unique key, first deletes it and then inserts the new record.
Another way to ensure data uniqueness is to add a UNIQUE index, as shown below:
CREATE TABLE person_tbl
(
first_name CHAR(20) NOT NULL,
last_name CHAR(20) NOT NULL,
sex CHAR(10),
UNIQUE (last_name, first_name)
);
Counting Duplicate Data
Below we will count the number of duplicate records for first_name and last_name in the table:
mysql> SELECT COUNT(*) as repetitions, last_name, first_name
-> FROM person_tbl
-> GROUP BY last_name, first_name
-> HAVING repetitions > 1;
The above query will return the number of duplicate records in the person_tbl table. In general, to query duplicate values, perform the following steps:
- Determine which column contains values that may be duplicated.
- Use COUNT(*) in the column selection list for those columns.
- List those columns in the GROUP BY clause.
- Set the duplicate count greater than 1 in the HAVING clause.
Filtering Duplicate Data
If you need to read non-duplicate data, you can use the DISTINCT keyword in the SELECT statement to filter out duplicate data.
mysql> SELECT DISTINCT last_name, first_name
-> FROM person_tbl;
You can also use GROUP BY to read non-duplicate data from the table:
mysql> SELECT last_name, first_name
-> FROM person_tbl
-> GROUP BY (last_name, first_name);
Remove duplicate data
If you want to delete duplicate data from the table, you can use the following SQL statement:
mysql> CREATE TABLE tmp SELECT last_name, first_name, sex FROM person_tbl GROUP BY (last_name, first_name, sex);
mysql> DROP TABLE person_tbl;
mysql> ALTER TABLE tmp RENAME TO person_tbl;
Of course, you can also add INDEX and PRIMAY KEY (primary key) to the table as a simple way to delete duplicate records. The method is as follows:
mysql> ALTER IGNORE TABLE person_tbl
-> ADD PRIMARY KEY (last_name, first_name);