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

Re: Equivalent for AUTOINCREMENT?

From: "D(dot) Dante Lorenso" <dante(at)lorenso(dot)com>
To: Michelle Konzack <linux4michelle(at)tamay-dogan(dot)net>
Cc: pgSQL - General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Equivalent for AUTOINCREMENT?
Date: 2008-10-31 17:35:28
Message-ID: 490B41E0.9060700@lorenso.com (view raw or flat)
Thread:
Lists: pgsql-general
Michelle Konzack wrote:
> I have a table where I have a serialnumber which shuld be  increased  be
> each INSERT.  I know I can use max() to get the highest number, but  how
> can I use it in a INSERT statement?
> 
> There was a message for some month a message describing it on this  list
> but I do not find the message anymore...

You want to use 'SERIAL' or 'BIGSERIAL' data type.  Then, for your 
insert statement, use the 'RETURNING' clause like this:

CREATE TABLE mytable (
    mypkey SERIAL NOT NULL,
    somecol VARCHAR(200)
);

INSERT INTO mytable (somecol)
VALUES ('this is a test)
RETURNING mypkey;

You'll get your insert statement to return the integer value generated 
by the SERIAL sequence.

These features are way more powerful than AUTO_INCREMENT.

-- Dante


> 
> Thanks, Greetings and nice Day/Evening
>     Michelle Konzack
>     Systemadministrator
>     24V Electronic Engineer
>     Tamay Dogan Network
>     Debian GNU/Linux Consultant
> 
> 


-- 
----------
D. Dante Lorenso
dante(at)lorenso(dot)com

In response to

pgsql-general by date

Next:From: Chris BrowneDate: 2008-10-31 17:47:39
Subject: Re: Are there plans to add data compression feature to postgresql?
Previous:From: Kevin MurphyDate: 2008-10-31 17:34:20
Subject: Re: perl-DBD-Pg package for CentOS 5?

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