The highest level of information that is available in the Information Schema concerns the whole MySQL instance. This includes such information as which character sets are available and which plugins are installed.
The views with system information are summarized in Table 7-1.
Table 7-1. Information Schema views with system information
View Name Description
CHARACTER_SETS The character sets available.
COLLATIONS The collations available for each character set. This includes the id of the collation which in some cases (e.g., in the binary log) is used to uniquely specify both the collation and character set.
COLLATION_CHARACTER_
SET_APPLICABILITY
The mapping of collations to character sets (the same as the first two columns of COLLATIONS).
ENGINES The storage engines that are known and whether they are loaded.
INNODB_FT_DEFAULT_
STOPWORD
A list of the default stopwords that are used when creating a full text index on an InnoDB table.
KEYWORDS A list of the keywords in MySQL and whether the keyword is reserved.
(continued)
114
The system-related views largely work as reference views with the RESOURCE_GROUPS table being somewhat a difference as it is possible to add resource groups as it will be discussed in Chapter 17.
The KEYWORDS view is, for example, useful when testing an upgrade as you can use it to verify whether any of your schema, table, column, routine, or parameter names matches a keyword in the new version. If that is the case, you will need to update the application to quote the identifier, if that is not already the case. To find all column names matching a keyword:
SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, RESERVED FROM information_schema.COLUMNS
INNER JOIN information_schema.KEYWORDS ON KEYWORDS.WORD = COLUMNS.COLUMN_NAME WHERE TABLE_SCHEMA NOT IN ('mysql',
'information_schema', 'performance_schema', 'sys'
)
ORDER BY TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME;
The query uses the COLUMNS view to find all column names except for the system schemas (you can choose to include those, if you use those in your application or in scripts). The COLUMNS view is one of several views describing the schema objects.
View Name Description
PLUGINS The plugins known to MySQL including the status.
RESOURCE_GROUPS The resource groups that are used by threads to do their work. A resource group specifies the priority of a thread and which CpUs it can use.
ST_SPATIAL_REFERENCE_
SYSTEMS
A list of the spatial reference systems including the SRS_ID column which contains the id used to specify a reference system for spatial columns.
Table 7-1. (continued)
ChApTer 7 The InForMATIon SCheMA
Schema Information
The views with information about the schema objects are among the most useful views in the Information Schema. These are also the source for several of the SHOW statements.
You can use the views to find information from everything from parameters for a stored routine to database names. The views with schema information are summarized in Table 7-2.
Table 7-2. Information Schema views with schema information
View Name Description
CHECK_CONSTRAINTS This view contains information about the CHECK constraints and is available in MySQL 8.0.16 and later.
COLUMN_STATISTICS The definition of histograms including the statistics. This is a very useful view for query performance tuning.
COLUMNS The column definitions.
EVENTS The definitions of the stored events.
FILES Information about InnoDB tablespace files.
INNODB_COLUMNS Metadata information for columns in InnoDB tables.
INNODB_DATAFILES This view links the InnoDB tablespace ids to the file system paths.
INNODB_FIELDS Metadata for columns included in InnoDB indexes.
INNODB_FOREIGN Metadata for the InnoDB foreign keys.
INNODB_FOREIGN_COLS Lists the child and parent columns of InnoDB foreign keys.
INNODB_FT_BEING_DELETED A snapshot of the INNODB_FT_DELETED view during an OPTIMIZE TABLE statement for the InnoDB table specified in the innodb_ft_aux_table option.
INNODB_FT_CONFIG Configuration information for full text indexes on the InnoDB table specified in the innodb_ft_aux_table option.
INNODB_FT_DELETED rows that have been deleted from full text indexes for the InnoDB table specified in the innodb_ft_aux_table option. InnoDB uses this extra list for performance reasons to avoid having to update the index itself for each DML statement.
(continued)
116
View Name Description
INNODB_FT_INDEX_CACHE newly inserted rows into the full text indexes for the InnoDB table specified in the innodb_ft_aux_table option. InnoDB uses this extra list for performance reasons to avoid having to update the index itself for each DML statement.
INNODB_FT_INDEX_TABLE The inverted full text index for the InnoDB table specified in the innodb_ft_aux_table option.
INNODB_INDEXES Information about indexes on InnoDB tables. This includes internal information such as the page number of the root page and the merge threshold.
INNODB_TABLES Metadata for the InnoDB tables.
INNODB_TABLESPACES Metadata for the InnoDB tablespaces.
INNODB_TABLESPACES_BRIEF This view combines the SPACE, NAME, FLAG, and SPACE_TYPE columns from INNODB_TABLESPACES with the PATH column from INNODB_DATAFILES to provide a summary of the InnoDB tablespace.
INNODB_TABLESTATS Table statistics for InnoDB tables. Some of these statistics are updated at the same time as index statistics; others are maintained at an ongoing basis.
INNODB_TEMP_TABLE_INFO Metadata for InnoDB temporary tables (both internal and explicit).
INNODB_VIRTUAL Internal metadata information about virtual generated columns on InnoDB tables.
KEY_COLUMN_USAGE Information about the primary keys, unique keys, and foreign keys.
PARAMETERS Information about the parameters for stored functions and stored procedures.
PARTITIONS Information about table partitions.
REFERENTIAL_CONSTRAINTS Information about foreign keys.
Table 7-2. (continued)
(continued)
ChApTer 7 The InForMATIon SCheMA
View Name Description
ROUTINES The definition of stored functions and stored procedures.
SCHEMATA Information about the schemas (databases). (Schemata is technically the correct word for the plural form of schema, but most use schemas nowadays.)
ST_GEOMETRY_COLUMNS Information about columns with a spatial data type.
STATISTICS The index definitions and statistics. This is one of the most useful views when it comes to query performance turning.
TABLE_CONSTRAINTS Summary of the primary, unique, and foreign keys and CHECK constraints.
TABLES Information about tables and views and their properties.
TABLESPACES This view is only used for nDB Cluster tablespaces.
TRIGGERS The trigger definitions.
VIEW_ROUTINE_USAGE Lists the stored functions used in views. This table was added in 8.0.13.
VIEW_TABLE_USAGE Lists the tables referenced by views. This view was added in 8.0.13.
VIEWS The view definitions.
Table 7-2. (continued)
Several of the views are closely related, for example, the columns are in tables which are in schemas and constraints refer to tables and columns. This means that some of the column names are present in several of the views. The most commonly used column names that relate to these views are
• TABLE_NAME: Used in the views not specific to InnoDB for the table name.
• TABLE_SCHEMA: Used in the views not specific to InnoDB for the schema name.
• COLUMN_NAME: Used in the views not specific to InnoDB for the column name.
118
• SPACE: Used in the InnoDB-specific views for the tablespace id.
• TABLE_ID: Used in the InnoDB-specific views to uniquely identify the table. This is also used internally in InnoDB.
• NAME: The InnoDB-specific views use a column called NAME to give the name of the object irrespective of the object type.
In addition to the use of the names as in this list, there are also examples where these column names are slightly modified like in the view KEY_COLUMN_USAGE where you find the columns REFERENCED_TABLE_SCHEMA, REFERENCED_TABLE_NAME, and REFERENCED_COLUMN_NAME that are used in the description of foreign keys. As an example, if you want to use the KEY_COLUMN_USAGE view to find the tables with foreign keys referencing the sakila.film table, you can use a query like this:
mysql> SELECT TABLE_SCHEMA, TABLE_NAME FROM information_schema.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = 'sakila' AND REFERENCED_TABLE_NAME = 'film';
+---+---+
| TABLE_SCHEMA | TABLE_NAME | +---+---+
| sakila | film_actor |
| sakila | film_category |
| sakila | inventory | +---+---+
3 rows in set (0.0078 sec)
This shows that the film_actor, film_category, and inventory tables all have foreign keys where the film table is the parent table. For example, if you look at the table definition for film_actor:
mysql> SHOW CREATE TABLE sakila.film_actor\G
*************************** 1. row ***************************
Table: film_actor
Create Table: CREATE TABLE `film_actor` ( `actor_id` smallint(5) unsigned NOT NULL, `film_id` smallint(5) unsigned NOT NULL, ChApTer 7 The InForMATIon SCheMA
`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`actor_id`,`film_id`), KEY `idx_fk_film_id` (`film_id`),
CONSTRAINT `fk_film_actor_actor` FOREIGN KEY (`actor_id`) REFERENCES
`actor` (`actor_id`) ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT `fk_film_actor_film` FOREIGN KEY (`film_id`) REFERENCES `film`
(`film_id`) ON DELETE RESTRICT ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.0097 sec)
The fk_film_actor_film constraint references the film_id column in the film table. You can use this as the starting point for finding the full chain of foreign keys either by manually executing the query for each table returned in the query against the KEY_COLUMN_USAGE view or by creating a recursive common table expression (CTE). This is left as an exercise for the reader.