Re: SQL (?) question

From: Mike Mascari <mascarm(at)mascari(dot)com>
To: Joost Kraaijeveld <J(dot)Kraaijeveld(at)Askesis(dot)nl>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: SQL (?) question
Date: 2004-07-20 21:24:28
Message-ID: 40FD8D8C.6000306@mascari.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Joost Kraaijeveld wrote:

> Hi all,
>
> Is there an easy way (e.g. short SQL script) to add a column to a
> table and fill it with integers (from 1- n) just like using an
> sequence? (I need to retofit a system id that is handled in the
> application, not in the database).

I assume there is some unique candidate key on the table already (or
worst case, an OID.) If so, I'd just do:

CREATE SEQUENCE my_sequence;

CREATE TEMPORARY TABLE foo AS
SELECT candidate_key, nextval('my_sequence') as new_key
FROM table_that_needs_fixed;

ALTER TABLE table_that_needs_fixed
ADD COLUMN new_key integer;

UPDATE table_that_needs_fixed
SET new_key = foo.new_key
WHERE candidate_key = foo.candidate_key;

Add appropriate constraints as necessary...

HTH,

Mike Mascari

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Lee Harr 2004-07-20 21:45:27 mail-news server down
Previous Message Joost Kraaijeveld 2004-07-20 20:31:54 SQL (?) question