Re: Help with INSERT into 2 tables

Поиск
Список
Период
Сортировка
От Roland Roberts
Тема Re: Help with INSERT into 2 tables
Дата
Msg-id m2g07ggf0j.fsf@tycho.rlent.pnet
обсуждение исходный текст
Ответ на Help with INSERT into 2 tables  (gntrs@hotmail.com (Gintas))
Список pgsql-sql
>>>>> "Gintas" == Gintas  <gntrs@hotmail.com> writes:
   Gintas> I am new to SQL and don't know how to INSERT records to 2 tables.   Gintas> The first table:
   Gintas>      CREATE TABLE aaaa ( aaaaid SERIAL PRIMARY KEY,   Gintas>                          text VARCHAR(20)
Gintas>                       );
 
   Gintas> Second table:
   Gintas>      CREATE TABLE bbbb ( bbbbid SERIAL PRIMARY KEY,   Gintas>                          aaaaid INTEGER
REFERENCESaaaa (aaaaid) ON   Gintas> DELETE CASCADE,   Gintas>                          sometext VARCHAR(200)   Gintas>
                      );
 
   Gintas> I want to insert related records to both table.  The   Gintas> problem is that in order to insert record to
thesecond   Gintas> table it's necessary to know "aaaaid" field from the first   Gintas> table("text" is not UNIQUE):
 
   Gintas> INSERT INTO aaaa (text) VALUES ('Some info');   Gintas> INSERT INTO bbbb (aaaaid, sometext) VALUES (?????,
'Sometext');
 
   Gintas> How is it possible to do that?  (I am inserting this from   Gintas> JAVA).

The normal way to do this is to explicitly pull out the serial value
from the underlying sequence.  I.e., something like this
   select nextval('aaaa_aaaaid_seq');   begin transaction;   insert into aaaa (aaaaid, text) values ($seqno, 'Some
info');  insert into bbbb (aaaaid, sometext) VALUES ($seqno, 'Some text');   end transaction;
 

where you use the jdbc calls to retrieve the result from the "select
nextval...".

SERIAL types are not quite as useful as people tend to think unless
you have the ability to identify the value from some other
characteristic.  If the ID is purely an ID and you don't care about
its value, you still have to have a way of *finding* it.

roland
--            PGP Key ID: 66 BC 3B CD
Roland B. Roberts, PhD                             RL Enterprises
roland@rlenter.com                     76-15 113th Street, Apt 3B
roland@astrofoto.org                       Forest Hills, NY 11375


В списке pgsql-sql по дате отправления:

Предыдущее
От: Roland Roberts
Дата:
Сообщение: Re: Is this a bug?
Следующее
От: Roland Roberts
Дата:
Сообщение: Re: INSERT question