Re: Filling Missing Primary Key Values

From: Steve Atkins <steve(at)blighty(dot)com>
To: pgsql-general General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Filling Missing Primary Key Values
Date: 2011-08-11 19:44:32
Message-ID: 232F46CB-CFB0-4991-AD70-4F7606D5B71D@blighty.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Aug 11, 2011, at 12:34 PM, Rich Shepard wrote:

> On Thu, 11 Aug 2011, Chris Travers wrote:
>
>> The simplest seems to me to be a sequence and use nextval() to populate
>> the null values. The major advantage would be that the sequence could stay
>> around in case you need it again. So for example:
>>
>> create sequence my_varchar_values;
>
>> UPDATE my_table set my_varchar =
>> nextval('my_varchar_values')::varchar(12) where my_varchar IS NULL;
>
> Chris,
>
> I was wondering if this was the best approach since I have new data to add
> to the table. Don't need a starting value, eh?

This will fail if any of the existing values are integers in the range that
you're inserting - and it may fail in the future, as you add new records
if they clash with existing entries.

It's still a good way to go, but might need some care or some tweaking -
adding a prefix, maybe.

Cheers,
Steve

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Geoffrey Myers 2011-08-11 20:11:10 Re: Is max connections in a table somewhere?
Previous Message David Johnston 2011-08-11 19:40:04 Re: Filling Missing Primary Key Values