pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Henry House <hajhouse@houseag.com>
To: Dado Feigenblatt <dado@wildbrain.com>
Cc: Pgsql-Sql <pgsql-sql@postgresql.org>
Subject: Re: nextval on insert by arbitrary sequence
Date: Fri, 20 Jul 2001 00:40:41 -0700
Message-ID: <20010720004041.A5624@houseag.com> (raw)
In-Reply-To: <3B5794B0.8060201@wildbrain.com>
References: <3B5794B0.8060201@wildbrain.com>

On Thu, Jul 19, 2001 at 07:17:20PM -0700, Dado Feigenblatt wrote:
> I'm not sure if I worded the subject right, but my problem is this:
> 
> I have a few entries in one table. Each row is the parent of many 
> entries in a second table.
> In the second table I have a lot of entries referencing the entries on 
> the first table.
> So far so good. Basic foreign key thing.
> The entries on the second table need to be numbered, but instead of a 
> single sequence for all rows,
> I need a sequence per group of rows, according to their parent record.
> 
> I have some ideas but they are not coming together.
> I thought of creating individual sequences for each new parent row, and 
> maybe store its name in the parent row itself, so it can be accessed by 
> it's children row.
> But then, how do I get a field on the second table to DEFAULT to 
> nextval() on it's parent's sequence?

I have done this manally, by creating a sequence and the grabbing a unique
number from the sequence using 'SELECT nextval('seq_name'). I then use this
number in all the INSERTs.

Even though it takes one extra query, it is a simple approach and PostgreSQL
guarantees that the sequence will return unique, sequential numbers as long
as it is used consistently.

-- 
Henry House
OpenPGP key available from http://romana.hajhouse.org/hajhouse.asc

view thread (9+ messages)  latest in thread

Message-ID: <20010720004041.A5624@houseag.com>
Permalink:  ../20010720004041.A5624@houseag.com/
Also on:    postgresql.org/message-id/20010720004041.A5624@houseag.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: hajhouse@houseag.com, dado@wildbrain.com
  Subject: Re: nextval on insert by arbitrary sequence
  In-Reply-To: <20010720004041.A5624@houseag.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox