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

DBA技术分享–MySQL三个关于主键PrimaryKeys的查询插图亿华云

概述

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

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

DBA技术分享–MySQL三个关于主键PrimaryKeys的查询插图1亿华云

列出 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

THE END
Copyright © 2024 亿华云