在 MySQL 中使用 INDEX 和 KEY 有什么区别?

2021-11-20 00:00:00 indexing key mysql

我知道如何在以下代码中使用 INDEX.我知道如何使用外键和主键.

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, 
  PRIMARY KEY (task_id), 
  INDEX parent (parent_id), 
  ....


但是我发现了一个使用 KEY 而不是 INDEX 的代码,如下所示.


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

...
KEY order_date (order_date) 
...


我在 MySQL 官方页面上找不到任何解释.谁能告诉我 KEY 和 INDEX 之间有什么区别?


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

我看到的唯一区别是当我使用KEY ...时,我需要重复这个词,例如
KEY order_date(order_date).

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

推荐答案

没有区别.它们是同义词.

There's no difference. They are synonyms.

来自 CREATE TABLE 手动输入:

From the CREATE TABLE manual entry:

KEY 通常是 INDEX 的同义词.键属性 PRIMARY KEY 可以在列定义中给出时,也可以仅指定为 KEY.这是为与其他数据库系统兼容而实施.

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.

相关文章