Re: Introducing another primary key field? - Mailing list pgsql-general

From Michael Glaesemann
Subject Re: Introducing another primary key field?
Date
Msg-id ABF16AB6-F890-11D8-A2A0-000A95C88220@myrealbox.com
Whole thread Raw
In response to Introducing another primary key field?  (Bjørn T Johansen <btj@havleik.com>)
List pgsql-general
On Aug 26, 2004, at 2:05 PM, Bjørn T Johansen wrote:

> I need to introduce an extra field in a table that has one field as
> primary key. The "problem" is that this new field should be combined
> with the existing pk to create a new combined pk for the table, how to
> I change the table?

Inside a transaction, you'll want to drop the old primary key
constraint (ALTER TABLE foo DROP CONSTRAINT ...), add the new column
(ALTER TABLE foo ADD COLUMN newcol ... ), fill it with some kind of
data (UPDATE foo set newcol = ... ), and add the new constraint (ALTER
TABLE foo ADD CONSTRAINT ... )

I think you'll have to set the new column NOT NULL also (ALTER TABLE
foo ALTER COLUMN newcol SET NOT NULL) to be able to make it part of a
key.

Here are some docs that might help you:
<http://www.postgresql.org/docs/current/static/sql-altertable.html>
<http://www.postgresql.org/docs/current/static/ddl-constraints.html>

Hope this helps!

Michael Glaesemann
grzm myrealbox com


pgsql-general by date:

Previous
From: Michael Glaesemann
Date:
Subject: Re: job for sql, pl/pgsql,gawk,perl or ??
Next
From: "Murali Mohan Kasetty"
Date:
Subject: unsubscribe