Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop a column from composite primarykey

Tags:

oracle

i want to drop a column from composite primary key(not to delete from table). if my table is like this.

create table "scott"."xyz"(
   "column1" not null,
   "column2" not null,
   "column3" not null,
   "column4" not null,
   "column5" not null,
   "column6",
    CONSTRAINT PRIMARY KEY ("column1","column2","column3","column4")
);

i want to alter this primary key to first three column without dropping it. beacuse i don't know CONSTRAINT name.

like image 653
mallikarjun Avatar asked Jul 12 '12 07:07

mallikarjun


1 Answers

You don't need the constraint name:

 ALTER TABLE "scott"."xyz" DROP PRIMARY KEY;
 ALTER TABLE "scott"."xyz" ADD PRIMARY KEY ("column1","column2","column3");

But it is probably a good idea to give your PK a name in the future:

 ALTER TABLE "scott"."xyz" 
   ADD CONSTRAINT pk_xyz PRIMARY KEY ("column1","column2","column3");

And I would not recommend to use quoted identifiers. You will have problems with various tools in the long run.

like image 130
a_horse_with_no_name Avatar answered Oct 15 '22 15:10

a_horse_with_no_name