5.4 获取有关数据库和表的信息

如果您忘记了数据库或表的名称,或者给定表的结构是什么(例如,它的列叫什么),该怎么办? 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值