Re: renumber table - Mailing list pgsql-general

From David Wilson
Subject Re: renumber table
Date
Msg-id e7f9235d0806191347x35d991ceoa887a71bfba6c9f5@mail.gmail.com
Whole thread Raw
In response to Re: renumber table  (Steve Clark <sclark@netwolves.com>)
List pgsql-general
On Thu, Jun 19, 2008 at 7:54 AM, Steve Clark <sclark@netwolves.com> wrote:

> I am not sure that will do what I want. As an example
> suppose I have 5 rows and the idfield is 1,2,3,4,5
> now row 1 is updated, not the idfield but another column, then row 3 is
> deleted.
> Now I would like to renumber  them such that 1 is 1, 2 is 2, 4 is 4 , 5 is
> 4.
>
> I don't think what you wrote will necessarily keep them in the same relative
> order that they
> were before.

It's ugly and a hack, but if you could do:

1. rename the table
2. create a new copy of the table. give the idfield a sequence.
3. select all the records from the renamed table, ordering by idfield,
and insert all fields other than idfield into the new table.
4. drop the renamed table

You'll maintain ordering and have a nice sequential idfield.
--
- David T. Wilson
david.t.wilson@gmail.com

pgsql-general by date:

Previous
From: "Scott Marlowe"
Date:
Subject: Re: finding firstname + lastname groups
Next
From: Charles Simard
Date:
Subject: FW: finding firstname + lastname groups