serial type vs. sequences

Started by Suchandra Thapaover 22 years ago4 messagesgeneral
Jump to latest
#1Suchandra Thapa
s-thapa-11@alumni.uchicago.edu

I was wondering if there was a reliable method to get the last id
inserted into a serial column in a table. I believe previous
recommendations given were to use a sequence, have your app get the
nextval for the sequence and then use that in the insert. I was
wondering if things had changed or if that is still the recommended
method.

--
Suchandra Thapa <s-thapa-11@alumni.uchicago.edu>

#2Bruno Wolff III
bruno@wolff.to
In reply to: Suchandra Thapa (#1)
Re: serial type vs. sequences

On Wed, Nov 12, 2003 at 14:59:33 -0600,
Suchandra Thapa <s-thapa-11@alumni.uchicago.edu> wrote:

I was wondering if there was a reliable method to get the last id
inserted into a serial column in a table. I believe previous
recommendations given were to use a sequence, have your app get the
nextval for the sequence and then use that in the insert. I was
wondering if things had changed or if that is still the recommended
method.

It is probably a little better to use currval rather than remembering
what nextval returned in your application.

#3Joshua D. Drake
jd@commandprompt.com
In reply to: Suchandra Thapa (#1)
Re: serial type vs. sequences

You are accurate.

Suchandra Thapa wrote:

I was wondering if there was a reliable method to get the last id
inserted into a serial column in a table. I believe previous
recommendations given were to use a sequence, have your app get the
nextval for the sequence and then use that in the insert. I was
wondering if things had changed or if that is still the recommended
method.

-- 
Command Prompt, Inc., home of Mammoth PostgreSQL - S/ODBC and S/JDBC
Postgresql support, programming shared hosting and dedicated hosting.
+1-503-222-2783 - jd@commandprompt.com - http://www.commandprompt.com
Editor-N-Chief - PostgreSQl.Org - http://www.postgresql.org
#4Jan Wieck
JanWieck@Yahoo.com
In reply to: Joshua D. Drake (#3)
Re: serial type vs. sequences

Joshua D. Drake wrote:

You are accurate.

Suchandra Thapa wrote:

I was wondering if there was a reliable method to get the last id
inserted into a serial column in a table. I believe previous
recommendations given were to use a sequence, have your app get the
nextval for the sequence and then use that in the insert. I was
wondering if things had changed or if that is still the recommended
method.

If you don't need the value itself in your application, it is safe to
use currval('seqname') in queries following the use of
nextval('seqname'). currval() returns the last value assigned in your
session and is not affected by other concurrent transactions.

Jan

--
#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me. #
#================================================== JanWieck@Yahoo.com #