English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

What does the KEY keyword mean in MySQL?

A key is a synonym for an index. If you want to create an index for a column, please use 'key'.

As per the official documentation:

KEY is often a synonym for INDEX. When the attribute PRIMARY KEY is given in the column definition, it can also be specified as KEY. This is done to ensure compatibility with other database systems.

This key can be used with the primary key:

Let's create a table first. This is the query to set the primary key for the column 'id'.

mysql> create table KeyDemo
    -> (
    -> id int,
    -> primary key(id)
    -> );

Insert two records.

mysql> insert into KeyDemo values(1);
mysql> insert into KeyDemo values(2);

Display all records.

mysql> select *from KeyDemo;

This is the output.

+----+
| id |
+----+
|  1 |
|  2 |
+----+
2 rows in set (0.00 sec)