分享作为DBA日常工作中,术分关于mysql主键的术分3个常用查询语句,分别如下:
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;
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 行并提供您的数据库名称。
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');
(责任编辑:休闲)
荣盛发展大股东质押公司7599万股股份 占公司总股本比例的1.75%
四川阿坝州提高孤儿基本生活最低养育标准 2022年1月起执行