是否有可能以某种方式获得MySQL数据库的结构,或只是一些简单的查询表?
或者有没有别的方法,我怎么做?
是否有可能以某种方式获得MySQL数据库的结构,或只是一些简单的查询表?
或者有没有别的方法,我怎么做?
看一下INFORMATION_SCHEMA。表的表。它包含关于所有表的元数据。
例子:
SELECT * FROM `INFORMATION_SCHEMA`.`TABLES`
WHERE TABLE_NAME LIKE 'table1'
与其他方法相比,这种方法的优点是您可以轻松地将上面的查询作为其他查询中的子查询使用。
要获得整个数据库结构的CREATE TABLE语句集,使用mysqldump:
mysqldump database_name --compact --no-data
对于单表,在mysqldump中将表名加在db名之后。使用SQL和SHOW CREATE TABLE可以得到相同的结果:
SHOW CREATE TABLE table;
如果您喜欢列列表,则使用DESCRIBE:
DESCRIBE table;
第一个答案的变化,我觉得很有用
打开你的命令提示符并输入(你不需要登录到你的mysql服务器)
mysqldump -hlocalhost -u<root> -p<password> <dbname> --compact --no-data > </path_to_mydump/>mysql.dmp
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'test' AND TABLE_NAME ='products';
哪里Table_schema是数据库名称
选择COLUMN_NAME 从INFORMATION_SCHEMA。列 在TABLE_SCHEMA = ' bodb ' 和TABLE_NAME = ' abc ';
适用于获取所有列名
在下面的例子中,
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 | |
+-------+-------------+-------------------+------+-----+---------+----------------+---------------------------------+---------+
您可以选择下面的任何一个命令。所有这些或多或少都是一样的:
创建表table_name
DESC TABLE_NAME;
显示table_name中的所有列;(用于列属性)
解释TABLE_NAME;
描述TABLE_NAME;