mysql数据库索引

mysql数据库索引

1.概念

索引类似于书的目录,可以更快的找到指定内容的位置。

在mysql中常见索引有两种:

  • InnoDB(默认) :事务优先 (适合高并发操作;行锁)
  • MyISAM :性能优先 (表锁)

2.操作

2.1创建表时指定数据库对象的引擎

create table tb(
	id int(4) auto_increment ,
	name varchar(5),
	dept varchar(5) ,
	primary key(id)		
)ENGINE=MyISAM AUTO_INCREMENT=1
 DEFAULT CHARSET=utf8;

2.2索引分类

​ 主键索引: 不能重复。id 不能是null
​ 唯一索引 :不能重复。id 可以是null
​ 单值索引 : 单列, age ;一个表可以多个单值索引,name。
​ 复合索引 :多个列构成的索引 (相当于 二级目录 : z: zhao) (name,age) (a,b,c,d,…,n)

2.3创建索引

方式一:
模板:create 索引类型 索引名 on 表(字段)

​ 单值:
​ create index dept_index on tb(dept);
​ 唯一:
​ create unique index name_index on tb(name) ;
​ 复合索引
​ create index dept_name_index on tb(dept,name);

方式二:

​ 模板:alter table 表名 add 索引类型 索引名(字段)

​ 单值:
​ alter table tb add index dept_index(dept) ;
​ 唯一:
​ alter table tb add unique index name_index(name);
​ 复合索引
​ alter table tb add index dept_name_index(dept,name);

注意:如果一个字段是primary key,则改字段默认就是 主键索引

2.4删除索引

​ drop index 索引名 on 表名 ;
​ drop index name_index on tb ;

2.5查询索引

​ show index from 表名 ;
​ show index from 表名 \G

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值