Re: How to auto-increment?

From: John R Pierce <pierce(at)hogranch(dot)com>
To: Andre Lopes <lopes80andre(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: How to auto-increment?
Date: 2009-12-02 23:09:09
Message-ID: 4B16F395.5000405@hogranch.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Andre Lopes wrote:
> Hi,
>
> I have a table like this:
>
> id_product
> id_increment
>
> and I need to increment values in id_increment like this
>
> prod_1
> 1
>
> prod_1
> 2
>
> prod_1
> 3
>
> prod_2
> 1
>
> Wich is the best way to do this? Using a trigger? Where can I find
> examples of plpgsql doing this?

offhand, I'd have another table that has (id_product primary key,
next_increment integer), and use it to populate your id_increment
fields, bumping the next_increment each time you fetch it.

I assume its OK if there are missing increment values, like if you
delete a product/increment from your table, or if in the middle of
inserting a new one, there's a transaction rollback for some reason?

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Alban Hertroys 2009-12-02 23:21:23 Re: How to get RTREE performance from GIST index?
Previous Message Greg Williamson 2009-12-02 23:07:19 Re: How to auto-increment?