pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Federico Passaro <fede@link.it>
To: PostGreSQL Interface <pgsql-interfaces@postgreSQL.org>
To: Wari Wahab <wari@technology.com>
Subject: Re: [INTERFACES] Autoincrement
Date: Fri, 17 Jul 1998 13:07:08 +0200
Message-ID: <35AF305B.14B1124B@link.it> (raw)
References: <199807171006.GAA29551@hub.org>

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




view thread (5+ messages)  latest in thread

Message-ID: <35AF305B.14B1124B@link.it>
Permalink:  ../35AF305B.14B1124B@link.it/
Also on:    postgresql.org/message-id/35AF305B.14B1124B@link.it

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-interfaces@postgresql.org
  Cc: fede@link.it, wari@technology.com
  Subject: Re: [INTERFACES] Autoincrement
  In-Reply-To: <35AF305B.14B1124B@link.it>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox