MYSQL 创建索引

目录

自动索引

手动创建

主键索引

唯一索引

普通索引

创建复合索引

查看索引

删除主键


自动索引

如果我们为一张表添加主键约束、外键约束、唯一约束时,MYSQL会为对应的列自动创建一个索引。如果不指定任何约束时,MYSQL会自动为每一列生成一个索引并用ROW_ID进行标识。

手动创建

主键索引

主键索引有三种创建方式,分别是:

1.创建表时创建主键

复制代码
-- 创建表的时候指定主键
create table t_pk1(
 id bigint PRIMARY KEY auto_increment,
 name varchar(20)
);
desc t_pk1;

desc t_pk1 是查看 t_pk1表的索引信息

2.创建表时单独指定主键列

复制代码
create table t_pk(
 id bigint  auto_increment,
 name varchar(20),
 PRIMARY KEY(id)
);
desc t_pk;

3.修改表中的列为主键索引

复制代码
create table t_pk2(
 id bigint,  
 name varchar(20)
);

alter table t_pk2 add primary key (id);
alter table t_pk2 modify id bigint auto_increment;

desc t_pk2;

这是未添加之前的,每一列的信息

这是添加之后的,

唯一索引

创建唯一索引的方式有三种,和上面添加主键索引大致上是相同的,故只给出对应的代码部分。

1.创建表时创建唯一键

复制代码
create table t_uk(
 id bigint primary key auto_increment,
 name varchar(20) unique
 );
 
 desc t_uk;

2.创建表时单独指定唯一列

复制代码
create table t_uk1(
 id bigint primary key auto_increment,
 name varchar(20),
 unique (name)
 );
 
 desc t_uk1;

3.修改表中的列为唯一索引

复制代码
create table t_uk2(
 id bigint primary key auto_increment,
 name varchar(20)
 );
 
 alter table t_uk2 add unique (name);
 desc t_uk2;
普通索引

创建时机有:

  1. 创建表时,明确知道某些列频繁查询
  2. 随着业务不断发展,在版本迭代过程中添加索引

我们也可在MYSQL中查看索引信息

复制代码
show index from t_pk1;

普通索引的三种创建方式和上面两种差不多,不过还是有一些不同的。

1.创建表的时候创建普通索引

复制代码
create table t_index1(
 id bigint primary key auto_increment,
 name varchar(20) unique,
 sno varchar(20),
 index (sno)
 );

index 是创建索引的关键字,括号里是索引列

其中:主键索引用PRI表示,唯一索引用UMI表示,普通索引用MUL表示。

2.修改表中的列为普通索引

复制代码
create table t_index2(
 id bigint primary key auto_increment,
 name varchar(20) unique,
 sno varchar(20)
 );
 
 alter table t_index2 add index (sno);

3.单独创建索引并指定索引名

复制代码
create table t_index3(
 id bigint primary key auto_increment,
 name varchar(20) unique,
 sno varchar(20)
 );
 
 create index index_name on t_index3(sno);
创建复合索引

创建符合索引与创建普通索引语法相同,只不过是指定多个列,列与列之间用逗号隔开。

1.创建表时指定索引列

复制代码
create table t_index4(
 id bigint primary key auto_increment,
 name varchar(20) unique,
 sno varchar(20),
 class_id bigint,
 index(sno, class_id)
 );

2.修改表中的列为复合索引

复制代码
create table t_index5(
 id bigint primary key auto_increment,
 name varchar(20) unique,
 sno varchar(20),
 class_id bigint
 );
 
alter table t_index5 add index(sno, class_id);

3.单独创建索引并指定索引名

复制代码
create table t_index6(
 id bigint primary key auto_increment,
 name varchar(20) unique,
 sno varchar(20),
 class_id bigint
 );
 
create index t_index5_sno_class_id on t_index5 (sno, class_id);
查看索引

查看索引的三种方式,在上面都有提到,现在可以进行一下总结

  1. show keys from 表名
  2. show index from表名
  3. desc表名
删除主键

主键索引

alter table表名 drop PRIMARY KEY;

第一次使用删除语句报错的原因是:由于自增列的错误,所以下面是先删除了自增属性,然后重新删除主键。

其他索引

alter table表名 drop index 索引名

相关推荐
唐青枫1 小时前
MySQL JSON 实战详解:从存储、查询、更新到 JSON_TABLE 与索引
sql·mysql
吃糖的小孩1 小时前
给 QQ AI 机器人设计“可控记忆”:会话摘要、手动长期记忆与角色卡边界
数据库
小满8782 小时前
5.Mysql事务隔离级别与锁机制
mysql
笃行35019 小时前
金仓数据库数据安全双防线:静态存储加密与传输加密实战
数据库
笃行35019 小时前
金仓数据库物理备份实战:sys_rman 全流程演练与误覆盖抢救
数据库
笃行35019 小时前
金仓数据库逻辑备份实战:从全库导出到 Schema 替换的完整闭环
数据库
元Y亨H19 小时前
技术笔记:MySQL 字符集排序规则与大小写敏感性问题解决方案
mysql
SelectDB2 天前
阶跃星辰基于 SelectDB 构建 PB 级 Agent 可观测平台
大数据·数据库·aigc
这个DBA有点耶2 天前
GROUP BY优化全解:如何写出既不丢数据又飞快的分组查询
数据库·mysql·架构