Auto incrementing fields. How?

Started by Harry Woodover 25 years ago5 messagesgeneral
Jump to latest
#1Harry Wood
harry.wood@ic.ac.uk

Anyone know how to create auto incrementing fields?

--
Harry
harry.wood@ic.ac.uk
http://www.doc.ic.ac.uk/~hw97/Nojer2
Nojer2 on chat.yahoo.com and zapidonia.com
ICQ number 18519769

#2Brett W. McCoy
bmccoy@chapelperilous.net
In reply to: Harry Wood (#1)
Re: Auto incrementing fields. How?

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"

#3Dan Wilson
phpPgAdmin@acucore.com
In reply to: Harry Wood (#1)
Re: Auto incrementing fields. How?

Use the column type of SERIAL in your create table statement. This will
automatically create a sequence and default for the given column.

http://www.postgresql.org/users-lounge/docs/7.0/user/sql-createsequence.htm
for more on sequences.

----- Original Message -----
From: "Harry Wood" <harry.wood@ic.ac.uk>
To: <pgsql-general@postgresql.org>
Sent: Tuesday, December 19, 2000 6:51 AM
Subject: [GENERAL] Auto incrementing fields. How?

Show quoted text

Anyone know how to create auto incrementing fields?

--
Harry
harry.wood@ic.ac.uk
http://www.doc.ic.ac.uk/~hw97/Nojer2
Nojer2 on chat.yahoo.com and zapidonia.com
ICQ number 18519769

#4Tulio Oliveira
mestredosmagos@marilia.com
In reply to: Harry Wood (#1)
Re: Auto incrementing fields. How?

Harry Wood wrote:

Anyone know how to create auto incrementing fields?

--
Harry
harry.wood@ic.ac.uk
http://www.doc.ic.ac.uk/~hw97/Nojer2
Nojer2 on chat.yahoo.com and zapidonia.com
ICQ number 18519769

CREATE TABLE test (
teste_id SERIAL PRIMARY KEY,
name varchar (30) );

--
======================================================
AKACIA TECNOLOGIA
Desenvolvimento de sistemas para Internet
www.akacia.com.br

#5Harry Wood
harry.wood@ic.ac.uk
In reply to: Harry Wood (#1)
Re: Auto incrementing fields. How?

I've had lots of responses to this, and I think I have it sussed now. If
anyone has the same problem, the answer is:

Use the SERIAL type!

http://www.postgresql.org/docs/postgres/datatype.htm#AEN949