MYSQL小白的FAQ系列—-6—-如何查看mysql中的“元数据”(类似于oracle的数据字典)

联系:QQ(5163721)

标题:MYSQL小白的FAQ系列—-6—-如何查看mysql中的“元数据”(类似于oracle的数据字典)

作者:Lunar©版权所有[文章允许转载,但必须以链接方式注明源地址,否则追究法律责任.]

mysql中,INFORMATION_SCHEMA提供了访问数据库元数据的方式.
元数据是关于数据的数据,如数据库名或表名,列的数据类型,或访问权限等。有些时候用于表述该信息的其他术语包括“数据词典”和“系统目录”。
在mysql5.5的官方文档中有以下内容:
第23章:INFORMATION_SCHEMA信息数据库

目录
23.1. INFORMATION_SCHEMA表
23.1.1. INFORMATION_SCHEMA SCHEMATA表
23.1.2. INFORMATION_SCHEMA TABLES表
23.1.3. INFORMATION_SCHEMA COLUMNS表
23.1.4. INFORMATION_SCHEMA STATISTICS表
23.1.5. INFORMATION_SCHEMA USER_PRIVILEGES表
23.1.6. INFORMATION_SCHEMA SCHEMA_PRIVILEGES表
23.1.7. INFORMATION_SCHEMA TABLE_PRIVILEGES表
23.1.8. INFORMATION_SCHEMA COLUMN_PRIVILEGES表
23.1.9. INFORMATION_SCHEMA CHARACTER_SETS表
23.1.10. INFORMATION_SCHEMA COLLATIONS表
23.1.11. INFORMATION_SCHEMA COLLATION_CHARACTER_SET_APPLICABILITY表
23.1.12. INFORMATION_SCHEMA TABLE_CONSTRAINTS表
23.1.13. INFORMATION_SCHEMA KEY_COLUMN_USAGE表
23.1.14. INFORMATION_SCHEMA ROUTINES表
23.1.15. INFORMATION_SCHEMA VIEWS表
23.1.16. INFORMATION_SCHEMA TRIGGERS表
23.1.17. 其他INFORMATION_SCHEMA表

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

mysql> use information_schema;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> desc information_schema.tables;
+-----------------+---------------------+------+-----+---------+-------+
| Field           | Type                | Null | Key | Default | Extra |
+-----------------+---------------------+------+-----+---------+-------+
| TABLE_CATALOG   | varchar(512)        | NO   |     |         |       |
| TABLE_SCHEMA    | varchar(64)         | NO   |     |         |       |
| TABLE_NAME      | varchar(64)         | NO   |     |         |       |
| TABLE_TYPE      | varchar(64)         | NO   |     |         |       |
| ENGINE          | varchar(64)         | YES  |     | NULL    |       |
| VERSION         | bigint(21) unsigned | YES  |     | NULL    |       |
| ROW_FORMAT      | varchar(10)         | YES  |     | NULL    |       |
| TABLE_ROWS      | bigint(21) unsigned | YES  |     | NULL    |       |
| AVG_ROW_LENGTH  | bigint(21) unsigned | YES  |     | NULL    |       |
| DATA_LENGTH     | bigint(21) unsigned | YES  |     | NULL    |       |
| MAX_DATA_LENGTH | bigint(21) unsigned | YES  |     | NULL    |       |
| INDEX_LENGTH    | bigint(21) unsigned | YES  |     | NULL    |       |
| DATA_FREE       | bigint(21) unsigned | YES  |     | NULL    |       |
| AUTO_INCREMENT  | bigint(21) unsigned | YES  |     | NULL    |       |
| CREATE_TIME     | datetime            | YES  |     | NULL    |       |
| UPDATE_TIME     | datetime            | YES  |     | NULL    |       |
| CHECK_TIME      | datetime            | YES  |     | NULL    |       |
| TABLE_COLLATION | varchar(32)         | YES  |     | NULL    |       |
| CHECKSUM        | bigint(21) unsigned | YES  |     | NULL    |       |
| CREATE_OPTIONS  | varchar(255)        | YES  |     | NULL    |       |
| TABLE_COMMENT   | varchar(2048)       | NO   |     |         |       |
+-----------------+---------------------+------+-----+---------+-------+
21 rows in set (0.00 sec)

mysql> 

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权限才能查看该表

mysql> SELECT table_name, table_schema,table_type, engine
    -> FROM information_schema.tables;
