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

后端 未结 5 1411
迷失自我
迷失自我 2020-11-28 01:54

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 UN         


        
相关标签:
5条回答
  • 2020-11-28 02:15

    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

    0 讨论(0)
  • 2020-11-28 02:18

    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.

    0 讨论(0)
  • 2020-11-28 02:25

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

    0 讨论(0)
  • 2020-11-28 02:25

    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

    0 讨论(0)
  • 2020-11-28 02:30

    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.

    0 讨论(0)
提交回复
热议问题