Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we should have an ID column in the table of users?

Tags:

database

mysql

It's obvious that we already have another unique information about each user, and that is username. Then, why we need another unique thing for each user? Why should we also have an id for each user? What would happen if we omit the id column?

like image 864
Behnam Rasooli Avatar asked Apr 03 '13 01:04

Behnam Rasooli


People also ask

Why id is important in database?

The id field is an example of a surrogate key. It is a good idea to use a surrogate key as a primary key in a database because it is totally unrelated to and therefore unaffected by external events in the real world.

Should tables always have an id column?

Answer. Most of the time, including a column to store unique id values, typically INTEGER values, for each row can be a good idea. A unique id allows for more convenience, and one of the only real downsides is the extra memory required to store the additional values.

What is id in table?

The id attribute assigns an identifier to the <table> element. The id allows JavaScript to easily access the <table> element. It is also used to point to a specific id selector in a style sheet.

Is id required in database?

If you are sure that any other column is going to have unique data for every row and isn't going to have NULL at any time then there is no need of separate ID column to distinguish each row from others, you can make that existing column primary key for your table.


1 Answers

Even if your username is unique, there are few advantages to having an extra id column instead of using the varchar as your primary key.

  • Some people prefer to use an integer column as the primary key, to serve as a surrogate key that never needs to change, even if other columns are subject to change. Although there's nothing preventing a natural primary key from being changeable too, you'd have to use cascading foreign key constraints to ensure that the foreign keys in related tables are updated in sync with any such change.

  • The primary key being a 32-bit integer instead of a varchar can save space. The choice between a int or a varchar foreign key column in every other table that references your user table can be a good reason.

  • Inserting to the primary key index is a little bit more efficient if you add new rows to the end of the index, compared to of wedging them into the middle of the index. Indexes in MySQL tables are usually B+Tree data structures, and you can study these to understand how they perform.

  • Some application frameworks prefer the convention that every table in your database has a primary key column called id, instead of using natural keys or compound keys. Following such conventions can make certain programming tasks simpler.

None of these issues are deal-breakers. And there are also advantages to using natural keys:

  • If you look up rows by username more often than you search by id, it can be better to choose the username as the primary key, and take advantage of the index-organized storage of InnoDB. Make your primary lookup column be the primary key, if possible, because primary key lookups are more efficient in InnoDB (you should be using InnoDB in MySQL).

  • As you noticed, if you already have a unique constraint on username, it seems a waste of storage to keep an extra id column you don't need.

  • Using a natural key means that foreign keys contain a human-readable value, instead of an arbitrary integer id. This allows queries to use the foreign key value without having to join back to the parent table for the "real" value.

The point is that there's no rule that covers 100% of cases. I often recommend that you should keep your options open, and use natural keys, compound keys, and surrogate keys even in a single database.

I cover some issues of surrogate keys in the chapter "ID Required" in my book SQL Antipatterns: Avoiding the Pitfalls of Database Programming.

like image 93
Bill Karwin Avatar answered Oct 13 '22 00:10

Bill Karwin