Thread: Auto incrementing fields. How?

Auto incrementing fields. How?

From
Harry Wood
Date:
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

Re: Auto incrementing fields. How?

From
"Dan Wilson"
Date:
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?


> 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


Re: Auto incrementing fields. How?

From
"Brett W. McCoy"
Date:
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"


Re: Auto incrementing fields. How?

From
Tulio Oliveira
Date:
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

Re: Auto incrementing fields. How?

From
Harry Wood
Date:
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