X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id AAD2AD1B4CB for ; Wed, 10 Mar 2004 22:11:14 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 95343-09 for ; Wed, 10 Mar 2004 18:11:14 -0400 (AST) Received: from dervish.jonathangardner.net (dervish.jonathangardner.net [66.92.192.241]) by svr1.postgresql.org (Postfix) with ESMTP id E82A3D1B4B6 for ; Wed, 10 Mar 2004 18:11:11 -0400 (AST) Received: from jonagard.desktop.amazon.com (207-171-180-101.amazon.com [207.171.180.101]) (authenticated bits=0) by dervish.jonathangardner.net (8.12.10/8.12.10) with ESMTP id i2AMAfxJ028162 (version=TLSv1/SSLv3 cipher=RC4-MD5 bits=128 verify=NO); Wed, 10 Mar 2004 14:10:43 -0800 From: Jonathan Gardner To: "David" Subject: Re: Changing primary keys Date: Wed, 10 Mar 2004 14:06:48 -0800 User-Agent: KMail/1.6 Cc: References: <007101c406c3$807a13d0$42660c81@ad.kent.ac.uk> In-Reply-To: <007101c406c3$807a13d0$42660c81@ad.kent.ac.uk> MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <200403101406.48454.jgardner@jonathangardner.net> Received-SPF: pass (dervish.jonathangardner.net: sender at 207.171.180.101 was authenticated) X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200403/120 X-Sequence-Number: 17219 On Wednesday 10 March 2004 09:17 am, David wrote: > Is it possible to change the primary key of a relation? I want to add an > attribute, that i already have in the realtion, to the primary key (yes i > realise i designed my model pretty badly) > It sure is. First, ensure that the values are indeed not NULL and unique. Next, alter the table to drop the primary key. (See ALTER TABLE). Then, alter the table to add the new primary key. If you have other tables that have a foreign key references to this table, they may have to change. I don't need to tell you to think really hard about schema changes and the impact it will have on the application before you do stuff. And be sure you are not doing this on the production database without testing it first! My personal preference would be to add the attribute, and then make a new primary key column. I really don't like multi-column primary keys as they are a bit more difficult to use. Even if the boss wants to use the old-pk + attribute as the pk, you will use the new pk as the actual pk for joins and such. You may put a not null unique constraint on the old-pk + attribute combination so that you are guaranteed that the data won't violate the boss's idea of what the pk should be. -- Jonathan Gardner jgardner@jonathangardner.net