Open Answers

MySQL Tables Want Integer ID Columns

Most Tables Need Unique Columns

Why Unique Columns Should be Meaningless, Auto-Incrementing Integers

When an ID Column is Not Needed

A unique integer ID is not needed when the row has other unique, internal data, usually a combination of other table IDs, and there is no need to reference the row externally.

This is often the case with joiner tables.

For example, a system where users submit ratings on stories might have a table like this:

CREATE TABLE 'story_ratings' (
  'story_id' INT4 NOT NULL default 0,
  'user_id' INT4 NOT NULL default 0,
  'rating' INT1 NOT NULL default 0,
  UNIQUE 'story_user' ('story_id','user_id'),
  KEY 'user' ('user_id')
) ENGINE=MyISAM;

Rows in this table can be referred to by the UNIQUE index on (story_id, user_id). The index will also enforce the business rule that users cannot submit two ratings for the same story.

How to Create Unique Auto-Incrementing Integer Indexes

The standard is to name unique ID columns "id", "ID", or after the table name, such as "book_id". Choose one and be consistent across your database tables.

When creating your table, make this the first field:

id INT4  NOT NULL AUTO_INCREMENT

When inserting new rows, either set the 'id' column to NULL, or do not set the 'id' column at all, and it will automatically receive a unique number.