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
Constraint names must be unique within a schema, not just within a table. If another table already has a constraint with that name, ADD CONSTRAINT or CREATE TABLE fails with ORA-02264.
Below are three common cases reproduced on a real database, with the fix we ran and its result.
01A copy table reuses the original table's constraint name
emp_hist was made with CREATE TABLE ... AS SELECT from emp. Adding CONSTRAINT emp_pk PRIMARY KEY (emp_id) to emp_hist failed with ORA-02264.
user_constraints showed EMP_PK P EMP: the name belonged to the original table. We used a name based on the new table, emp_hist_pk, and the ALTER succeeded.
Setup
CREATE TABLE emp (emp_id NUMBER CONSTRAINT emp_pk PRIMARY KEY, name VARCHAR2(30));
CREATE TABLE emp_hist AS SELECT * FROM emp WHERE 1 = 0;Table created.
Table created.ALTER TABLE emp_hist ADD CONSTRAINT emp_pk PRIMARY KEY (emp_id);ALTER TABLE emp_hist ADD CONSTRAINT emp_pk PRIMARY KEY (emp_id)
*
ERROR at line 1:
ORA-02264: name already used by an existing constraint
Help: https://docs.oracle.com/error-help/db/ora-02264/COLUMN constraint_name FORMAT A10
COLUMN table_name FORMAT A10
SELECT constraint_name, constraint_type, table_name FROM user_constraints WHERE constraint_name = 'EMP_PK';CONSTRAINT C TABLE_NAME
---------- - ----------
EMP_PK P EMP
1 row selected.Naming constraints as table name + suffix (_pk, _fk, _ck) avoids this.
ALTER TABLE emp_hist ADD CONSTRAINT emp_hist_pk PRIMARY KEY (emp_id);Table altered.02The old table was renamed, then the new table uses the same constraint name
orders was renamed to orders_old to keep it as a backup. Creating a new orders with CONSTRAINT orders_pk PRIMARY KEY failed with ORA-02264. RENAME TO changes only the table name; the constraint was still ORDERS_PK on ORDERS_OLD, and its index was also ORDERS_PK.
We renamed both: ALTER TABLE orders_old RENAME CONSTRAINT orders_pk TO orders_old_pk and ALTER INDEX orders_pk RENAME TO orders_old_pk. The new orders was then created, and both tables showed their own PK and index names.
Setup
CREATE TABLE orders (order_id NUMBER CONSTRAINT orders_pk PRIMARY KEY, amount NUMBER);
ALTER TABLE orders RENAME TO orders_old;Table created.
Table altered.CREATE TABLE orders (order_id NUMBER CONSTRAINT orders_pk PRIMARY KEY, amount NUMBER, status VARCHAR2(10));CREATE TABLE orders (order_id NUMBER CONSTRAINT orders_pk PRIMARY KEY, amount NUMBER, status VARCHAR2(10))
*
ERROR at line 1:
ORA-02264: name already used by an existing constraint
Help: https://docs.oracle.com/error-help/db/ora-02264/COLUMN constraint_name FORMAT A12
COLUMN table_name FORMAT A12
COLUMN index_name FORMAT A12
SELECT constraint_name, table_name, index_name FROM user_constraints WHERE table_name LIKE 'ORDERS%' AND constraint_type = 'P';CONSTRAINT_N TABLE_NAME INDEX_NAME
------------ ------------ ------------
ORDERS_PK ORDERS_OLD ORDERS_PK
1 row selected.ALTER TABLE orders_old RENAME CONSTRAINT orders_pk TO orders_old_pk;
ALTER INDEX orders_pk RENAME TO orders_old_pk;Table altered.
Index altered.Rename the index too. When we renamed only the constraint, CREATE TABLE then failed with ORA-00955 because the index name ORDERS_PK was still taken.
CREATE TABLE orders (order_id NUMBER CONSTRAINT orders_pk PRIMARY KEY, amount NUMBER, status VARCHAR2(10));
COLUMN constraint_name FORMAT A14
COLUMN table_name FORMAT A12
COLUMN index_name FORMAT A14
SELECT constraint_name, table_name, index_name FROM user_constraints WHERE table_name LIKE 'ORDERS%' AND constraint_type = 'P' ORDER BY table_name;Table created.
CONSTRAINT_NAM TABLE_NAME INDEX_NAME
-------------- ------------ --------------
ORDERS_PK ORDERS ORDERS_PK
ORDERS_OLD_PK ORDERS_OLD ORDERS_OLD_PK
2 rows selected.03The same ADD CONSTRAINT script was run again
products_price_ck CHECK (price >= 0) had already been added. Running the same ALTER TABLE products ADD CONSTRAINT ... again failed with ORA-02264.
user_constraints showed PRODUCTS_PRICE_CK C price >= 0 ENABLED, so the constraint was already in place. For scripts that may run more than once, we used a PL/SQL block that adds it only if the name is not in user_constraints. It printed skip: PRODUCTS_PRICE_CK already exists.
Setup
CREATE TABLE products (product_id NUMBER PRIMARY KEY, price NUMBER);
ALTER TABLE products ADD CONSTRAINT products_price_ck CHECK (price >= 0);Table created.
Table altered.ALTER TABLE products ADD CONSTRAINT products_price_ck CHECK (price >= 0);ALTER TABLE products ADD CONSTRAINT products_price_ck CHECK (price >= 0)
*
ERROR at line 1:
ORA-02264: name already used by an existing constraint
Help: https://docs.oracle.com/error-help/db/ora-02264/COLUMN constraint_name FORMAT A18
COLUMN search_condition FORMAT A12
SELECT constraint_name, constraint_type, search_condition, status FROM user_constraints WHERE table_name = 'PRODUCTS' AND constraint_type = 'C';CONSTRAINT_NAME C SEARCH_CONDI STATUS
------------------ - ------------ ------------------------------
PRODUCTS_PRICE_CK C price >= 0 ENABLED
1 row selected.SET SERVEROUTPUT ON
DECLARE
n NUMBER;
BEGIN
SELECT COUNT(*) INTO n FROM user_constraints WHERE constraint_name = 'PRODUCTS_PRICE_CK';
IF n = 0 THEN
EXECUTE IMMEDIATE 'ALTER TABLE products ADD CONSTRAINT products_price_ck CHECK (price >= 0)';
DBMS_OUTPUT.PUT_LINE('added');
ELSE
DBMS_OUTPUT.PUT_LINE('skip: PRODUCTS_PRICE_CK already exists');
END IF;
END;
/skip: PRODUCTS_PRICE_CK already exists
PL/SQL procedure successfully completed.FAQ
What does ORA-02264 mean?
Another constraint in the same schema already has that name. Constraint names are unique per schema, not per table.
How do I find which table uses a constraint name?
Query user_constraints with WHERE constraint_name = 'NAME' and look at table_name.
Does renaming a table rename its constraints?
No. In our test the constraint and its index kept their names after ALTER TABLE ... RENAME TO.
How do I rename a constraint in Oracle?
Use ALTER TABLE t RENAME CONSTRAINT old_name TO new_name. For PK and UNIQUE, also rename the index with ALTER INDEX ... RENAME TO.
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