Received: from proxy.bazzanese.com (sferac@ip161.tol11.interbusiness.it [195.120.106.161]) by hub.org (8.8.8/8.7.5) with ESMTP id HAA15613 for ; Tue, 19 May 1998 07:08:22 -0400 (EDT) Received: from localhost (sferac@localhost) by proxy.bazzanese.com (8.8.8/8.8.8/Debian/GNU) with SMTP id JAA00443; Tue, 19 May 1998 09:50:07 GMT Date: Tue, 19 May 1998 09:50:07 +0000 (UTC) From: "Jose' Soares Da Silva" X-Sender: sferac@proxy.bazzanese.com To: The Web Administrator cc: PGsql Subject: Re: [SQL] How to handle a requirement for nextval In-Reply-To: <35609B0F.52A63C2@wizard.ca> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII On Mon, 18 May 1998, The Web Administrator wrote: > I am still struggling on a few issues, have got my first little database > up and running, and am quite happy with PostGres, but I cannot figure > out how to create a table which can have a field that autoincrements.. > I am sure that somewhere in the oid terminology it is built in, but I > want to have a counter on some table entries.. ie... > CREATE TABLE clothing_type ( > type_id INT NOT NULL, > description TEXT, > for_sex CHAR > ); > INSERT INTO clothing_type ( > description, > for_sex > ) VALUES ( > 'pants', > 'm' > ); > > What I want is that the Primary Key (Only Key) be type_id, and int, and > the first item that I insert should have type_id as '1', next will be > '2' etc.. > I could have every insert into this table include a type_id, but that > seems unessary. > Can I have something like default='nextval' ? CREATE SEQUENCE serial START 101; CREATE TABLE distributors ( did DECIMAL(03) DEFAULT NEXTVAL('serial'), name VARCHAR(40) ); -- Use sequence in insert: -- INSERT INTO distributors VALUES (NEXTVAL('serial')); Jose'