成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

DBA技術分享-MySQL外鍵查詢語句

數據庫 MySQL
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
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 子句并提供您的數據庫名稱。

DBA技術分享(六)-分享收藏的mysql外鍵查詢語句

2、查詢所有引用具有外鍵的特定的表。

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;

說明:

  • foreign_table - 外部表名 - 您要查找的表。
  • rel - 涉及 FK 和方向的關系符號。
  • primary_table - 主要(引用)表名 - 您作為參數提供的表。

DBA技術分享(六)-分享收藏的mysql外鍵查詢語句

3、查詢沒有外鍵的表

DBA技術分享(六)-分享收藏的mysql外鍵查詢語句

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;

說明:

  • database_name - 數據庫的名稱(模式)。
  • table_name - 表的名稱。
  • foreign_keys - 表示缺少 FK 的符號。

DBA技術分享(六)-分享收藏的mysql外鍵查詢語句

4、查找沒有關系的表 - Loner Tables

DBA技術分享(六)-分享收藏的mysql外鍵查詢語句

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;

說明:

  • refs - 表示缺少外鍵約束引用的圖標。
  • table- 表的名稱。
  • fks - 象征缺少外鍵約束的圖標。

DBA技術分享(六)-分享收藏的mysql外鍵查詢語句

5、MySQL 數據庫中沒有關系表的比率

DBA技術分享(六)-分享收藏的mysql外鍵查詢語句

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;

說明:

  • table_count - 數據庫中的表數(模式)。
  • loner_tables - 數據庫中Loner 表的數量(模式)。
  • loner_ratio -孤獨者比率- 數據庫中孤獨者表的百分比(模式)。

DBA技術分享(六)-分享收藏的mysql外鍵查詢語句

三、小結

mysql外鍵是我們工作中經常遇到的,這幾個關于外鍵查詢,可以幫忙提高數據庫維護的效率。

責任編輯:姜華 來源: 今日頭條
相關推薦

2022-06-09 17:20:21

DBAMySQL

2010-11-22 10:08:03

Mysql外鍵用法

2010-11-22 09:59:32

MySQL外鍵設置

2019-10-09 08:52:59

SQLMySQL數據庫

2011-05-12 14:33:37

MySQL外鍵

2010-10-09 09:55:41

MySQL外鍵

2010-10-09 10:04:48

MySQL定義外鍵

2010-11-22 09:43:07

MySQL定義外鍵

2010-05-11 18:46:46

MYSQL 外鍵

2010-09-25 11:39:37

SQL語句

2010-07-19 16:54:21

SQL

2010-10-19 17:28:08

SQL SERVER外

2010-06-17 17:50:31

SQL Server外

2010-10-09 09:46:28

MySQL外鍵

2019-11-05 08:20:13

MySQL數據庫外鍵

2010-10-09 10:29:29

MySQL外鍵

2010-05-11 19:11:13

MySQL外鍵

2011-09-01 10:56:34

2010-01-05 09:24:42

MySQL外鍵約束

2019-05-30 05:19:30

MySQL優化語句執行
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 精精国产xxxx视频在线播放 | 亚洲精品国产精品国自产在线 | 中文字幕国产视频 | 欧美一区二区三区在线看 | 欧美黄色片在线观看 | 亚洲免费视频一区二区 | xxxcom在线观看 | av在线免费观看网站 | 精国产品一区二区三区四季综 | 国产精品亚洲第一区在线暖暖韩国 | 日韩在线视频一区 | 免费影视在线观看 | 国产欧美日韩在线观看 | 欧美中文字幕一区二区三区亚洲 | 久久久久九九九女人毛片 | 日产久久| 午夜精品一区 | 一区二区三区四区在线视频 | 在线播放一区二区三区 | 欧美日韩不卡合集视频 | 在线不卡一区 | 日韩三级在线 | 欧美精品久久久 | yeyeav | 午夜a√| www.婷婷 | a级黄色毛片免费播放视频 国产精品视频在线观看 | 久操伊人 | 欧美电影在线观看网站 | 久久免费看 | 亚洲精品免费视频 | 国产精品99久久久久久www | 国产高清一区二区 | 一区二区精品视频 | 91伊人 | 免费一级片 | 一区二区三区四区av | 亚洲精选久久 | 日本一区二区在线视频 | 日本在线视频一区二区 | 色婷婷综合久久久久中文一区二区 |