Adsense Ad

Wednesday, 24 May 2017

Oracle: ORA-01452 Error Message

ORA-01452 Error Message

Learn the cause and how to resolve the ORA-01452 error message in Oracle.

Description

When you encounter an ORA-01452 error, the following error message will appear:
  • ORA-01452: cannot CREATE UNIQUE INDEX; duplicate keys found

Cause

You tried to execute a CREATE UNIQUE INDEX statement on one or more columns that contain duplicate values.

Resolution

The option(s) to resolve this Oracle error are:

Option #1

If the entries do not need to be unique, you can remove the UNIQUE keyword from your CREATE UNIQUE INDEX statement and rerun the command.

Option #2

If the entries need to be unique, delete all entries from the table that create duplicate values. Then re-execute your CREATE UNIQUE INDEX statement.
For example, if you had a table called suppliers defined as follows:
CREATE TABLE suppliers
( supplier_name varchar2(50),
  city varchar2(35)
);
Then executed the following INSERT statements:
INSERT INTO suppliers
(supplier_name, city)
VALUES ('IBM', 'New York');

INSERT INTO suppliers
(supplier_name, city)
VALUES ('IBM', 'Silicon Valley');
You then tried to create a unique index with the following statement:
CREATE UNIQUE INDEX supplier_idx
  ON suppliers (supplier_name);
You would receive the following error message:
Oracle PLSQL
You could correct this by creating your INDEX as a non-unique index.
CREATE INDEX supplier_idx
  ON suppliers (supplier_name);
OR you could remove one of the entries in the suppliers table for IBM and then re-run your CREATE UNIQUE INDEX statement. For example:
DELETE FROM suppliers
WHERE supplier_name = 'IBM'
AND city = 'Silicon Valley';

CREATE UNIQUE INDEX supplier_idx
  ON suppliers (supplier_name);

No comments: