大家在安装或使用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权限才能查看该表.
以下官方文档:
INFORMATION_SCHEMA
provides access to
database metadata, information about the MySQL server such as the name of a database or table, the
data type of a column, or access privileges. Other terms that are sometimes used for this information are data
dictionary and system catalog.
INFORMATION_SCHEMA Usage Notes
INFORMATION_SCHEMA
is a database within
each MySQL instance, the place that stores information about all the other databases that the MySQL server maintains. The INFORMATION_SCHEMA
database
contains several read-only tables. They are actually views, not base tables, so there are no files associated with them, and you cannot set triggers on them. Also, there is no database directory with that name.
Although you can select INFORMATION_SCHEMA
as
the default database with a USE
statement,
you can only read the contents of tables, not performINSERT
, UPDATE
,
or DELETE
operations
on them.
Example
Here is an example of a statement that retrieves information from INFORMATION_SCHEMA
:
mysql> SELECT table_name, table_type, engine
FROM information_schema.tables
WHERE table_schema = 'db5'
ORDER BY table_name;
+------------+------------+--------+
| table_name | table_type | engine |
+------------+------------+--------+
| fk | BASE TABLE | InnoDB |
| fk2 | BASE TABLE | InnoDB |
| goto | BASE TABLE | MyISAM |
| into | BASE TABLE | MyISAM |
| k | BASE TABLE | MyISAM |
| kurs | BASE TABLE | MyISAM |
| loop | BASE TABLE | MyISAM |
| pk | BASE TABLE | InnoDB |
| t | BASE TABLE | MyISAM |
| t2 | BASE TABLE | MyISAM |
| t3 | BASE TABLE | MyISAM |
| t7 | BASE TABLE | MyISAM |
| tables | BASE TABLE | MyISAM |
| v | VIEW | NULL |
| v2 | VIEW | NULL |
| v3 | VIEW | NULL |
| v56 | VIEW | NULL |
+------------+------------+--------+
17 rows in set (0.01 sec)
Explanation: The statement requests a list of all the tables in database db5
,
showing just three pieces of information: the name of the table, its type, and its storage engine.
Character Set Considerations
The definition for character columns (for example, TABLES.TABLE_NAME
)
is generally VARCHAR(
where N
)
CHARACTER SET utf8N
is
at least 64. MySQL uses the default collation for this character set (utf8_general_ci
)
for all searches, sorts, comparisons, and other string operations on such columns.
Because some MySQL objects are represented as files, searches in INFORMATION_SCHEMA
string
columns can be affected by file system case sensitivity. For more information, see Section 10.1.8.7,
“Using Collation in INFORMATION_SCHEMA Searches”.
INFORMATION_SCHEMA as Alternative to SHOW Statements
The SELECT ... FROM INFORMATION_SCHEMA
statement
is intended as a more consistent way to provide access to the information provided by the various SHOW
statements
that MySQL supports (SHOW
DATABASES
, SHOW
TABLES
, and so forth). Using SELECT
has
these advantages, compared to SHOW
:
-
It conforms to Codd's rules, because all access is done on tables.
-
You can use the familiar syntax of the
SELECT
statement, and only need to learn some table and column names. -
The implementor need not worry about adding keywords.
-
You can filter, sort, concatenate, and transform the results from
INFORMATION_SCHEMA
queries into whatever format your application needs, such as a data structure or a text representation to parse. -
This technique is more interoperable with other database systems. For example, Oracle Database users are familiar with querying tables in the Oracle data dictionary.
Because SHOW
is
familiar and widely used, the SHOW
statements
remain as an alternative. In fact, along with the implementation of INFORMATION_SCHEMA
,
there are enhancements to SHOW
as
described in Section 21.33,
“Extensions to SHOW Statements”.
Privileges
Each MySQL user has the right to access these tables, but can see only the rows in the tables that correspond to objects for which the user has the proper access privileges. In some cases (for example, the ROUTINE_DEFINITION
column
in the INFORMATION_SCHEMA.ROUTINES
table),
users who have insufficient privileges see NULL
.
These restrictions do not apply for InnoDB
tables;
you can see them with only the PROCESS
privilege.
The same privileges apply to selecting information from INFORMATION_SCHEMA
and
viewing the same information through SHOW
statements.
In either case, you must have some privilege on an object to see information about it.
Performance Considerations
INFORMATION_SCHEMA
queries that search
for information from more than one database might take a long time and impact performance. To check the efficiency of a query, you can use EXPLAIN
.
For information about using EXPLAIN
output
to tune INFORMATION_SCHEMA
queries,
see Section 8.2.3,
“Optimizing INFORMATION_SCHEMA Queries”.
Standards Considerations
The implementation for the INFORMATION_SCHEMA
table
structures in MySQL follows the ANSI/ISO SQL:2003 standard Part 11 Schemata. Our intent is approximate
compliance with SQL:2003 core feature F021 Basic information schema.
Users of SQL Server 2000 (which also follows the standard) may notice a strong similarity. However, MySQL has omitted many columns that are not relevant for our implementation, and added columns that are MySQL-specific. One such column is the ENGINE
column
in the INFORMATION_SCHEMA.TABLES
table.
Although other DBMSs use a variety of names, like syscat
or system
,
the standard name is INFORMATION_SCHEMA
.
To avoid using any name that is reserved in the standard or in DB2, SQL Server, or Oracle, we changed the names of some columns marked “MySQL
extension”. (For example, we changed COLLATION
to TABLE_COLLATION
in
the TABLES
table.)
See the list of reserved words near the end of this article:https://web.archive.org/web/20070428032454/http://www.dbazine.com/db2/db2-disarticles/gulutzan5.
Conventions in the INFORMATION_SCHEMA Reference Sections
The following sections describe each of the tables and columns in INFORMATION_SCHEMA
.
For each column, there are three pieces of information:
-
“
INFORMATION_SCHEMA
Name” indicates the name for the column in theINFORMATION_SCHEMA
table. This corresponds to the standard SQL name unless the “Remarks” field says “MySQL extension.” -
“
SHOW
Name” indicates the equivalent field name in the closestSHOW
statement, if there is one. -
“Remarks” provides additional information where applicable. If this field is
NULL
, it means that the value of the column is alwaysNULL
. If this field says “MySQL extension,” the column is a MySQL extension to standard SQL.
Many sections indicate what SHOW
statement
is equivalent to a SELECT
that
retrieves information from INFORMATION_SCHEMA
.
For SHOW
statements
that display information for the default database if you omit a FROM
clause,
you can often select information for the default database by adding an db_name
AND
TABLE_SCHEMA = SCHEMA()
condition to the WHERE
clause
of a query that retrieves information from an INFORMATION_SCHEMA
table.
Related Information
These sections discuss additional INFORMATION_SCHEMA
-related
topics:
-
information about
INFORMATION_SCHEMA
tables specific to theInnoDB
storage engine: Section 21.29, “InnoDB INFORMATION_SCHEMA Tables” -
information about
INFORMATION_SCHEMA
tables specific to theNDB
storage engine (NDB Cluster): Section 21.30, “NDB Cluster INFORMATION_SCHEMA Tables” -
information about
INFORMATION_SCHEMA
tables specific to the thread pool plugin: Section 21.31, “Thread Pool INFORMATION_SCHEMA Tables” -
Answers to questions that are often asked concerning the
INFORMATION_SCHEMA
database: Section A.7, “MySQL 5.6 FAQ: INFORMATION_SCHEMA” -
INFORMATION_SCHEMA
queries and the optimizer: Section 8.2.3, “Optimizing INFORMATION_SCHEMA Queries” -
The effect of collation on
INFORMATION_SCHEMA
comparisons: Section 10.1.8.7, “Using Collation in INFORMATION_SCHEMA Searches”
大家在安装或使用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权限才能查看该表.