Skip site navigation (1) Skip section navigation (2)

Re: Make an id field max(id)+1 rather than SERIAL

From: Rory Campbell-Lange <rory(at)campbell-lange(dot)net>
To: Oliver Elphick <olly(at)lfix(dot)co(dot)uk>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Make an id field max(id)+1 rather than SERIAL
Date: 2002-09-19 17:09:35
Message-ID: 20020919170935.GA7462@campbell-lange.net (view raw or flat)
Thread:
Lists: pgsql-novice
On 19/09/02, Oliver Elphick (olly(at)lfix(dot)co(dot)uk) wrote:
> On Thu, 2002-09-19 at 15:14, Rory Campbell-Lange wrote:
> > I have problems after exporting then importing data into newly created
> > databases that the sequence and the indexed serial fields get out of
> > alignment.
> > 
> > I wonder if it isn't better to always simply insert ids using some sort
> > of function or other procedure to calculate on each insert a new unique
> > id number.
> 
> After you import data, you must also reset the sequence:
> 
>   SELECT setval('sequence_name',
>         (SELECT MAX(sequence_column) FROM table));

Excellent stuff. Thanks.
Rory
-- 
Rory Campbell-Lange 
<rory(at)campbell-lange(dot)net>
<www.campbell-lange.net>

In response to

Responses

pgsql-novice by date

Next:From: Thilo HilleDate: 2002-09-20 11:35:11
Subject: table corruption... :(
Previous:From: Oliver ElphickDate: 2002-09-19 16:59:43
Subject: Re: Make an id field max(id)+1 rather than SERIAL

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group