From: Robert B. Easter <reaster@comptechnews.com>
To: Lea, Michael <MLea@mpi.mb.ca>
To: 'pgsql-sql@postgresql.org' <pgsql-sql@postgresql.org>
Subject: Re: retrieving a serial number
Date: Wed, 28 Jun 2000 16:12:38 -0400
Message-ID: <00062816190002.00209@comptechnews> (raw)
In-Reply-To: <C0E5077572DCD211812B00A0C95A0A1F03A934F7@ntexch01>
References: <C0E5077572DCD211812B00A0C95A0A1F03A934F7@ntexch01>
You can do something like this:
$var = SELECT nextval('seq_name');
INSERT INTO master (id, ...) VALUES ($var, ...);
INSERT INTO slave1 (id, ...) VALUES ($var, ...);
...
You can do a \d master, in psql to find out the name of the sequence. For
the SERIAL type, nextval('seq_name') is just its default anyway.
On Wed, 28 Jun 2000, Lea, Michael wrote:
> I have one table (call it "master") that, among other things, contains a
> serial number. I have a number of other tables (call them "slaves") that,
> among other things, contain a foreign key referring to the serial number in
> the first table. I will be inserting one row into the master table, and one
> row into zero or more slave tables in each transaction.
>
> What I want to do is use a sequence in the master table to let PostgreSQL
> automatically generate a unique serial number for me, but I need to know the
> serial number in order to insert the corresponding rows into the slave
> table(s). Is there any way of retrieving the serial number that will be used
> before the transaction has been committed? Or will I have to generate my own
> serial numbers?
>
> Michael Lea
> Information Security
> Manitoba Public Insurance
> Phone: (204) 985-8224
--
Robert B. Easter
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-sql@postgresql.org
Cc: reaster@comptechnews.com, MLea@mpi.mb.ca
Subject: Re: retrieving a serial number
In-Reply-To: <00062816190002.00209@comptechnews>
References: <C0E5077572DCD211812B00A0C95A0A1F03A934F7@ntexch01> <00062816190002.00209@comptechnews>
* 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