Increment primary key

Started by impalaabout 24 years ago2 messagesgeneral
Jump to latest
#1impala
bhnnc@yahoo.co.uk

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/

#2Noname
wsheldah@lexmark.com
In reply to: impala (#1)
Re: Increment primary key

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