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

Re: currval, lastval, nextvar?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: currval, lastval, nextvar?
Date: 2009-04-23 16:07:08
Message-ID: 28852.1240502828@sss.pgh.pa.us (view raw or flat)
Thread:
Lists: pgsql-novice
"A. Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com> writes:
> Yes. You don't need call nextval.

Well, you do, but the point is that the default expression for a
serial column includes the nextval() call.  Look at the table with
\d, eg

postgres=# create table t1(id serial primary key);
NOTICE:  CREATE TABLE will create implicit sequence "t1_id_seq" for serial column "t1.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "t1_pkey" for table "t1"
CREATE TABLE
postgres=# \d t1
                         Table "public.t1"
 Column |  Type   |                    Modifiers                    
--------+---------+-------------------------------------------------
 id     | integer | not null default nextval('t1_id_seq'::regclass)
Indexes:
    "t1_pkey" PRIMARY KEY, btree (id)

postgres=# 

So if you do an insert that doesn't provide a value for that column,
the nextval is done implicitly.

			regards, tom lane

In response to

Responses

pgsql-novice by date

Next:From: Andreas KretschmerDate: 2009-04-23 16:17:39
Subject: Re: currval, lastval, nextvar?
Previous:From: A. KretschmerDate: 2009-04-23 15:59:52
Subject: Re: currval, lastval, nextvar?

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