Re: An easy question about creating a primary key

From: "Christopher Kings-Lynne" <chriskl(at)familyhealth(dot)com(dot)au>
To: "Ligia Pimentel" <lmpimentel(at)yahoo(dot)com>, <pgsql-sql(at)postgresql(dot)org>
Subject: Re: An easy question about creating a primary key
Date: 2001-12-04 01:36:18
Message-ID: GNELIHDDFBOCMGBFGEFOOEKGCAAA.chriskl@familyhealth.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-odbc pgsql-sql

Hi Ligia,

I have submitted code for 7.2b3 that allows ADD UNIQUE after table creation,
but you'll have to wait until 7.3 for ADD PRIMARY KEY after table createion.
What you can do however is something like this:

1. Make sure the column you want to make a primary key is NOT NULL and there
are no other PRIMARY KEYs on the table.

2.
BEGIN;
CREATE UNIQUE INDEX blah ON table(field);
UPDATE pg_index SET indisprimary=true WHERE indexrelid=(SELECT oid FROM
pg_class WHERE relname='blah'))
COMMIT;

Not that as far as postgres is concerned a UNIQUE, NOT NULL index is exactly
the same as a PRIMARY KEY index. All that the above catalog tweak does is
actually mark the index as being primary in pg_dump, etc.

Chris

> -----Original Message-----
> From: pgsql-sql-owner(at)postgresql(dot)org
> [mailto:pgsql-sql-owner(at)postgresql(dot)org]On Behalf Of Ligia Pimentel
> Sent: Friday, 30 November 2001 7:22 AM
> To: pgsql-sql(at)postgresql(dot)org
> Subject: [SQL] An easy question about creating a primary key
>
>
> Hello.
>
> I'm using postgres 7.1. I have an easy question...
>
> I want to create a primary key constraint on an existing table. The
> documentation says I can't . Please confirm. If this is true... How can I
> rename the existing table so I can create the new one and copy the data?
>
> Thank you
>
> Ligia
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
>

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2001-12-04 02:12:21 Re: An easy question about creating a primary key
Previous Message Hiroshi Inoue 2001-12-04 00:34:37 Re: case sensititvity bug in foreign keys on cygwin

Browse pgsql-odbc by date

  From Date Subject
Next Message Tom Lane 2001-12-04 02:12:21 Re: An easy question about creating a primary key
Previous Message Josh Berkus 2001-12-03 19:20:07 Re: An easy question about creating a primary key

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2001-12-04 02:12:21 Re: An easy question about creating a primary key
Previous Message Tom Lane 2001-12-03 19:23:07 Re: Error in executing plpgsql function