Thread: Increment primary key

Increment primary key

From
impala
Date:
Hi

I created a table and the primary key must increment
automatically with e.g 1 each time I insert a new row.
How do I write the psql code to make this happen?

Nic

__________________________________________________
Do You Yahoo!?
Yahoo! Tax Center - online filing with TurboTax
http://taxes.yahoo.com/

Re: Increment primary key

From
wsheldah@lexmark.com
Date:

The easiest way is to do it in the CREATE TABLE statement:
CREATE TABLE mytable (id SERIAL PRIMARY KEY, name text);

That will make id an integer, automatically create a separate SEQUENCE, and bind
id to that sequence so that it is incremented properly.
The online docs elaborate on this; look for CREATE SEQUENCE, CREATE TABLE, and
SERIAL.  Best of luck!

Wes




impala <bhnnc%yahoo.co.uk@interlock.lexmark.com> on 04/03/2002 01:58:32 PM

To:   pgsql-general%postgresql.org@interlock.lexmark.com
cc:    (bcc: Wesley Sheldahl/Lex/Lexmark)
Subject:  [GENERAL] Increment primary key


Hi

I created a table and the primary key must increment
automatically with e.g 1 each time I insert a new row.
How do I write the psql code to make this happen?

Nic

__________________________________________________
Do You Yahoo!?
Yahoo! Tax Center - online filing with TurboTax
http://taxes.yahoo.com/

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org