Skip site navigation (1) Skip section navigation (2)

Re: Serial key

From: "Chris Smith" <cdsmith(at)twu(dot)net>
To: "Bhushan Bhangale" <bbhangale(at)Lastminute(dot)com>,<pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: Serial key
Date: 2004-02-13 17:46:05
Message-ID: 007c01c3f259$41d22c90$6f00000a@KYA (view raw, whole thread or download thread mbox)
Lists: pgsql-jdbc
Bhushan Bhangale wrote:
> I have a table in which the primary key is of type SERIAL. I insert a
> record and then I want the generated id because of this, so that I
> can use the id to insert records in child table.
> I know about a solution in which generate a id before hand and then
> use the id to insert in table and also in child tables.

IMO, that's definitely the best way to go.

> The other stuff which I read in JDK 1.4 is of getGeneratedKeys().
> The problem is I have to use JDK 1.3.1.
> Is there any solution for this problem?

Incidentally, using 1.4 wouldn't help you because the PostgreSQL JDBC driver
doesn't implement getGeneratedKeys anyway.  The only other approach is to use
some unique set of columns in the table other than your numeric key, and
immediately follow your insert statement with a select.  This only works if
your records are unique without regard to the generated key.  As an example,
you'd do:

    insert into mytable (name, department, position, hiredate)
        values('jdoe', 'eng', 'manager', DATE '10-12-1994');

    select empid from mytable where
        name = 'jdoe' AND
        department = 'eng' AND
        position = 'manager' AND
        hiredate = DATE '10-12-1994';

The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

In response to

  • Serial key at 2004-02-13 17:23:03 from Bhushan Bhangale


pgsql-jdbc by date

Next:From: Chris SmithDate: 2004-02-13 17:46:57
Subject: Startup message issues
Previous:From: Chris SmithDate: 2004-02-13 17:27:43
Subject: Startup messages

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group