是否有可能以某种方式获得MySQL数据库的结构,或只是一些简单的查询表?

或者有没有别的方法,我怎么做?


当前回答

这就是SHOW CREATE TABLE查询。您还可以查询SCHEMA TABLES。

SHOW CREATE TABLE YourTableName;

其他回答

第一个答案的变化,我觉得很有用

打开你的命令提示符并输入(你不需要登录到你的mysql服务器)

mysqldump -hlocalhost -u<root> -p<password>  <dbname>  --compact --no-data > </path_to_mydump/>mysql.dmp

在下面的例子中,

Playground是数据库名,equipment是表名

另一种方法是使用SHOW-COLUMNS:5.5(也适用于5.5>)

$ mysql -uroot -p<password> -h<host> -P<port> -e \
    "SHOW COLUMNS FROM playground.equipment"

输出:

mysql: [Warning] Using a password on the command line interface can be insecure.
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| type  | varchar(50) | YES  |     | NULL    |                |
| quant | int(11)     | YES  |     | NULL    |                |
| color | varchar(25) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+

你也可以像下面这样使用mysqlshow-client(也适用于5.5>):

$ mysqlshow -uroot -p<password> -h<host> -P<port> \
    playground equipment

输出:

mysqlshow: [Warning] Using a password on the command line interface can be insecure.
Database: playground  Table: equipment
+-------+-------------+-------------------+------+-----+---------+----------------+---------------------------------+---------+
| Field | Type        | Collation         | Null | Key | Default | Extra          | Privileges                      | Comment |
+-------+-------------+-------------------+------+-----+---------+----------------+---------------------------------+---------+
| id    | int(11)     |                   | NO   | PRI |         | auto_increment | select,insert,update,references |         |
| type  | varchar(50) | latin1_swedish_ci | YES  |     |         |                | select,insert,update,references |         |
| quant | int(11)     |                   | YES  |     |         |                | select,insert,update,references |         |
| color | varchar(25) | latin1_swedish_ci | YES  |     |         |                | select,insert,update,references |         |
+-------+-------------+-------------------+------+-----+---------+----------------+---------------------------------+---------+

看一下INFORMATION_SCHEMA。表的表。它包含关于所有表的元数据。

例子:

SELECT * FROM `INFORMATION_SCHEMA`.`TABLES`
WHERE TABLE_NAME LIKE 'table1'

与其他方法相比,这种方法的优点是您可以轻松地将上面的查询作为其他查询中的子查询使用。

这就是SHOW CREATE TABLE查询。您还可以查询SCHEMA TABLES。

SHOW CREATE TABLE YourTableName;

您可以选择下面的任何一个命令。所有这些或多或少都是一样的:

创建表table_name

DESC TABLE_NAME;

显示table_name中的所有列;(用于列属性)

解释TABLE_NAME;

描述TABLE_NAME;