Re: SQL (?) question - Mailing list pgsql-general

From Mike Mascari
Subject Re: SQL (?) question
Date
Msg-id 40FD8D8C.6000306@mascari.com
Whole thread Raw
In response to SQL (?) question  ("Joost Kraaijeveld" <J.Kraaijeveld@Askesis.nl>)
List 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



pgsql-general by date:

Previous
From: "Joost Kraaijeveld"
Date:
Subject: SQL (?) question
Next
From: "Lee Harr"
Date:
Subject: mail-news server down