Re: Add quto increment to existing column - Mailing list pgsql-general

From Greg Williamson
Subject Re: Add quto increment to existing column
Date
Msg-id 1317948611.27175.YahooMailNeo@web46113.mail.sp1.yahoo.com
Whole thread Raw
In response to Add quto increment to existing column  (Robert Buckley <robertdbuckley@yahoo.com>)
List 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
theprimary 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 --
setthe sequence using select setval('hist_id_seq', maxid)  so that the next value you get from the sequence doesn't
collidewith existing values. 

HTH,

Greg Williamson


pgsql-general by date:

Previous
From: Gavin Flower
Date:
Subject: Re: Selecting All Columns Associated With Maximum Value of One Column
Next
From: Adrian Klaver
Date:
Subject: Re: Backup Database Question