245

I know how to use INDEX as in the following code. And I know how to use foreign key and primary key.

CREATE TABLE tasks ( 

    task_id        int unsigned NOT NULL AUTO_INCREMENT, 
    parent_id      int unsigned NOT NULL DEFAULT 0, 
    task           varchar(100) NOT NULL, 
    date_added     timestamp    NOT NULL, 
    date_completed timestamp        NULL, 

    PRIMARY KEY ( task_id ), 
    INDEX parent ( parent_id )
)

However I found a code using KEY instead of INDEX as following.

CREATE TABLE orders (
    
    order_id        int unsigned NOT NULL AUTO_INCREMENT,
    -- etc 

    KEY order_date ( order_date )
)

I could not find any explanation on the official MySQL page. Could anyone tell me what is the differences between KEY and INDEX?

The only difference I see is that when I use KEY ..., I need to repeat the word, e.g. KEY order_date ( order_date ).

Dai
  • 141,631
  • 28
  • 261
  • 374
shin
  • 31,901
  • 69
  • 184
  • 271

5 Answers5

338

There's no difference. They are synonyms, though INDEX should be preferred (as INDEX is ISO SQL compliant, while KEY is a MySQL-specific, non-portable, extension).

From the CREATE TABLE manual entry:

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.


By "The key attribute PRIMARY KEY can also be specified as just KEY when given in a column definition.", it means that these three CREATE TABLE statements below are equivalent and generate identical TABLE objects in the database:

CREATE TABLE orders1 (
    order_id int PRIMARY KEY
);

CREATE TABLE orders2 (
    order_id int KEY
);

CREATE TABLE orders3 (
    order_id int NOT NULL,

    PRIMARY KEY ( order_id )
);

...while these 2 statements below (for orders4, orders5) are equivalent with each other, but not with the 3 statements above, as here KEY and INDEX are synonyms for INDEX, not a PRIMARY KEY:

CREATE TABLE orders4 (
    order_id int NOT NULL,

    KEY ( order_id )
);

CREATE TABLE orders5 (
    order_id int NOT NULL,

    INDEX ( order_id )
);

...as the KEY ( order_id ) and INDEX ( order_id ) members do not define a PRIMARY KEY, they only define a generic INDEX object, which is nothing like a KEY at all (as it does not uniquely identify a row).

As can be seen by running SHOW CREATE TABLE orders1...5:

Table SHOW CREATE TABLE...
orders1 CREATE TABLE orders1 (
order_id int NOT NULL,
PRIMARY KEY ( order_id )
)
orders2 CREATE TABLE orders2 (
order_id int NOT NULL,
PRIMARY KEY ( order_id )
)
orders3 CREATE TABLE orders3 (
order_id int NOT NULL,
PRIMARY KEY ( order_id )
)
orders4 CREATE TABLE orders4 (
order_id int NOT NULL,
KEY ( order_id )
)
orders5 CREATE TABLE orders5 (
order_id int NOT NULL,
KEY ( order_id )
)
Dai
  • 141,631
  • 28
  • 261
  • 374
nos
  • 223,662
  • 58
  • 417
  • 506
  • 1
    When looking at table info in the latest version of SQLYog it shows `KEY keyname (column_name)` when I created the table with `INDEX keyname (column_name)`. The fact that it is a synonym explains it perfectly. – crmpicco Apr 02 '14 at 10:10
  • 2
    "The key attribute `PRIMARY KEY` can also be specified as just `KEY` when given in a column definition." What does this mean? – Zyl Oct 05 '20 at 09:13
  • "The key attribute `PRIMARY KEY` can also be specified as just KEY when given in a column definition." please explain how?. – Naeem Ul Wahhab Oct 17 '22 at 16:29
  • 2
    @Zyl I've updated this answer to answer your question. – Dai Dec 10 '22 at 08:24
  • 2
    @NaeemUlWahhab I've updated this answer to answer your question. – Dai Dec 10 '22 at 08:24
15

Here is a nice description about the "difference":

"MySQL requires every Key also be indexed, that's an implementation detail specific to MySQL to improve performance."

informatik01
  • 16,038
  • 10
  • 74
  • 104
Stefan
  • 10,010
  • 7
  • 61
  • 117
  • 5
    Question is about the use of SQL Identifiers KEY and INDEX in MySQL. Not the difference between a key and an index. – Josh J Feb 02 '17 at 21:44
  • 1
    I searched for the difference with a search engine and this was the first result. In my opinion there is no need to open an extra question. But feel free to do so if you want to. – Stefan Mar 25 '20 at 16:00
10

Keys are special fields that play very specific roles within a table, and the type of key determines its purpose within the table.

An index is a structure that RDBMS(database management system) provides to improve data processing. An index has nothing to do with a logical database structure.

SO...

Keys are logical structures you use to identify records within a table and indexes are physical structures you use to optimize data processing.

Source: Database Design for Mere Mortals

Author: Michael Hernandez

JayD
  • 6,173
  • 4
  • 20
  • 24
  • 6
    Question is about the use of SQL Identifiers KEY and INDEX in MySQL. Not the difference between a key and an index. – Josh J Feb 02 '17 at 21:43
  • Really?! I read the question differently, and this answer in my opinion is the only one that states what's the difference right. – funder7 Jun 24 '22 at 18:11
7

It is mentioned as a synonym for INDEX in the 'create table' docs: MySQL 5.5 Reference Manual :: 13 SQL Statement Syntax :: 13.1 Data Definition Statements :: 13.1.17 CREATE TABLE Syntax

@Nos already cited the section and linked the help for 5.1.

Like PRIMARY KEY creates a primary key and an index for you, KEY creates an index only.

fineliner
  • 152
  • 2
  • 5
0

A key is a set of columns or expressions on which we build an index.

  1. While an index is a structure that is stored in database, keys are strictly a logical concept.

  2. Index help us in fast accessing a record, whereas keys just identify the records uniquely.

  3. Every table will necessarily have a key, but having an index is not mandatory.

Check on https://docs.oracle.com/cd/E11882_01/server.112/e40540/indexiot.htm#CNCPT721

  • 4
    Question is about the use of SQL Identifiers KEY and INDEX in MySQL. Not the difference between a key and an index. – Josh J Feb 02 '17 at 21:42
  • 1
    @Josh J Even if the original motivation was to ask about identifiers, searching for the difference about a key and an index with a search engine yields this topic. It could make sense to improve the title of the question to be more specific. On the other hand, writing a question about the difference between a key and an index in mysql would be possibly marked as a duplicate. =>I find such complementary answers very useful. The "real" answer will get the highest score, anyway. So I do not see a reason for negative votes on complementary answers. – Stefan Apr 08 '18 at 06:00
  • I have capitalized INDEX and KEY in the question to make this clear. – reinierpost Oct 17 '18 at 13:16