索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部分),它们包含着对数据表里所有记录的引用指针。
更通俗的说,数据库索引好比是一本书前面的目录,能加快数据库的查询速度。
索引的类型
1、普通索引 2、唯一性索引 3、主键索引(主索引) 4、复合索引 1. 普通索引最基本的索引,不具备唯一性,就是加快查询速度
1.1 创建普通索引 1.1.1 方法一,创建表时添加索引语法:
create table 表名( 列定义 index 索引名称 (字段) index 索引名称 (字段) )注:可以使用key关键字,也可以使用index关键字。索引名称,可以加也可以不加,不加使用字段名作为索引名。
MariaDB [book]> desc books; +------------+------------------------------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+------------------------------------------------+------+-----+---------+----------------+ | bId | int(4) | NO | PRI | NULL | auto_increment | | bName | varchar(255) | YES | | NULL | | | bTypeId | enum('1','2','3','4','5','6','7','8','9','10') | YES | | NULL | | | publishing | varchar(255) | YES | | NULL | | | price | int(4) | YES | | NULL | | | pubDate | date | YES | | NULL | | | author | varchar(30) | YES | | NULL | | | ISBN | varchar(255) | YES | | NULL | | +------------+------------------------------------------------+------+-----+---------+----------------+ 8 rows in set (0.00 sec) MariaDB [book]> create table demo(id int(4),name varchar(20),pwd varchar(20),index(pwd)); Query OK, 0 rows affected (0.08 sec) MariaDB [book]> create table demo1(id int(4),name varchar(20),pwd varchar(20),key(pwd)); Query OK, 0 rows affected (0.01 sec) MariaDB [book]> create table demo2(id int(4),name varchar(20),pwd varchar(20),key index_pwd(pwd)); # 为索引加上名称 Query OK, 0 rows affected (0.02 sec)注意
index和 key作用是相同的
1.1.2 方法二,当表创建完成后,使用alter为表添加索引:语法:
alter table 表名 add index 索引名称 (字段1,字段2.....);例子:
MariaDB [book]> desc books; +------------+------------------------------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+------------------------------------------------+------+-----+---------+----------------+ | bId | int(4) | NO | PRI | NULL | auto_increment | | bName | varchar(255) | YES | | NULL | | | bTypeId | enum('1','2','3','4','5','6','7','8','9','10') | YES | | NULL | | | publishing | varchar(255) | YES | | NULL | | | price | int(4) | YES | | NULL | | | pubDate | date | YES | | NULL | | | author | varchar(30) | YES | | NULL | | | ISBN | varchar(255) | YES | | NULL | | +------------+------------------------------------------------+------+-----+---------+----------------+ 8 rows in set (0.00 sec) MariaDB [book]> alter table books add key (bName,bTypeId); Query OK, 44 rows affected (0.01 sec) Records: 44 Duplicates: 0 Warnings: 0 MariaDB [book]> desc books; # 可以看到在bName字段上添加的索引 +------------+------------------------------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+------------------------------------------------+------+-----+---------+----------------+ | bId | int(4) | NO | PRI | NULL | auto_increment | | bName | varchar(255) | YES | MUL | NULL | | | bTypeId | enum('1','2','3','4','5','6','7','8','9','10') | YES | | NULL | | | publishing | varchar(255) | YES | | NULL | | | price | int(4) | YES | | NULL | | | pubDate | date | YES | | NULL | | | author | varchar(30) | YES | | NULL | | | ISBN | varchar(255) | YES | | NULL | | +------------+------------------------------------------------+------+-----+---------+----------------+ 8 rows in set (0.00 sec) 1.2 查看索引语法:
desc 索引名(或索引字段)例子:
MariaDB [book]> desc demo; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(4) | YES | | NULL | | | name | varchar(20) | YES | | NULL | | | pwd | varchar(20) | YES | MUL | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) MariaDB [book]> desc demo1; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(4) | YES | | NULL | | | name | varchar(20) | YES | | NULL | | | pwd | varchar(20) | YES | MUL | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) MariaDB [book]> desc demo2; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(4) | YES | | NULL | | | name | varchar(20) | YES | | NULL | | | pwd | varchar(20) | YES | MUL | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) MariaDB [book]> desc books; +------------+------------------------------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+------------------------------------------------+------+-----+---------+----------------+ | bId | int(4) | NO | PRI | NULL | auto_increment | | bName | varchar(255) | YES | MUL | NULL | | | bTypeId | enum('1','2','3','4','5','6','7','8','9','10') | YES | | NULL | | | publishing | varchar(255) | YES | | NULL | | | price | int(4) | YES | | NULL | | | pubDate | date | YES | | NULL | | | author | varchar(30) | YES | | NULL | | | ISBN | varchar(255) | YES | | NULL | | +------------+------------------------------------------------+------+-----+---------+----------------+ 8 rows in set (0.00 sec)