KB-1413 MySQL Best Practices - Primary Key or unique index on every table

When creating new MySQL database table it is important to create a primary key or unique index.  When a primary key or unique index exists, lookup and count operations will be more efficient.

A primary key is a column that can uniquely identify every row in the table. If there is no natural key, then auto-increment can be used to create a synthetic key.

If it is still not possible to create a primary key then a unique index should be created.  A unique index is a column or set of columns that is known to be unique.

Adding a Primary Key to a New Table In phpMyAdmin

When creating a new table in phpMyAdmin, select PRIMARY from the Index dropdown on the column that will be the primary key.  Check the box in the A_I column if this will be an auto incrementing key.

Adding a Primary Key to an Existing Table in phpMyAdmin

While on the Structure tab for the table, click Primary in the Action column next to the column name.

When prompted, click OK.

Creating a Unique Key in phpMyAdmin

The easiest way to create a unique key in phpMyAdmin is to create the table first.  Once the table is created, select the check boxes next to the columns that are unique when combined, and click Unique underneath the table.

Related
Recommended