pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Populate unique column with default value
4+ messages / 2 participants
[nested] [flat]

* Populate unique column with default value
@ 2007-10-01 15:24 Jon Horsman <horshaq@gmail.com>
  2007-10-01 15:33 ` Re: Populate unique column with default value Jon Horsman <horshaq@gmail.com>
  2007-10-01 15:39 ` Re: Populate unique column with default value Rodrigo De Le� <rdeleonp@gmail.com>
  0 siblings, 2 replies; 4+ messages in thread

From: Jon Horsman @ 2007-10-01 15:24 UTC (permalink / raw)
  To: pgsql-sql

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.



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Populate unique column with default value
  2007-10-01 15:24 Populate unique column with default value Jon Horsman <horshaq@gmail.com>
@ 2007-10-01 15:33 ` Jon Horsman <horshaq@gmail.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Jon Horsman @ 2007-10-01 15:33 UTC (permalink / raw)
  To: pgsql-sql

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.
>



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Populate unique column with default value
  2007-10-01 15:24 Populate unique column with default value Jon Horsman <horshaq@gmail.com>
@ 2007-10-01 15:39 ` Rodrigo De Le� <rdeleonp@gmail.com>
  2007-10-01 15:51   ` Re: Populate unique column with default value Jon Horsman <horshaq@gmail.com>
  1 sibling, 1 reply; 4+ messages in thread

From: Rodrigo De Le� @ 2007-10-01 15:39 UTC (permalink / raw)
  To: Jon Horsman <horshaq@gmail.com>; +Cc: pgsql-sql

On 10/1/07, Jon Horsman <horshaq@gmail.com> wrote:
> ... Is there an elegant way i can set the username
> to be the extension of the current row ...

UPDATE usertable
SET username = extension
WHERE username IS NULL;



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Populate unique column with default value
  2007-10-01 15:24 Populate unique column with default value Jon Horsman <horshaq@gmail.com>
  2007-10-01 15:39 ` Re: Populate unique column with default value Rodrigo De Le� <rdeleonp@gmail.com>
@ 2007-10-01 15:51   ` Jon Horsman <horshaq@gmail.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Jon Horsman @ 2007-10-01 15:51 UTC (permalink / raw)
  To: Rodrigo De Le� <rdeleonp@gmail.com>; +Cc: pgsql-sql

> On 10/1/07, Jon Horsman <horshaq@gmail.com> wrote:
> > ... Is there an elegant way i can set the username
> > to be the extension of the current row ...
>
> UPDATE usertable
> SET username = extension
> WHERE username IS NULL;

Hmm, that simple eh, don't I feel stupid =)

Thanks a ton for the quick response.

Jon.



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2007-10-01 15:51 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-10-01 15:24 Populate unique column with default value Jon Horsman <horshaq@gmail.com>
2007-10-01 15:33 ` Jon Horsman <horshaq@gmail.com>
2007-10-01 15:39 ` Rodrigo De Le� <rdeleonp@gmail.com>
2007-10-01 15:51   ` Jon Horsman <horshaq@gmail.com>

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