Re: SERIAL Field - Mailing list pgsql-admin

From Joel Burton
Subject Re: SERIAL Field
Date
Msg-id JGEPJNMCKODMDHGOBKDNEEPCCMAA.joel@joelburton.com
Whole thread Raw
In response to SERIAL Field  ("Gaetano Mendola" <mendola@bigfoot.com>)
List pgsql-admin
> Hi all,
>
> having a table with a serial field what is the corrected method
> to retrieve the value after an insert ?

Use currval(_name_of_sequence_):

joel=# create table test ( id serial );
NOTICE:  CREATE TABLE will create implicit sequence 'test_id_seq' for SERIAL
column 'test.id'
NOTICE:  CREATE TABLE / UNIQUE will create implicit index 'test_id_key' for
table 'test'
joel=# insert into test values (default);
INSERT 16617 1
joel=# select currval('test_id_seq');
 currval
---------
       1
(1 row)

See also nextval() and setval() for other functions for sequences.


Joel BURTON | joel@joelburton.com | joelburton.com | aim: wjoelburton
Knowledge Management & Technology Consultant


pgsql-admin by date:

Previous
From: "Mark McEahern"
Date:
Subject: configure datatype name > 31?
Next
From: "Rajesh Kumar Mallah."
Date:
Subject: Re: SERIAL Field