Using Indexes in MySQL

You can define a column that will be indexed in MySQL when you create a new table as a clause in the CREATE TABLE statement, or on an existing table using the CREATE INDEX command.

Defining Key Columns

You have already seen that the primary key on a table can be defined by adding the keywords PRIMARY KEY after a column in the table definition. You also can add a key definition as a separate item in the CREATE TABLE statement.

A primary key is specified using the keywords PRIMARY KEY followed by the name of the column in parentheses. You can also specify a UNIQUE KEY or UNIQUE INDEX, or simply a KEY or INDEX with no further constraints. The column name is given in parentheses after the key type.

The following statement ...

Get Sams Teach Yourself MySQL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.