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

nextval() clarification

From: Greg Donald <destiney(at)gmail(dot)com>
To: pgsql-novice <pgsql-novice(at)postgresql(dot)org>
Subject: nextval() clarification
Date: 2004-09-13 18:59:31
Message-ID: ea9da26c04091311591b7acff6@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-novice
Long time MySQL user, six weeks into Postgres here.

I'm converting a PHP script that uses MySQL to use Postgres instead. 
To substitue the missing MySQL functionaility with mysql_insert_id()
and auto_increment I am using code like this:

$sql = "SELECT nextval('companies_company_id_seq'::text)";

And then I do my insert with that value.

It works fine but I don't understand why it always gives me a +1 value
of what I'm expecting.  For example when I have an empty table it
gives me 2 as the first nextval instead of a 1, or if I have two rows
in there already it gives me a 4.  Is this by design or am I missing
something?  It's not a problem I just want to know what's going on.

TIA..

-- 
Greg Donald
http://destiney.com/

Responses

pgsql-novice by date

Next:From: Sandip BhattacharyaDate: 2004-09-13 19:08:58
Subject: Re: nextval() clarification
Previous:From: A GilmoreDate: 2004-09-13 16:30:54
Subject: Re: timestamp precision

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