游客发表

分享作为DBA日常工作中,技键关于mysql主键的术分3个常用查询语句,分别如下:
列出 MySQL 数据库中的享M询所有主键 (PK) 及其列。列出用户数据库(模式)中没有主键的个关表。查询显示了用户数据库(模式)中有多少没有主键的于主表,以及占总表的技键百分比。
tab
inner join information_schema.statistics assta
on sta.table_schema = tab.table_schema and sta.table_name = tab.table_name and sta.index_name = primarywhere tab.table_schema = your database name and tab.table_type = BASE TABLEorder by tab.table_name, column_id;1.2.3.4.5.6.7.8.9.10.11.12.13.14. 列说明:table_schema - PK 数据库(模式)名称。pk_name - PK 约束名称。享M询column_id - 索引 (1,个关 2, ...) 中列的 id。高防服务器2 或更高表示键是于主复合键(包含多于一列)。column_name - 主键列名。技键table_name - PK 表名。术分输出示例:
tab
left join information_schema.table_constraintstco
on tab.table_schema = tco.table_schema and tab.table_name = tco.table_name and tco.constraint_type = PRIMARY KEYwhere 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 hereorder by tab.table_schema, tab.table_name;1.2.3.4.5.6.7.8.9.10.11.12.13.14.注意:如果您需要特定数据库(模式)的信息,请取消注释 table_schema 行并提供您的数据库名称。
列说明:database_name - 数据库(模式)名称。服务器租用table_name - 表名。示例:
no_pk_percent
from information_schema.tablestab
left join information_schema.table_constraintstco
on tab.table_schema = tco.table_schema and tab.table_name = tco.table_name and tco.constraint_type = PRIMARY KEYwhere 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);1.2.3.4.5.6.7.8.9.10.11.12.13. 列说明:all_tables - 数据库中所有表的数量no_pk_tables - 没有主键的表数no_pk_percent - 所有表中没有主键的表的百分比示例:
随机阅读
热门排行
友情链接