8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

MySQL 8.4 参考手册 / 5 教程 / 5.4 获取有关数据库和表的信息
中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

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

如果你忘记了数据库或表的名称,或者忘记了某个表的结构(例如,它的列叫什么),该怎么办?MySQL 通过几条提供其支持的数据库和表信息的语句来解决这个问题。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.

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

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

如果你还没有选择任何数据库,结果将是 NULL。If you have not yet selected any database, the result is 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                 |
+---------------------+

此语句生成的输出中列的名称始终为 Tables_in_db_name,其中 db_name 是数据库的名称。有关更多信息,请参见 第 15.7.7.39 节,“SHOW TABLES 语句”。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. See Section 15.7.7.39, “SHOW TABLES Statement”, for more information.

如果您想了解表的结构,DESCRIBE 语句非常有用;它显示表中每个列的信息: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:

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 表示列名,Type 是列的数据类型,NULL 表示该列是否可以包含 NULL 值,Key 表示该列是否被索引,Default 指定列的默认值。Extra 显示列的特殊信息:如果某列是通过 AUTO_INCREMENT 选项创建的,其值为 auto_increment,而非空。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 是 DESCRIBE 的简写形式。详见 第 15.8.1 节,“DESCRIBE 语句”,了解更多信息。DESC is a short form of DESCRIBE. See Section 15.8.1, “DESCRIBE Statement”, for more information.

您可以使用 SHOW CREATE TABLE 语句获取创建现有表所需的 CREATE TABLE 语句。请参见 第 15.7.7.11 节,“SHOW CREATE TABLE 语句”。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”.

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