Re: [SQL] How to handle a requirement for nextval

From: "Jose' Soares Da Silva" <sferac(at)bo(dot)nettuno(dot)it>
To: The Web Administrator <wwwadmin(at)wizard(dot)ca>
Cc: PGsql <pgsql-sql(at)postgreSQL(dot)org>
Subject: Re: [SQL] How to handle a requirement for nextval
Date: 1998-05-19 09:50:07
Message-ID: Pine.LNX.3.96.980519094533.412B-100000@proxy.bazzanese.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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'

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Jose' Soares Da Silva 1998-05-19 12:31:42 Re: [SQL] Case in-sensitive searches
Previous Message Jose' Soares Da Silva 1998-05-19 09:45:18 Re: [SQL] How to handle a requirement for nextval