Unix Technical Forum

Creating Unique Usernames

This is a discussion on Creating Unique Usernames within the MySQL forums, part of the Database Server Software category; --> I am importing data from a student information system into MySQL. I need to create unique usernames in the ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-13-2008, 01:59 PM
Shane
 
Posts: n/a
Default Creating Unique Usernames

I am importing data from a student information system into MySQL. I
need to create unique usernames in the format of "first.last". If
there is a duplicate, I would like to be able to insert the username
as "first.last[n]". Currently I have 262 duplicate combinations of
first and last names.

I imagine this can be done with some sort of looping structure, but I
would like to ensure that it remains fast (currently importing 10,000+
users in under 10 seconds).

I am thinking of doing the following (not any particular language, but
you should get the idea):
<code>
i = 1
dups = false
dupnum = 0

dupnum = SELECT COUNT(*) FROM students s WHERE s.uername IS NULL

if dupnum > 0 then
dups = true
else
dups = false
end

Do while dups = true
UPDATE IGNORE students s SET s.user_name = CONCAT(s.firstname, '.',
s.lastname, i)
dupnum = SELECT COUNT(*) FROM students s WHERE s.username IS NULL
if dupnum >= 1 then
dups = true
else
dups = false
Loop
</code>

Any thoughts or better ideas?

Thanks!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-13-2008, 01:59 PM
Jerry Stuckle
 
Posts: n/a
Default Re: Creating Unique Usernames

Shane wrote:
> I am importing data from a student information system into MySQL. I
> need to create unique usernames in the format of "first.last". If
> there is a duplicate, I would like to be able to insert the username
> as "first.last[n]". Currently I have 262 duplicate combinations of
> first and last names.
>
> I imagine this can be done with some sort of looping structure, but I
> would like to ensure that it remains fast (currently importing 10,000+
> users in under 10 seconds).
>
> I am thinking of doing the following (not any particular language, but
> you should get the idea):
> <code>
> i = 1
> dups = false
> dupnum = 0
>
> dupnum = SELECT COUNT(*) FROM students s WHERE s.uername IS NULL
>
> if dupnum > 0 then
> dups = true
> else
> dups = false
> end
>
> Do while dups = true
> UPDATE IGNORE students s SET s.user_name = CONCAT(s.firstname, '.',
> s.lastname, i)
> dupnum = SELECT COUNT(*) FROM students s WHERE s.username IS NULL
> if dupnum >= 1 then
> dups = true
> else
> dups = false
> Loop
> </code>
>
> Any thoughts or better ideas?
>
> Thanks!
>


A PK should be independent of the data. What are you going to do if,
for instance, a student's name changes (i.e. gets married)?

Use an auto_increment column for the primary key instead. Or, if you
have a student id number, that would be a second choice.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-13-2008, 01:59 PM
Gordon Burditt
 
Posts: n/a
Default Re: Creating Unique Usernames

>I am importing data from a student information system into MySQL. I
>need to create unique usernames in the format of "first.last". If
>there is a duplicate, I would like to be able to insert the username
>as "first.last[n]". Currently I have 262 duplicate combinations of
>first and last names.


Are you going to do this *ONCE*, or *MULTIPLE TIMES*?
If you do the import once, a few more weeks pass and a few more John Smiths
enroll, and then you need to do the import again, is it a problem that
John.Smith65535 isn't the same guy as previously had John.Smith65535?

I hope this username is NOT a primary key. Obviously it's not being
imported from the student information system since you are trying
to create it. I hope there is a different primary (student number?
but that might cause privacy issues) to relate records on SIS with
records on your system.

>I imagine this can be done with some sort of looping structure, but I
>would like to ensure that it remains fast (currently importing 10,000+
>users in under 10 seconds).


>I am thinking of doing the following (not any particular language, but
>you should get the idea):
><code>
>i = 1
>dups = false
>dupnum = 0
>
>dupnum = SELECT COUNT(*) FROM students s WHERE s.uername IS NULL
>
>if dupnum > 0 then
> dups = true
>else
> dups = false
>end


Assuming that records are initially inserted with username as NULL,
and you have a unique index on `username`, these queries can set up username:

update ignore students set username = concat(fn, '.', 'ln') where username is null;
update ignore students set username = concat(fn, '.', 'ln', '1') where username is null;
update ignore students set username = concat(fn, '.', 'ln', '2') where username is null;
....

Keep repeating until the number of records where username is null
is zero. After the first update, only records with username still
NULL are affected, so speed should be related to the number of
records with still-unassigned usernames. You can use these as
fire-and-forget queries in a batch file without a lot of support
from a language like PHP, as long as the highest number you need.

You can also do a partial import of *new* records and use the same
set of queries above to assign new usernames while leaving
previously-assigned usernames unchanged.

This depends on a couple of features of MySQL which I'm not sure are standard:
1. You can have a UNIQUE index with null values in it.
2. UPDATE IGNORE will cause key conflicts to not abort the update, and
the first query above will assign all possible non-conflicting usernames,
leaving NULL for conflicting ones.

>Do while dups = true
> UPDATE IGNORE students s SET s.user_name = CONCAT(s.firstname, '.',
>s.lastname, i)
> dupnum = SELECT COUNT(*) FROM students s WHERE s.username IS NULL
> if dupnum >= 1 then
> dups = true
> else
> dups = false
>Loop

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 04:29 AM.


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