MySQL information_schema的介绍

大家在安装或使用MYSQL时,会发现除了自己安装的数据库以外,还有一个 information_schema数据库。information_schema数据库是做什么用的呢,使用WordPress博客的朋友可能会想, 是不是安装模板添加的数据库呀?看完本片文章后,你就会对information_schema数据库有所了解。

information_schema数据库是MySQL自带的,它提供了访问数据库元数据的方式。什么是元数据呢?元数据是关于数据的数据,如数据库名或表名,列的数据类型,或访问权限等。有些时候用于表述该信息的其他术语包括“数据词典”和“系统目录”。
在 MySQL中,把 information_schema 看作是一个数据库,确切说是信息数据库。其中保存着关于MySQL服务器所维护的所有其他数据库的信息。如数据库名,数据库的表,表栏的数据类型与访问权 限等。在INFORMATION_SCHEMA中,有数个只读表。它们实际上是视图,而不是基本表,因此,你将无法看到与之相关的任何文件。

information_schema数据库表说明:

SCHEMATA表:提供了当前mysql实例中所有数据库的信息。是show databases的结果取之此表。

TABLES表:提供了关于数据库中的表的信息(包括视图)。详细表述了某个表属于哪个schema,表类型,表引擎,创建时间等信息。是show tables from schemaname的结果取之此表。

COLUMNS表:提供了表中的列信息。详细表述了某张表的所有列以及每个列的信息。是show columns from schemaname.tablename的结果取之此表。

STATISTICS表:提供了关于表索引的信息。是show index from schemaname.tablename的结果取之此表。

USER_PRIVILEGES(用户权限)表:给出了关于全程权限的信息。该信息源自mysql.user授权表。是非标准表。

SCHEMA_PRIVILEGES(方案权限)表:给出了关于方案(数据库)权限的信息。该信息来自mysql.db授权表。是非标准表。

TABLE_PRIVILEGES(表权限)表:给出了关于表权限的信息。该信息源自mysql.tables_priv授权表。是非标准表。

COLUMN_PRIVILEGES(列权限)表:给出了关于列权限的信息。该信息源自mysql.columns_priv授权表。是非标准表。

CHARACTER_SETS(字符集)表:提供了mysql实例可用字符集的信息。是SHOW CHARACTER SET结果集取之此表。

COLLATIONS表:提供了关于各字符集的对照信息。

COLLATION_CHARACTER_SET_APPLICABILITY表:指明了可用于校对的字符集。这些列等效于SHOW COLLATION的前两个显示字段。

TABLE_CONSTRAINTS表:描述了存在约束的表。以及表的约束类型。

KEY_COLUMN_USAGE表:描述了具有约束的键列。

ROUTINES表:提供了关于存储子程序(存储程序和函数)的信息。此时,ROUTINES表不包含自定义函数(UDF)。名为“mysql.proc name”的列指明了对应于INFORMATION_SCHEMA.ROUTINES表的mysql.proc表列。

VIEWS表:给出了关于数据库中的视图的信息。需要有show views权限,否则无法查看视图信息。

TRIGGERS表:提供了关于触发程序的信息。必须有super权限才能查看该表。

+---------------------------------------+

| Tables_in_information_schema |
+---------------------------------------+
| CHARACTER_SETS |
| COLLATIONS |
| COLLATION_CHARACTER_SET_APPLICABILITY |
| COLUMNS |
| COLUMN_PRIVILEGES |
| ENGINES |
| EVENTS |
| FILES |
| GLOBAL_STATUS |
| GLOBAL_VARIABLES |
| KEY_COLUMN_USAGE |
| PARAMETERS |
| PARTITIONS |
| PLUGINS |
| PROCESSLIST |
| PROFILING |
| REFERENTIAL_CONSTRAINTS |
| ROUTINES |
| SCHEMATA |
| SCHEMA_PRIVILEGES |
| SESSION_STATUS |
| SESSION_VARIABLES |
| STATISTICS |
| TABLES |
| TABLESPACES |
| TABLE_CONSTRAINTS |
| TABLE_PRIVILEGES |
| TRIGGERS |
| USER_PRIVILEGES |
| VIEWS |
| INNODB_CMP_RESET |
| INNODB_TRX |
| INNODB_CMPMEM_RESET |
| INNODB_LOCK_WAITS |
| INNODB_CMPMEM |
| INNODB_CMP |
| INNODB_LOCKS |
+---------------------------------------+

 

