您的位置:首页 > 数据库 > MySQL

MySQL--- 索引

2019-07-05 21:15 218 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/weixin_44342108/article/details/94765783

一、索引的简介

​ 在数据库中可以建立类似的目录的数据库对象,实现数据的快速查询,这就是索引。索引是将表中的一个或者多个 字段的值按照特定结构进行排序后储存。

二、索引的分类

​ 索引可为:

  1. 普通索引,它是最基本的索引,没有任何限制。

  2. 唯一索引,如果某个字段的值唯一,那么在这个字段创建索引的时候就可以使用关键字UNIQUE 把它定 义为一个唯一索引。创建唯一索引的好处:简化了 MySQL 对索引的管理工作,唯一索引也因此而变得更有效率。

  3. 主键索引,主键索引是为主键字段设置的索引,是一种特殊的唯一索引。主键索引与唯一索引的区别是在于:前者在定义时使用的关键字是 PRIMARY KEY,而后者使的是 UNIQUE;前者定义索引的字段值不允许有空值,而后者允许。

  4. 全文索引,全文索引适用于在一大串文本中进行查找,并且创建该类型索引的字段的数据类型必须是 CHAR、VARCHAR 或者 TEXT。

  5. 空间索引,设置为空间索引字段的数据类型必须是空间数据类型,如 GEOMETRY、POINT、LINESTRING、POLYGON,并且该字段必须设置为 NOT NULL。

  6. 复合索引,复合索引是指在多个字段上创建的索引,这种索引只有在查询条件中使用了创建索引时的第一个字段,该索引才会被触发,这是因为使用复合索引时遵循“最左前缀”的原因。例如:当索引字段为(id, name)时,只有查询条件中适用了 id 字段,该索引才会被使用;如果查询条件中只有 name 字段是不会使用该索引的。

    三、创建索引

    ​ 索引的创建有两种情况:(1)自动创建索引:当在表中定义一个PRIMRY KEY或者UNIQUE 约束条件,MySQL数据库会自动创建一个对应的主键索引或者唯一索引。(2)手动创建索引:用户可以在创建表时创建索引,也可以为已存在的表添加索引。

    1、自动创建索引

    create table student1(
    stu_id(10)primary key,
    stu_name varchar(20) unique,
    stu_sex varchar(1)
    );
    show index from student1; #查看索引

    2、手动创建索引

    ​ 手动创建索引可以在创建表的时候使用“CREATE TABLE”语句来指定表中的指定字段创建索引,也可以使用“CREATE INDEX”或者“ALTER TABLE” 语句来为已经存在的表创建索引。

    a. 在创建表时创建索引

    #创建普通的索引
    create table student2(
    stu_id int(3),
    stu_name varchar(20),
    stu_age int(3),
    index(stu_id)/unique index(stu_id)/primary key(stu_id)/fulltext index(stu_id)#分别创建了普通索引,唯一索引,主键索引,全文索引
    );
    show index from student2;#查看索引

    ​ 空间索引的创建

    '''
    创建空间索引时需要注意,索引字段的数据类型必须是空间数据类型,如 GEOMETRY、
    POINT、LINESTRING、POLYGON,并且该字段必须设置为 NOT NULL,否则会提示“All
    parts of a SPATIAL index must be NOT NULL”错误。空.
    '''
    create table student4(
    stu_id int(3),
    stu_name varchar(20),
    stu_loc point not null, #空间索引必须是空间数据类型并且非空
    spatial index(stu_loc) #spatial 为空间索引的关键字
    );

    ​ 复合索引的创建

    create table student5(
    stu_id int(2),
    stu_name varchar(20),
    stu_age int(2),
    index(stu_id,stu_name)
    );

    b. 在已存在的表添加索引

    1)用create index 创建​

    #普通索引
    create index index_id on student5(stu_id);#index_id为创建索引名
    #创建唯一索引
    create unique index index_id on student5;
    #创建全文索引
    create fulltext index index_id on student5;
    #创建空间索引
    create spatial index index_loc on student5;
    #创建复合索引
    create index index_id on student5(stu_id,stu_name);
    [ol] 用alter table 创建索引
#普通索引
alter table student6 add index index_id(stu_id);
#唯一索引
alter table student6 add unique index index_id(stu_id);
#主键索引
alter table student6 add primary key(stu_id);
#全文索引
alter table student6 add fulltext index index_info(stu_info);
#空间索引
alter table student6 add spatial index index_loc(stu_loc);
#创建复合索引
alter table student6 add index index_id(stu_id,stu_name);

四、删除索引

1、使用 alter table 语句删除索引

alter table student6 drop index index_name; # index_name 是索引名称

2、使用drop index 删除索引

drop index index_name on table_name;

show index from table_name; #查看是否删除成功

注意:

​ 使用“alter table table_name drop index|key index_name;”或者“drop index index_name on

table_name;”语法格式的 SQL 语句并不能删除主键索引;如果要删除主键索引,需要使用“alter
table table_name drop primary key”语句。

[/ol]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: