Re: Equivalent for AUTOINCREMENT?

From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Michelle Konzack <linux4michelle(at)tamay-dogan(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Equivalent for AUTOINCREMENT?
Date: 2008-11-06 18:55:40
Message-ID: 200811061855.mA6Ite916848@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Michelle Konzack wrote:
-- Start of PGP signed section.
> Hello Michael,
>
> Am 2008-10-31 11:15:54, schrieb Michael Hall:
> > > 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?
> >
> > Have a look in the manual for the SERIAL data type.
> > For fields with a SERIAL data type, you can use DEFAULT in an INSERT statement.
>
> OK, the SERIAL is now working, but there is ONE problem.
>
> If the INSERT fails, the SERIAL is in any case increased...
>
> So this was the reason, why I have tried to use max(serno) and add +1
> the get the next value for "serno".
>
> Is there a solution for it?

You should read our three FAQ entries about sequences:

http://www.postgresql.org/docs/faqs.FAQ.html#item4.11.1

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Michelle Konzack 2008-11-06 19:42:49 Re: Equivalent for AUTOINCREMENT?
Previous Message Martin Gainty 2008-11-06 18:02:14 RE: [GENERAL] Ayuda