Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it ok to use character values for primary keys?

Is there a performance gain or best practice when it comes to using unique, numeric ID fields in a database table compared to using character-based ones?

For instance, if I had two tables:

athlete

id ... 17, name ... Rickey Henderson, teamid ... 28

team

teamid ... 28, teamname ... Oakland

The athlete table, with thousands of players, would be easier to read if the teamid was, say, "OAK" or "SD" instead of "28" or "31". Let's take for granted the teamid values would remain unique and consistent in character form.

I know you CAN use characters, but is it a bad idea for indexing, filtering, etc for any reason?

Please ignore the normalization argument as these tables are more complicated than the example.

like image 621
Randy Burgess Avatar asked Jan 15 '09 04:01

Randy Burgess


Video Answer


2 Answers

I find primary keys that are meaningless numbers cause less headaches in the long run.

like image 117
brian Avatar answered Sep 24 '22 20:09

brian


Text is fine, for all the reasons you mentioned.

If the string is only a few characters, then it will be nearly as small an an integer anyway. The biggest potential drawback to using strings is the size: database performance is related to how many disk accesses are needed. Making the index twice as big, for example, could create disk-cache pressure, and increase the number of disk seeks.

like image 37
Doug Currie Avatar answered Sep 24 '22 20:09

Doug Currie