码迷,mamicode.com
首页 > 数据库 > 详细

(2.8)Mysql之SQL基础——索引的分类与使用

时间:2019-02-14 23:37:42      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:uniq   聚集   test   创建   from   ima   length   bsp   分类   

(2.8)Mysql之SQL基础——索引的分类与使用

 

按逻辑分类:

1、主键索引(聚集索引)(也是唯一索引,不允许有空值)

2、普通索引或单列索引

3、多列索引(复合索引)

4、唯一索引或非唯一索引(非唯一索引其实就是普通/多列索引)

5、空间索引

6、创建索引的基本形式

7、索引的操作

1.查看索引
2.创建单列索引
3.复合索引
4.唯一索引(允许多个空值,每列唯一)
5.主键索引(不允许空值,唯一)
6.索引的删除
7.删除自增auto_increment

 

6.创建索引的基本形式

create [unique|fulltext|spatial] index index_name

[index_type]

  on table_name(index_col_name,...)

[index_option]

[alogorithm_option | lock_option]...

index_colname:

  col_name[(length)][asc | desc]

1.
[unique|fulltext|spatial] 可选参数,分别是唯一索引、全文索引、空间索引
2.index 创建索引的关键字,或者也可以用(key)
3.index_col_name 表中要创建索引的列对象
4.index_name 创建的索引名字
5.length 可选参数,索引的长度,只能用于字符串
6.[asc | desc] 索引值得存储方式

最简单最常用的方式:
  create index 索引名 on 表名(列名);
  create index ix_test101_id on test101(id);
  create index ix_test101_name on test101(name(10)); #截取该字段前10个字符作为索引
  alter table test101 add index 索引名(列名);

 

7、索引的操作

0.建表时创建索引

  
create table test102(
id int primary key auto_increment,
name varchar(12),
description varchar(200),
index ix_test102_description(description)
);

1.查看索引   show index from table_name; 2.单列索引   create index 索引名 on 表名(列名);   create index ix_test101_id on test101(id);   create index ix_test101_name on test101(name(10)); #截取该字段前10个字符作为索引   alter table test101 add index 索引名(列名); 3.复合索引   create index 索引名 on 表名(列名1,列名2);   alter table test101 add index 索引名(列名1,列名2); 4.唯一索引(允许多个空值,每列唯一)
  create unique index 索引名 on 表名(列名);
  alter table test101 add unique index 索引名(列名);
5.主键索引(不允许空值,唯一)
  alter table test101 add primary key (列名)
  
6.索引的删除   1).单列/多列/唯一索引删除:drop index 索引名 on 表名; or alter table test101 drop
  2).主键索引删除: alter table test101 drop primary key;(如果有自增字段,需要先删除自增)

7.删除自增auto_increment
  alter table test101 change id int;

 

(2.8)Mysql之SQL基础——索引的分类与使用

标签:uniq   聚集   test   创建   from   ima   length   bsp   分类   

原文地址:https://www.cnblogs.com/gered/p/10381282.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!