Re: Help with INSERT into 2 tables

From: Jeff Eckermann <jeff_eckermann(at)yahoo(dot)com>
To: Gintas <gntrs(at)hotmail(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Help with INSERT into 2 tables
Date: 2001-11-15 15:47:17
Message-ID: 20011115154717.89887.qmail@web20809.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

You will need to:
1. Insert into aaaa
2. Get value of aaaaid for inserted record
3. Include aaaaid value explicitly in your insert to
bbbb.

The only question is how to do 2 above. AFAIK doing a
separate command "SELECT
currval(aaaaid_sequence_name)" should work.
Alternatively, if the oid of the last inserted record
is returned to your Java program (sorry, I don't know
anything about the JDBC driver functionality, so don't
know whether that is so or not), then you can do
"SELECT aaaaid FROM aaaa WHERE oid = <oid of inserted
record>": which would probably be safer.
--- Gintas <gntrs(at)hotmail(dot)com> wrote:
> I am new to SQL and don't know how to INSERT records
> to 2 tables.
>
> The first table:
>
> CREATE TABLE aaaa ( aaaaid SERIAL PRIMARY KEY,
> text VARCHAR(20)
> );
>
> Second table:
>
> CREATE TABLE bbbb ( bbbbid SERIAL PRIMARY KEY,
> aaaaid INTEGER REFERENCES
> aaaa (aaaaid) ON
> DELETE CASCADE,
> sometext VARCHAR(200)
> );
>
> I want to insert related records to both table. The
> problem is that
> in order to insert record to the second table it's
> necessary to know
> "aaaaid" field from the first table("text" is not
> UNIQUE):
>
> INSERT INTO aaaa (text) VALUES ('Some info');
> INSERT INTO bbbb (aaaaid, sometext) VALUES (?????,
> 'Some text');
>
> How is it possible to do that?
> (I am inserting this from JAVA).
>
> Thanks for help.
>
>
>
> Gintaras
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to
majordomo(at)postgresql(dot)org

__________________________________________________
Do You Yahoo!?
Find the one for you at Yahoo! Personals
http://personals.yahoo.com

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Jeff Eckermann 2001-11-15 15:48:43 Re: Insert values from one existing table into a new table.
Previous Message Stephan Szabo 2001-11-15 15:11:04 Re: Help with INSERT into 2 tables