mysqldude

Adding unique constraint

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
ALTER TABLE ... ADD PRIMARY KEY with duplicates (expect 1062)
original post
✓ 1062✓ 1062✓ 1062✓ 1062✓ 1062✓ 1062✓ 1062✓ 1062✓ 1062
ALTER IGNORE TABLE ... ADD PRIMARY KEY (original)
original post
⚠ 1681✗ 1064✗ 1064✗ 1064✗ 1064✓≠≠≠
ALTER IGNORE TABLE ... ADD UNIQUE KEY (same idea, unique index)
version check
⚠ 1681✗ 1064✗ 1064✗ 1064✗ 1064✓✓✓✓
ALTER IGNORE TABLE ... ADD UNIQUE KEY on a NOT NULL column
version check
⚠ 1681✗ 1064✗ 1064✗ 1064✗ 1064✓≠≠≠
Find duplicates with GROUP BY ... HAVING (original)
original post
✓✓✓✓✓✓✓✓✓
De-duplicate with ROW_NUMBER() then add PK (modern)
modern rewrite
✗ 1064✗ 1064✓✓✓✓✓✓✓
ALTER TABLE ... ADD PRIMARY KEY with duplicates (expect 1062) original post
MySQL 5.6✓ 10625.7✓ 10628.0✓ 10628.4✓ 10629✓ 1062
MariaDB 10.6✓ 106210.11✓ 106211.4✓ 106211.8✓ 1062
ALTER IGNORE TABLE ... ADD PRIMARY KEY (original) original post
MySQL 5.6⚠ 16815.7✗ 10648.0✗ 10648.4✗ 10649✗ 1064
MariaDB 10.6✓10.11≠11.4≠11.8≠
ALTER IGNORE TABLE ... ADD UNIQUE KEY (same idea, unique index) version check
MySQL 5.6⚠ 16815.7✗ 10648.0✗ 10648.4✗ 10649✗ 1064
MariaDB 10.6✓10.11✓11.4✓11.8✓
ALTER IGNORE TABLE ... ADD UNIQUE KEY on a NOT NULL column version check
MySQL 5.6⚠ 16815.7✗ 10648.0✗ 10648.4✗ 10649✗ 1064
MariaDB 10.6✓10.11≠11.4≠11.8≠
Find duplicates with GROUP BY ... HAVING (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
De-duplicate with ROW_NUMBER() then add PK (modern) modern rewrite
MySQL 5.6✗ 10645.7✗ 10648.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-23 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9
What the warnings and failures say (9)
  • ALTER IGNORE TABLE ... ADD PRIMARY KEY (original) (MySQL 5.6): warning 1681: 'IGNORE' is deprecated and will be removed in a future release.
  • ALTER IGNORE TABLE ... ADD PRIMARY KEY (original) (MySQL 5.7, MySQL 8.0, MySQL 8.4, MySQL 9): error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IGNORE TABLE emp ADD CONSTRAINT PRIMARY KEY (
  • ALTER IGNORE TABLE ... ADD PRIMARY KEY (original) (MariaDB 10.11, MariaDB 11.4, MariaDB 11.8): rows differ
  • ALTER IGNORE TABLE ... ADD UNIQUE KEY (same idea, unique index) (MySQL 5.6): warning 1681: 'IGNORE' is deprecated and will be removed in a future release.
  • ALTER IGNORE TABLE ... ADD UNIQUE KEY (same idea, unique index) (MySQL 5.7, MySQL 8.0, MySQL 8.4, MySQL 9): error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IGNORE TABLE emp ADD UNIQUE KEY (emp_id)' at
  • ALTER IGNORE TABLE ... ADD UNIQUE KEY on a NOT NULL column (MySQL 5.6): warning 1681: 'IGNORE' is deprecated and will be removed in a future release.
  • ALTER IGNORE TABLE ... ADD UNIQUE KEY on a NOT NULL column (MySQL 5.7, MySQL 8.0, MySQL 8.4, MySQL 9): error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IGNORE TABLE emp ADD UNIQUE KEY (emp_id)' at
  • ALTER IGNORE TABLE ... ADD UNIQUE KEY on a NOT NULL column (MariaDB 10.11, MariaDB 11.4, MariaDB 11.8): rows differ
  • De-duplicate with ROW_NUMBER() then add PK (modern) (MySQL 5.6, MySQL 5.7): error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(PARTITION BY emp_id ORDER BY name) rn FROM e

Related errors: 1062

The original article

First published 2012-06-01. The text below is the original; the table above shows how it behaves on current versions.

MySQL offers a nice feature which both gets rid of duplicates and adds a unique/primary key. The syntax for the MySQL command is ALTER IGNORE TABLE table_name which allows you to either add a unique key or a primary key.

Here is an example:

mysql> create table emp(emp_id int, name varchar(100), salary decimal(5,0));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into emp(emp_id, name, salary) values (1,'Ron',1000),(2,'Tom',1100),(3,'Dick',1000),(3,'Harry',1250);
Query OK, 4 rows affected (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from emp;
+--------+-------+--------+
| emp_id | name  | salary |
+--------+-------+--------+
|      1 | Ron   |   1000 |
|      2 | Tom   |   1100 |
|      3 | Dick  |   1000 |
|      3 | Harry |   1250 |
+--------+-------+--------+
4 rows in set (0.00 sec)

mysql> alter table emp add constraint primary key (emp_id);
ERROR 1062 (23000): Duplicate entry '3' for key 'PRIMARY'
mysql> alter ignore table emp add constraint primary key (emp_id);
Query OK, 4 rows affected, 1 warning (0.03 sec)
Records: 4  Duplicates: 1  Warnings: 1

mysql> select * from emp;
+--------+------+--------+
| emp_id | name | salary |
+--------+------+--------+
|      1 | Ron  |   1000 |
|      2 | Tom  |   1100 |
|      3 | Dick |   1000 |
+--------+------+--------+
3 rows in set (0.00 sec)

To identify the entries that have a duplicate we can issue the following SQL.

mysql> drop table emp;
Query OK, 0 rows affected (0.01 sec)

mysql> create table emp(emp_id int, name varchar(100), salary decimal(5,0));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into emp(emp_id, name, salary) values (1,'Ron',1000),(2,'Tom',1100),(3,'Dick',1000),(3,'Harry',1250);
Query OK, 4 rows affected (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select * from emp;
+--------+-------+--------+
| emp_id | name  | salary |
+--------+-------+--------+
|      1 | Ron   |   1000 |
|      2 | Tom   |   1100 |
|      3 | Dick  |   1000 |
|      3 | Harry |   1250 |
+--------+-------+--------+
4 rows in set (0.00 sec)

mysql> select emp_id
    -> from emp
    -> group by emp_id
    -> having count(*) > 1;
+--------+
| emp_id |
+--------+
|      3 |
+--------+
1 row in set (0.00 sec)