索引

索引

索引是对记录集的多个字段进行排序的方法。
就像书的目录。

类型:
Btree、B+tree、hash

优点:
通过创建唯一性索引,可以保证数据库表中每一行数据的唯一性,可以加快数据的检索速度。

缺点:
当对表中的数据进行增加/删除/修改的时候,索引也要动态的维护,降低了数据的维护速度。
索引需要占用物理空间
###################################################################################
键值类型

index:普通索引
unique:唯一索引
fulltext:全文索引
primary key:主键
foreign key:外键
###################################################################################
index普通索引

使用说明:
一个表中可以有多个index字段
字段的值允许有重复,且可以赋null值
经常把做查询条件的字段设置为index字段
index普通索引字段的key标志是mul
###################################################################################
在建表时创建普通索引:(创建普通索引时可以创建一个或者多个)
注意:当某个字段建立index索引后,其key字段的值为MUL

用法:
index(字段1),index(字段2)

MariaDB [db1]> create table c(
-> id char(6) not null,
-> name varchar(4) not null,
-> age int(3) not null,
-> gender enum("girl","boy") default "boy",
-> index(id),index(name)
-> );


MariaDB [db1]> desc c;
+--------+--------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------------+------+-----+---------+-------+
| id | char(6) | NO | MUL | NULL | | 当此字段建立index索引后,其key字段的值为MUL
| name | varchar(4) | NO | MUL | NULL | | 当此字段建立index索引后,其key字段的值为MUL
| age | int(3) | NO | | NULL | |
| gender | enum('girl','boy') | YES | | boy | |
+--------+--------------------+------+-----+---------+-------+
###################################################################################
创建索引:(在已建立号的表上建立索引)

用法:
create index 索引名 on 表名(字段名);

MariaDB [db1]> create index age on c(age);

MariaDB [db1]> desc c;
+--------+--------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------------+------+-----+---------+-------+
| id | char(6) | NO | MUL | NULL | |
| name | varchar(4) | NO | MUL | NULL | |
| age | int(3) | NO | MUL | NULL | |
| gender | enum('girl','boy') | YES | | boy | |
+--------+--------------------+------+-----+---------+-------+

常见错误:
MariaDB [db1]> create index age on c; 忘记在表名后面写(字段名)了,所以报错
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1
###################################################################################
删除索引

用法:
drop index 索引名 on 表名;

MariaDB [db1]> drop index name on c;

MariaDB [db1]> desc c;
+--------+--------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------------+------+-----+---------+-------+
| id | char(6) | NO | MUL | NULL | |
| name | varchar(4) | NO | | NULL | |
| age | int(3) | NO | MUL | NULL | |
| gender | enum('girl','boy') | YES | | boy | |
+--------+--------------------+------+-----+---------+-------+
##################################################################################
查看表的索引信息
show index from 表名\G;


MariaDB [db1]> show index from c\G;
*************************** 1. row ***************************
Table: c
Non_unique: 1
Key_name: id 索引名
Seq_in_index: 1
Column_name: id 字段名
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE 可以看到使用BTREE(二叉树) 算法
Comment:
Index_comment:
*************************** 2. row ***************************
Table: c
Non_unique: 1
Key_name: age
Seq_in_index: 1
Column_name: age
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE 可以看到使用B树算法
Comment:
Index_comment:
#############################################################################
student表----------》数据库目录/库名/student.frm
.ibd

 

posted @ 2019-04-30 22:22  安于夏  阅读(98)  评论(0编辑  收藏  举报