17 - 05 - 28 MySQL的 information_schema




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權限才能查看該表.



以下官方文檔:

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 performINSERTUPDATE, 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(N) CHARACTER SET utf8 where N 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 DATABASESSHOW 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 the INFORMATION_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 closest SHOW 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 always NULL. 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 db_name clause, you can often select information for the default database by adding an 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:


_______________________________________________________________________________________轉載

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權限才能查看該表.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章