如果您忘记了数据库或表的名称,或者给定表的结构是什么(例如,它的列叫什么),该怎么办? MySQL 通过几个语句来解决此问题,这些语句提供有关它支持的数据库和表的信息。
您之前已经看到过SHOW DATABASES,其中列出了服务器管理的数据库。要找出当前选择了哪个数据库,请使用以下 DATABASE()函数:
mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| menagerie |
+------------+
如果您尚未选择任何数据库,结果是 NULL。
要找出默认数据库包含哪些表(例如,当您不确定表的名称时),请使用以下语句:
mysql> SHOW TABLES;
+---------------------+
| Tables_in_menagerie |
+---------------------+
| event |
| pet |
+---------------------+
此语句生成的输出中的列名称始终为 ,其中是数据库的名称。有关更多信息, 请参见第 15.7.7.38 节“SHOW TABLES 语句” 。Tables_in_db_namedb_name
如果您想了解表的结构,该 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. Extra displays special information about columns: If a column was created with the AUTO_INCREMENT option, the value is auto_increment rather than empty.
DESC is a short form of DESCRIBE. See Section 15.8.1, “DESCRIBE Statement”, for more information.
You can obtain the CREATE TABLE statement necessary to create an existing table using the SHOW CREATE TABLE statement. See Section 15.7.7.11, “SHOW CREATE TABLE Statement”.
如果表上有索引,则生成有关它们的信息。有关此语句的更多信息, 请参见第 15.7.7.23 节“SHOW INDEX 语句” 。SHOW INDEX FROM tbl_name