元數據--MySQL獲取元數據的方法


元數據:數據的數據,用以描述數據的信息也是數據,被稱為元數據
 
[MySQL]獲取元數據的方法
 
MySQL提供了以下三種方法用於獲取數據庫對象的元數據:
1)show語句
2)從INFORMATION_SCHEMA數據庫里查詢相關表(information_schema是一個虛擬數據庫,並不物理存在,它儲存數據的信息的數據庫)
3)命令行程序,如mysqlshow, mysqldump
 
--用SHOW語句獲取元數據
MySQL用show語句獲取元數據是最常用的方法,下面提供了幾種典型用法:
 
[sql]
show databases;  --列出所有數據庫
show create database db_name;  --查看數據庫的DDL
show tables; --列出默認數據庫的所有表
show tables from db_name;  --列出指定數據庫的所有表
show table status;  --查看表的描述性信息
show table status from db_name;
show create table tbl_name;  --查看表的DDL
show columns from tbl_name;  --查看列信息
show index from tbl_name;  --查看索引信息
 
 
有幾種show語句還可以帶有一條like 'pattern'字句,用來限制語句的輸出范圍,其中'pattern'允許包含'%'和'_'通配符,比如下面這條語句返回domaininfo表中以s開頭的所有列:
 
[sql]
show columns from domaininfo like 's%';
 
 
像上面這張支持like字句的所有show都可以改寫成一條where字句,如:
 
[sql]
show columns from domaininfo where field='sysdomain';
 
 
注:desc tbl_name和explain tbl_name的效果和show columns from tbl_name一致。
 
--從INFORMATION_SCHEMA數據庫里查詢相關表
INFORMATION_SCHEMA是MySQL自帶的一個系統數據庫,它里面存儲了所有的元數據,通過select里面的相關表就可以獲取你想要的元數據。和show語句相比,它比較麻煩,但它的好處是標准的SQL語句,更具有可移植性,且更靈活,可以通過各種表達式獲取你真正需要的信息。
 
從命令行獲取元數據
前面兩種方法都必須得在MySQL命令行里執行,而mysqlshow和mysqldump提供了從OS命令行獲取元數據庫的方法,如:
 
[plain]
mysqlshow  --列出所有數據庫
mysqlshow db_name  --列出給定數據庫的所有表
mysqlshow db_name tbl_name  --列出給定數據庫表的所有列
mysqlshow --keys db_name tbl_name  --列出索引信息
mysqlshow --status db_name  --列出數據庫的描述性信息
mysqldump可以讓你看到create table語句(就想show create table語句一樣),如:
[sql]
mysqldump --no-data db_name [tbl_name] ...
 
*注意:在用mysqldump查看表結構時,一定要加上--no-data,否則你看到的將是數據庫表里的數據。
 
 
--MySql數據庫信息information_schema的查詢使用
從MySQL 5開始, 你可以看到多了一個系統數據庫information_schema . information_schema 存貯了其他所有數據庫的信息。讓我們來看看幾個使用這個數據庫的例子:
 
<!--more-->
1. 取得關於 information_schema的基本信息
 
information_schema是一個虛擬數據庫,並不物理存在,在select的時候,從其他數據庫獲取相應的信息。
 
    mysql> show databases;  
    +--------------------+  
    | Database           |  
    +--------------------+  
    | information_schema |  
    | bugs               |  
    | mysql              |  
    | sugarcrm           |  
    +--------------------+  
    4 rows in set (0.00 sec)  
 
 
 
以下是information_schema數據庫中的表.
 
 
    mysql> use information_schema;  
    mysql> show tables;  
    +---------------------------------------+  
    | Tables_in_information_schema          |  
    +---------------------------------------+  
    | CHARACTER_SETS                        |  
    | COLLATIONS                            |  
    | COLLATION_CHARACTER_SET_APPLICABILITY |  
    | COLUMNS                               |  
    | COLUMN_PRIVILEGES                     |  
    | KEY_COLUMN_USAGE                      |  
    | PROFILING                             |  
    | ROUTINES                              |  
    | SCHEMATA                              |  
    | SCHEMA_PRIVILEGES                     |  
    | STATISTICS                            |  
    | TABLES                                |  
    | TABLE_CONSTRAINTS                     |  
    | TABLE_PRIVILEGES                      |  
    | TRIGGERS                              |  
    | USER_PRIVILEGES                       |  
    | VIEWS                                 |  
    +---------------------------------------+  
    17 rows in set (0.00 sec)  
 
2. 查詢表中數據超過1000行的表
 
    以下的語句可以查出超過1000行數據的表  
     
    mysql> select concat(table_schema,'.',table_name) as table_name,table_rows  
        -> from information_schema.tables where table_rows > 1000  
        -> order by table_rows desc;  
     
    +----------------------------------+------------+  
    | table_name                       | table_rows |  
    +----------------------------------+------------+  
    | bugs.series_data                 |      52778 |  
    | bugs.bugs_activity               |      26436 |  
    | bugs.longdescs                   |      21473 |  
    | bugs.email_setting               |       5370 |  
    | bugs.attachments                 |       4714 |  
    | bugs.attach_data                 |       4651 |  
    | bugs.cc                          |       4031 |  
    | bugs.bugs                        |       2190 |  
    | bugs.namedqueries_link_in_footer |       1228 |  
    +----------------------------------+------------+  
    9 rows in set (0.04 sec)  
 
3. 查詢所有沒有主鍵的表
 
    This example gives a list of all the tables without primary key.  
     
    SELECT CONCAT(t.table_name,".",t.table_schema) as table_name  
    FROM information_schema.TABLES t  
    LEFT JOIN information_schema.TABLE_CONSTRAINTS tc  
    ON t.table_schema = tc.table_schema  
    AND t.table_name = tc.table_name  
    AND tc.constraint_type = 'PRIMARY KEY'  
    WHERE tc.constraint_name IS NULL  
    AND t.table_type = 'BASE TABLE';  
 
4. 實現表的歷史數據information_schema
 
Putting the MySQL information_schema to Use article implements a history database using the information schema. The first half of this article describes the requirements for the history database, and a generic design to implement it. The second half describes the stepwise construction of code-generator that creates the SQL to construct and load the history database. The code-generator is driven by the information schema and some features of the information schema are discussed in detail.
 
 
 
5. 查詢5個最大表
 
    mysql> SELECT concat(table_schema,'.',table_name) table_name,  
        -> concat(round(data_length/(1024*1024),2),'M') data_length  
        -> FROM information_schema.TABLES  
        -> ORDER BY data_length DESC LIMIT 5;  
     
    +--------------------+-------------+  
    | table_name         | data_length |  
    +--------------------+-------------+  
    | bugs.attach_data   | 706.89M     |  
    | bugs.longdescs     | 3.45M       |  
    | bugs.bugs_activity | 1.45M       |  
    | bugs.series_data   | 0.75M       |  
    | bugs.attachments   | 0.51M       |  
    +--------------------+-------------+  
    5 rows in set (0.05 sec)  
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM