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

Re: Last ID Problem

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: operationsengineer1(at)yahoo(dot)com
Cc: Mitch Pirtle <mitch(dot)pirtle(at)gmail(dot)com>,pgsql-novice(at)postgresql(dot)org
Subject: Re: Last ID Problem
Date: 2005-02-09 17:38:28
Message-ID: 20050209173828.GA6212@winnie.fuhr.org (view raw or flat)
Thread:
Lists: pgsql-hackerspgsql-novice
On Wed, Feb 09, 2005 at 08:53:18AM -0800, operationsengineer1(at)yahoo(dot)com wrote:

> so, i should use nextval() if i use mitch's
> methodology of calling the id first then performing
> the insert.

Right.  You can call nextval() first and then explicitly insert the
value you obtained, or you can do the insert first and let the
serial column's default expression call nextval() automatically,
and you can then find out the value it used with a subsequent call
to currval().

> giving that nextval increments on a refresh, is there
> anything i need to worry about?

nextval() increments the sequence each time it's called.  If you're
using the sequence values as keys, then that's what you need.
Presumably you'd only call nextval() when you're going to insert a
new record, so what's the concern?

-- 
Michael Fuhr
http://www.fuhr.org/~mfuhr/

In response to

pgsql-novice by date

Next:From: Michael FuhrDate: 2005-02-09 17:51:35
Subject: Re: Returning a long string (varchar from a function)
Previous:From: Oisin GlynnDate: 2005-02-09 17:30:51
Subject: Returning a long string (varchar from a function)

pgsql-hackers by date

Next:From: Gavin M. RoyDate: 2005-02-09 19:41:19
Subject: Re: PHP/PDO Database Abstraction Layer
Previous:From: operationsengineer1Date: 2005-02-09 16:53:18
Subject: Re: Last ID Problem

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