Re: How to auto-increment? - Mailing list pgsql-general

From John R Pierce
Subject Re: How to auto-increment?
Date
Msg-id 4B16F395.5000405@hogranch.com
Whole thread Raw
In response to How to auto-increment?  (Andre Lopes <lopes80andre@gmail.com>)
List 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?



pgsql-general by date:

Previous
From: Greg Williamson
Date:
Subject: Re: How to auto-increment?
Next
From: Alban Hertroys
Date:
Subject: Re: How to get RTREE performance from GIST index?