Oracle 23.26ConstraintsLab-verified · 2026-11-26

ORA-02261: The unique or primary key already exists in the table

ALTER TABLE ... ADD CONSTRAINT tried to add a UNIQUE or PRIMARY KEY on columns that already have one.

Oracle AI Database 26ai Free 23.26.3.0.0
ORA-02261: The unique or primary key already exists in the table.

What changed in 26ai error messages →

Jump to a cause

  1. 01Adding UNIQUE to a column that is already the primary key
  2. 02An unnamed UNIQUE (SYS_C...) already exists and you add it again with a name
  3. 03Turning a UNIQUE column into the primary key

Lab-verifiedReproduced and fixed on Oracle AI Database 26ai Free 23.26.3.0.0 · 2026-11-26 · container image gvenzl/oracle-free:23-slim

A table can have only one UNIQUE or PRIMARY KEY constraint on the same column list. Adding a second one on the same columns, under any name, fails with ORA-02261.

Below are three common cases reproduced on a real database, with the fix we ran and its result.

01Adding UNIQUE to a column that is already the primary key

members.member_id was the primary key MEMBERS_PK. ALTER TABLE members ADD CONSTRAINT members_id_uk UNIQUE (member_id) failed with ORA-02261.

Joining user_constraints and user_cons_columns showed MEMBERS_PK P MEMBER_ID. A primary key is already unique, so no extra constraint is needed. For scripts that may run more than once, we used a block that adds the UNIQUE only if no single-column P/U constraint on MEMBER_ID exists. It printed skip: MEMBER_ID is already unique.

Setup
Setuprun as lab_a
CREATE TABLE members (member_id NUMBER CONSTRAINT members_pk PRIMARY KEY, name VARCHAR2(30));
OUTPUT
Table created.
Reproducerun as lab_a
ALTER TABLE members ADD CONSTRAINT members_id_uk UNIQUE (member_id);
✕ ERROR
ALTER TABLE members ADD CONSTRAINT members_id_uk UNIQUE (member_id)
                                                 *
ERROR at line 1:
ORA-02261: The unique or primary key already exists in the table.
Help: https://docs.oracle.com/error-help/db/ora-02261/
Checkrun as lab_a
COLUMN constraint_name FORMAT A14
COLUMN column_name FORMAT A12
SELECT c.constraint_name, c.constraint_type, cc.column_name FROM user_constraints c JOIN user_cons_columns cc ON cc.constraint_name = c.constraint_name WHERE c.table_name = 'MEMBERS' AND c.constraint_type IN ('P', 'U') ORDER BY c.constraint_name;
OUTPUT
CONSTRAINT_NAM C COLUMN_NAME
-------------- - ------------
MEMBERS_PK     P MEMBER_ID

1 row selected.

Check existing P and U constraints on the same columns before adding one.

After the fixrun as lab_a
SET SERVEROUTPUT ON
DECLARE
  n NUMBER;
BEGIN
  SELECT COUNT(*) INTO n FROM user_constraints c JOIN user_cons_columns cc ON cc.constraint_name = c.constraint_name
   WHERE c.table_name = 'MEMBERS' AND c.constraint_type IN ('P', 'U') AND cc.column_name = 'MEMBER_ID'
     AND (SELECT COUNT(*) FROM user_cons_columns x WHERE x.constraint_name = c.constraint_name) = 1;
  IF n = 0 THEN
    EXECUTE IMMEDIATE 'ALTER TABLE members ADD CONSTRAINT members_id_uk UNIQUE (member_id)';
    DBMS_OUTPUT.PUT_LINE('added');
  ELSE
    DBMS_OUTPUT.PUT_LINE('skip: MEMBER_ID is already unique');
  END IF;
END;
/
✓ SUCCESS
skip: MEMBER_ID is already unique

PL/SQL procedure successfully completed.

02An unnamed UNIQUE (SYS_C...) already exists and you add it again with a name

users.email was declared UNIQUE inline without a name. Adding CONSTRAINT users_email_uk UNIQUE (email) later failed with ORA-02261.

The existing constraint was SYS_C009163 U EMAIL. If the goal is only a proper name, rename it: a PL/SQL block found the U constraint on EMAIL and ran ALTER TABLE users RENAME CONSTRAINT ... TO users_email_uk. The constraint then showed as USERS_EMAIL_UK.

Setup
Setuprun as lab_a
CREATE TABLE users (user_id NUMBER PRIMARY KEY, email VARCHAR2(50) UNIQUE);
OUTPUT
Table created.
Reproducerun as lab_a
ALTER TABLE users ADD CONSTRAINT users_email_uk UNIQUE (email);
✕ ERROR
ALTER TABLE users ADD CONSTRAINT users_email_uk UNIQUE (email)
                                                *
