当前位置: 首页 > 科技观察

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

时间:2023-03-13 13:33:19 科技观察

一、概述作为一名DBA,分享一下工作中常见的几种外键查询。具体如下:如何查询用户数据库(schema)中定义的外键约束。如何查询对具有外键的特定表的所有引用。如何在没有外键的情况下查询表。如何查找没有关系的表-LonerTables。如何在没有关系表的情况下查询MySQL数据库中的比率。二、相关SQL1、查询用户数据库(schema)中定义的外键约束selectconcat(fks.constraint_schema,'.',fks.table_name)asforeign_table,'->'asrel,concat(fks.unique_constraint_schema,'.',fks.referenced_table_name)asprimary_table,fks.constraint_name,group_concat(kcu.column_nameorderbyposition_in_unique_constraintseparator',')asfk_columnsfrominformation_schema.referential_constraintsfksjoininformation_schema.key_column_usagekcuonfks.constraint_schema=kcu.table_schemaandfks.table_name=kcu.table_nameandfks.constraint_name=kcu.constraint_name--其中fks.constraint_schema='databasename'groupbyfks.constraint_schema,fks.table_name,fks.unique_constraint_schema,fks.referenced_table_name,fks.constraint_nameorderbyfks.constraint_schema,fkstable.;注意:如果您需要特定数据库(架构)的信息,请取消注释where子句并提供您的数据库名称。2.查询所有引用特定表的外键。selectdistinctconcat(table_schema,'.',table_name)asforeign_table,'>-'asrel,concat(referenced_table_schema,'.',referenced_table_name)asprimary_tablefrominformation_schema.key_column_usagewherereferenced_table_name='表名'--提供表名-andtable_schema='databasename'orderbyforeign_table;解释:foreign_table-外部表名-你正在寻找的表。rel-涉及FK和方向的关系符号。primary_table-主(参考)表名-您作为参数提供的表。3.查询没有外键的表selecttab.table_schemaasdatabase_name,tab.table_name,'>-noFKs'asforeign_keysfrominformation_schema.tablestableftjoininformation_schema.table_constraintsfksonfks.table_schema=tab.table_schemaandfks.table_name=tab.table_nameandfks.constraint_type='FOREIGNKEY'wheretab.table_type='BASETABLE'andtab.table_schemanotin('mysql','information_schema','performance_schema','sys')andfks.table_nameisnull--andtab.table_schema='你的数据库名'orderbytab.table_schema,tab.table_name;说明:database_name-数据库的名称(模式)。table_name-表的名称。foreign_keys-代表缺失外键的符号。4.查找没有关系的表-LonerTables选择'NoFKs>-'作为refs,concat(tab.table_schema,'.',tab.table_name)作为'table','>-noFKs'作为fksfrominformation_schema。tablestableftjoininformation_schema.referential_constraintsrefontab.table_schema=ref.constraint_schemaandtab.table_name=ref.table_nameleftjoininformation_schema.referential_constraintsref_byontab.table_schema=ref_by.unique_constrainthereschemaandtab.table_name=nconstraint_referenced_reftable_nameconstraint_name为空且tab.table_type='BASETABLE'andtab.table_schemanotin('mysql','information_schema','performance_schema','sys')--andtab.table_schema='yourdatabasename'orderbytab.table_schema,tab.table_name;说明:refs-一个图标,指示缺少外键约束引用。表-表的名称。fks-缺少外键约束的图标。5、MySQL数据库中没有相关表的比率selectall_tablesastable_count,no_relasloner_tables,concat(cast(100.0*(no_rel/all_tables)asdecimal(5,2)),'%')asloner_ratiofrom(selectcount(distinctconcat(tab.table_schema,'.',tab.table_name))asall_tables,SUM(当ref.constraint_name为null且ref_by.constraint_name为null时1else0end)asno_relfrominformation_schema.tablestableftjoininformation_schema。referential_constraintsrefontab.table_schema=ref.constraint_schemaandtab.table_name=ref.table_nameleftjoininformation_schema.referential_constraintsref_byontab.table_schema=ref_by.unique_constraint_schemaandtab.table_name=ref_by.referenced_table_namewheretab.table_type='BASETABLE'andtab.table_schema不在('mysql','information_schema','sys','performance_schema'))temp;说明:table_count-数据库中的表(modes)个数loner_tables-数据库中Loner表的个数(modes)。lonely_ratio-数据库(模式)中孤独表的百分比。三、总结Mysql的外键在我们的工作中经常会遇到。这几个关于外键的查询可以帮助提高数据库维护的效率。