Re: Normalising an existing table - how?

From: "Phil Endecott" <spam_from_postgresql_sql(at)chezphil(dot)org>
To: Graham Leggett <minfrin(at)sharp(dot)fm>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Normalising an existing table - how?
Date: 2004-06-24 23:15:15
Message-ID: 4268211@chezphil.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Graham Leggett <minfrin(at)sharp(dot)fm> 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.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message sad 2004-06-25 04:16:47 Re: feature request ?
Previous Message Geoffrey 2004-06-24 22:43:12 Re: feature request ?