3.4 Getting Information About Databases and Tables获取有关数据库和表的信息

What if you forget the name of a database or table, or what the structure of a given table is (for example, what its columns are called)? 如果忘记了数据库或表的名称,或者忘记了给定表的结构(例如,其列的名称),该怎么办?MySQL addresses this problem through several statements that provide information about the databases and tables it supports.MySQL通过几个语句来解决这个问题,这些语句提供了有关它所支持的数据库和表的信息。

You have previously seen SHOW DATABASES, which lists the databases managed by the server. 您以前看到过SHOW DATABASES,它列出了服务器管理的数据库。To find out which database is currently selected, use the DATABASE() function:要找出当前选择的数据库,请使用DATABASE()函数:

mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| menagerie  |
+------------+

If you have not yet selected any database, the result is NULL.如果尚未选择任何数据库,则结果为NULL

To find out what tables the default database contains (for example, when you are not sure about the name of a table), use this statement:要了解默认数据库包含哪些表(例如,当您不确定表的名称时),请使用以下语句:

mysql> SHOW TABLES;
+---------------------+
| Tables_in_menagerie |
+---------------------+
| event               |
| pet                 |
+---------------------+

The name of the column in the output produced by this statement is always Tables_in_db_name, where db_name is the name of the database. 此语句生成的输出中的列的名称始终是Tables_in_db_name,其中db_name是数据库的名称。See Section 13.7.7.39, “SHOW TABLES Statement”, for more information.有关更多信息,请参阅第13.7.7.39节,“SHOW TABLES语句”

If you want to find out about the structure of a table, the DESCRIBE statement is useful; it displays information about each of a table's columns:如果想了解表的结构,DESCRIBE语句很有用;它显示有关表中每个列的信息:

mysql> DESCRIBE pet;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name    | varchar(20) | YES  |     | NULL    |       |
| owner   | varchar(20) | YES  |     | NULL    |       |
| species | varchar(20) | YES  |     | NULL    |       |
| sex     | char(1)     | YES  |     | NULL    |       |
| birth   | date        | YES  |     | NULL    |       |
| death   | date        | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+

Field indicates the column name, Type is the data type for the column, NULL indicates whether the column can contain NULL values, Key indicates whether the column is indexed, and Default specifies the column's default value. Field表示列名,Type表示列的数据类型,NULL表示列是否可以包含NULL值,Key表示列是否已编制索引,Default指定列的默认值。Extra displays special information about columns: If a column was created with the AUTO_INCREMENT option, the value is auto_increment rather than empty.Extra显示有关列的特殊信息:如果列是使用AUTO_INCREMENT选项创建的,则该值为auto_increment而不是空的。

DESC is a short form of DESCRIBE. DESCDESCRIBE的缩写形式。See Section 13.8.1, “DESCRIBE Statement”, for more information.更多信息,请参阅第13.8.1节,“DESCRIBE语句”

You can obtain the CREATE TABLE statement necessary to create an existing table using the SHOW CREATE TABLE statement. 可以使用SHOW CREATE TABLE语句获取创建现有表所需的CREATE TABLE语句。See Section 13.7.7.10, “SHOW CREATE TABLE Statement”.请参阅第13.7.7.10节,“SHOW CREATE TABLE语句”

If you have indexes on a table, SHOW INDEX FROM tbl_name produces information about them. 如果表上有索引,则SHOW INDEX FROM tbl_name会生成有关它们的信息。See Section 13.7.7.22, “SHOW INDEX Statement”, for more about this statement.有关本声明的更多信息,请参阅第13.7.7.22节,“SHOW INDEX语句”