Re: INSERT question

From: "Wilco Boschman" <w(dot)boschman(at)xs4all(dot)nl>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: INSERT question
Date: 2001-11-13 21:51:53
Message-ID: 3bf19679$0$189$e4fe514c@newszilla.xs4all.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Insert the row into the parent table (the one with the primary key), then do
a
select max(<serial-column>) from parent_table;
This will give you the greatest number in the table, that is if everything
went ok the number from the row you just inserted. Then insert the row(s)
into the second table and use the value you got from the parent table in the
foreign key

cheers

Wilco

"Brian" <Brian(at)McSweeney(dot)iol(dot)ie> schreef in bericht
news:1f5f2b44(dot)0111130130(dot)17e8e57a(at)posting(dot)google(dot)com(dot)(dot)(dot)
| Hi everyone,
| I've a little question about using insert statements. I've got a
| parent table with a "serial" (automatically incrementing integer)
| primary key. I have a child table with a foreign key which references
| that primary key. My question is:
|
| To insert values into the child table corresponding to an entry in the
| parent table, how do I get a reference to the serial primary key (so
| as I can reference it for the foreign key entry)
|
| Hope you understand what I mean. This should be a regular occurance
| and seeing as I'm not an sql guru, I just don't have a clue!
|
| Any help would be SOOO appreciated.
| Brian

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Marc Spitzer 2001-11-13 22:27:37 Re: how do I update or insert efficently in postgres
Previous Message Jason Earl 2001-11-13 21:46:08 Re: Full text search