From: Stephan Szabo <sszabo@megazone23.bigpanda.com>
To: Bhuvan A <bhuvansql@yahoo.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Adding a New Composite Constraint to an Existing Table.
Date: Sat, 9 Feb 2002 09:43:58 -0800 (PST)
Message-ID: <20020209093613.L59069-100000@megazone23.bigpanda.com> (raw)
In-Reply-To: <Pine.LNX.4.20.0202091548400.8097-100000@Larry.bks>
On Sat, 9 Feb 2002, Bhuvan A wrote:
> I am using postgresql 7.1. I was unable to add a new PRIMARY KEY
> constraint to an existing table.
>
> i have table,
> CREATE TABLE xx
> (
> id int,
> name text,
> amount int
> );
>
> I have say some 10 records in this table, which are unique and not
> null (primary). Fine! Now, i am willing to have primary key constraint
> to all the 3 fields.
>
> I tried,
>
> bhuvan=> ALTER TABLE xx ADD primary key (id, name, amount);
> ERROR: ALTER TABLE / ADD CONSTRAINT is not implemented for that
> constraint type.
>
> Where i am wrong? How else, can i get that primary key constraint
> WITHOUT RECREATING that table?
On 7.1 you need a little magic depending on how exactly you want to
make the primary key. I don't have a 7.1 machine available, but...
You should make a unique index xx_pkey on xx(id, name, amount)
You may want to update pg_index to set the indisprimary for
that index, but I don't think that'll affect the actual running of
the constraint.
You'll then need to set the three columns NOT NULL by something like:
update pg_attribute set attnotnull=true from pg_class where
pg_class.oid=attrelid and relname='xx' and attname in ('id', 'name',
'amount');
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: sszabo@megazone23.bigpanda.com, bhuvansql@yahoo.com
Subject: Re: Adding a New Composite Constraint to an Existing Table.
In-Reply-To: <20020209093613.L59069-100000@megazone23.bigpanda.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox