日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
DBA技術(shù)分享--MySQL三個(gè)關(guān)于主鍵PrimaryKeys的查詢

概述

分享作為DBA日常工作中,關(guān)于mysql主鍵的3個(gè)常用查詢語(yǔ)句,分別如下:

創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括曲阜網(wǎng)站建設(shè)、曲阜網(wǎng)站制作、曲阜網(wǎng)頁(yè)制作以及曲阜網(wǎng)絡(luò)營(yíng)銷策劃等。多年來(lái),我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,曲阜網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到曲阜省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

  • 列出 MySQL 數(shù)據(jù)庫(kù)中的所有主鍵 (PK) 及其列。
  • 列出用戶數(shù)據(jù)庫(kù)(模式)中沒(méi)有主鍵的表。
  • 查詢顯示了用戶數(shù)據(jù)庫(kù)(模式)中有多少?zèng)]有主鍵的表,以及占總表的百分比。

列出 MySQL 數(shù)據(jù)庫(kù)中的所有主鍵 (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;

列說(shuō)明:

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

輸出示例:

輸出結(jié)果說(shuō)明:

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

列出用戶數(shù)據(jù)庫(kù)(模式)中沒(méi)有主鍵的表

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;

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

列說(shuō)明:

  • database_name - 數(shù)據(jù)庫(kù)(模式)名稱。
  • table_name - 表名。

示例:

輸出結(jié)果說(shuō)明:

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

查詢顯示了用戶數(shù)據(jù)庫(kù)(模式)中有多少?zèng)]有主鍵的表,以及占總表的百分比

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');

列說(shuō)明:

  • all_tables - 數(shù)據(jù)庫(kù)中所有表的數(shù)量
  • no_pk_tables - 沒(méi)有主鍵的表數(shù)
  • no_pk_percent - 所有表中沒(méi)有主鍵的表的百分比

示例:


網(wǎng)站標(biāo)題:DBA技術(shù)分享--MySQL三個(gè)關(guān)于主鍵PrimaryKeys的查詢
路徑分享:http://m.5511xx.com/article/ccojcgg.html