mysql的key和index多少有點使人迷惑,這實際上考察對數據庫體系結構的瞭解的。php
1 key 是數據庫的物理結構,它包含兩層意義,一是約束(偏重於約束和規範數據庫的結構完整性),二是索引(輔助查詢用的)。包括primary key, unique key, foreign key 等。
primary key 有兩個做用,一是約束做用(constraint),用來規範一個存儲主鍵和惟一性,但同時也在此key上創建了一個index;
unique key 也有兩個做用,一是約束做用(constraint),規範數據的惟一性,但同時也在這個key上創建了一個index;
foreign key也有兩個做用,一是約束做用(constraint),規範數據的引用完整性,但同時也在這個key上創建了一個index;
可見,mysql的key是同時具備constraint和index的意義,這點和其餘數據庫表現的可能有區別。(至少在oracle上創建外鍵,不會自動創建index),所以建立key也有以下幾種方式:
(1)在字段級以key方式創建, 如 create table t (id int not null primary key);
(2)在表級以constraint方式創建,如create table t(id int, CONSTRAINT pk_t_id PRIMARY key (id));
(3)在表級以key方式創建,如create table t(id int, primary key (id));
其它key建立相似,但無論那種方式,既創建了constraint,又創建了index,只不過index使用的就是這個constraint或key。
2 index是數據庫的物理結構,它只是輔助查詢的,它建立時會在另外的表空間(mysql中的innodb表空間)以一個相似目錄的結構存儲。索引要分類的話,分爲前綴索引、全文本索引等;
所以,索引只是索引,它不會去約束索引的字段的行爲(那是key要作的事情)。
如,create table t(id int, index inx_tx_id (id));
3 最後的釋疑:
(1)咱們說索引分類,分爲主鍵索引、惟一索引、普通索引(這纔是純粹的index)等,也是基因而不是把index看做了key。
好比 create table t(id int, unique index inx_tx_id (id)); --index看成了key使用
(2)最重要的也就是,無論如何描述,理解index是純粹的index,仍是被看成key,看成key時則會有兩種意義或起兩種做用。html
實踐結果:mysql
新建一張User表,包含字段id, name。sql
(1)第一種狀況:數據庫
mysql> create table user(id int, name varchar(50), age int, primary key(id));
Query OK, 0 rows affected (0.01 sec)
oracle
mysql> show create table user;
+-------+------------------------------------
------------------------------------------+
| Table | Create Table
|
+-------+------------------------------------
------------------------------------------+
| user | CREATE TABLE `user` (
`id` int(11) NOT NULL default '0',
`name` varchar(50) default NULL,
`age` int(11) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
app
mysql> show index from user;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed |Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| user | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
1 row in set (0.00 sec)
ui
mysql> show keys from user;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed |Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| user | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
1 row in set (0.00 sec)
url
(2)第二種狀況:spa
mysql> create table user(id int, name varchar(50), age int, key(id));
Query OK, 0 rows affected (0.01 sec)
mysql> show create table user;
+-------+------------------------------
------------------------------+
| Table | Create Table
|
+-------+------------------------------
------------------------------+
| user | CREATE TABLE `user` (
`id` int(11) default NULL,
`name` varchar(50) default NULL,
`age` int(11) default NULL,
KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
mysql> show index from user;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed |Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| user | 1 | id | 1 | id | A | 0 | NULL | NULL | YES | BTREE | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
1 row in set (0.00 sec)
mysql> show keys from user;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed |Null| Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| user | 1 | id | 1 | id | A | 0 | NULL | NULL | YES | BTREE | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
1 row in set (0.00 sec)
(3)第三種狀況:
mysql> create table user(id int, name varchar(50), age int, index(id));
Query OK, 0 rows affected (0.01 sec)
mysql> show create table user;
+-------+-----------------------------
------------------------------+
| Table | Create Table
|
+-------+-----------------------------
------------------------------+
| user | CREATE TABLE `user` (
`id` int(11) default NULL,
`name` varchar(50) default NULL,
`age` int(11) default NULL,
KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
mysql> show index from user;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed |Null| Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| user | 1 | id | 1 | id | A | 0 | NULL | NULL |YES | BTREE | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
1 row in set (0.00 sec)
mysql> show keys from user;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| user | 1 | id | 1 | id | A | 0 | NULL | NULL |YES | BTREE | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
1 row in set (0.00 sec)
3、總結
(1)當建表時,創建primary key的鍵,同時默認創建對應的index
(2)當建表時,指定某列爲key時,那麼同時爲該鍵創建index,key和index對應的鍵容許null
(3)當建表時,指定某列爲index時,那麼同時爲該鍵創建key,index和key對應的鍵容許null。從建表語句中能夠看出key ‘id’ (id),等價於(2)中的狀況。
根據(1)(2)(3)說明在以上的使用狀況中,index和key沒有什麼區別。