暂无图片
暂无图片
1
暂无图片
暂无图片
暂无图片
mysql索引管理.txt
112
13页
1次
2022-12-22
10墨值下载
目录
1.查看索引/约束
2.单列普通索引操作
3.联合索引操作
4.主键和唯一索引的操作
5.全文检索
6.索引设计规范
7.列属性的补充---defaultcommentauto_increment
8.约束的介绍和管理
8.1 非空约束和唯一约束
8.2 主键 PK 管理
8.3 外键约束的介绍和管理
8.4 check 约束的介绍和管理
9. mysql 中视图的介绍
10.补充:创建 hash 索引
MySQL 8.0 新增了函数索引、不可见索引、索引跳扫、降序索引等索引特性。
1.查看索引
1.1 已库里查看表的索引(简单方式)
desc city; #只能查看列上索引的类型,不能查看索引的名字
SHOW INDEX FROM city; #查看此表所有索引的信息,比如索引类型、名称等
SHOW INDEX FROM city\G #列的方式查看
1.2 在库外查看表的索引(以下都为全面的方式)
show index from itpux06 from itpuxdb; #注意:前面 from 是表面,后面 from 是库
PS:本质查询的信息来源于 information_schema.STATISTICS
1.3 index---通过系统视图查看索引(STATISTICS 视图)
select * from information_schema.STATISTICS where TABLE_SCHEMA='itpuxdb';
#查询指定库下有索引的所有表
select * from information_schema.STATISTICS where TABLE_SCHEMA='itpuxdb' and
table_name='itpux06'; #查看全库下指定表的索引信息
select * from information_schema.columns where TABLE_SCHEMA='itpuxdb' and
table_name='itpux06'; #查看指定库下的表,索引创建在表的那些列上
1.4 index---通过系统视图查找出所有库中没有创建索引的表,排除系统表之外(tables 系统视图)
select t.table_schema,t.table_name from information_schema.tables as t left join
(
select distinct table_schema, table_name from
information_schema.KEY_COLUMN_USAGE)
as kt on kt.table_schema=t.table_schema and kt.table_name = t.table_name
where t.table_schema
not in ('mysql','information_schema','performance_schema','test','sys')
and kt.table_name is null;
1.5 primary key---通过系统视图找出全库下(除系统表以外的)没有主键 PK 的表
select table_schema,table_name from information_schema.TABLES
where
table_schema not in
('mysql','information_schema','performance_schema','test','sys')
and TABLE_NAME not in (
select table_name
from
information_schema.table_constraints t
join information_schema.key_column_usage k using (
constraint_name,table_schema,table_name)
where
t.constraint_type = 'PRIMARY KEY'
and t.table_schema not in
('mysql','information_schema','performance_schema','test','sys')
);
#查询指定库下没有 PK 的表
select table_schema,table_name from information_schema.TABLES
where
table_schema = 'yf' #替换需要查询
的库名
and TABLE_NAME not in (
select table_name
from
information_schema.table_constraints t
join information_schema.key_column_usage k using (
constraint_name,table_schema,table_name)
where
t.constraint_type = 'PRIMARY KEY'
and t.table_schema = 'yf' #替换需要查询
的库名
);
#通过系统视图找出全库下(除系统表以外的)有主键约束或主键索引的表
select * from information_schema.table_constraints where
CONSTRAINT_TYPE='PRIMARY KEY' and TABLE_SCHEMA not in
('mysql','information_schema','performance_schema','test','sys');
1.6 unique---通过系统视图找出全库下(除系统以外的)没有唯一约束或没有唯一索引的表
select table_schema,table_name from information_schema.TABLES
where
table_schema not in
('mysql','information_schema','performance_schema','test','sys')
and TABLE_NAME not in (
select table_name
from
information_schema.table_constraints t
join information_schema.key_column_usage k using (
constraint_name,table_schema,table_name)
where
t.constraint_type = 'UNIQUE'
and t.table_schema not in
('mysql','information_schema','performance_schema','test','sys')
);
#通过系统视图找出全库下(除系统以外的)有唯一约束或唯一索引的表
select * from information_schema.table_constraints where
of 13
10墨值下载
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文档的来源(墨天轮),文档链接,文档作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论

关注
最新上传
暂无内容,敬请期待...
下载排行榜
Top250 周榜 月榜