索引

分类

  1.单值索引:单列,一个表可以有多个单值索引

  2.唯一索引:不能重复,比如id,可以为null

  3.复合索引:多个列构成的索引

  4.主键索引:如果在创建语句设置字段的时候设置了primary key ,则默认为主键索引,严格来说,主键索引也是唯一索引,但是主键索引不能为null,这是他们的主要区别

创建索引

  方式一:

    create 索引类型 索引名 on 表(字段)

    单值:create index password_index on shine (password);

    唯一:create unique index name_index on shine (name);

    复合:create index name_password_index on shine (name,password);

  方式二:

    alter table 表名 add 索引类型 索引名(字段);

    单值:alter table shine add index name_index (name);

    唯一:alter table shine add unique index id_index (id);

    复合:alter table shine add index name_password_index (name,password);

删除索引:

  drop index 索引名 on 表名;

  drop index id_index on shine;

查询索引:

  show index from 表名;

  show index from shine; 

  show index from shine\G

 

注意:

  在使用的时候可以用三种方式结尾,分别是; , \g , \G

  三者都代表结束的意思。\g完全等同于“;”;这两个是以行来排列。\G就是以列来排列,这是他们的主要区别

  比如以\G结尾

  

 

 

 

  以;或者\g结尾

  

 

 

  

posted @ 2020-01-16 11:52  初雨诗清风  阅读(132)  评论(0编辑  收藏  举报