从MySQL 5开始, 你可以看到多了一个系统数据库information_schema . information_schema 存贮了其他所有数据库的信息。让我们来看看几个使用这个数据库的例子:

<!--more-->

1. 取得关于 information_schema的基本信息

information_schema是一个虚拟数据库,并不物理存在,在select的时候,从其他数据库获取相应的信息。

  1. mysql> show databases;
  2. +--------------------+
  3. | Database           |
  4. +--------------------+
  5. | information_schema |
  6. | bugs               |
  7. | mysql              |
  8. | sugarcrm           |
  9. +--------------------+
  10. 4 rows in set (0.00 sec)

 

 

以下是information_schema数据库中的表.

  1. mysql> use information_schema;
  2. mysql> show tables;
  3. +---------------------------------------+
  4. | Tables_in_information_schema          |
  5. +---------------------------------------+
  6. | CHARACTER_SETS                        |
  7. | COLLATIONS                            |
  8. | COLLATION_CHARACTER_SET_APPLICABILITY |
  9. | COLUMNS                               |
  10. | COLUMN_PRIVILEGES                     |
  11. | KEY_COLUMN_USAGE                      |
  12. | PROFILING                             |
  13. | ROUTINES                              |
  14. | SCHEMATA                              |
  15. | SCHEMA_PRIVILEGES                     |
  16. | STATISTICS                            |
  17. | TABLES                                |
  18. | TABLE_CONSTRAINTS                     |
  19. | TABLE_PRIVILEGES                      |
  20. | TRIGGERS                              |
  21. | USER_PRIVILEGES                       |
  22. | VIEWS                                 |
  23. +---------------------------------------+
  24. 17 rows in set (0.00 sec)

 

 

2. 查询表中数据超过1000行的表

  1. 以下的语句可以查出超过1000行数据的表
  2. mysql> select concat(table_schema,'.',table_name) as table_name,table_rows
  3. -> from information_schema.tables where table_rows > 1000
  4. -> order by table_rows desc;
  5. +----------------------------------+------------+
  6. | table_name                       | table_rows |
  7. +----------------------------------+------------+
  8. | bugs.series_data                 |      52778 |
  9. | bugs.bugs_activity               |      26436 |
  10. | bugs.longdescs                   |      21473 |
  11. | bugs.email_setting               |       5370 |
  12. | bugs.attachments                 |       4714 |
  13. | bugs.attach_data                 |       4651 |
  14. | bugs.cc                          |       4031 |
  15. | bugs.bugs                        |       2190 |
  16. | bugs.namedqueries_link_in_footer |       1228 |
  17. +----------------------------------+------------+
  18. 9 rows in set (0.04 sec)

3. 查询所有没有主键的表

 

 

 

 

  1. This example gives a list of all the tables without primary key.
  2. SELECT CONCAT(t.table_name,".",t.table_schema) as table_name
  3. FROM information_schema.TABLES t
  4. LEFT JOIN information_schema.TABLE_CONSTRAINTS tc
  5. ON t.table_schema = tc.table_schema
  6. AND t.table_name = tc.table_name
  7. AND tc.constraint_type = 'PRIMARY KEY'
  8. WHERE tc.constraint_name IS NULL
  9. 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个嘴大表

 

  1. mysql> SELECT concat(table_schema,'.',table_name) table_name,
  2. -> concat(round(data_length/(1024*1024),2),'M') data_length
  3. -> FROM information_schema.TABLES
  4. -> ORDER BY data_length DESC LIMIT 5;
  5. +--------------------+-------------+
  6. | table_name         | data_length |
  7. +--------------------+-------------+
  8. | bugs.attach_data   | 706.89M     |
  9. | bugs.longdescs     | 3.45M       |
  10. | bugs.bugs_activity | 1.45M       |
  11. | bugs.series_data   | 0.75M       |
  12. | bugs.attachments   | 0.51M       |
  13. +--------------------+-------------+
  14. 5 rows in set (0.05 sec)

 

 

 

发表评论