Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 4AB181337B2F for ; Mon, 6 Dec 2010 11:27:33 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 05918-07 for ; Mon, 6 Dec 2010 15:27:26 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtp.iq.pl (smtp.iq.pl [86.111.241.19]) by mail.postgresql.org (Postfix) with ESMTP id D348F133655C for ; Mon, 6 Dec 2010 11:27:24 -0400 (AST) Received: (qmail 29840 invoked from network); 6 Dec 2010 15:27:25 -0000 Received: from unknown (HELO thebe.iq.pl) ([86.111.241.3]) (envelope-sender ) by iq.pl with SMTP for ; 6 Dec 2010 15:27:25 -0000 Received: from 104-219.echostar.pl (104-219.echostar.pl [213.156.104.219]) by horde.iq.pl (Horde MIME library) with HTTP for ; Mon, 06 Dec 2010 16:27:25 +0100 Message-ID: <20101206162725.o3q9xfug2ioo08g8@horde.iq.pl> Date: Mon, 06 Dec 2010 16:27:25 +0100 From: mike@if-then-else.pl To: grandebuzon@gmail.com Cc: pgsql-sql@postgresql.org Subject: Re: The best option to insert data with primary id References: In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-2; format="flowed" Content-Disposition: inline Content-Transfer-Encoding: quoted-printable User-Agent: IQ WebMail 2.0 X-Originating-IP: 213.156.104.219 X-Remote-Browser: Mozilla/5.0 (X11; U; Linux x86_64; en-GB; rv:1.9.2.12) Gecko/20101027 Ubuntu/10.04 (lucid) Firefox/3.6.12 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.2 tagged_above=-5 required=5 tests=BAYES_05=-0.5, RCVD_IN_DNSWL_LOW=-0.7 X-Spam-Level: X-Archive-Number: 201012/31 X-Sequence-Number: 34867 Quoting - : > I have a question about how best to insert and manipulate the table with > primary key id for better productivity. I need to insert data into the ta= ble > and get last id. > > 1. First option to take counter Postgres SEQUENCE: > INSERT INTO table (id, ...) VALUES ((SELECT nextval ('seq_table')), ...) > RETURNING (SELECT currval ('seq_table')) AS id Much simplier: INSERT INTO table (name, email, salary) VALUES ('joe', 'joe@example.com', 100) RETURNING id; Cheers, -Mike -- Micha=B3 Roszka mike@if-then-else.pl