Re: serial nextval() question

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Jean-Christian Imbeault <jc(at)mega-bucks(dot)co(dot)jp>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: serial nextval() question
Date: 2002-08-16 12:48:31
Message-ID: 20020816124831.GB14620@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Aug 16, 2002 at 18:11:45 +0900,
Jean-Christian Imbeault <jc(at)mega-bucks(dot)co(dot)jp> wrote:
>
> I'd like to have li_id be an serial but not for the whole table, only as
> li_id relates to cart_id ... i.e. I'd like li_id to increment relative
> to cart_id so that I can generate sequences of the kind:
>
> cart_id li_id
> 1 1
> 1 2
> 1 3
> 2 1
> 2
> 2
>
> How could I do this?

It doesn't sound like you want to use serial for this application.
serial gives you unique monotonicly increasing values. It doesn't
guarenty that there won't be gaps. You would need to have a separate
sequence for each value of cart_id, which isn't a good idea.

You can implement something like the above using sequel by using max or
count to figure out the next number. (Just remember to do select for update
when doing the calculation.) You can use a contraint to make sure
li_id, cart_id pairs are unique.

This is slower than using serials, but that problem can usually be solved
with hardware.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Jean-Christian Imbeault 2002-08-16 13:35:54 Good PL/pgSQL ressources>
Previous Message Moritz Sinn 2002-08-16 12:24:06 Re: writing own cast