Received: from bologna.nettuno.it (bologna.nettuno.it [193.43.2.1]) by hub.org (8.9.3/8.9.3) with ESMTP id JAA78125 for ; Wed, 9 Jun 1999 09:17:52 -0400 (EDT) (envelope-from jose@sferacarta.com) Received: from proxy.sferacarta.com (mail@sfcabop1.nettuno.it [193.207.10.213]) by bologna.nettuno.it (8.8.6/8.8.8/NETTuno 3.2) with ESMTP id PAA04167; Wed, 9 Jun 1999 15:17:37 +0200 (MDT) Received: from rosso.sferacarta.com (sferacarta.com) [10.20.30.5] by proxy.sferacarta.com with esmtp (Exim 2.05 #1 (Debian)) id 10rk7V-00073y-00; Wed, 9 Jun 1999 15:18:25 +0000 Message-ID: <375E6904.12A6A039@sferacarta.com> Date: Wed, 09 Jun 1999 15:15:48 +0200 From: =?iso-8859-1?Q?Jos=E9?= Soares X-Mailer: Mozilla 4.5 [it] (Win95; I) X-Accept-Language: it MIME-Version: 1.0 To: Nuchanard Chiannilkulchai CC: pgsql-sql@hub.org Subject: Re: [SQL] automatic incrementation References: <375E2A1D.FC614366@valigene.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Nuchanard Chiannilkulchai ha scritto: > hello, > > I have a table with the field 'id' which i would like to increment at > each insert automatically. > The idea is to create a function which determines the max(id) and then a > rule to insert instead > the real insertion instruction, with the next id value. > > -- table > create table toto ( > id int4 primary key, > project char(8) not null, > comments text > ); > insert into toto values (1,'PTest','ndfjSHJG'); > insert into toto values (2,'PTest','ndfjSHJG'); > insert into toto values (3,'PTest','ndfjSHJG'); > > -- function > create function nextid(toto) returns int4 > as ' select max($1.id)+1 as id ; ' > language 'sql'; > > **** ERROR: type id lookup of 12 failed > This is my first problem > > -- rule > create rule r_id as on insert to toto > do instead > insert into toto (id, project, comments) values (nextid(), > new.project, new.comments); > > **** The next problem may be the rule writing !!! > I would like to have , even some other suggestions to make it works. > I can't use a sequence table because I've already one in the database. > > Thanks, > Nuch What about a SERIAL data type ? create table toto ( id SERIAL, project char(8) not null, comments text); -- ______________________________________________________________ PostgreSQL 6.5.0 on i586-pc-linux-gnu, compiled by gcc 2.7.2.3 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Jose'