Mysql查詢所有表和字段信息的方法
1 MySQL中information_schema是什么
- information_schema數(shù)據(jù)庫(kù)是MySQL自帶的,它提供了訪問(wèn)數(shù)據(jù)庫(kù)元數(shù)據(jù)的方式。
- 元數(shù)據(jù):元數(shù)據(jù)是關(guān)于數(shù)據(jù)的數(shù)據(jù),如數(shù)據(jù)庫(kù)名或表名,列的數(shù)據(jù)類型,或訪問(wèn)權(quán)限等。有些時(shí)候用于表述該信息的其他術(shù)語(yǔ)包括“數(shù)據(jù)字典”和“系統(tǒng)目錄”。
- 在MySQL中,把information_schema看作是一個(gè)數(shù)據(jù)庫(kù),確切說(shuō)是信息數(shù)據(jù)庫(kù)。其中保存著關(guān)于MySQL服務(wù)器所維護(hù)的所有其他數(shù)據(jù)庫(kù)的信息。如數(shù)據(jù)庫(kù)名,數(shù)據(jù)庫(kù)的表,表欄的數(shù)據(jù)類型與訪問(wèn)權(quán)限等。
- information_schema數(shù)據(jù)庫(kù)表說(shuō)明:
schemata表:提供了當(dāng)前mysql實(shí)例中所有數(shù)據(jù)庫(kù)的信息。是show databases的結(jié)果取之此表。
tables表:提供了關(guān)于數(shù)據(jù)庫(kù)中的表的信息(包括視圖)。詳細(xì)表述了某個(gè)表屬于哪個(gè)schema,表類型,表引擎,創(chuàng)建時(shí)間等信息。是show tables from schemaname的結(jié)果取之此表。
columns表:提供了表中的列信息。詳細(xì)表述了某張表的所有列以及每個(gè)列的信息。是show columns from schemaname.tablename的結(jié)果取之此表。
statistics表:提供了關(guān)于表索引的信息。是show index from schemaname.tablename的結(jié)果取之此表。
user_privileges(用戶權(quán)限表)表:給出了關(guān)于用戶權(quán)限的信息。該信息源自mysql.user授權(quán)表。是非標(biāo)準(zhǔn)表。
schema_privileges(方案權(quán)限表)表:給出了關(guān)于方案(數(shù)據(jù)庫(kù))權(quán)限的信息。該信息來(lái)自mysql.db授權(quán)表。是非標(biāo)準(zhǔn)表。
table_privileges(表權(quán)限)表:給出了關(guān)于表權(quán)限的信息。該信息源自mysql.tables_priv授權(quán)表。是非標(biāo)準(zhǔn)表。
column_privileges(列權(quán)限)表:給出了關(guān)于列權(quán)限的信息。該信息源自mysql.columns_priv授權(quán)表。是非標(biāo)準(zhǔn)表。
character_sets(字符集)表:提供了mysql實(shí)例可用字符集的信息。是show character set結(jié)果集取之此表。
collations表:提供了關(guān)于各字符集的對(duì)照信息。
collation_character_set_applicability表:指明了可用于校對(duì)的字符集。這些列等效于show collation的前兩個(gè)顯示字段。
table_constraints表:描述了存在約束的表。以及表的約束類型。
key_column_usage表:描述了具有約束的鍵列。
routines表:提供了關(guān)于存儲(chǔ)子程序(存儲(chǔ)程序和函數(shù))的信息。此時(shí),routines表不包含自定義函數(shù)(UDF)。名為“mysql.proc name”的列指明了對(duì)應(yīng)于information_schema.routines表的mysql.proc表列。
views表:給出了關(guān)于數(shù)據(jù)庫(kù)中的視圖的信息。需要有show views權(quán)限,否則無(wú)法查看視圖信息。
triggers表:提供了關(guān)于觸發(fā)程序的信息。必須有super權(quán)限才能查看該表。
select * from information_schema.schemata; select * from information_schema.tables; select * from information_schema.columns; select * from information_schema.statistics; select * from information_schema.user_privileges; select * from information_schema.schema_privileges; select * from information_schema.table_privileges; select * from information_schema.column_privileges; select * from information_schema.character_sets; select * from information_schema.collations; select * from information_schema.collation_character_set_applicability; select * from information_schema.table_constraints; select * from information_schema.key_column_usage; select * from information_schema.routines; select * from information_schema.views; select * from information_schema.triggers;
2 根據(jù)庫(kù)名獲取所有表的信息
【information_schema.`TABLES`】
select * from information_schema. tables where table_schema = '庫(kù)名';
3 根據(jù)庫(kù)名獲取所有的字段信息
【information_schema.`COLUMNS`】
select * from information_schema. columns
4 根據(jù)庫(kù)名獲取所有的表和表字段的基本信息
select c.table_schema as '庫(kù)名', t.table_name as '表名', t.table_comment as '表注釋', c.column_name as '列名', c.column_comment as '列注釋', c.ordinal_position as '列的排列順序', c.column_default as '默認(rèn)值', c.is_nullable as '是否為空', c.data_type as '數(shù)據(jù)類型', c.character_maximum_length as '字符最大長(zhǎng)度', c.numeric_precision as '數(shù)值精度(最大位數(shù))', c.numeric_scale as '小數(shù)精度', c.column_type as 列類型, c.column_key 'KEY', c.extra as '額外說(shuō)明' from information_schema. tables t left join information_schema. columns c on t.table_name = c.table_name and t.table_schema = c.table_schema where t.table_schema = 'dvmdm' order by c.table_name, c.ordinal_position;
5 查詢某個(gè)字段在多個(gè)數(shù)據(jù)庫(kù)表中的分布
select group_concat(table_schema separator ',') as '庫(kù)名', group_concat(table_name separator ',') as '表名', column_name as '列名', group_concat(column_type separator ',') as '列類型', group_concat(column_comment separator ',') as '注釋' from information_schema. columns where table_schema in ('庫(kù)1', '庫(kù)2', '庫(kù)3') group by column_name order by group_concat(table_schema separator ','), column_name
到此這篇關(guān)于Mysql查詢所有表和字段信息的方法的文章就介紹到這了,更多相關(guān)mysql查詢所有表和字段信息內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
傻瓜式用Eclipse連接MySQL數(shù)據(jù)庫(kù)
本來(lái)不想寫這么簡(jiǎn)單人文章,在百度上搜索我這個(gè)標(biāo)題,完全符合標(biāo)題的一大堆。但我按照那些文章?lián)v鼓了很久,就是不行。2015-09-09深入理解mysql SET NAMES和mysql(i)_set_charset的區(qū)別
最近公司組織了個(gè)PHP安全編程的培訓(xùn), 其中涉及到一部分關(guān)于Mysql的 SET NAMES 和mysql_set_charset (mysqli_set_charset)的內(nèi)容2012-01-01mysql根據(jù)逗號(hào)將一行數(shù)據(jù)拆分成多行數(shù)據(jù)
本文主要介紹了mysql根據(jù)逗號(hào)將一行數(shù)據(jù)拆分成多行數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12基于Mysql的Sequence實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇基于Mysql的Sequence實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09