Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
819 views
in Technique[技术] by (71.8m points)

indexing - What's the difference between using INDEX vs KEY in MySQL?

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), 
  ....


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

...
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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There's no difference. They are synonyms.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...