0

Possible Duplicate:
Increment value of a table ID for each INSERT

I need to set DEFAULT VALUE on one table,

with structure:

column1    |     column2     |   column3
-----------+-----------------+-------------
10001      |      chair      |     23
10002      |      lamp       |     10

where If I

INSERT INTO column2 and column3 values (vase, 30), column1 will be autoincremented with value 10003 and each time with +1 value.

Can somebody help me with that?

Community
  • 1
  • 1
ononononon
  • 1,033
  • 4
  • 14
  • 25

1 Answers1

3

You need: 1) Create a squence. Details here

CREATE SEQUENCE seq_name -- sequence name
START 10003 -- value for the next row 

2) Set nextval('seq_name') as default for column column1. Details here.

ALTER TABLE table_name
ALTER COLUMN column1
SET DEFAULT nextval('seq_name');

UPD SQLFiddle with example.

Ihor Romanchenko
  • 26,995
  • 8
  • 48
  • 44
  • +1 - apparently you can do this in postgreSQL - DB2 (my version, at least) only allows constants or certain 'special registers', not sequences. Although, conceptually, most people are probably going to expect an 'autogenerated' column, not as the 'default' value. – Clockwork-Muse Nov 30 '12 at 19:24