索引是一种 特殊的数据库结果,由数据表中的一列或多列组合而成,可以用来快速查询数据表中某一些特定值的记录。
通过索引,查询数据是不用读完记录的所有信息,而只是查询索引列。否则,数据库系统将读取每条记录的所有信息进行匹配。
可以把索引必做新华字典的音序表。例如,要查‘‘库’’字,如果不使用音序,就需要从字典的400页中逐页来找,但是,如果提取拼音出来,构成音序表,就只需要从10多页的音序表中直接查找。这样就可以大大节省时间。
因此,使用索引可以很大程度上提高数据库的查询速度,还有效的提高了数据库系统的性能。
索引就是根据表中的一列或若干列按照一定顺序建立的列值与记录之间的对应关系表,实质上是一张描述索引列的列值与原表中记录行之间一一对应关系的有序表。
索引是MySQL中十分重要的数据库对象,是数据库性能调优技术的基础,常用于实现数据的快速检索。
顺序访问是在表中实行全表扫描,从头到尾逐行遍历,字到在无序的行数据中找到符合条件目标数据。
索引访问是通过遍历索引来直接访问表中记录的方式。
使用这种方式的前提是对表建立一个索引,在列上创建索引后,查找数据时可以直接根据该列上的索引找到对应记录行的位置,从而快捷的查找到数据。索引存储了指定列数据值的指针,根据指定的排序顺序对这些指针排序。
例如:
在学生基本信息表tb_students中,若果基于student_id建立了索引,系统就建立了一张索引到实际记录的映射表。当用户需要查找student_id为120022的数据的时候,系统现在student_i索引上找到该记录,然后通过映射表直接找到数据行,并且返回该行数据。因为扫描索引的速度一般远远大于扫描实际数据行的速度,所以采用索引的方式可以大大提高数据库的工作效率。
简而言之。不使用索引,MySQL就必须从第一条记录开始读完整个表,直到找出相关的行。表越大,查询数据所花费的时间就越多。如果表中查询的列有一个索引,MySQL就能快速的到达一个位置去收索数据文件,而不必查看所有的数据,这样将会节省很大一部分时间。
通过创建唯一索引可以保证数据库表中每一行数据的唯一性。
可以给所有的MySQL列类型设置索引。
可以大大加快数据的查询速度,这是使用索引最主要的原因。
在实现数据的参考完整性方面可以加速表与表之间的连接。
在使用分组和排序子句进行数据查询时也可以显著减少查询中分组和排序的时间
数据库索引可以分为以下几类:
MySQL提供了三种创建索引的方法:
可以使用create index在一个已有的表上创建索引,但该语句不能创建主键。
create 索引名 on 表名...
索引也可以在创建表的同时创建。在create table语句后添加语句:
constraint primary key [索引类型]...
在create table语句中添加以下语句,表示创建新表的同时创建该表的主键。
key | index [索引名]...
在create table语句中添加以下语句,表示在创建新表的同时创建该表的索引。
unique [index | key] [索引名]...
在create table语句中添加以下语句,表示在创建新表的同时创建该表的唯一性索引。
foreign key 索引名 列名...
在create table中添加此语句,表示在创建新表的同时创建该表的外键。
在使用create table定义列选项是,可以通过直接在某个列后面添加primary key的方式来创建主键。而当主键是有多个列组合成的多列索引时,则不能使用这种方法,只能在语句的最后加上一个primary key来实现
create index语句可以在一个已有的表上创建索引,alter table语句也可以在一个已有的表上创建索引。在使用alter table语句修改表的同时,可以向已有的表添加索引。具体的做法是在alter table语句中添加一下语句的某一项或几项。
add index [索引名]
在alter table语句中添加此语句,表示在修改表的同时为该表添加索引。
add primary key [索引类型]
在alter table中添加此语句,表示在修改表的同时为该表添加主键。
add unique [index | key] 索引名
在alter table语句中添加此语句,表示在修改表的同时为该表添加唯一性索引。
add foreign key[索引名]...
在alter table 中添加此语句,表示在修改表的同时为该表添加外键。
##创建member表
mysql> create table member(id int(10),name varchar(10),cardid varchar(18),phone varchar(11),address varchar(50),remark text);
> desc member;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
| cardid | varchar(18) | YES | | NULL | |
| phone | varchar(11) | YES | | NULL | |
| address | varchar(50) | YES | | NULL | |
| remark | text | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
##插入数据
mysql> insert into member values(1,'wangyi','10010','111111','wuhan','this is vip');
Query OK, 1 row affected (0.01 sec)
mysql> insert into member values(2,'wanger','10020','222222','changsha','this is vip');
Query OK, 1 row affected (0.01 sec)
mysql> insert into member values(3,'wangsan','10030','333333','qingdao','this is normal');
Query OK, 1 row affected (0.01 sec)
mysql> insert into member values(3,'wangsi','10040','444444','nanjing','this is normal');
Query OK, 1 row affected (0.01 sec)
mysql> insert into member values(3,'wangwu','10050','555555','beijing','this is vip');
Query OK, 1 row affected (0.01 sec)
mysql> select * from member;
+------+---------+--------+--------+----------+----------------+
| id | name | cardid | phone | address | remark |
+------+---------+--------+--------+----------+----------------+
| 1 | wangyi | 10010 | 111111 | wuhan | this is vip |
| 2 | wanger | 10020 | 222222 | changsha | this is vip |
| 3 | wangsan | 10030 | 333333 | qingdao | this is normal |
| 3 | wangsi | 10040 | 444444 | nanjing | this is normal |
| 3 | wangwu | 10050 | 555555 | beijing | this is vip |
+------+---------+--------+--------+----------+----------------+
5 rows in set (0.00 sec)
mysql> create table member2 select * from member;
Query OK, 5 rows affected (0.03 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from member2;
+------+---------+--------+--------+----------+----------------+
| id | name | cardid | phone | address | remark |
+------+---------+--------+--------+----------+----------------+
| 1 | wangyi | 10010 | 111111 | wuhan | this is vip |
| 2 | wanger | 10020 | 222222 | changsha | this is vip |
| 3 | wangsan | 10030 | 333333 | qingdao | this is normal |
| 3 | wangsi | 10040 | 444444 | nanjing | this is normal |
| 3 | wangwu | 10050 | 555555 | beijing | this is vip |
+------+---------+--------+--------+----------+----------------+
5 rows in set (0.00 sec)
创建普通索引,通常使用index关键字。
create index 索引名 on 表名...
mysql> create index phone_index on member(phone);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table member;
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table
|
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| member | CREATE TABLE `member` (
`id` int DEFAULT NULL,
`name` varchar(10) DEFAULT NULL,
`cardid` varchar(18) DEFAULT NULL,
`phone` varchar(11) DEFAULT NULL,
`address` varchar(50) DEFAULT NULL,
`remark` text,
KEY `phone_index` (`phone`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
alter table 表名 add index 索引名(列名);
mysql> alter table member add index id_index(id);
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table member;
+--------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table
|
+--------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| member | CREATE TABLE `member` (
`id` int DEFAULT NULL,
`name` varchar(10) DEFAULT NULL,
`cardid` varchar(18) DEFAULT NULL,
`phone` varchar(11) DEFAULT NULL,
`address` varchar(50) DEFAULT NULL,
`remark` text,
KEY `phone_index` (`phone`),
KEY `id_index` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+--------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> create table test( id int(4) not null,name varchar(10) not null ,cardid varchar(18) not null,index id_index(id));
Query OK, 0 rows affected, 1 warning (0.04 sec)
mysql> show create table test;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table
|
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test | CREATE TABLE `test` (
`id` int NOT NULL,
`name` varchar(10) NOT NULL,
`cardid` varchar(18) NOT NULL,
KEY `id_index` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
创建唯一索引,通常使用unique关键字
mysql> create unique index address_index on member(address);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table member;
+--------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table
|
+--------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| member | CREATE TABLE `member` (
`id` int DEFAULT NULL,
`name` varchar(10) DEFAULT NULL,
`cardid` varchar(18) DEFAULT NULL,
`phone` varchar(11) DEFAULT NULL,
`address` varchar(50) DEFAULT NULL,
`remark` text,
UNIQUE KEY `address_index` (`address`),
KEY `phone_index` (`phone`),
KEY `id_index` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+--------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> alter table member add unique cardid_index(cardid);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table member;
+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table
|
+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| member | CREATE TABLE `member` (
`id` int DEFAULT NULL,
`name` varchar(10) DEFAULT NULL,
`cardid` varchar(18) DEFAULT NULL,
`phone` varchar(11) DEFAULT NULL,
`address` varchar(50) DEFAULT NULL,
`remark` text,
UNIQUE KEY `address_index` (`address`),
UNIQUE KEY `cardid_index` (`cardid`),
KEY `phone_index` (`phone`),
KEY `id_index` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> create table test0 (id int,name varchar(20),unique id_index(id));
Query OK, 0 rows affected (0.04 sec)
mysql> show create table test0;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table
|
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test0 | CREATE TABLE `test0` (
`id` int DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
UNIQUE KEY `id_index` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)