作为DBA分享几个工作中关于外键的常用查询。具体如下 :
目前成都创新互联已为数千家的企业提供了网站建设、域名、网站空间、网站托管、企业网站设计、温州网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
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
order by 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'
group by fks.constraint_schema,
fks.table_name,
fks.unique_constraint_schema,
fks.referenced_table_name,
fks.constraint_name
order by fks.constraint_schema,
fks.table_name;
注意:如果您需要特定数据库(模式)的信息,请取消注释 where 子句并提供您的数据库名称。
select distinct 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'
order by foreign_table;
说明:
select tab.table_schema as 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_schema not in ('mysql', 'information_schema',
'performance_schema', 'sys')
and fks.table_name is null
-- and tab.table_schema = 'your database name'
order by tab.table_schema,
tab.table_name;
说明:
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_name is null
and ref_by.constraint_name is null
and tab.table_type = 'BASE TABLE'
and tab.table_schema not in ('mysql', 'information_schema',
'performance_schema', 'sys')
-- and tab.table_schema = 'your database name'
order by tab.table_schema,
tab.table_name;
说明:
select all_tables as table_count,
no_rel as loner_tables,
concat(cast(100.0*(no_rel/all_tables) as decimal(5,2)), '%')
as loner_ratio
from
(select count(distinct concat(tab.table_schema, '.', tab.table_name))
as all_tables,
SUM(case when ref.constraint_name is null
and ref_by.constraint_name is null
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_schema not in ('mysql', 'information_schema',
'sys', 'performance_schema')
) temp;
说明:
mysql外键是我们工作中经常遇到的,这几个关于外键查询,可以帮忙提高数据库维护的效率。
文章题目:DBA技术分享-MySQL外键查询语句
转载源于:http://www.csdahua.cn/qtweb/news41/542841.html
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网