186

In this MySQL table definition:

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 KEY keyword 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?

2
  • 2
    It appears as though the "KEY" operator is no longer in use in MySQL Server 5.5. Not sure when it was removed, but an issue I'm having with it is present on 5.1, but not 5.5.
    – slant
    Nov 5, 2012 at 23:03
  • 1
    See also stackoverflow.com/questions/1401572/…
    – Yves M.
    Sep 4, 2019 at 15:19

2 Answers 2

225

Quoting from create-table - indexes andkeys

{INDEX|KEY}

So KEY is usually an INDEX

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.

11
  • 13
    Notation {INDEX|KEY} in general does not mean that INDEX and KEY have the same meaning, or KEY is an INDEX ;)
    – sergtk
    Jan 4, 2014 at 0:45
  • 1
    @sergtk: As per the notation in the MySQL manual, FOO|BAR means that either the keyword FOO or the keyword BAR can be used. Id est, they are synonyms.
    – dotancohen
    Jan 6, 2014 at 17:33
  • 5
    Exactly! MySql manual clarifies that INDEX and KEY are synonyms - this is why I upvoted the answer. But it can be concluded from the answer that BNF Notation {|} described synonyms always, not only in this case.
    – sergtk
    Jan 6, 2014 at 21:28
  • 5
    This is not correct. I found the best answer in Quora (see user Kristian Kohntopp) quora.com/…: KEY is a constraint, INDEX is the data structure necessary to implement that constraint. In practice, if you have a FK for example KEY key_name (user_id), CONSTRAINT foreign_key_constraint_name FOREIGN KEY (user_id) REFERENCES auth_user (id) then you might additionally want to specify what INDEX is used (HASH vs BTREE). This example shows KEY and INDEX aren't synonyms.
    – user1950164
    Jan 31, 2017 at 12:19
  • 9
    Nobody ever explained what's the use of that keyword, if it's obsolete or not. If I have a primary key, then why have a "key" too? what is the point of two indexes? plz enlighten me I can't find anything about this Jun 28, 2019 at 18:52
38

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.

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.