pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feedHow to get automatically generated "id" field
2+ messages / 2 participants
[nested] [flat]
* How to get automatically generated "id" field
@ 2005-03-12 22:24 Nikola Milutinovic <Nikola.Milutinovic@ev.co.yu>
0 siblings, 1 reply; 2+ messages in thread
From: Nikola Milutinovic @ 2005-03-12 22:24 UTC (permalink / raw)
To: pgsql-jdbc
Hi all.
This may be a bit more for general ML, but I'm in a fix and need an
advice. I have several tables with auto-generated "id" field, like this.
CREATE TABLE photo (
id SERIAL PRIMARY KEY,
size INT8,
file VARCHAR(256)
)
I would like to be able to insert values and get back the "id" of a
newly inserted record. What is a good way to do it?
I imagine I could open a transaction and read the value of the sequence,
but that is ugly, since the sequence name is autogenerated and PG specific.
Would it be better to change "SERIAL" to just INT8 and do in a transaction:
...start transaction
executeQuery( "SELECT max( id )+1 AS new_id FROM photo" );
..read "new_id"
executeUpdate( "INSERT INTO photo (id, size, file) VALUES (...)" );
..commit
Nix.
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: How to get automatically generated "id" field
@ 2005-03-12 23:48 Dave Cramer <pg@fastcrypt.com>
parent: Nikola Milutinovic <Nikola.Milutinovic@ev.co.yu>
0 siblings, 0 replies; 2+ messages in thread
From: Dave Cramer @ 2005-03-12 23:48 UTC (permalink / raw)
To: Nikola Milutinovic <Nikola.Milutinovic@ev.co.yu>; +Cc: pgsql-jdbc
Nikola,
While the below works it will be very slow after you have many id's. The
correct way to do this is to use either nextval('sequence_name') before
the insert and then
insert this value, or use currval('sequence_name') after the insert. You
don't have to do this in a transaction. If you use currval you need to
make sure it is the same connection.
yes, this is postgres specific, but it is the way to do it.
Dave
Nikola Milutinovic wrote:
> Hi all.
>
> This may be a bit more for general ML, but I'm in a fix and need an
> advice. I have several tables with auto-generated "id" field, like this.
>
> CREATE TABLE photo (
> id SERIAL PRIMARY KEY,
> size INT8,
> file VARCHAR(256)
> )
>
> I would like to be able to insert values and get back the "id" of a
> newly inserted record. What is a good way to do it?
>
> I imagine I could open a transaction and read the value of the
> sequence, but that is ugly, since the sequence name is autogenerated
> and PG specific.
>
> Would it be better to change "SERIAL" to just INT8 and do in a
> transaction:
>
> ...start transaction
> executeQuery( "SELECT max( id )+1 AS new_id FROM photo" );
> ..read "new_id"
> executeUpdate( "INSERT INTO photo (id, size, file) VALUES (...)" );
> ..commit
>
> Nix.
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo@postgresql.org so that your
> message can get through to the mailing list cleanly
>
>
--
Dave Cramer
http://www.postgresintl.com
519 939 0336
ICQ#14675561
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2005-03-12 23:48 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-03-12 22:24 How to get automatically generated "id" field Nikola Milutinovic <Nikola.Milutinovic@ev.co.yu>
2005-03-12 23:48 ` Dave Cramer <pg@fastcrypt.com>
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