Re: Auto incrementing fields. How?

From: "Brett W(dot) McCoy" <bmccoy(at)chapelperilous(dot)net>
To: Harry Wood <harry(dot)wood(at)ic(dot)ac(dot)uk>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Auto incrementing fields. How?
Date: 2000-12-19 16:18:01
Message-ID: Pine.LNX.4.30.0012191109530.18420-100000@chapelperilous.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, 19 Dec 2000, Harry Wood wrote:

> Anyone know how to create auto incrementing fields?

create sequence my_seq;

create table my_table (
my_id integer primary key default nextval('my_seq'),
another_field varchar(10),
...
);

OR, you can create an implicit sequence:

create table my_table (
my_id SERIAL primary key,
...
);

If you drop the table later on, though, you will need to drop the created
sequence manually.

When you insert data into the table, do not specify the autoincrement
field in the insert statement:

insert into my_table(another_field) values('some data');

Refer to the Postgres documentation for full details.

-- Brett
http://www.chapelperilous.net/~bmccoy/
---------------------------------------------------------------------------
Politics, as a practice, whatever its professions, has always been the
systematic organisation of hatreds.
-- Henry Adams, "The Education of Henry Adams"

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Dan Wilson 2000-12-19 16:37:58 Re: newbie question:
Previous Message Raymond Chui 2000-12-19 15:37:42 TODAY and CURRENT?