Lab-verifiedReproduced and fixed on Oracle AI Database 26ai Free 23.26.3.0.0 · 2026-11-29 · container image gvenzl/oracle-free:23-slim
CREATE UNIQUE INDEX reads every existing row. If two rows have the same key value, Oracle stops with ORA-01452 and no index is created. The fix is to find those rows with GROUP BY ... HAVING COUNT(*) > 1 and decide which to keep or change.
The key is whatever the index is built on. For a function-based index such as UPPER(email) that is the function result, and in a composite key a NULL in one column does not make the rows different.
01Duplicate rows already in the column
customers had kim@example.com twice (ids 1 and 3), so CREATE UNIQUE INDEX customers_email_uk ON customers (email) failed with ORA-01452.
SELECT email, COUNT(*) ... GROUP BY email HAVING COUNT(*) > 1 showed kim@example.com with 2. Deleting every row except the one with the smallest ROWID (WHERE c.ROWID > (SELECT MIN(x.ROWID) ... WHERE x.email = c.email)) removed 1 row; the index was then created and ids 1 and 2 remained.
Setup
CREATE TABLE customers (id NUMBER PRIMARY KEY, email VARCHAR2(50), name VARCHAR2(20));
INSERT INTO customers VALUES (1, 'kim@example.com', 'Kim');
INSERT INTO customers VALUES (2, 'lee@example.com', 'Lee');
INSERT INTO customers VALUES (3, 'kim@example.com', 'Kim');
COMMIT;Table created.
1 row created.
1 row created.
1 row created.
Commit complete.CREATE UNIQUE INDEX customers_email_uk ON customers (email);CREATE UNIQUE INDEX customers_email_uk ON customers (email)
*
ERROR at line 1:
ORA-01452: cannot CREATE UNIQUE INDEX; duplicate keys found
Help: https://docs.oracle.com/error-help/db/ora-01452/COLUMN email FORMAT A20
SELECT email, COUNT(*) AS cnt FROM customers GROUP BY email HAVING COUNT(*) > 1;EMAIL CNT
-------------------- ----------
kim@example.com 2
1 row selected.DELETE FROM customers c
WHERE c.ROWID > (SELECT MIN(x.ROWID) FROM customers x WHERE x.email = c.email);
COMMIT;1 row deleted.
Commit complete.CREATE UNIQUE INDEX customers_email_uk ON customers (email);
SELECT id, email FROM customers ORDER BY id;Index created.
ID EMAIL
---------- --------------------------------------------------
1 kim@example.com
2 lee@example.com
2 rows selected.02Case-insensitive unique index on UPPER(email)
kim@example.com and KIM@example.com are different values, but CREATE UNIQUE INDEX ... (UPPER(email)) compares them after UPPER(), where they are equal. It failed with ORA-01452.
Grouping by UPPER(email) found ids 1 and 2. After deleting id 2 the function-based unique index was created.
Setup
CREATE TABLE customers (id NUMBER PRIMARY KEY, email VARCHAR2(50));
INSERT INTO customers VALUES (1, 'kim@example.com');
INSERT INTO customers VALUES (2, 'KIM@example.com');
INSERT INTO customers VALUES (3, 'lee@example.com');
COMMIT;Table created.
1 row created.
1 row created.
1 row created.
Commit complete.CREATE UNIQUE INDEX customers_email_uk ON customers (UPPER(email));CREATE UNIQUE INDEX customers_email_uk ON customers (UPPER(email))
*
ERROR at line 1:
ORA-01452: cannot CREATE UNIQUE INDEX; duplicate keys found
Help: https://docs.oracle.com/error-help/db/ora-01452/COLUMN email FORMAT A20
SELECT id, email FROM customers
WHERE UPPER(email) IN (SELECT UPPER(email) FROM customers GROUP BY UPPER(email) HAVING COUNT(*) > 1)
ORDER BY id; ID EMAIL
---------- --------------------
1 kim@example.com
2 KIM@example.com
2 rows selected.DELETE FROM customers WHERE id = 2;
COMMIT;1 row deleted.
Commit complete.CREATE UNIQUE INDEX customers_email_uk ON customers (UPPER(email));Index created.03Composite key with NULL in one column
emp_phone had two rows (100, NULL) for (emp_id, phone_type). Oracle does not index rows where every key column is NULL, but here emp_id is 100, so the two rows count as the same key and the unique index failed with ORA-01452.
GROUP BY emp_id, phone_type showed 100 with an empty phone_type and count 2. Setting phone_type to MOBILE and HOME for the two numbers let the index be created.
Setup
CREATE TABLE emp_phone (emp_id NUMBER, phone_type VARCHAR2(10), phone VARCHAR2(20));
INSERT INTO emp_phone VALUES (100, NULL, '010-1111-2222');
INSERT INTO emp_phone VALUES (100, NULL, '010-3333-4444');
INSERT INTO emp_phone VALUES (200, 'MOBILE', '010-5555-6666');
COMMIT;Table created.
1 row created.
1 row created.
1 row created.
Commit complete.CREATE UNIQUE INDEX emp_phone_uk ON emp_phone (emp_id, phone_type);CREATE UNIQUE INDEX emp_phone_uk ON emp_phone (emp_id, phone_type)
*
ERROR at line 1:
ORA-01452: cannot CREATE UNIQUE INDEX; duplicate keys found
Help: https://docs.oracle.com/error-help/db/ora-01452/COLUMN phone_type FORMAT A10
SELECT emp_id, phone_type, COUNT(*) AS cnt FROM emp_phone GROUP BY emp_id, phone_type HAVING COUNT(*) > 1; EMP_ID PHONE_TYPE CNT
---------- ---------- ----------
100 2
1 row selected.UPDATE emp_phone SET phone_type = 'MOBILE' WHERE emp_id = 100 AND phone = '010-1111-2222';
UPDATE emp_phone SET phone_type = 'HOME' WHERE emp_id = 100 AND phone = '010-3333-4444';
COMMIT;1 row updated.
1 row updated.
Commit complete.CREATE UNIQUE INDEX emp_phone_uk ON emp_phone (emp_id, phone_type);Index created.FAQ
How do I find the duplicate rows behind ORA-01452?
Group by the index columns (or the index expression) and keep groups with HAVING COUNT(*) > 1. For UPPER(email) group by UPPER(email), as above.
How do I delete duplicates and keep one row?
Delete rows whose ROWID is greater than the MIN(ROWID) for the same key. Above this removed 1 row and kept ids 1 and 2.
Doesn't NULL never equal NULL in a unique index?
Only when all key columns are NULL. With (100, NULL) twice the rows are duplicates, as the composite key case above showed.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary users created for the test; system is the DBA account. Oracle AI Database 26ai Free Release 23.26.3.0.0 - Develop, Learn, and Run for Free 23.26.3.0.0