Re: [INTERFACES] Autoincrement

From: Federico Passaro <fede(at)link(dot)it>
To: PostGreSQL Interface <pgsql-interfaces(at)postgreSQL(dot)org>, Wari Wahab <wari(at)technology(dot)com>
Subject: Re: [INTERFACES] Autoincrement
Date: 1998-07-17 11:07:08
Message-ID: 35AF305B.14B1124B@link.it
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-interfaces pgsql-sql

Antonio Garcia Mari wrote:

> > Hi, is there a way to do a field that can automatically increment itself,
> > like in paradox or dbase?
> >
>
> this is a hack, but it works...
>
> CREATE SEQUENCE key_s INCREMENT 1 START 1;
> CREATE TABLE cliente (
> key int4 NOT NULL DEFAULT nextval('key_s') PRIMARY KEY,
> name varchar(100) UNIQUE NOT NULL,
> username varchar(8) NOT NULL
> );
> Antonio Garcia Mari
> Mallorca (Spain)

You are right, but it's better to put the autoincrementing field as the last
one like in:

CREATE TABLE cliente (
name varchar(100) UNIQUE NOT NULL,
username varchar(8) NOT NULL ,
key int4 NOT NULL DEFAULT nextval('key_s') PRIMARY KEY,
);

This way you can use the sintax

insert into cliente values ('JACK', 'postgres');

in place of

insert into cliente (name, username) values ('JACK', 'postgres');

A more robust solution is to use a trigger. Look at the files
<PostGreSQL source dir>/contrib/spi/autoinc.*

federico

In response to

Responses

Browse pgsql-interfaces by date

  From Date Subject
Next Message James Olin Oden 1998-07-17 12:27:45 Re: [INTERFACES] Autoincrement
Previous Message leif 1998-07-17 10:36:00 Re: [INTERFACES] ODBC driver for Applixware and Unix

Browse pgsql-sql by date

  From Date Subject
Next Message James Olin Oden 1998-07-17 12:27:45 Re: [INTERFACES] Autoincrement
Previous Message Antonio Garcia Mari 1998-07-17 09:47:04 Re: [INTERFACES] Autoincrement