+---------------------------------------+--------------------+-------------+--------+
| table_name                            | table_schema       | table_type  | engine |
+---------------------------------------+--------------------+-------------+--------+
| CHARACTER_SETS                        | information_schema | SYSTEM VIEW | MEMORY |
| COLLATIONS                            | information_schema | SYSTEM VIEW | MEMORY |
| COLLATION_CHARACTER_SET_APPLICABILITY | information_schema | SYSTEM VIEW | MEMORY |
| COLUMNS                               | information_schema | SYSTEM VIEW | MyISAM |
| COLUMN_PRIVILEGES                     | information_schema | SYSTEM VIEW | MEMORY |
| ENGINES                               | information_schema | SYSTEM VIEW | MEMORY |
| EVENTS                                | information_schema | SYSTEM VIEW | MyISAM |
| FILES                                 | information_schema | SYSTEM VIEW | MEMORY |
| GLOBAL_STATUS                         | information_schema | SYSTEM VIEW | MEMORY |
| GLOBAL_VARIABLES                      | information_schema | SYSTEM VIEW | MEMORY |
| KEY_COLUMN_USAGE                      | information_schema | SYSTEM VIEW | MEMORY |
| OPTIMIZER_TRACE                       | information_schema | SYSTEM VIEW | MyISAM |
| PARAMETERS                            | information_schema | SYSTEM VIEW | MyISAM |
| PARTITIONS                            | information_schema | SYSTEM VIEW | MyISAM |
| PLUGINS                               | information_schema | SYSTEM VIEW | MyISAM |
| PROCESSLIST                           | information_schema | SYSTEM VIEW | MyISAM |
| PROFILING                             | information_schema | SYSTEM VIEW | MEMORY |
| REFERENTIAL_CONSTRAINTS               | information_schema | SYSTEM VIEW | MEMORY |
| ROUTINES                              | information_schema | SYSTEM VIEW | MyISAM |
| SCHEMATA                              | information_schema | SYSTEM VIEW | MEMORY |
| SCHEMA_PRIVILEGES                     | information_schema | SYSTEM VIEW | MEMORY |
| SESSION_STATUS                        | information_schema | SYSTEM VIEW | MEMORY |
| SESSION_VARIABLES                     | information_schema | SYSTEM VIEW | MEMORY |
| STATISTICS                            | information_schema | SYSTEM VIEW | MEMORY |
| TABLES                                | information_schema | SYSTEM VIEW | MEMORY |
| TABLESPACES                           | information_schema | SYSTEM VIEW | MEMORY |
| TABLE_CONSTRAINTS                     | information_schema | SYSTEM VIEW | MEMORY |
| TABLE_PRIVILEGES                      | information_schema | SYSTEM VIEW | MEMORY |
| TRIGGERS                              | information_schema | SYSTEM VIEW | MyISAM |
| USER_PRIVILEGES                       | information_schema | SYSTEM VIEW | MEMORY |
| VIEWS                                 | information_schema | SYSTEM VIEW | MyISAM |
| INNODB_LOCKS                          | information_schema | SYSTEM VIEW | MEMORY |
| INNODB_TRX                            | information_schema | SYSTEM VIEW | MEMORY |
| INNODB_SYS_DATAFILES                  | information_schema | SYSTEM VIEW | MEMORY |
| INNODB_LOCK_WAITS                     | information_schema | SYSTEM VIEW | MEMORY |
| INNODB_SYS_TABLESTATS                 | information_schema | SYSTEM VIEW | MEMORY |
| INNODB_CMP                            | information_schema | SYSTEM VIEW | MEMORY |
| INNODB_METRICS                        | information_schema | SYSTEM VIEW | MEMORY |
| INNODB_CMP_RESET                      | information_schema | SYSTEM VIEW | MEMORY |
| INNODB_CMP_PER_INDEX                  | information_schema | SYSTEM VIEW | MEMORY |
| INNODB_CMPMEM_RESET                   | information_schema | SYSTEM VIEW | MEMORY |
| INNODB_FT_DELETED                     | information_schema | SYSTEM VIEW | MEMORY |
| INNODB_BUFFER_PAGE_LRU                | information_schema | SYSTEM VIEW | MEMORY |
| INNODB_SYS_FOREIGN                    | information_schema | SYSTEM VIEW | MEMORY |
| INNODB_SYS_COLUMNS                    | information_schema | SYSTEM VIEW | MEMORY |
| INNODB_SYS_INDEXES                    | information_schema | SYSTEM VIEW | MEMORY |
| INNODB_FT_DEFAULT_STOPWORD            | information_schema | SYSTEM VIEW | MEMORY |
| INNODB_SYS_FIELDS                     | information_schema | SYSTEM VIEW | MEMORY |
| INNODB_CMP_PER_INDEX_RESET            | information_schema | SYSTEM VIEW | MEMORY |
| INNODB_BUFFER_PAGE                    | information_schema | SYSTEM VIEW | MEMORY |
| INNODB_CMPMEM                         | information_schema | SYSTEM VIEW | MEMORY |
| INNODB_FT_INDEX_TABLE                 | information_schema | SYSTEM VIEW | MEMORY |
| INNODB_FT_BEING_DELETED               | information_schema | SYSTEM VIEW | MEMORY |
| INNODB_SYS_TABLESPACES                | information_schema | SYSTEM VIEW | MEMORY |
| INNODB_FT_INDEX_CACHE                 | information_schema | SYSTEM VIEW | MEMORY |
| INNODB_SYS_FOREIGN_COLS               | information_schema | SYSTEM VIEW | MEMORY |
| INNODB_SYS_TABLES                     | information_schema | SYSTEM VIEW | MEMORY |
| INNODB_BUFFER_POOL_STATS              | information_schema | SYSTEM VIEW | MEMORY |
| INNODB_FT_CONFIG                      | information_schema | SYSTEM VIEW | MEMORY |
| columns_priv                          | mysql              | BASE TABLE  | MyISAM |
| db                                    | mysql              | BASE TABLE  | MyISAM |
| event                                 | mysql              | BASE TABLE  | MyISAM |
| func                                  | mysql              | BASE TABLE  | MyISAM |
| general_log                           | mysql              | BASE TABLE  | CSV    |
| help_category                         | mysql              | BASE TABLE  | MyISAM |
| help_keyword                          | mysql              | BASE TABLE  | MyISAM |
| help_relation                         | mysql              | BASE TABLE  | MyISAM |
| help_topic                            | mysql              | BASE TABLE  | MyISAM |
| innodb_index_stats                    | mysql              | BASE TABLE  | InnoDB |
| innodb_table_stats                    | mysql              | BASE TABLE  | InnoDB |
| ndb_binlog_index                      | mysql              | BASE TABLE  | MyISAM |
| plugin                                | mysql              | BASE TABLE  | MyISAM |
| proc                                  | mysql              | BASE TABLE  | MyISAM |
| procs_priv                            | mysql              | BASE TABLE  | MyISAM |
| proxies_priv                          | mysql              | BASE TABLE  | MyISAM |
| servers                               | mysql              | BASE TABLE  | MyISAM |
| slave_master_info                     | mysql              | BASE TABLE  | InnoDB |
| slave_relay_log_info                  | mysql              | BASE TABLE  | InnoDB |
| slave_worker_info                     | mysql              | BASE TABLE  | InnoDB |
| slow_log                              | mysql              | BASE TABLE  | CSV    |
| tables_priv                           | mysql              | BASE TABLE  | MyISAM |
| time_zone                             | mysql              | BASE TABLE  | MyISAM |
| time_zone_leap_second                 | mysql              | BASE TABLE  | MyISAM |
| time_zone_name                        | mysql              | BASE TABLE  | MyISAM |
| time_zone_transition                  | mysql              | BASE TABLE  | MyISAM |
| time_zone_transition_type             | mysql              | BASE TABLE  | MyISAM |
| user                                  | mysql              | BASE TABLE  | MyISAM |
+---------------------------------------+--------------------+-------------+--------+
87 rows in set (0.01 sec)

mysql> 

SELECT table_name, table_schema,table_type, engine
FROM information_schema.tables
WHERE table_schema = 'mysql'
ORDER BY table_name DESC;

INFORMATION_SCHEMA是信息数据库,其中保存着关于MySQL服务器所维护的所有其他数据库的信息。在INFORMATION_SCHEMA中,有数个只读表。
它们实际上是视图,而不是基本表,因此,你将无法看到与之相关的任何文件。
每位MySQL用户均有权访问这些表,但仅限于表中的特定行,在这类行中含有用户具有恰当访问权限的对象。

此条目发表在 FAQ 分类目录,贴了 , , 标签。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注