Re: sequence - Mailing list pgsql-general

From Scott Marlowe
Subject Re: sequence
Date
Msg-id dcc563d10712090804r78543a9ci74b6da563d07d2e7@mail.gmail.com
Whole thread Raw
In response to Re: sequence  ("Alain Roger" <raf.news@gmail.com>)
Responses Re: sequence
List pgsql-general
On Dec 9, 2007 9:56 AM, Alain Roger <raf.news@gmail.com> wrote:
> Hi Tom,
>
> but when i let pgsql setup everything (i mean when i create table -> pgsql
> creates sequence)
> ), i have called = no, before using any select nextval()...
> and in this case, it works great.
>
> but once called = yes, select nextval(sequence_name); always gives me
> current value +1 :-(

The whole point of the serial type is that you don't have to call
nextval yourself.

smarlowe=# create table test (i serial primary key, info text);
NOTICE:  CREATE TABLE will create implicit sequence "test_i_seq" for
serial column "test.i"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index
"test_pkey" for table "test"
CREATE TABLE
smarlowe=# insert into test (info) values ('this is a row');
INSERT 0 1
smarlowe=# select * from test;
 i |     info
---+---------------
 1 | this is a row

If you need the current value to insert into another table, you use currval:

smarlowe=# select currval('test_i_seq');
 currval
---------
       1

pgsql-general by date:

Previous
From: "Alain Roger"
Date:
Subject: Re: sequence
Next
From: "Alain Roger"
Date:
Subject: insert into...