4

I am designing a user registration table for my website, we need to support login by username/email/mobile using the same password.

The user can register by a username, email or mobile, once they get registered they can bind their email or phone.

Here is the table that I am thinking, but it has some problems:

user_id is AUTO_INCREMENT

user_id |   user_name   |   email   |   mobile  |   pwd
--------------------------------------------------------
   1        test            
   2                    [email protected]          
   3                                    123456          

The following registration should fail:

   4        123456              // illegal, 123456 is present in mobile                     
   5        [email protected]      // illegal, [email protected] is present in email                                  

The requirement is just to support login by username/email/mobile, and there is no limitation regarding the username format, so here username or email or mobile must be unique among the three columns.

It seems that it is impossible to add unique constraint cross multiple columns, any better solution?

1
  • 2
    A user should have only one row. Why are you using insert for additional values rather than update? Commented Mar 7, 2016 at 13:27

2 Answers 2

7

How about a table that holds the user_id, and their password:

user_account (user_id int, pwd binary(128))

And a separate table to hold user registration:

user_registration (user_id int, registration nvarchar(300), registration_type int)

You would then avoid having non-unique registrations belonging to different users, and your various applications can pass in the type or registration they are handling when authenticating a user.

You also avoid having a table with a bunch of null values when 95% of the user-base gravitates towards using mobile login exclusively (for example).

-- user_acount data

user_id pwd
------- ---------------
1       0x234524305762
2       0x403958634589
3       0x345656753546

-- user_registration data   --Types username=1, email=2, mobile=3

user_id registration        registration_type   
1       '[email protected]'   2                   
1       '4443332233'        3
2       'userTWO'           1
3       'User-Three'        1
3       '[email protected]'   2
3       '5554443322'        3

You are basically allowing the user to create a "user name alias" for each of your different registration modes, without making them have a value for each mode. This could be viewed as both good, flexible login options for user; and bad as hacking into an account would become exponentially easier with the first half of the credential possibly/likely being the phone number of the user.

5
  • maybe in the second table user_registration , we need another id, because one user may bind email/mobile, then we need add the binded items to that table, this seems to work!
    – seaguest
    Commented Mar 7, 2016 at 14:38
  • You would use the registration_type value to allow a single user to own more than one type of registration.
    – Max Sorin
    Commented Mar 7, 2016 at 14:39
  • A single user could then have as many entries in the user_registration table as you have ways to register, or log in.
    – Max Sorin
    Commented Mar 7, 2016 at 14:40
  • yes, I was thinking the primary key for this table, then according to you, (user_id, registration_type) would be the pimary key.
    – seaguest
    Commented Mar 7, 2016 at 14:44
  • Yes, user_id + registration_type would be your primary. You be using those to in your login processing join operations. As far as registration, you can add a unique constraint to a column without making it the primary key of the table.
    – Max Sorin
    Commented Mar 7, 2016 at 14:50
2

It seems to me (without knowing further details about your requirements) that the best approach would be to have each user have a single row in the table:

user_id    user_name    email             mobile    password
1          test         [email protected]    123456    mypassword

You just need to always know which type of login you're dealing with (user name, mobile, or email address) at each point in your application so that you know which column to look at.

3
  • that's the point, for a given input(username/email/mobile), how can we determine which login mode is being used? I'd like to distinguish automatically the login mode by input.
    – seaguest
    Commented Mar 7, 2016 at 14:28
  • No one can answer that but you. What are the requirements? Are usernames allowed to be in the form, [email protected]? Can a username be ###-###-####? If they can then you'll need to have the application determine it before passing anything back to the DB, maybe via a drop down box or radio button. If the call is made from a mobile version of the app automatically then maybe it knows already that it's a phone number.
    – Tom H
    Commented Mar 7, 2016 at 14:34
  • in the real life, there is no rule to correctly determine the type from a string, I think Max Sorin 's solution would work.
    – seaguest
    Commented Mar 7, 2016 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.