Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ALTER TABLE in Amazon Redshift to set two columns as Primary Key?

I would like to alter table in Amazon Redshift, and put two columns as a composite Primary key?

I tried this code snippet:

ALTER TABLE tableNameHere ADD CONSTRAINT PK_1 PRIMARY KEY (col1);

But it worked just to set just one single primary key.

Then I tried:

ALTER TABLE tableNameHere ADD CONSTRAINT PK_1 PRIMARY KEY (col1, col2);

And I get an error:

Invalid operation: multiple primary keys for table "t_sim_data" are not allowed;

PS_1: I already read Redshift : defining composite primary key but in that question it is about creating not altering a table.

PS_2: I have other tables in our cluster where I can see that they have a composite (of two columns) primary key. So it is definitely possible to have a composite Primary Key in Amazon Redshift.

PS_3: I know the power of Redshift is in setting DistKey and SortKey, but I need to set a composite primary key for a table for a specific need.

like image 484
ZelelB Avatar asked Oct 29 '25 01:10

ZelelB


1 Answers

1st drop the PK_1 constraint then create the 2nd

ALTER TABLE tableNameHere DROP CONSTRAINT PK_1;

then

ALTER TABLE tableNameHere ADD CONSTRAINT PK_1 PRIMARY KEY (col1, col2);
like image 143
Zaynul Abadin Tuhin Avatar answered Oct 30 '25 17:10

Zaynul Abadin Tuhin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!