pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jon Horsman <horshaq@gmail.com>
To: sql pgsql <pgsql-sql@postgresql.org>
Subject: Re: Populate unique column with default value
Date: Mon, 1 Oct 2007 11:33:00 -0400
Message-ID: <4f4c2a010710010833v53afcaafn5221a3f7d4d964a3@mail.gmail.com> (raw)
In-Reply-To: <4f4c2a010710010824t7a2fbd59kb9102c879b389a5d@mail.gmail.com>
References: <4f4c2a010710010824t7a2fbd59kb9102c879b389a5d@mail.gmail.com>

I guess what i'm looking for is something like the following, but i
don't know how i'd do it.

"UPDATE usertable SET username='SELECT extension FROM usertable WHERE
id=<current row>' WHERE username ISNULL",

Is this possible?

Thanks,

Jon.

On 10/1/07, Jon Horsman <horshaq@gmail.com> wrote:
> Hey,
>
> I'm updating the user table in our db to have a new column "username"
> as follows.
> ALTER TABLE usertable ADD COLUMN username varchar(64)
> UPDATE usertable SET username='<extension of current row>' WHERE username ISNULL
> ALTER TABLE usertable ALTER COLUMN username SET NOT NULL
> ALTER TABLE usertable ADD CONSTRAINT usertable_username_key UNIQUE(username)
>
> I want to essentially do what i have above but need to populate the
> username field with some unique value so that the last ALTER will run.
>  I have another column in this table that is unique, an phone
> extension, and am hoping to use this as a default username.  Is there
> an elegant way i can set the username to be the extension of the
> current row or should i just write a little loop that goes threw and
> populates my username with the users extension manually for each user
> in my db and then run the last ALTER.
>
> Thanks,
>
> Jon.
>



view thread (4+ messages)  latest in thread

Message-ID: <4f4c2a010710010833v53afcaafn5221a3f7d4d964a3@mail.gmail.com>
Permalink:  ../4f4c2a010710010833v53afcaafn5221a3f7d4d964a3@mail.gmail.com/
Also on:    postgresql.org/message-id/4f4c2a010710010833v53afcaafn5221a3f7d4d964a3@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: horshaq@gmail.com
  Subject: Re: Populate unique column with default value
  In-Reply-To: <4f4c2a010710010833v53afcaafn5221a3f7d4d964a3@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox