MySQL difference between the index and the key

key is the physical structure of the database, which contains two meanings, one constraint (constraint and the emphasis on the structural integrity of the specification database), the second is an index (the auxiliary queries). Including primary key, unique key, foreign key and so on.
primary key has two functions, one binding effect (constraint), for storing a specification of the primary key and unique, but also in this index is established on a Key;
UNIQUE Key has two functions, one binding effect (constraint ), the only norm data, but also established a index on the key;
Foreign key has two functions, one binding effect (constraint), standardized data referential integrity, but also to establish this key an index;
visible, mysql is of key significance constraint and index the same time, there may be the difference between this and other database performance. (At least in Oracle build on the foreign key, does not automatically index), so creating key also has the following ways:

  1. Establish a key way in the field level, such as create table t (id int not null primary key);
  2. Establish a table level constraint manner, such as create table t (id int, CONSTRAINT pk_t_id PRIMARY key (id));
  3. Establish table-level key in the manner as create table t (id int, primary key (id));

Other key to create a similar, but regardless of that way, not only the establishment of a constraint, but also the establishment of index, but the index is the key constraint or use.

index is the physical structure of the database, it is only secondary query, the structure will be stored in another table space (innodb table space in mysql) when it is created in a similar directory. Index to be classified, it is divided into prefix indexes, full-text indexing;
therefore, the index is only an index, it will not be bound to conduct field index (that is key to do).
Such as, create table t (id int, index inx_tx_id (id));

Published 80 original articles · won praise 96 · views 360 000 +

Guess you like

Origin blog.csdn.net/Alen_xiaoxin/article/details/104772078