Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Partha.

Asked: December 24, 2015 - 8:27 am UTC

Last updated: December 25, 2015 - 3:22 am UTC

Version: 11g

Viewed 1000+ times

You Asked

Hi Tom,

Please note that I am getting an error wile creating Constraints.
I am using the Syntax:

ALTER TABLE UAT.TABLE_NAME ADD CONSTRAINT TABLE_PK PRIMARY KEY (KEY1);

While I am using this syntax I am getting the Error as:
SQL Error: ORA-00955: name is already used by an existing object
00955. 00000 - "name is already used by an existing object"
*Cause:
*Action:

However the Constraint TABLE_PK is definitely existing for other schemas other than UAT. The same Constraint is existing for SIT/DEV as we are using the same database.

Please advice the correct syntax for creating the constraint TABLE_PK in UAT as well.

Thanks,
Partha.

and Connor said...

Your syntax is fine. You probably have an index floating about. A common cause is when you want to *change* the definition of what the primary key is. For example:

SQL> grant dba to uat identified by uat;

Grant succeeded.

SQL> conn uat/uat
Connected.

SQL> create table uat.my_table ( x int , y int);

Table created.

SQL> create index PK on uat.my_table ( x ) ;

Index created.

SQL> alter table uat.my_table add constraint PK primary key ( x ) ;

Table altered.

SQL> alter table uat.my_table drop constraint PK;

Table altered.

SQL> alter table uat.my_table add constraint PK primary key ( x,y ) ;
alter table uat.my_table add constraint PK primary key ( x,y )
*
ERROR at line 1:
ORA-00955: name is already used by an existing object



We created an index on 'x', then when we dropped the constraint, the index was kept. When we wanted to added a new PK constraint on (x,y) we wanted to create an index called PK on (x,y) ... and we couldnt.

Hope this helps.

Is this answer out of date? If it is, please let us know via a Comment