Re: INSERT question

Поиск
Список
Период
Сортировка
От Roland Roberts
Тема Re: INSERT question
Дата
Msg-id m2snbgggjx.fsf@tycho.rlent.pnet
обсуждение исходный текст
Ответ на INSERT question  (Brian@McSweeney.iol.ie (Brian))
Список pgsql-sql
>>>>> "Brian" == Brian  <Brian@McSweeney.iol.ie> writes:
   Brian> To insert values into the child table corresponding to an   Brian> entry in the parent table, how do I get a
referenceto the   Brian> serial primary key (so as I can reference it for the   Brian> foreign key entry)
 

To do this in a transaction-safe manner you need to explicitly select
the "parent" row and get it's primary key.  That, of course, can be
tricky.

The way I've usually seen this done is to explicitly pull a value from
the sequence and use it for both inserts, e.g.,
   select nextval('sequence_name');   insert into foo values ($seqno, ...);   insert into bar values ($seqno, ...);

The above is sketchy because I don't know how you are accessing the
database.  If you are using Perl or Tcl or somesuch, you will need to
save the value from the select and use in the inserts.
   Brian> Hope you understand what I mean. This should be a regular   Brian> occurance and seeing as I'm not an sql
guru,I just don't   Brian> have a clue!
 

One question here: when you say "parent" and "child" are you referring
to derived tables or are you just trying to describe the foreign key
constraint?  PostgreSQL has "child" tables in the sense of
   create table foo ( a serial );   create table bar ( b varchar(32) ) inherits (foo);

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: Help with INSERT into 2 tables
Следующее
От: Roland Roberts
Дата:
Сообщение: Re: problem: index on number not honoured