pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedretrieving a serial number
4+ messages / 4 participants
[nested] [flat]
* retrieving a serial number
@ 2000-06-28 17:35 Lea, Michael <MLea@mpi.mb.ca>
2000-06-28 20:12 ` Re: retrieving a serial number Robert B. Easter <reaster@comptechnews.com>
0 siblings, 1 reply; 4+ messages in thread
From: Lea, Michael @ 2000-06-28 17:35 UTC (permalink / raw)
To: pgsql-sql
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
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: retrieving a serial number
2000-06-28 17:35 retrieving a serial number Lea, Michael <MLea@mpi.mb.ca>
@ 2000-06-28 20:12 ` Robert B. Easter <reaster@comptechnews.com>
2000-07-02 12:28 ` Re: retrieving a serial number Joachim Trinkwitz <jtr@uni-bonn.de>
0 siblings, 1 reply; 4+ messages in thread
From: Robert B. Easter @ 2000-06-28 20:12 UTC (permalink / raw)
To: Lea, Michael <MLea@mpi.mb.ca>; pgsql-sql
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
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: retrieving a serial number
2000-06-28 17:35 retrieving a serial number Lea, Michael <MLea@mpi.mb.ca>
2000-06-28 20:12 ` Re: retrieving a serial number Robert B. Easter <reaster@comptechnews.com>
@ 2000-07-02 12:28 ` Joachim Trinkwitz <jtr@uni-bonn.de>
0 siblings, 0 replies; 4+ messages in thread
From: Joachim Trinkwitz @ 2000-07-02 12:28 UTC (permalink / raw)
To: pgsql-sql
"Robert B. Easter" <reaster@comptechnews.com> writes:
> You can do something like this:
> $var = SELECT nextval('seq_name');
> INSERT INTO master (id, ...) VALUES ($var, ...);
> INSERT INTO slave1 (id, ...) VALUES ($var, ...);
Great feaure. What to do when importing some previous data from ascii
files -- can you automatically give a serial number in this way to?
Thanks in advance,
joachim
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: retrieving a serial number
@ 2000-06-28 18:13 Philip Hallstrom <philip@adhesivemedia.com>
0 siblings, 0 replies; 4+ messages in thread
From: philip@adhesivemedia.com @ 2000-06-28 18:13 UTC (permalink / raw)
To: pgsql-sql
There's probably a way to get that serial number.
I think I remember reading that when you specify it as SERIAL,
postgresql actually creates a separate sequence. If that's the case
then you could do "SELECT currval('mysequence')" -- although I'm not
sure what "mysequence" should be set to.
The other way to do it would be to remove the SERIAL attribute from the
table, making it an INT4 (or whatever) and then create a separate
sequence...so...
SELECT nextval('mysequence');
insert into master... (using the value returned from above)
insert into slave... (using the value returned from above)
insert into slave... (using the value returned from above)
insert into slave... (using the value returned from above)
insert into slave... (using the value returned from above)
would work just fine.
In article <am.pgsql.sql.962213794.3300@illiad.adhesivemedia.com>,
Lea, Michael <MLea@mpi.mb.ca> 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
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2000-07-02 12:28 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2000-06-28 17:35 retrieving a serial number Lea, Michael <MLea@mpi.mb.ca>
2000-06-28 20:12 ` Robert B. Easter <reaster@comptechnews.com>
2000-07-02 12:28 ` Joachim Trinkwitz <jtr@uni-bonn.de>
2000-06-28 18:13 Re: retrieving a serial number Philip Hallstrom <philip@adhesivemedia.com>
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