Re: sequence

From: "Scott Marlowe" <scott(dot)marlowe(at)gmail(dot)com>
To: "Alain Roger" <raf(dot)news(at)gmail(dot)com>
Cc: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-general(at)postgresql(dot)org
Subject: Re: sequence
Date: 2007-12-09 16:04:38
Message-ID: dcc563d10712090804r78543a9ci74b6da563d07d2e7@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Dec 9, 2007 9:56 AM, Alain Roger <raf(dot)news(at)gmail(dot)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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alain Roger 2007-12-09 16:05:15 insert into...
Previous Message Alain Roger 2007-12-09 15:56:56 Re: sequence