MySQL KEY关键字是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/924265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:21:32  来源:igfitidea点击:

What does the KEY keyword mean?

mysqlindexing

提问by MartinodF

In this MySQL table definition:

在这个 MySQL 表定义中:

CREATE TABLE groups (
  ug_main_grp_id smallint NOT NULL default '0',
  ug_uid smallint  default NULL,
  ug_grp_id smallint  default NULL,
  KEY (ug_main_grp_id)
);

What does the KEYkeyword mean? It's not a primary key, it's not a foreign key, so is it just an index? If so, what is so special about this type of index created with KEY?

什么是KEY关键字是什么意思?它不是主键,也不是外键,那么它只是一个索引吗?如果是这样,这种类型的索引有KEY什么特别之处?

回答by MartinodF

Quoting from http://dev.mysql.com/doc/refman/5.1/en/create-table.html

引自http://dev.mysql.com/doc/refman/5.1/en/create-table.html

{INDEX|KEY}

So KEYis an INDEX;)

所以KEY是一个INDEX;)

回答by sergtk

KEYis normally a synonym for INDEX. The key attribute PRIMARY KEYcan also be specified as just KEYwhen given in a column definition. This was implemented for compatibility with other database systems.

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

column_definition:
      data_type [NOT NULL | NULL] [DEFAULT default_value]
      [AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY]
      ...

Ref: http://dev.mysql.com/doc/refman/5.1/en/create-table.html

参考:http: //dev.mysql.com/doc/refman/5.1/en/create-table.html