20.2. Using INFORMATION_SCHEMA to Obtain MetadataThe INFORMATION_SCHEMA database serves as a central repository for database metadata. It is a "virtual database" in the sense that it is not stored on disk anywhere, but it contains tables like any other database, and the contents of its tables can be accessed using SELECT like any other tables. Furthermore, you can use SELECT to obtain information about INFORMATION_SCHEMA itself. For example, to list the names of its tables, use the following statement: mysql> SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES -> WHERE TABLE_SCHEMA = 'INFORMATION_SCHEMA' -> ORDER BY TABLE_NAME; +---------------------------------------+ | TABLE_NAME | +---------------------------------------+ | CHARACTER_SETS | | COLLATIONS | | COLLATION_CHARACTER_SET_APPLICABILITY | | COLUMNS | | COLUMN_PRIVILEGES | | KEY_COLUMN_USAGE | | ROUTINES | | SCHEMATA | | SCHEMA_PRIVILEGES | | STATISTICS | | TABLES | | TABLE_CONSTRAINTS | | TABLE_PRIVILEGES | | TRIGGERS | | USER_PRIVILEGES | | VIEWS | +---------------------------------------+ The tables shown in that list contain the following types of information:
To display the names of the columns in a given INFORMATION_SCHEMA table, use a statement of the following form, where the TABLE_NAME comparison value names the table in which you're interested: mysql> SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS -> WHERE TABLE_SCHEMA = 'INFORMATION_SCHEMA' -> AND TABLE_NAME = 'VIEWS'; +-----------------+ | COLUMN_NAME | +-----------------+ | TABLE_CATALOG | | TABLE_SCHEMA | | TABLE_NAME | | VIEW_DEFINITION | | CHECK_OPTION | | IS_UPDATABLE | +-----------------+ The names of the INFORMATION_SCHEMA database, its tables, and columns are not case sensitive: mysql> SELECT column_name FROM information_schema.columns -> WHERE table_schema = 'information_schema' -> AND table_name = 'views'; +-----------------+ | column_name | +-----------------+ | TABLE_CATALOG | | TABLE_SCHEMA | | TABLE_NAME | | VIEW_DEFINITION | | CHECK_OPTION | | IS_UPDATABLE | +-----------------+ This study guide does not go into any detail about the columns in INFORMATION_SCHEMA tables. For a more comprehensive description of each table's columns, see the MySQL Reference Manual. When you retrieve metadata from INFORMATION_SCHEMA by using SELECT statements, you have the freedom to use any of the usual SELECT features that you expect:
The following examples demonstrate how to exploit various features of SELECT to pull out information in different ways from INFORMATION_SCHEMA:
INFORMATION_SCHEMA is read-only. Its tables cannot be modified with statements such as INSERT, DELETE, or UPDATE. If you try, an error occurs: mysql> DELETE FROM INFORMATION_SCHEMA.VIEWS; ERROR 1288 (HY000): The target table VIEWS of the DELETE is not updatable |