Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use an auto-incrementing primary key when other unique fields exist?

I'm taking a course called "database systems" and for our class project I have to design a website.

Here's an example of a table I created:

CREATE TABLE users (   uid INT NOT NULL AUTO_INCREMENT,   username VARCHAR(60),   passhash VARCHAR(255),   email VARCHAR(60),   rdate DATE,   PRIMARY KEY(uid) ); 

The professor told me "uid" (user id) was completely useless and unnecessary and I should have used the username as the primary key, since no two users can have the same username.

I told him it was convenient for me use a user id because when I call something like domain.com/viewuser?id=5 I just check the parameter with: is_numeric($_GET['id'])... needless to say he was not convinced.

Since I've seen user_id and other similar attributes (thread_id, comment_id, among others) on plenty of tutorials and looking at the database schema of popular software (eg. vbulletin) there must be plenty of other (stronger) reasons.

So my question is: How would you justify the need of a not null auto incrementing id as a primary key vs using another attribute like the username?

like image 910
cnandreu Avatar asked Nov 05 '10 03:11

cnandreu


People also ask

Should you use auto increment for primary key?

Auto-increment should be used as a unique key when no unique key already exists about the items you are modelling. So for Elements you could use the Atomic Number or Books the ISBN number.

What is the benefit of using an auto incrementing surrogate key?

Auto-incremented key, also called as surrogate key is a single table column which contains unique numeric values which can be used to uniquely identify a single row of data in a table.

Why is it important for a primary key to be unique?

Without the primary key and closely related foreign key concepts, relational databases would not work. In fact, since a table can easily contain thousands of records (including duplicates), a primary key is necessary to ensure that a table record can always be uniquely identified.

How can auto increment primary key be used as foreign key?

In operation, you would insert a new row into the parent table (the one with the auto-incrementing primary key column) then, using a copy of the value automatically assigned to that key, insert a row into the dependent table setting the foreign key column there to the value copied from the parent table's primary key.


2 Answers

Auto-incrementing primary keys are useful for several reasons:

  • They allow duplicate user names as on Stack Overflow
  • They allow the user name (or email address, if that's used to login) to be changed (easily)
  • Selects, joins and inserts are faster than varchar primary keys as its much faster to maintain a numeric index
  • As you mentioned, validation becomes very simple: if ((int)$id > 0) { ... }
  • Sanitation of input is trivial: $id = (int)$_GET['id']
  • There is far less overhead as foreign keys don't have to duplicate potentially large string values

I would say trying to use any piece of string information as a unique identifier for a record is a bad idea when an auto-incrementing numeric key is so readily available.

Systems with unique user names are fine for very small numbers of users, but the Internet has rendered them fundamentally broken. When you consider the sheer number of people named "john" that might have to interact with a website, it's ridiculous to require each of them to use a unique display name. It leads to the awful system we see so frequently with random digits and letters decorating a username.

However, even in a system where you enforced unique usernames, it's still a poor choice for a primary key. Imagine a user with 500 posts: The foreign key in the posts table is going to contain the username, duplicated 500 times. The overhead is prohibitive even before you consider that somebody might eventually need to change their username.

like image 167
meagar Avatar answered Oct 26 '22 23:10

meagar


If the username is the primary key and a user changes his/her username, you will need to update all the tables which have foreign key references to the users table.

like image 38
Madison Caldwell Avatar answered Oct 26 '22 22:10

Madison Caldwell