It looks like you have both a surrogate key ( int userId ) and a natural key ( char or varchar username ). Any column can be used as the primary key for the table, and in any case, you can still ensure the uniqueness of another key.
There are many discussions about the trade-offs between natural and surrogate keys - you will need to decide what works for you and what is the βstandardβ in your organization.
Here are some considerations when choosing a method:
Case for using surrogate keys (e.g. UserId INT AUTO_INCREMENT)
If you use a surrogate (for example, UserId INT AUTO_INCREMENT ) as the primary key, then all tables that reference the MyUsers table should then use UserId as a foreign key.
However, you can still ensure that the user's username column is unique by using an additional unique index , for example:
CREATE TABLE 'MyUsers' ( 'userId' int NOT NULL AUTO_INCREMENT, 'username' varchar(100) NOT NULL, ... other columns PRIMARY KEY('userId'), UNIQUE KEY UQ_UserName ('username')
According to @Dagon, using a narrow primary key (like int ) has performance and storage advantages over using a wider (and variable length) value like varchar . This advantage also affects other tables that reference MyUsers , as the foreign key for userid will narrow.
Another advantage of the surrogate integer key is that the username can be easily changed without affecting the tables that reference MyUsers . If username used as a natural key, then the tables were associated with MyUsers using username , which makes it more inconvenient to change the username (since otherwise the foreign key relationship would be violated). If you want to update user names for tables using username as a foreign key, you will need to use a method similar to ON UPDATE CASCADE to maintain data integrity.
Natural Keys use case (i.e. username)
On the other hand, to use surrogate keys for other tables that reference MyUsers through a surrogate key, you always need to join with the MyUsers table to get the username. One of the potential benefits of natural keys is that if a query requires only the " Username column from the table MyUsers to" MyUsers , it does not need to join MyUsers to get the username, which saves some overhead.
Further references to natural versus surrogate debate and compromise here and here