Thread: Adding a Primary Key to an exisiting table

Adding a Primary Key to an exisiting table

From
Maly Kaing
Date:
Hi

I just exported my MS Access databases into postgres using an ODBC driver,
but the thing is during the export postgres doesn't recognize any of my
primary keys in Access.
Is there any way to alter the tables so that i can make one of my current
fields the primary key?  I don't want to drop the column since I already
have data in the table and i don't want to create/drop tables as some of
the documentation suggests since most of my tables have 10+ fields.. any
suggestions??

mtk



Re: Adding a Primary Key to an exisiting table

From
Bhuvan A
Date:
> Hi
> 
> I just exported my MS Access databases into postgres using an ODBC driver,
> but the thing is during the export postgres doesn't recognize any of my
> primary keys in Access.
> Is there any way to alter the tables so that i can make one of my current
> fields the primary key?  I don't want to drop the column since I already
> have data in the table and i don't want to create/drop tables as some of
> the documentation suggests since most of my tables have 10+ fields.. any
> suggestions??
> 

AFAIK, it can be done in any of the below two ways:

1. Create unique index  CREATE UNIQUE INDEX index_name ON table_name USING btree (field1, ..,   fieldn);
2. Add primary key constraint  ALTER TABLE table_name ADD PRIMARY KEY (field1, .., fieldn);

For this to happen, the fields should have unique values.

regards,
bhuvaneswaran