当前位置: 首页 > news >正文

MySQL索引-索引的分类

主键索引

1.表中的列设定为主键后,数据库会自动建立主键索引。

2.单独创建和删除主键索引语法:

创建主键索引语法: alter table 表名 add primary key (字段);

删除主键索引语法: alter table 表名 drop primary key;

​​​​​​​唯一索引

  1. 表中的列创建了唯一约束时,数据库会自动建立唯一索引。
  2. 单独创建和删除唯一索引语法:

创建唯一索引语法:alter table 表名 add unique 索引名(字段);

或  create unique index 索引名 on 表名(字段);

删除唯一索引语法:drop index 索引名 on 表名;

​​​​​​​单值索引

 即一个索引只包含单个列,一个表可以有多个单值索引。

  1. 建表时可随表一起建立单值索引
  2. 单独创建和删除单值索引:

创建单值索引: alter table 表名 add index 索引名(字段);

或   create index 索引名 on 表名(字段);

删除单值索引:drop index 索引名 on 表名;

​​​​​​​复合索引

  即一个索引包含多个列:

  1. 建表时可随表一起建立复合索引
  2. 单独创建和删除复合索引:

创建复合索引:create index 索引名 on 表名(字段1,字段2);

或 alter table 表名 add index 索引名(字段,字段2);

   删除复合索引: drop index 索引名 on 表名;

#随表建立索引
create table customer(
	id int(10) auto_increment,
	customer_no varchar(20),
	customer_name varchar(20),
	primary key(id) ,
	unique idx_customer_no(customer_no),
	key idx_customer_name(customer_name),
	key idex_customer_no_name(customer_no,customer_name)
);

drop table if exists customer;
create table customer(
	id int(10) ,
	customer_no varchar(20),
	customer_name varchar(20)
);

#创建主键索引
alter table customer add primary key(id);
#删除主键索引
alter table customer drop primary key;

#创建唯一索引
alter table customer add unique idx_customer_no(customer_no);
#删除唯一索引
drop index idx_customer_no on customer;

#创建单值索引
alter table customer add index idx_customer_name(customer_name);
#删除单值索引
drop index idx_customer_name on customer;

#创建复合索引
alter table customer add index idx_customer_no_name(customer_no,customer_name);
#删除复合索引
drop  index idx_customer_no_name on customer;

相关文章:

  • 机器学习之softmax
  • DBCO-PEG-FITC二苯基环辛炔-聚乙二醇-异硫氰基荧光素说明;DBCO用于无铜点击化学。在没有铜催化剂的情况下,与叠氮化合物的反应迅速。
  • 预焙阳极行业现状:供给格局边际将改善 “双碳”下优质产品迎新机遇
  • 【CSDN竞赛第17期】简要题解 92.5分
  • 小记 Java stream 中 peek()
  • 即时通讯音视频开发视频编解码理论
  • Go 性能优化之race实战
  • SpringBoot Disruptor 构建高性能内存队列
  • SVN培训笔记(下拉项目、同步修改、添加文件、修改文件、删除文件、改名文件等)
  • Python代码实现栈 2括号匹配算法3、通用括号匹配算法;index()方法
  • 鸡兔同笼:笼子里一共有鸡和兔子35只,一共有94条退, 笼子里一共有鸡和兔子共多少只
  • Windows 下使用 Docker + MySQL 安装 Wiki.js
  • 企业成功认定国家专精特新的申报条件
  • NVM安装
  • 3GPP中URLLC标准研究进展
  • 【考研英语】作文套话(自用)
  • Microsoft Graph PowerShell v2 发布公开预览版 - 一半的大小,加速的自动化体验
  • 安卓面经(11/30)IntentService全解析
  • 基于R语言的DICE(Dynamic Integrated Model of Climate and Economy)模型
  • import语句写烦了,怎么办?