Re: Add quto increment to existing column

From: Greg Williamson <gwilliamson39(at)yahoo(dot)com>
To: Robert Buckley <robertdbuckley(at)yahoo(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Add quto increment to existing column
Date: 2011-10-07 00:50:11
Message-ID: 1317948611.27175.YahooMailNeo@web46113.mail.sp1.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Robert --

>Hi,
>
>
>I have a column in a table called hist_id with the datatype "integer". When I created the table I assigned this column the primary key constraint but didn´t make it an auto-increment column.
>
>
>How could I do this to an the already existing column?
>
>
>I have created the sequence with the following command but don´t know how to change the existing column to auto-increment.
>
>
>
>
>$ create sequence hist_id_seq;
>

Perhaps:

ALTER TABLE history_foo ALTER COLUMN hist_id SET DEFAULT nextval('hist_id_seq');

?? (untested)

You may need to to update the sequence to reflect your current highest value for hist_id (or whatever the column is -- set the sequence using select setval('hist_id_seq', maxid)  so that the next value you get from the sequence doesn't collide with existing values.

HTH,

Greg Williamson

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2011-10-07 02:29:55 Re: Backup Database Question
Previous Message Richard Price 2011-10-06 23:48:15 PostgreSQL consulting companies in the Bay Area