当前位置: 代码网 > it编程>数据库>Mysql > MySQL的索引语法详解(两套写法示例)

MySQL的索引语法详解(两套写法示例)

2026年09月24日 • Mysql •我要评论
一、索引的创建语法(最核心)mysql 创建索引主要有两套写法:✅ 建表时创建(create table)✅ 表存在后添加(alter table / create index)二、建表时创建索引(c

一、索引的创建语法(最核心)

mysql 创建索引主要有两套写法:

✅ 建表时创建(create table)

✅ 表存在后添加(alter table / create index)

二、建表时创建索引(create table)

1.普通索引(index / key)

📌 语法:

create table 表名 (
  字段定义...,
  index 索引名(字段名)
);

✅ 示例:

create table user (
  id bigint primary key,
  age int,
  index idx_age(age)
);

📌 常见场景:

  • where age = ?

  • where age > ?

  • 经常用来筛选/排序的字段

2.唯一索引(unique index)

唯一索引的作用:保证某个字段的值不重复(比如邮箱、手机号)。

📌 语法:

create table 表名 (
  字段定义...,
  unique index 索引名(字段名)
);

✅ 示例:

create table user (
  id bigint primary key,
  email varchar(100),
  unique index uk_email(email)
);

📌 注意:

  • 唯一索引会限制重复插入

  • 插入重复值会报错(duplicate entry)

3.主键索引(primary key)

主键索引的特点:唯一 + 非空,并且一张表只能有一个。

📌 语法:

create table 表名 (
  id bigint not null,
  ...,
  primary key(id)
);

✅ 示例:

create table user (
  id bigint not null,
  name varchar(50),
  primary key(id)
);

4.联合索引(多列索引)

联合索引就是:一个索引里放多个字段。

📌 语法:

create table 表名 (
  字段定义...,
  index 索引名(字段1, 字段2, 字段3)
);

✅ 示例:

create table orders (
  id bigint primary key,
  user_id bigint,
  status tinyint,
  create_time datetime,
  index idx_user_status_time(user_id, status, create_time)
);

📌 常见场景:

  • 查询某个用户的某种状态订单,并按时间排序

select * from orders
where user_id = 1001 and status = 1
order by create_time desc;

5.全文索引(fulltext)

全文索引用于文本检索,适合文章内容搜索。

📌 语法:

create table 表名 (
  字段定义...,
  fulltext index 索引名(字段名)
);

✅ 示例:

create table article (
  id bigint primary key,
  content text,
  fulltext index ft_content(content)
);

三、表已经建好了:怎么添加索引?(alter table)

📌 在项目里最常用的就是这一套(上线后优化慢sql)。

添加普通索引

alter table user add index idx_age(age);

添加唯一索引

alter table user add unique index uk_email(email);

添加联合索引

alter table orders add index idx_user_status(user_id, status);

添加全文索引

alter table article add fulltext index ft_content(content);

📌 注意:

  • alter table 会修改表结构

  • 大表加索引可能会比较慢(线上要小心)

四、另一套添加方式:create index(也很常见)

这个写法更“直观”,专门用来创建索引。

创建普通索引

create index idx_age on user(age);

创建唯一索引

create unique index uk_email on user(email);

创建联合索引

create index idx_user_status_time on orders(user_id, status, create_time);

五、删除索引(drop index)

删除普通/唯一/联合索引

📌 语法:

drop index 索引名 on 表名;

✅ 示例:

drop index idx_age on user;

删除主键索引(特殊)

主键索引不是普通索引名,删除方式不同:

alter table user drop primary key;

六、查看索引(排查必备)

查看表有哪些索引

show index from user;

查看建表语句(索引也会显示)

show create table user;

查看建表语句(包含索引)

show create table user\g

📌 常见用途:

  • 怀疑索引没建成功

  • 想确认索引名字到底叫什么

  • 看联合索引的字段顺序

七、不常见语法

前缀索引(适合长字符串)

如果字段很长(例如 email / url),可以只取前几个字符建索引节省空间。

create index idx_email_prefix on user(email(10));

📌 注意:

  • 只索引前 10 个字符

  • 区分度可能会下降(要看数据分布)

联合索引最左前缀(面试必考)

有索引:

create index idx_a_b_c on t(a, b, c);

能命中索引:

where a = 1
where a = 1 and b = 2
where a = 1 and b = 2 and c = 3

❌ 不容易命中:

where b = 2
where c = 3
where b = 2 and c = 3

一句话理解:联合索引要从最左列开始连续使用。

强制使用/忽略索引(排查慢sql偶尔用)

强制使用:

select * from user force index(idx_age)
where age = 18;

忽略索引:

select * from user ignore index(idx_age)
where age = 18;

总结

到此这篇关于mysql索引语法详解的文章就介绍到这了,更多相关mysql索引语法内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

赞 (0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com