DBA技术分享-MySQL外键查询语句

一、概述

作为DBA分享几个工作中关于外键的常用查询。具体如下 :

  • 如何查询用户数据库(模式)中定义的外键约束。
  • 如何查询所有引用具有外键的特定的表。
  • 如何查询没有外键的表。
  • 如何查找没有关系的表 – Loner Tables。
  • 如何查询MySQL 数据库中没有关系表的比率。

二、相关SQL

1、查询用户数据库(模式)中定义的外键约束

select concat(fks.constraint_schema,'.', fks.table_name)as foreign_table,
'->'as rel,
concat(fks.unique_constraint_schema,'.', fks.referenced_table_name)
as primary_table,
fks.constraint_name,
group_concat(kcu.column_name
orderby position_in_unique_constraint separator ', ')
as fk_columns
from information_schema.referential_constraints fks
join information_schema.key_column_usage kcu
on fks.constraint_schema= kcu.table_schema
and fks.table_name= kcu.table_name
and fks.constraint_name= kcu.constraint_name
-- where fks.constraint_schema = 'database name'
groupby fks.constraint_schema,
fks.table_name,
fks.unique_constraint_schema,
fks.referenced_table_name,
fks.constraint_name
orderby fks.constraint_schema,
fks.table_name;

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

2、查询所有引用具有外键的特定的表。

selectdistinct concat(table_schema,'.', table_name)as foreign_table,
'>-'as rel,
concat(referenced_table_schema,'.', referenced_table_name)
as primary_table
from information_schema.key_column_usage
where referenced_table_name ='table name'-- provide table name here
-- and table_schema = 'database name'
orderby foreign_table;

说明:

  • foreign_table – 外部表名 – 您要查找的表。
  • rel – 涉及 FK 和方向的关系符号。
  • primary_table – 主要(引用)表名 – 您作为参数提供的表。

3、查询没有外键的表

select tab.table_schemaas database_name,
tab.table_name,
'>- no FKs'as foreign_keys
from information_schema.tables tab
left join information_schema.table_constraints fks
on fks.table_schema= tab.table_schema
and fks.table_name= tab.table_name
and fks.constraint_type='FOREIGN KEY'
where tab.table_type='BASE TABLE'
and tab.table_schemanotin('mysql','information_schema',
'performance_schema','sys')
and fks.table_nameisnull
-- and tab.table_schema = 'your database name'
orderby tab.table_schema,
tab.table_name;

说明:

  • database_name – 数据库的名称(模式)。
  • table_name – 表的名称。
  • foreign_keys – 表示缺少 FK 的符号。

4、查找没有关系的表 – Loner Tables

select'No FKs >-'as refs,
concat(tab.table_schema,'.', tab.table_name)as'table',
'>- no FKs'as fks
from information_schema.tables tab
left join information_schema.referential_constraints ref
on tab.table_schema= ref.constraint_schema
and tab.table_name= ref.table_name
left join information_schema.referential_constraints ref_by
on tab.table_schema= ref_by.unique_constraint_schema
and tab.table_name= ref_by.referenced_table_name
where ref.constraint_nameisnull
and ref_by.constraint_nameisnull
and tab.table_type='BASE TABLE'
and tab.table_schemanotin('mysql','information_schema',
'performance_schema','sys')
-- and tab.table_schema = 'your database name'
orderby tab.table_schema,
tab.table_name;

说明:

  • refs – 表示缺少外键约束引用的图标。
  • table– 表的名称。
  • fks – 象征缺少外键约束的图标。

5、MySQL 数据库中没有关系表的比率

select all_tables as table_count,
no_rel as loner_tables,
concat(cast(100.0*(no_rel/all_tables)asdecimal(5,2)),'%')
as loner_ratio
from
(selectcount(distinct concat(tab.table_schema,'.', tab.table_name))
as all_tables,
SUM(case when ref.constraint_nameisnull
and ref_by.constraint_nameisnull
then 1
else 0 end)as no_rel
from information_schema.tables tab
left join information_schema.referential_constraints ref
on tab.table_schema= ref.constraint_schema
and tab.table_name= ref.table_name
left join information_schema.referential_constraints ref_by
on tab.table_schema= ref_by.unique_constraint_schema
and tab.table_name= ref_by.referenced_table_name
where tab.table_type='BASE TABLE'
and tab.table_schemanotin('mysql','information_schema',
'sys','performance_schema')
) temp;

说明:

  • table_count – 数据库中的表数(模式)。
  • loner_tables – 数据库中Loner 表的数量(模式)。
  • loner_ratio -孤独者比率- 数据库中孤独者表的百分比(模式)。

三、小结

mysql外键是我们工作中经常遇到的,这几个关于外键查询,可以帮忙提高数据库维护的效率。

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/271871.html<

(0)
运维的头像运维
上一篇2025-05-08 11:50
下一篇 2025-05-08 11:51

相关推荐

  • 个人主题怎么制作?

    制作个人主题是一个将个人风格、兴趣或专业领域转化为视觉化或结构化内容的过程,无论是用于个人博客、作品集、社交媒体账号还是品牌形象,核心都是围绕“个人特色”展开,以下从定位、内容规划、视觉设计、技术实现四个维度,详细拆解制作个人主题的完整流程,明确主题定位:找到个人特色的核心主题定位是所有工作的起点,需要先回答……

    2025-11-20
    0
  • 社群营销管理关键是什么?

    社群营销的核心在于通过建立有温度、有价值、有归属感的社群,实现用户留存、转化和品牌传播,其管理需贯穿“目标定位-内容运营-用户互动-数据驱动-风险控制”全流程,以下从五个维度展开详细说明:明确社群定位与目标社群管理的首要任务是精准定位,需明确社群的核心价值(如行业交流、产品使用指导、兴趣分享等)、目标用户画像……

    2025-11-20
    0
  • 香港公司网站备案需要什么材料?

    香港公司进行网站备案是一个涉及多部门协调、流程相对严谨的过程,尤其需兼顾中国内地与香港两地的监管要求,由于香港公司注册地与中国内地不同,其网站若主要服务内地用户或使用内地服务器,需根据服务器位置、网站内容性质等,选择对应的备案路径(如工信部ICP备案或公安备案),以下从备案主体资格、流程步骤、材料准备、注意事项……

    2025-11-20
    0
  • 如何企业上云推广

    企业上云已成为数字化转型的核心战略,但推广过程中需结合行业特性、企业痛点与市场需求,构建系统性、多维度的推广体系,以下从市场定位、策略设计、执行落地及效果优化四个维度,详细拆解企业上云推广的实践路径,精准定位:明确目标企业与核心价值企业上云并非“一刀切”的方案,需先锁定目标客户群体,提炼差异化价值主张,客户分层……

    2025-11-20
    0
  • PS设计搜索框的实用技巧有哪些?

    在PS中设计一个美观且功能性的搜索框需要结合创意构思、视觉设计和用户体验考量,以下从设计思路、制作步骤、细节优化及交互预览等方面详细说明,帮助打造符合需求的搜索框,设计前的规划明确使用场景:根据网站或APP的整体风格确定搜索框的调性,例如极简风适合细线条和纯色,科技感适合渐变和发光效果,电商类则可能需要突出搜索……

    2025-11-20
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注