Thread: Re: [GENERAL] auto increment?

Re: [GENERAL] auto increment?

From
"David Hartwig"
Date:
You can explicitly set the values in the nextval() column.   Nextval() is
only a default.    You can also query and manipulate the sequence number
after it is created.    (using curval() nextval() setval()).

Some developers do not use the auto increment behavior.    They will do
something like:

    CREATE SEQUENCE ai_seq;
    CREATE TABLE foo (
         ai integer primary key,
         bar integer
    );
    INSERT INTO foo (ai, bar) VALUES (nextval('ai_seq'), 99);

        -OR-

    SELECT nextval('ai_seq');
    -- save the result in code
    -- then
    INSERT INTO foo (ai, bar) VALUES ({nextvalresult}, 99);



----- Original Message -----
From: Robert Williams <rwilliams@jrc-utah.com>
To: Postgres <pgsql-general@postgreSQL.org>
Sent: Saturday, January 23, 1999 7:28 PM
Subject: Re: [GENERAL] auto increment?


>That does the job.  Thank you.
>
>Another related question:  I need to use /i file to import my
>current database into postgres.  It seems that I have to use
>nextval(...) when doing an insert.  Is it possible to insert a
>row without having to use 'nextval()'?
>
>David Hartwig wrote:
>>
>> Each PostgreSQL table has a, system assigned, oid (object ID) column
which
>> is useful for uniquely identifying rows.   Users have vertually no
control
>> over it value.
>>
>> There is also:
>>
>>     CREATE TABLE foo (
>>         ai    serial primary key,
>>         bar integer
>>     );
>>
>> which is a short cut in the parser for:
>>
>> CREATE SEQUENCE ai_seq;
>> CREATE TABLE foo (
>>         ai integer default nextval('ai_seq') primary key,
>>         bar integer
>> );
>>
>> The ai column in each create statement does not require the user code to
>> explicitly assign its values.
>>
>> See CREATE_SEQUENCE in the doc.
>>
>> ----- Original Message -----
>> From: Robert Williams <rwilliams@jrc-utah.com>
>> To: Postgres <pgsql-general@postgreSQL.org>
>> Sent: Saturday, January 23, 1999 4:11 PM
>> Subject: [GENERAL] auto increment?
>>
>> >How do I set up a column as auto increment?  I have looked
>> >everywhere for the postgres equivalent ROWID as in Oracle.
>> >
>> >--
>> >Robert Williams          rwilliams@jrc-utah.com
>> >Jarob Consulting         rwilliams@kde.org
>> >Provo, Utah              rwilliams71@mailexcite.com
>
>--
>Robert Williams          rwilliams@jrc-utah.com
>Jarob Consulting         rwilliams@kde.org
>Provo, Utah              rwilliams71@mailexcite.com