Re: CREATE TABLE initial value for PRIMARY KEY

Lists: pgsql-general
From: Richard Broersma Jr <rabroersma(at)yahoo(dot)com>
To: Maurice Yarrow <yarrow(at)best(dot)com>
Cc: General PostgreSQL List <pgsql-general(at)postgresql(dot)org>
Subject: Re: CREATE TABLE initial value for PRIMARY KEY
Date: 2006-10-27 20:34:24
Message-ID: 13265.79301.qm@web31810.mail.mud.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

> I thought about using a DEFAULT value, but I had presumed
> that this was only for repeated intializations. So then is it the
> case that a
> CREATE TABLE mytable ( id INTEGER PRIMARY KEY DEFAULT 100000, ...
> only applies this default to the very first row of such a table, and then
> sensibly, increments from there ?
> (Guess I could easily try this out...)

Ah, I think I know what you are looking for. You want an auto-incrementing number. There are
special sudo-data-types called serial bigserial. These are really auto-incrementing
integers/bigintegers. For more details on how to use this see:

http://www.postgresql.org/docs/8.1/interactive/datatype.html#DATATYPE-SERIAL

Also, when relying, don't forget to reply also to the list that way everyone can participate.

Regards,

Richard Broersma Jr.


From: Maurice Yarrow <yarrow(at)best(dot)com>
To: Richard Broersma Jr <rabroersma(at)yahoo(dot)com>
Cc: General PostgreSQL List <pgsql-general(at)postgresql(dot)org>
Subject: Re: CREATE TABLE initial value for PRIMARY KEY
Date: 2006-10-27 23:03:25
Message-ID: 4542903D.5080207@best.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hello Richard

Thanks for the tip.
So it turned out to be possible to do it like this:

CREATE SEQUENCE id_seq;
SELECT setval('id_seq',100111);
CREATE TABLE customer( id INTEGER DEFAULT nextval('id_seq'), name
VARCHAR(30) );

INSERT INTO customer (name) VALUES ('SomeName');
INSERT INTO customer (name) VALUES ('SomeOtherName');

Then
SELECT * FROM customer;
id | name
--------+---------------
100112 | SomeName
100113 | SomeOtherName
(2 rows)

And it's that "setval" that is critical.

Note also that alternatively it can be done as follows:
CREATE TABLE customer ( id SERIAL, name VARCHAR(30) );
SELECT setval('customer_id_seq',100111);

INSERT INTO customer (name) VALUES ('SomeName');
INSERT INTO customer (name) VALUES ('SomeOtherName');

Then
SELECT * FROM customer;
id | name
--------+---------------
100112 | SomeName
100113 | SomeOtherName
(2 rows)

Thanks again for the suggestion. Ultimately, for the exact
syntaxes I went to Momjian's book:
(7.4 Creating Sequences, 7.5 Using Sequences to Number Rows)

Maurice Yarrow

Richard Broersma Jr wrote:

>>I thought about using a DEFAULT value, but I had presumed
>>that this was only for repeated intializations. So then is it the
>>case that a
>>CREATE TABLE mytable ( id INTEGER PRIMARY KEY DEFAULT 100000, ...
>>only applies this default to the very first row of such a table, and then
>>sensibly, increments from there ?
>>(Guess I could easily try this out...)
>>
>>
>
>Ah, I think I know what you are looking for. You want an auto-incrementing number. There are
>special sudo-data-types called serial bigserial. These are really auto-incrementing
>integers/bigintegers. For more details on how to use this see:
>
>http://www.postgresql.org/docs/8.1/interactive/datatype.html#DATATYPE-SERIAL
>
>Also, when relying, don't forget to reply also to the list that way everyone can participate.
>
>Regards,
>
>Richard Broersma Jr.
>
>
>
>


From: "John D(dot) Burger" <john(at)mitre(dot)org>
To: Maurice Yarrow <yarrow(at)best(dot)com>
Cc: General PostgreSQL List <pgsql-general(at)postgresql(dot)org>
Subject: Re: CREATE TABLE initial value for PRIMARY KEY
Date: 2006-10-30 18:59:54
Message-ID: FEA2F973-B33B-4B62-A35A-F03B8A45BFB6@mitre.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Maurice Yarrow wrote:

> So it turned out to be possible to do it like this:
>
> CREATE SEQUENCE id_seq;
> SELECT setval('id_seq',100111);

FYI, you could have done this:

CREATE SEQUENCE id_seq START 100111;

- John D. Burger
MITRE