MySQL Study之--MySQL schema_information數據庫
information_schema數據庫是在mysql的版本5.0之後產生的,一個虛擬數據庫,物理上並不存在。
information_schema數據庫類似與“數據字典”,提供了訪問數據庫元數據的方式,即數據的數據。比如數據庫名或表名,列類型,訪問權限(更加細化的訪問方式)。
案例:
mysql> show databases;
+--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | prod | | test | +--------------------+ 5 rows in set (0.00 sec)訪問information_schema:
+---------------------------------------+ | Tables_in_information_schema | +---------------------------------------+ | CHARACTER_SETS | | COLLATIONS | | COLLATION_CHARACTER_SET_APPLICABILITY | | COLUMNS | | COLUMN_PRIVILEGES | | ENGINES | | EVENTS | | FILES | | GLOBAL_STATUS | | GLOBAL_VARIABLES | | KEY_COLUMN_USAGE | | OPTIMIZER_TRACE | | PARAMETERS | | PARTITIONS | | PLUGINS | | PROCESSLIST | | PROFILING | | REFERENTIAL_CONSTRAINTS | | ROUTINES | | SCHEMATA | | SCHEMA_PRIVILEGES | | SESSION_STATUS | | SESSION_VARIABLES | | STATISTICS | | TABLES | | TABLESPACES | | TABLE_CONSTRAINTS | | TABLE_PRIVILEGES | | TRIGGERS | | USER_PRIVILEGES | | VIEWS | | INNODB_LOCKS | | INNODB_TRX | | INNODB_SYS_DATAFILES | | INNODB_LOCK_WAITS | | INNODB_SYS_TABLESTATS | | INNODB_CMP | | INNODB_METRICS | | INNODB_CMP_RESET | | INNODB_CMP_PER_INDEX | | INNODB_CMPMEM_RESET | | INNODB_FT_DELETED | | INNODB_BUFFER_PAGE_LRU | | INNODB_SYS_FOREIGN | | INNODB_SYS_COLUMNS | | INNODB_SYS_INDEXES | | INNODB_FT_DEFAULT_STOPWORD | | INNODB_SYS_FIELDS | | INNODB_CMP_PER_INDEX_RESET | | INNODB_BUFFER_PAGE | | INNODB_CMPMEM | | INNODB_FT_INDEX_TABLE | | INNODB_FT_BEING_DELETED | | INNODB_SYS_TABLESPACES | | INNODB_FT_INDEX_CACHE | | INNODB_SYS_FOREIGN_COLS | | INNODB_SYS_TABLES | | INNODB_BUFFER_POOL_STATS | | INNODB_FT_CONFIG | +---------------------------------------+ 59 rows in set (0.00 sec)mysql> desc SCHEMATA;
+----------------------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------------------------+--------------+------+-----+---------+-------+ | CATALOG_NAME | varchar(512) | NO | | | | | SCHEMA_NAME | varchar(64) | NO | | | | | DEFAULT_CHARACTER_SET_NAME | varchar(32) | NO | | | | | DEFAULT_COLLATION_NAME | varchar(32) | NO | | | | | SQL_PATH | varchar(512) | YES | | NULL | | +----------------------------+--------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)mysql> select SCHEMA_NAME,DEFAULT_CHARACTER_SET_NAME from SCHEMATA;
+--------------------+----------------------------+ | SCHEMA_NAME | DEFAULT_CHARACTER_SET_NAME | +--------------------+----------------------------+ | information_schema | utf8 | | mysql | latin1 | | performance_schema | utf8 | | prod | latin1 | | test | latin1 | +--------------------+----------------------------+ 5 rows in set (0.00 sec)mysql> desc 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> select table_schema,table_name,table_type,engine,table_rows from tables where table_schema='prod';
+--------------+------------+------------+--------+------------+ | table_schema | table_name | table_type | engine | table_rows | +--------------+------------+------------+--------+------------+ | prod | t1 | BASE TABLE | InnoDB | 2 | | prod | t2 | BASE TABLE | InnoDB | 2 | +--------------+------------+------------+--------+------------+ 2 rows in set (0.00 sec)mysql> select * from prod.t1;
+------+-------+ | id | name | +------+-------+ | 10 | tom | | 20 | jerry | | 30 | rose | | 40 | ellen | +------+-------+ 4 rows in set (0.00 sec)mysql> explain select * from prod.t1;
+----+-------------+-------+------+---------------+------+---------+------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+------+-------+ | 1 | SIMPLE | t1 | ALL | NULL | NULL | NULL | NULL | 2 | NULL | +----+-------------+-------+------+---------------+------+---------+------+------+-------+ 1 row in set (0.00 sec)
其中重要的表有:
SCHEMATA表:
SCHEMATA表提供了當前mysql實例中所有數據庫的信息。是show databases的結果取之此表。
TABLES表:
TABLES表提供了關於數據庫中的表的信息(包括視圖)。詳細表述了某個表屬於哪個schema,表類型,表引擎,創建時間,等等信息。是show tables from schemaname的結果取之此表。
COLUMNS表:
COLUMNS表提供了表中的列信息。詳細表述了某張表的所有列以及每個列的信息。是show columns from schemaname.tablename的結果取之此表。
STATISTICS表:
STATISTICS表提供了關於表索引的信息。是show index from schemaname.tablename的結果取之此表。
USER_PRIVILEGES表:
USER_PRIVILEGES(用戶權限)表給出了關於全程權限的信息。該信息源自mysql.user授權表。是非標准表。
SCHEMA_PRIVILEGES表:
SCHEMA_PRIVILEGES(方案權限)表給出了關於方案(數據庫)權限的信息。該信息來自mysql.db授權表。是非標准表。
TABLE_PRIVILEGES表:
TABLE_PRIVILEGES(表權限)表給出了關於表權限的信息。該信息源自mysql.tables_priv授權表。是非標准表。
COLUMN_PRIVILEGES表;
COLUMN_PRIVILEGES(列權限)表給出了關於列權限的信息。該信息源自mysql.columns_priv授權表。是非標准表。
CHARACTER_SETS表:
CHARACTER_SETS(字符集)表提供了mysql實例可用字符集的信息。是SHOW CHARACTER SET結果集取之此表。
COLLATIONS表;
COLLATIONS表提供了關於各字符集的對照信息。
COLLATION_CHARACTER_SET_APPLICABILITY表:
COLLATION_CHARACTER_SET_APPLICABILITY表指明了可用於校對的字符集。這些列等效於SHOW COLLATION的前兩個顯示字段。
TABLE_CONSTRAINTS表;
TABLE_CONSTRAINTS表描述了存在約束的表。以及表的約束類型。
KEY_COLUMN_USAGE表:
KEY_COLUMN_USAGE表描述了具有約束的鍵列。
ROUTINES表:
ROUTINES表提供了關於存儲子程序(存儲程序和函數)的信息。此時,ROUTINES表不包含自定義函數(UDF)。名為“mysql.proc name”的列指明了對應於INFORMATION_SCHEMA.ROUTINES表的mysql.proc表列。
VIEWS表:
VIEWS表給出了關於數據庫中的視圖的信息。需要有show views權限,否則無法查看視圖信息。
TRIGGERS表;
TRIGGERS表提供了關於觸發程序的信息。必須有super權限才能查看該表。
另外,在未來的版本中會有新的表添加。
【注】information_schema是一個由數據庫的元數據組成的數據庫。裡面存儲的是mysql的數據庫基本信息。並隨時改變。用於查看信息以及系統決策時作為重要的信息提供者。