X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 6F9A4D1B175 for ; Thu, 24 Jun 2004 20:15:33 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 60614-08 for ; Thu, 24 Jun 2004 23:15:30 +0000 (GMT) Received: from www.postgresql.com (www.postgresql.com [200.46.204.209]) by svr1.postgresql.org (Postfix) with ESMTP id B76E6D1B7CF for ; Thu, 24 Jun 2004 20:15:27 -0300 (ADT) Received: from mail231.csoft.net (resin.csoft.net [63.111.22.86]) by www.postgresql.com (Postfix) with SMTP id 4F739CF4AF4 for ; Thu, 24 Jun 2004 20:15:25 -0300 (ADT) Received: (qmail 9829 invoked from network); 24 Jun 2004 23:15:15 -0000 Received: from unknown (HELO chezphil.org) (63.111.27.87) by mail231.csoft.net with SMTP; 24 Jun 2004 23:15:15 -0000 Content-Disposition: inline Content-Transfer-Encoding: binary Content-Type: text/plain MIME-Version: 1.0 X-Mailer: MIME::Lite 3.01 (F2.72; A1.17; B2.21; Q2.21) From: "Phil Endecott" To: Graham Leggett , pgsql-sql@postgresql.org Subject: Re: Normalising an existing table - how? Date: Thu, 24 Jun 2004 19:15:15 -0400 X-Mailer: Phil's WebMail Message-Id: <4268211@chezphil.org> In-Reply-To: <40DB4AD0.8040805@sharp.fm> References: <40DB229C.9040306@sharp.fm> <7184150@chezphil.org> <40DB4AD0.8040805@sharp.fm> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200406/248 X-Sequence-Number: 18193 Graham Leggett wrote: > >>- Select the money column from the table > >>- Populate the new normalised table with each row containing > >> the value from the original money column > >>- Write the primary keys of the new rows in the normalised > >> table, back to a new column in the original table added for > >> this purpose. > > > Change the order. Do the third step first: > > > > alter table T add column X integer; > > update T set X = nextval(somesequence); > > > > Now do the first and second steps together: > > > > select X, MoneyColumn from T into NewTable; > > > > Is this the sort of thing you need? > > I think it is - though the select foo into NewTable part, does > NewTable have to be empty first, or can it already exist? > > In my case NewTable has some rows in it already, as the database is > currently partially normalised - I need to finish the job. Check the docs. I believe that SELECT INTO does the same as CREATE TABLE AS, i.e. it creates a new table. It will presumably fail if the table already exists. You probably need INSERT SELECT, i.e. insert into NewTable select X, MoneyColumn from T; --Phil.