MySQL中索引的创建有三种方法,索引的删除有两种方法。

一、创建索引

(1)使用create index

# 1.创建普通索引
create index 索引名 on 表名 (列名[(限制索引长度)]);
# 2.创建唯一性索引
create unique index 索引名 on 表名 (列名);
# 3.创建全文索引
create fulltext index 索引名 on 表名 (列名);

注意:

  • 索引名:指定索引名。一个表可以创建多个索引,但每个索引在该表中的名称是唯一的。
  • 表名:指定要创建索引的表名。
  • 列名:指定要创建索引的列名。通常可以考虑将查询语句中在 JOIN 子句和 WHERE 子句里经常出现的列作为索引列。
  • 限制索引长度:可选项。指定使用列前的 length 个字符来创建索引。使用列的一部分创建索引有利于减小索引文件的大小,节省索引列所占的空间。

(2)修改表的方式创建索引

#在修改表的同时为该表添加索引
# 1.普通索引
alter table examination_info add index 索引名(列名);
# 2.唯一性索引
alter table examination_info add unique index 索引名(列名);
# 3.全文索引
alter table examination_info add fulltext index 索引名(列名);
#在修改表的同时为该表添加主键
alter table examination_info ADD PRIMARY KEY [索引名] (列名,...);
#在修改表的同时为该表添加外键
alter table examination_info ADD FOREIGN KEY [索引名] (列名,...);

注意:

  • 索引类型:普通索引、唯一性索引、全文索引

(3)建表的时候创建索引

#在创建新表的同时创建该表的索引
create table tableName(  
  id int not null,   
  列名  列的类型,
  unique | index [索引名] [索引类型] (列名,...);
);
#在创建新表的同时创建该表的外键
create table tableName(  
  id int not null,   
  列名  列的类型,
  foreign key [索引名] 列名;
);

二、删除索引

(1)使用drop index

drop index 索引名 on 表名;

(2)使用alter table

alter table 表名 drop index 索引名;

参考文章
MySQL创建索引(CREATE INDEX)