DBA技术分享--MySQL三个关于主键PrimaryKeys的查询

数据库 MySQL
本篇给大家分享作为DBA日常工作中,关于MySQL主键的3个常用查询语句,希望能够帮助到你!

概述

分享作为DBA日常工作中,关于mysql主键的3个常用查询语句,分别如下:

  • 列出 MySQL 数据库中的所有主键 (PK) 及其列。
  • 列出用户数据库(模式)中没有主键的表。
  • 查询显示了用户数据库(模式)中有多少没有主键的表,以及占总表的百分比。

DBA技术分享(三)-MYSQL三个关于主键Primary keys的查询

列出 MySQL 数据库中的所有主键 (PK) 及其列

select tab.table_schema as database_schema,
sta.index_name as pk_name,
sta.seq_in_index as column_id,
sta.column_name,
tab.table_name
from information_schema.tables as tab
inner join information_schema.statistics as sta
on sta.table_schema = tab.table_schema
and sta.table_name = tab.table_name
and sta.index_name = 'primary'
where tab.table_schema = 'your database name'
and tab.table_type = 'BASE TABLE'
order by tab.table_name,
column_id;

列说明:

  • table_schema - PK 数据库(模式)名称。
  • pk_name - PK 约束名称。
  • column_id - 索引 (1, 2, ...) 中列的 id。2 或更高表示键是复合键(包含多于一列)。
  • column_name - 主键列名。
  • table_name - PK 表名。

输出示例:

DBA技术分享(三)-MYSQL三个关于主键Primary keys的查询

输出结果说明:

  • 一行:代表一个主键列。
  • 行范围:数据库中所有 PK 约束的列(模式)。
  • 排序方式:表名、列id。

列出用户数据库(模式)中没有主键的表

select tab.table_schema as database_name,
tab.table_name
from information_schema.tables tab
left join information_schema.table_constraints tco
on tab.table_schema = tco.table_schema
and tab.table_name = tco.table_name
and tco.constraint_type = 'PRIMARY KEY'
where tco.constraint_type is null
and tab.table_schema not in('mysql', 'information_schema',
'performance_schema', 'sys')
and tab.table_type = 'BASE TABLE'
-- and tab.table_schema = 'sakila' -- put schema name here
order by tab.table_schema,
tab.table_name;

注意:如果您需要特定数据库(模式)的信息,请取消注释 table_schema 行并提供您的数据库名称。

列说明:

  • database_name - 数据库(模式)名称。
  • table_name - 表名。

示例:

DBA技术分享(三)-MYSQL三个关于主键Primary keys的查询

输出结果说明:

  • 一行:表示数据库中没有主键的一张表(模式)。
  • 行范围:数据库中没有主键的所有表(模式)。
  • 排序方式:数据库(模式)名称、表名。

查询显示了用户数据库(模式)中有多少没有主键的表,以及占总表的百分比

select count(*) as all_tables,
count(*) - count(tco.constraint_type) as no_pk_tables,
cast( 100.0*(count(*) - count(tco.constraint_type)) / count(*)
as decimal(5,2)) as no_pk_percent
from information_schema.tables tab
left join information_schema.table_constraints tco
on tab.table_schema = tco.table_schema
and tab.table_name = tco.table_name
and tco.constraint_type = 'PRIMARY KEY'
where tab.table_type = 'BASE TABLE'
-- and tab.table_schema = 'database_name' -- put your database name here
and tab.table_schema not in('mysql', 'information_schema',
'sys', 'performance_schema');

列说明:

  • all_tables - 数据库中所有表的数量
  • no_pk_tables - 没有主键的表数
  • no_pk_percent - 所有表中没有主键的表的百分比

示例:

DBA技术分享(三)-MYSQL三个关于主键Primary keys的查询

责任编辑:姜华 来源: 今日头条
相关推荐

2022-06-26 06:32:28

MySQL数据库维护

2015-07-10 10:27:05

云技术混合架构认知误区

2018-11-23 09:43:26

2021-08-11 08:47:31

SASE网络安全零信任

2022-04-14 14:09:25

数据治理数字化转型工具

2021-02-18 16:12:06

5G4G技术

2010-06-07 13:07:26

IPv6协议技术

2022-06-15 16:23:09

智能公寓物联网

2009-08-10 22:31:00

光纤通道技术光纤接入

2017-01-16 15:53:46

大数据人工智能AI

2021-07-30 16:27:13

密码安全网络安全网络攻击

2009-11-10 12:12:57

VB.NET函数

2010-10-12 09:41:26

mysql触发器

2017-01-12 09:11:07

2017-01-15 10:32:49

大数据技术信息

2023-06-26 08:06:39

重构代码冗余

2018-08-02 15:15:19

2010-10-12 10:38:29

mysql触发器

2017-12-12 12:35:07

云安全云服务安全

2017-12-22 11:05:28

云技术混合云信息化
点赞
收藏

51CTO技术栈公众号