pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Roland Roberts <roland@astrofoto.org>
To: pgsql-sql@postgresql.org
Subject: Re: Help with INSERT into 2 tables
Date: 15 Nov 2001 11:44:44 -0500
Message-ID: <m2g07ggf0j.fsf@tycho.rlent.pnet> (raw)
In-Reply-To: <558f8e45.0111130542.1e617f99@posting.google.com>
References: <558f8e45.0111130542.1e617f99@posting.google.com>

>>>>> "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 REFERENCES aaaa (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 the second
    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 (?????, 'Some text');

    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



view thread (6+ messages)  latest in thread

Message-ID: <m2g07ggf0j.fsf@tycho.rlent.pnet>
Permalink:  ../m2g07ggf0j.fsf@tycho.rlent.pnet/
Also on:    postgresql.org/message-id/m2g07ggf0j.fsf@tycho.rlent.pnet

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: roland@astrofoto.org
  Subject: Re: Help with INSERT into 2 tables
  In-Reply-To: <m2g07ggf0j.fsf@tycho.rlent.pnet>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox