Skip site navigation (1) Skip section navigation (2)

Re: retrieving a serial number

From: philip(at)adhesivemedia(dot)com (Philip Hallstrom)
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: retrieving a serial number
Date: 2000-06-28 18:13:32
Message-ID: 200006281830.LAA04059@illiad.adhesivemedia.com (view raw or flat)
Thread:
Lists: 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(dot)pgsql(dot)sql(dot)962213794(dot)3300(at)illiad(dot)adhesivemedia(dot)com>,
Lea, Michael <MLea(at)mpi(dot)mb(dot)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


pgsql-sql by date

Next:From: Skeets and Kim NorquistDate: 2000-06-28 18:27:53
Subject: plpgsql function gets wierd with Null parameters
Previous:From: Tom LaneDate: 2000-06-28 18:12:04
Subject: Re: Procedures cannot take more than 16 arguments

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group