Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are usernames a valid candidate for a primary key?

I know surrogate primary keys are generally recommended over natural primary keys, but are there any arguments in favor of surrogate primary keys when it comes to usernames?

like image 607
Emanuil Rusev Avatar asked Jan 27 '10 08:01

Emanuil Rusev


4 Answers

If you don't want to get duplicate usernames, create a UNIQUE constraint.

What if DWong1145 wants to change it's username? Will you to make all database relationships to be UPDATE CASCADE?

like image 173
Rubens Farias Avatar answered Oct 19 '22 21:10

Rubens Farias


NO, you should use username only as a unique key/constant. Username can be changed, just and example: it may be a trademark and the owner requests you to drop it.

like image 39
sorin Avatar answered Oct 19 '22 20:10

sorin


It is a good candidate in some sense, however you have to consider if you really want to do it. For instance, you have a user with a certain user name, then the user gets "deleted" (or marked as deleted). So, there is actually no reason for not allowing to create another user with the same username, but it is already "taken", since it's a primary key.

like image 5
naivists Avatar answered Oct 19 '22 20:10

naivists


From a customer standpoint, you bet. I don't want my username to be DWong1145.

like image 4
DWong Avatar answered Oct 19 '22 22:10

DWong