Heim  >  Artikel  >  Datenbank  >  MySQL中文参考手册--获得数据库和表的信息_MySQL

MySQL中文参考手册--获得数据库和表的信息_MySQL

WBOY
WBOYOriginal
2016-06-01 13:54:11898Durchsuche

如果你忘记一个数据库或表的名字,或一个给定的表的结构是什么(例如,它的列叫什么),怎么办? MySQL通过提供数据库及其支持的表的信息的几个语句解决这个问题。
  
  你已经见到了SHOW DATABASES,它列出由服务器管理的数据库。为了找出当前选择了哪个数据库,使用DATABASE()函数:
  
  mysql> SELECT DATABASE();
  +------------+
  | DATABASE() |
  +------------+
  | menagerie |
  +------------+
  
  如果你还没选择任何数据库,结果是空的。
  
  为了找出当前的数据库包含什么表(例如,当你不能确定一个表的名字),使用这个命令:
  
  mysql> SHOW TABLES;
  +---------------------+
  | Tables in menagerie |
  +---------------------+
  | event        |
  | pet         |
  +---------------------+
  
  如果你想要知道一个表的结构,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显示列名字,Type是为列的数据类型,Null表示列是否能包含NULL值,Key显示列是否被索引而Default指定列的缺省值。
  
  如果你在一个表上有索引,SHOW INDEX FROM tbl_name生成有关它们的信息

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn