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_
, where db_name
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
. DESC
是DESCRIBE
的缩写形式。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
produces information about them. tbl_name
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语句”。