X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id AC15B55791 for ; Sat, 12 Mar 2005 23:48:41 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 03169-10 for ; Sat, 12 Mar 2005 23:48:32 +0000 (GMT) Received: from net2.micro-automation.com (net2.micro-automation.com [64.7.141.29]) by svr1.postgresql.org (Postfix) with SMTP id CD599564CA for ; Sat, 12 Mar 2005 23:48:30 +0000 (GMT) Received: (qmail 18764 invoked from network); 12 Mar 2005 23:47:31 -0000 Received: from dcdsl.ebox.com (HELO ?192.168.1.36?) (davec@64.7.143.116) by net2.micro-automation.com with SMTP; 12 Mar 2005 23:47:31 -0000 Message-ID: <42337FCB.3040401@fastcrypt.com> Date: Sat, 12 Mar 2005 18:48:27 -0500 From: Dave Cramer Reply-To: pg@fastcrypt.com Organization: Postgres International User-Agent: Mozilla Thunderbird 1.0 (X11/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Nikola Milutinovic Cc: pgsql-jdbc@postgresql.org Subject: Re: How to get automatically generated "id" field References: <42336C35.7080003@ev.co.yu> In-Reply-To: <42336C35.7080003@ev.co.yu> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.039 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200503/131 X-Sequence-Number: 12523 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