0

I have a primary key PK1 for the table TABLE1. Need to add one more column to the existing primary key. I was tying the following alter script

ALTER TABLE TABLE1
ADD CONSTRAINT "PRIMARYKEYS" PRIMARY KEY
("PK1",
"PK2");

Error report:
SQL Error: ORA-02260: table can have only one primary key
02260. 00000 -  "table can have only one primary key"
*Cause:    Self-evident.
*Action:   Remove the extra primary key.

How can I add one more column to the primary key, without affecting the data(data has been verified , there are no duplication.)

Nidheesh
  • 4,390
  • 29
  • 87
  • 150
  • 1
    possible duplicate of [Change Primary Key Oracle](http://stackoverflow.com/questions/2310561/change-primary-key-oracle) – Noel Mar 25 '14 at 06:38

2 Answers2

2

If you have concern about new data that violates PK can be added during the time interval when the old PK dropped, but the new one is not created, you can create a unique index first :

CREATE UNIQUE INDEX IDXU_TABLE1_PK ON TABLE1(PK1,PK2);
ALTER TABLE TABLE1 DROP CONSTRAINT [old_pk_constraint_name] ;
ALTER TABLE TABLE1 ADD CONSTRAINT "PRIMARYKEYS" PRIMARY KEY
   (PK1,PK2) USING INDEX IDXU_TABLE1_PK;

Another option is to keep index associated with the old PK constraint until new PK is created :

ALTER TABLE TABLE1 DROP CONSTRAINT [old_pk_constraint_name] KEEP INDEX;
ALTER TABLE TABLE1 ADD CONSTRAINT "PRIMARYKEYS" PRIMARY KEY
   (PK1,PK2) ;
DROP INDEX [name of unique index associated with the old PK constraint];
a1ex07
  • 36,826
  • 12
  • 90
  • 103
0

You need to drop old primary key first.

Suor
  • 2,845
  • 1
  • 22
  • 28