ERROR at line 1:
ORA-02261: The unique or primary key already exists in the table.
Help: https://docs.oracle.com/error-help/db/ora-02261/
Checkrun as lab_a
COLUMN constraint_name FORMAT A14
COLUMN column_name FORMAT A12
SELECT c.constraint_name, c.constraint_type, cc.column_name FROM user_constraints c JOIN user_cons_columns cc ON cc.constraint_name = c.constraint_name WHERE c.table_name = 'USERS' AND c.constraint_type = 'U';
OUTPUT
CONSTRAINT_NAM C COLUMN_NAME
-------------- - ------------
SYS_C009163    U EMAIL

1 row selected.
Fixrun as lab_a
BEGIN
  FOR r IN (SELECT c.constraint_name FROM user_constraints c JOIN user_cons_columns cc ON cc.constraint_name = c.constraint_name
             WHERE c.table_name = 'USERS' AND c.constraint_type = 'U' AND cc.column_name = 'EMAIL') LOOP
    EXECUTE IMMEDIATE 'ALTER TABLE users RENAME CONSTRAINT ' || r.constraint_name || ' TO users_email_uk';
  END LOOP;
END;
/
OUTPUT
PL/SQL procedure successfully completed.

The SYS_C number differs per database, so look it up by table and column instead of hard-coding it.

After the fixrun as lab_a
COLUMN constraint_name FORMAT A16
COLUMN column_name FORMAT A12
SELECT c.constraint_name, c.constraint_type, cc.column_name FROM user_constraints c JOIN user_cons_columns cc ON cc.constraint_name = c.constraint_name WHERE c.table_name = 'USERS' AND c.constraint_type = 'U';
✓ SUCCESS
CONSTRAINT_NAME  C COLUMN_NAME
---------------- - ------------
USERS_EMAIL_UK   U EMAIL

1 row selected.

03Turning a UNIQUE column into the primary key

products.product_code had products_code_uk UNIQUE. ADD CONSTRAINT products_pk PRIMARY KEY (product_code) failed with ORA-02261, because a UNIQUE on the same column already existed.

We dropped products_code_uk first, then ran the same ADD CONSTRAINT. It succeeded, and user_constraints showed only PRODUCTS_PK P.

Setup
Setuprun as lab_a
CREATE TABLE products (product_code VARCHAR2(10) CONSTRAINT products_code_uk UNIQUE, name VARCHAR2(30));
INSERT INTO products VALUES ('A100', 'Pen');
INSERT INTO products VALUES ('B200', 'Note');
COMMIT;
OUTPUT
Table created.


1 row created.


1 row created.


Commit complete.
Reproducerun as lab_a
ALTER TABLE products ADD CONSTRAINT products_pk PRIMARY KEY (product_code);
✕ ERROR
ALTER TABLE products ADD CONSTRAINT products_pk PRIMARY KEY (product_code)
                                                *
ERROR at line 1:
ORA-02261: The unique or primary key already exists in the table.
Help: https://docs.oracle.com/error-help/db/ora-02261/
Fixrun as lab_a
ALTER TABLE products DROP CONSTRAINT products_code_uk;
OUTPUT
Table altered.

Drop the UNIQUE and add the PK in one maintenance window: between the two statements, the column is not protected against duplicates.

After the fixrun as lab_a
ALTER TABLE products ADD CONSTRAINT products_pk PRIMARY KEY (product_code);
COLUMN constraint_name FORMAT A12
SELECT constraint_name, constraint_type FROM user_constraints WHERE table_name = 'PRODUCTS' AND constraint_type IN ('P', 'U');
✓ SUCCESS
Table altered.


CONSTRAINT_N C
------------ -
PRODUCTS_PK  P

1 row selected.

FAQ

What does ORA-02261 mean?

The table already has a UNIQUE or PRIMARY KEY constraint on exactly the columns you listed. A second one on the same columns is not allowed.

How do I find existing unique constraints on a column?

Join user_constraints with user_cons_columns on constraint_name and filter constraint_type IN ('P', 'U') for the table.

How do I rename a SYS_C constraint in Oracle?

Use ALTER TABLE t RENAME CONSTRAINT SYS_Cxxxx TO new_name. Look up the SYS_C name first, since it differs per database.

How do I change a unique constraint to a primary key?

Drop the UNIQUE constraint, then add the PRIMARY KEY on the same column.

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