Received: from draco.over-yonder.net ([63.147.13.14]) by mail.postgresql.org (8.11.1/8.11.1) with ESMTP id f0C3PRp78138 for ; Thu, 11 Jan 2001 22:25:27 -0500 (EST) (envelope-from gh@over-yonder.net) Received: by draco.over-yonder.net (Postfix, from userid 1012) id EF1A462D06; Thu, 11 Jan 2001 21:25:23 -0600 (CST) Date: Thu, 11 Jan 2001 21:25:23 -0600 From: GH To: svanegmond@home.com Cc: "Julio Cuz, Jr." , pgsql-php@postgresql.org Subject: Re: Automatic increment Message-ID: <20010111212523.A76845@over-yonder.net> References: <4.3.2.7.2.20010111190805.00b4b6a0@rccd.cc.ca.us> <20010111222205.A31717@bang.dhs.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: <20010111222205.A31717@bang.dhs.org>; from svanegmond@bang.dhs.org on Thu, Jan 11, 2001 at 10:22:05PM -0500 X-OS: FreeBSD X-Archive-Number: 200101/45 X-Sequence-Number: 110 On Thu, Jan 11, 2001 at 10:22:05PM -0500, some SMTP stream spewed forth: > Julio Cuz, Jr. (jcuz@rccd.cc.ca.us) wrote: > > Some of you guys suggested to use the 'SERIAL' type for a field that needs > > to be AUTOMATICALLY INCREASED BY 1 by PGSQL without user intervention, but > > it doesn't seem to work. What am I doing wrong? > > Though I wasn't here for that, here's how I normally declare > automatically-incrementing values: > > CREATE SEQUENCE foo_id; > > CREATE TABLE foo ( > foo_id integer not null primary key default nextval(foo_id); > > ... > ); > > insertions to foo get their own unique ID. With the php extension, you > can get the oid of the row that was inserted and go get the ID if you > happen to need it. I believe that it is commonly preferred to do: select nextval('sequence'); insert into foo (foo_id,...) values (, ...); That is, select the nextval() first, then use that value in the insert. This avoids numerous issues (like searching through the table, even by oid) which would be covered in the archives of this list. gh >