What does the KEY keyword mean in MySQL?

MySQLMySQLi Database

Key is synonymous to an index. If you want to create an index for a column, then use ‘Key’.

As stated in the official docs:

KEY is normally a synonym for INDEX. The key attribute PRIMARY KEY can also be specified as just KEY when given in a column definition. This was implemented for compatibility with other database systems.

The key can be used with Primary Key:

Let us first create a table. Here is the query to set primary key for a column “id”.

mysql> create table KeyDemo
    -> (
    -> id int,
    -> primary key(id)
    -> );
Query OK, 0 rows affected (0.55 sec)

Inserting two records.

mysql> insert into KeyDemo values(1);
Query OK, 1 row affected (0.12 sec)

mysql> insert into KeyDemo values(2);
Query OK, 1 row affected (0.14 sec)

To display all the records.

mysql> select *from KeyDemo;

Here is the output.

+----+
| id |
+----+
|  1 |
|  2 |
+----+
2 rows in set (0.00 sec)
raja
Published on 20-Nov-2018 17:33:03
Advertisements