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

DBA技術分享--MySQL三個關于主鍵PrimaryKeys的查詢

數據庫 MySQL
本篇給大家分享作為DBA日常工作中,關于MySQL主鍵的3個常用查詢語句,希望能夠幫助到你!

概述

分享作為DBA日常工作中,關于mysql主鍵的3個常用查詢語句,分別如下:

  • 列出 MySQL 數據庫中的所有主鍵 (PK) 及其列。
  • 列出用戶數據庫(模式)中沒有主鍵的表。
  • 查詢顯示了用戶數據庫(模式)中有多少沒有主鍵的表,以及占總表的百分比。

DBA技術分享(三)-MYSQL三個關于主鍵Primary keys的查詢

列出 MySQL 數據庫中的所有主鍵 (PK) 及其列

select tab.table_schema as database_schema,
sta.index_name as pk_name,
sta.seq_in_index as column_id,
sta.column_name,
tab.table_name
from information_schema.tables as tab
inner join information_schema.statistics as sta
on sta.table_schema = tab.table_schema
and sta.table_name = tab.table_name
and sta.index_name = 'primary'
where tab.table_schema = 'your database name'
and tab.table_type = 'BASE TABLE'
order by tab.table_name,
column_id;

列說明:

  • table_schema - PK 數據庫(模式)名稱。
  • pk_name - PK 約束名稱。
  • column_id - 索引 (1, 2, ...) 中列的 id。2 或更高表示鍵是復合鍵(包含多于一列)。
  • column_name - 主鍵列名。
  • table_name - PK 表名。

輸出示例:

DBA技術分享(三)-MYSQL三個關于主鍵Primary keys的查詢

輸出結果說明:

  • 一行:代表一個主鍵列。
  • 行范圍:數據庫中所有 PK 約束的列(模式)。
  • 排序方式:表名、列id。

列出用戶數據庫(模式)中沒有主鍵的表

select tab.table_schema as database_name,
tab.table_name
from information_schema.tables tab
left join information_schema.table_constraints tco
on tab.table_schema = tco.table_schema
and tab.table_name = tco.table_name
and tco.constraint_type = 'PRIMARY KEY'
where 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 here
order by tab.table_schema,
tab.table_name;

注意:如果您需要特定數據庫(模式)的信息,請取消注釋 table_schema 行并提供您的數據庫名稱。

列說明:

  • database_name - 數據庫(模式)名稱。
  • table_name - 表名。

示例:

DBA技術分享(三)-MYSQL三個關于主鍵Primary keys的查詢

輸出結果說明:

  • 一行:表示數據庫中沒有主鍵的一張表(模式)。
  • 行范圍:數據庫中沒有主鍵的所有表(模式)。
  • 排序方式:數據庫(模式)名稱、表名。

查詢顯示了用戶數據庫(模式)中有多少沒有主鍵的表,以及占總表的百分比

select count(*) as all_tables,
count(*) - count(tco.constraint_type) as no_pk_tables,
cast( 100.0*(count(*) - count(tco.constraint_type)) / count(*)
as decimal(5,2)) as no_pk_percent
from information_schema.tables tab
left join information_schema.table_constraints tco
on tab.table_schema = tco.table_schema
and tab.table_name = tco.table_name
and tco.constraint_type = 'PRIMARY KEY'
where 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');

列說明:

  • all_tables - 數據庫中所有表的數量
  • no_pk_tables - 沒有主鍵的表數
  • no_pk_percent - 所有表中沒有主鍵的表的百分比

示例:

DBA技術分享(三)-MYSQL三個關于主鍵Primary keys的查詢

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

2022-06-26 06:32:28

MySQL數據庫維護

2015-07-10 10:27:05

云技術混合架構認知誤區

2018-11-23 09:43:26

2021-08-11 08:47:31

SASE網絡安全零信任

2022-04-14 14:09:25

數據治理數字化轉型工具

2021-02-18 16:12:06

5G4G技術

2010-06-07 13:07:26

IPv6協議技術

2022-06-15 16:23:09

智能公寓物聯網

2009-08-10 22:31:00

光纖通道技術光纖接入

2017-01-16 15:53:46

大數據人工智能AI

2009-11-10 12:12:57

VB.NET函數

2021-07-30 16:27:13

密碼安全網絡安全網絡攻擊

2017-01-12 09:11:07

2017-01-15 10:32:49

大數據技術信息

2018-08-02 15:15:19

2023-04-26 11:14:11

IT領導者遠程工作

2023-06-26 08:06:39

重構代碼冗余

2010-03-16 16:56:45

云計算

2021-04-26 10:35:49

物聯網智慧城市IoT

2017-12-12 12:35:07

云安全云服務安全
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲久视频 | 久草新视频 | 日日骚网 | 精品自拍视频 | 天天草天天爱 | 麻豆久久精品 | 欧美精品欧美精品系列 | 欧美黄页 | 精品9999| 国产91久久精品一区二区 | 欧美色性 | 国产精品久久7777777 | 成人精品啪啪欧美成 | 日韩av美女电影 | av网站推荐 | 亚洲国产精品成人无久久精品 | 成人h视频在线 | 亚洲一区二区三区在线播放 | 国产视频一区二区在线观看 | 精品在线一区二区三区 | 亚洲小视频在线观看 | 亚洲一区二区av | 北条麻妃99精品青青久久主播 | 欧美亚洲国产一区 | 久草网址| 91偷拍精品一区二区三区 | 在线一区二区三区 | 亚洲人在线观看视频 | 亚洲图片视频一区 | 99爱国产| 免费成人高清在线视频 | 无吗视频 | 超碰97在线免费 | 亚洲第一成年免费网站 | av黄色免费在线观看 | 狠狠色网 | 欧美日韩高清免费 | 成人av一区 | 影视先锋av资源噜噜 | 日本aaaa| h视频免费在线观看 |