Unix Technical Forum

Populate unique column with default value

This is a discussion on Populate unique column with default value within the pgsql Sql forums, part of the PostgreSQL category; --> Hey, I'm updating the user table in our db to have a new column "username" as follows. ALTER TABLE ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Sql

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-19-2008, 05:55 PM
Jon Horsman
 
Posts: n/a
Default Populate unique column with default value

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.

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-19-2008, 05:55 PM
Jon Horsman
 
Posts: n/a
Default Re: Populate unique column with default value

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


---------------------------(end of broadcast)---------------------------
TIP 7: You can help support the PostgreSQL project by donating at

http://www.postgresql.org/about/donate

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-19-2008, 05:55 PM
=?UTF-8?Q?Rodrigo_De_Le=EF=BF=BD?=
 
Posts: n/a
Default Re: Populate unique column with default value

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;

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-19-2008, 05:55 PM
Jon Horsman
 
Posts: n/a
Default Re: Populate unique column with default value

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

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:18 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com