1、通过desc 描述表结构,key字段显示有 PRI,UNI,MUL,如下:
mysql> desc student;
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| SCHOOL | varchar(64) | YES | | NULL | |
| NAME | varchar(16) | YES | UNI | NULL | |
| AGE | int(11) | NO | MUL | NULL | |
+--------+-------------+------+-----+---------+----------------+
2、表示的意思如下:
PRI:主键
UNI:唯一键索引
MUL:非唯一键索引
3、特别注意:对于多列索引,也就是多个字段组成的索引,往往只显示前导字段,也就是索引的第一个字段。
而且显示的规则有些复杂,可以通过show index查看具体的信息。如下:
mysql> create index index_name_age_school on student(name,age,school);
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc student;
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| SCHOOL | varchar(64) | YES | | NULL | |
| NAME | varchar(16) | YES | UNI | NULL | |
| AGE | int(11) | NO | MUL | NULL | |
+--------+-------------+------+-----+---------+----------------+
4 rows in set
mysql> show index from student;
+---------+------------+-----------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+-----------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| student | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| student | 0 | index_name | 1 | NAME | A | 0 | NULL | NULL | YES | BTREE | | |
| student | 1 | index_age | 1 | AGE | A | 0 | NULL | NULL | | BTREE | | |
| student | 1 | index_name_age_school | 1 | NAME | A | 0 | NULL | NULL | YES | BTREE | | |
| student | 1 | index_name_age_school | 2 | AGE | A | 0 | NULL | NULL | | BTREE | | |
| student | 1 | index_name_age_school | 3 | SCHOOL | A | 0 | NULL | NULL | YES | BTREE | | |
+---------+------------+-----------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
6 rows in set
- 参见