Using string as PK vs using GUID or int Id with Unique Constraint for Names

Hi I was wondering what is the best practice for tables in which you have a record that must be unique. I've seen the two ways of doing that: use a Primary Key or add a Unique constraint to the column.

If you use a primary key, is it bad practice to have a primary key such as "UserName" that is varchar(*)? Does that impact performance enough that it is problematic? Is it best to use an integer id with a unique constraint on the username?

I see some other factors that may impact choosing a column as PK vs Unique. Am I right about these?

PK - Column should be one that doesn't ever need to be changed

Unique - Column could be changed later on


Having a primary key on the UserName is not the best idea, but it isn't so bad in performance as you maybe think.

The best idea would be using a ID (INT) as PRIMARY KEY and the UserName as UNIQUE.

Usernames change over time, that is why they are a bad candidate for a PK especally since it is extremely likely you have child records associated with the username. For instance suppose my username included some variation of my real name. If I then got divorced and returned to my maiden name, the last thing I want to do is be reminded of that SOB I was married to and so I change my username. Do you really want to change the 2 million posts I've made in the last ten years as well? I didn't think so.

Yes string comparisons are slower but this may or may not be an issue depending on the overall amount of action the database will get. Small copmany database with less than 200 users, probaly not a problem, Internet site with millions of users, much more likely to be a problem.

It may or may not be a good idea as others have already discussed. Let me just add one more detail...

I see some other factors that may impact choosing a column as PK vs Unique.

The main difference is usually related to clustering. Most DBMSes (that support clustering) automatically use PK as a clustering index. For example MySQL/InnoDB always clusters data and you can't event turn it off, while MS SQL Server clusters by default (you have to use special syntax to turn it off).

Should you choose to use clustering (or are forced by your DBMS), having fewer indexes is usually better (e.g. see "Disadvantages of clustering" in this article), even when leading to "fatter" foreign keys.

Need Your Help

creating one var object vs multiple var


For better code structure, I want to use a javascript object holding all properties instead of using multiple vars:

Datagrid multitrigger referencing values from code-behind

c# wpf datagrid triggers local-variables

I'm still bloody green in WPF and have not yet fully grasped the concept behind it. I've got the following problem: