How do I create unique IDs for an existing set of records

From: "Nick Fankhauser" <nickf(at)ontko(dot)com>
To: "PGSQL-SQL" <pgsql-sql(at)postgresql(dot)org>
Subject: How do I create unique IDs for an existing set of records
Date: 2002-04-26 15:58:16
Message-ID: NEBBLAAHGLEEPCGOBHDGOELNENAA.nickf@ontko.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi-

There's got to be a simple way to do this, but it's eluding me so far...

I have a table that is already populated with about 600,000 rows.

I have created a new unique id field for it. (alter table test add new_pk
int8 not null;)

I have also createtd a sequence (create sequence test_new_pk)

Now I want to populate the key for all of the existing records.

I tried:

update test set new_pk = (select nextval('test_new_pk_seq'));

but it wants to update *all* of the rows to the current nextval (I was
hoping it would evaluate nextval on each row).

How do I write an update to assign a sequential id for the existing records?

Any ideas appreciated!

-Nick

--------------------------------------------------------------------------
Nick Fankhauser nickf(at)ontko(dot)com Phone 1.765.935.4283 Fax 1.765.962.9788
Ray Ontko & Co. Software Consulting Services http://www.ontko.com/

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Robert Wynter 2002-04-26 16:04:12 union query format
Previous Message Josh Berkus 2002-04-26 15:42:34 Wierd error for COPY command