pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Mike Mascari <mascarm@mascari.com>
To: Joost Kraaijeveld <J.Kraaijeveld@Askesis.nl>
Cc: pgsql-general@postgresql.org
Subject: Re: SQL  (?) question
Date: Tue, 20 Jul 2004 17:24:28 -0400
Message-ID: <40FD8D8C.6000306@mascari.com> (raw)
In-Reply-To: <A3D1526C98B7C1409A687E0943EAC41015E6AD@obelix.askesis.nl>
References: <A3D1526C98B7C1409A687E0943EAC41015E6AD@obelix.askesis.nl>

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





view thread (71+ messages)  latest in thread

Message-ID: <40FD8D8C.6000306@mascari.com>
Permalink:  ../40FD8D8C.6000306@mascari.com/
Also on:    postgresql.org/message-id/40FD8D8C.6000306@mascari.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: mascarm@mascari.com, J.Kraaijeveld@Askesis.nl
  Subject: Re: SQL  (?) question
  In-Reply-To: <40FD8D8C.6000306@mascari.com>
  References: <A3D1526C98B7C1409A687E0943EAC41015E6AD@obelix.askesis.nl> <40FD8D8C.6000306@mascari.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox