information_schema 這是一個(gè)MySQL自帶的庫(kù)(虛擬庫(kù)) 屬于系統(tǒng)庫(kù) 不能手動(dòng)創(chuàng)建和刪除? 只要一啟動(dòng) 自動(dòng)生成這個(gè)庫(kù) 以及這里邊固定好的 視圖
是不是類(lèi)似于環(huán)境變量呢 只要一開(kāi)機(jī)自動(dòng)加載
use?information_schema;
mysql> show tables;? 里邊有好多類(lèi)似于表的東西
什么是視圖
? ? 在工作中查詢(xún)一個(gè)東西需要一個(gè)很大的代碼量
? ? 有沒(méi)有一中方法把所有的代碼操作 打進(jìn)一個(gè)
? ? 類(lèi)似于文件的東西? 這個(gè)類(lèi)似于文件的東西就叫視圖
? 創(chuàng)建視圖
? create view test as select a.mane as aname,b.name as bname,b.surfacearea
? from city as a
? left join country as b
? on a.countrycode=b.code
? where a.population<100; ?
? 使用視圖
? ? ? select * from test;
? 效果等價(jià)于select a.mane,b.name,b.surfacearea
? ? ? ? ? ? from city as a
? ? ? ? ? ? left join country as b
? ? ? ? ? ? on a.countrycode=b.code
? ? ? ? ? ? where a.population<100;
information_schema這個(gè)MySQL自帶的庫(kù) 是干什么的
? 先來(lái)搞清楚什么是元數(shù)據(jù)
? 比如 創(chuàng)建一個(gè)txt文件
? 除了里邊的內(nèi)容 還包括屬性 大小 權(quán)限 歸屬 時(shí)間
? 這些東西會(huì)單獨(dú)存放在 這個(gè)文件的inode中
? 這就是這個(gè)文件的元數(shù)據(jù)
? 相對(duì)的對(duì)于數(shù)據(jù)庫(kù)中的庫(kù) 和 表 來(lái)說(shuō) 除了里邊的數(shù)據(jù)行之外
? ? ? 也有自己的屬性 大小 權(quán)限 歸屬 時(shí)間這些類(lèi)似的東西
? 還有狀態(tài) 等 都屬于元數(shù)據(jù)
元數(shù)據(jù) 存放在 基表中
-----> 基表 是無(wú)法直接查詢(xún)和修改的
-----> 只能通過(guò) DDL對(duì)數(shù)據(jù)進(jìn)行操作來(lái)修改元數(shù)據(jù)
-----> 只能通過(guò)show ,desc,information_schema(全局累的統(tǒng)計(jì)和查詢(xún))來(lái)查看? 無(wú)法cat
? ? 今天僅僅通過(guò)information_schema.tables這一個(gè)視圖先了解 其他的以后再說(shuō)
? ? 視圖是可以通過(guò)select來(lái)查詢(xún)的? 但是在查詢(xún)之前 應(yīng)該先了解這個(gè)視圖存放了什么
查看他的業(yè)務(wù)? 想查看報(bào)表的列屬性一樣 查看一下
use?information_schema;
mysql> desc tables;
| TABLE_SCHEMA? ? 表所在的庫(kù)
| TABLE_NAME? ? ? 表名
| TABLE_TYPE? ? ? 表類(lèi)型
| ENGINE? ? ? ? ? 存儲(chǔ)引擎
| TABLE_ROWS? ? ? 表的行數(shù)
| AVG_ROW_LENGTH? 行平均長(zhǎng)度
| INDEX_LENGTH? ? 索引的長(zhǎng)度
查詢(xún)整個(gè)數(shù)據(jù)庫(kù)中所有的庫(kù)對(duì)應(yīng)的表名
select table_schema,table_name from information_schema.tables;
查詢(xún)world庫(kù)和school庫(kù)對(duì)應(yīng)的表名
select table_schema,table_name
from information_schema.tables
where table_schema='world'
union all
select table_schema,table_name
from information_schema.tables
where table_schema='school';
查詢(xún)整個(gè)數(shù)據(jù)庫(kù)中所有的庫(kù)對(duì)應(yīng)的表名
select table_schema,concat(table_name)
from information_schema.tables
? ? group by table_schema;
查詢(xún)整個(gè)數(shù)據(jù)庫(kù)中所有的庫(kù)對(duì)應(yīng)的表的個(gè)數(shù)
select table_schema,count(table_name)
from information_schema.tables
? ? group by table_schema;
查詢(xún)整個(gè)數(shù)據(jù)庫(kù)的真實(shí)數(shù)據(jù)量
? 一張表數(shù)據(jù)量=(平均航長(zhǎng)度*行數(shù)+索引長(zhǎng)度)
數(shù)據(jù)庫(kù)=所有表的總和
select SUM(AVG_ROW_LENGTH*TABLE_ROWS+INDEX_LENGTH)/1024/1024 as table-GB
from information_schema.tables;
后邊的as是為了顯示出來(lái)的表頭為table-GB