Re: One sequence for different tables

From: Brian McCane <bmccane(at)mccons(dot)net>
To: <pgsql-admin(at)postgresql(dot)org>
Subject: Re: One sequence for different tables
Date: 2002-03-12 03:52:14
Message-ID: 20020311214915.H78800-100000@fw.mccons.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

On Mon, 11 Mar 2002, Stephan Szabo wrote:

You can add a trigger, BEFORE INSERT on each table to check the key in the
other table to prevent cross table duplications. I have done this in the
past, but I no longer do. It didn't work as well as I intended. I also
occasionally got duplicates anyway, which I could never explain. But that
was way back on a 6.? server I used to have.

- brian

> On Mon, 11 Mar 2002 Robert(dot)Farrugia(at)go(dot)com(dot)mt wrote:
>
> > Is it possible to create one sequence to be used for more than one table ?
> > Let's say I create a pkey_seq sequence.
> > Can I use it in table A (pkey default nextval('pkey_seq') PRIMARY KEY) and
> > in table B as (pkey default nextval('pkey_seq') PRIMARY KEY) ?
>
> Yes.
>
> > Is it safe in that each table will have unique values i.e. if I have a
> > third table C which is used to link data both from A and B using the pkey
> > field, will there be records having the same pkey but from different
> > tables?
>
> As long as you never insert an explicit value into the column and don't
> have to roll over the sequence, you should be safe.
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
> (send "unregister YourEmailAddressHere" to majordomo(at)postgresql(dot)org)
>

Wm. Brian McCane | Life is full of doors that won't open
Search http://recall.maxbaud.net/ | when you knock, equally spaced amid those
Usenet http://freenews.maxbaud.net/ | that open when you don't want them to.
Auction http://www.sellit-here.com/ | - Roger Zelazny "Blood of Amber"

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Jayaram Bhat 2002-03-12 04:24:13 can i use it on windows
Previous Message Marc G. Fournier 2002-03-12 02:32:14 Re: News server broken ?