Re: Alter Table Auto_Increment

From: Thom Brown <thom(at)linux(dot)com>
To: Carlos Mennens <carlos(dot)mennens(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Alter Table Auto_Increment
Date: 2010-09-20 18:53:42
Message-ID: AANLkTinrTbbz+u_JHpzHCmoCrC0AWnhAeE14zpjtHc3d@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 20 September 2010 19:40, Carlos Mennens <carlos(dot)mennens(at)gmail(dot)com> wrote:
> On Mon, Sep 20, 2010 at 10:08 AM, Thom Brown <thom(at)linux(dot)com> wrote:
>> CREATE SEQUENCE seq_blades_id;
>> SELECT setval('seq_blades_id', max(id)) FROM blades; -- set the
>> sequence's value to the maximum value of "id"
>> ALTER TABLE blades ALTER COLUMN id SET DEFAULT
>> nextval('seq_blades_id'); -- make default value get value from
>> sequence
>
> That appeared to work fine. My question that I can't find in the
> documentation is that if I already have one row created where my 'id =
> 1' for AUTO_INCREMENT, how do I enter future values into my table and
> force id to auto_increment rather than having me type the value into
> the SQL command? This way I can let PostgreSQL add the next INTEGER
> rather than me having to know and avoid duplicates.
>
> INSERT INTO blades VALUES (
> '2',
> 'data',
> 'data',
> 'data',
> '1234',
> '2010-09-20
> );
>
> Is there a way I can omit having to type the '2' above in my SQL
> command if 'id' column is configured for AUTO_INCREMENT

You need to specify the columns you're inserting into:

INSERT INTO blades (ilom_ip, host_os, host_ip, hostname, "serial", gfe, admin)
VALUES ('value', 'value', 'value', 'value', 'value', 'value', 'value');

The id column will then pick up its default value.

--
Thom Brown
Twitter: @darkixion
IRC (freenode): dark_ixion
Registered Linux user: #516935

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Carlos Mennens 2010-09-20 18:54:29 Re: Alter Table Auto_Increment
Previous Message Bruce Momjian 2010-09-20 18:44:41 Re: A note on pg_upgrade and missing "pg_upgrade_support.so"