如何在 MySQL 数据库中显示表的架构?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1498777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:12:00  来源:igfitidea点击:

How do I show the schema of a table in a MySQL database?

mysqldatabaseschemadatabase-schema

提问by dlamblin

From the MySQLconsole, what command displays the schema of any given table?

MySQL控制台,什么命令显示任何给定表的架构?

回答by Omry Yadan

describe [db_name.]table_name;

for formatted output, or

用于格式化输出,或

show create table [db_name.]table_name;

for the SQL statement that can be used to create a table.

对于可用于创建表的 SQL 语句。

回答by Bobby

SHOW CREATE TABLE yourTable;

or

或者

SHOW COLUMNS FROM yourTable;

回答by Somnath Muluk

You can also use shorthand for describe as descfor table description.

您还可以使用速记来描述desc表描述。

desc [db_name.]table_name;

desc [db_name.]table_name;

or

或者

use db_name;
desc table_name;

使用 db_name;
desc 表名;

You can also use explainfor table description.

您还可以explain用于表描述。

explain [db_name.]table_name;

解释 [db_name.]table_name;

See official doc

查看官方文档

Will give output like:

将给出如下输出:

+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(10)     | NO   | PRI | NULL    |       |
| name     | varchar(20) | YES  |     | NULL    |       |
| age      | int(10)     | YES  |     | NULL    |       |
| sex      | varchar(10) | YES  |     | NULL    |       |
| sal      | int(10)     | YES  |     | NULL    |       |
| location | varchar(20) | YES  |     | Pune    |       |
+----------+-------------+------+-----+---------+-------+

回答by Paul Campbell

Perhaps the question needs to be slightly more precise here about what is required because it canbe read it two different ways. i.e.

也许这里的问题需要更精确一些,因为它可以通过两种不同的方式阅读。IE

  1. How do I get the structure/definition for a table in mysql?
  2. How do I get the name of the schema/database this table resides in?
  1. 如何在 mysql 中获取表的结构/定义?
  2. 如何获取此表所在的架构/数据库的名称?

Given the accepted answer, the OP clearly intended it to be interpreted the first way. For anybody reading the question the other way try

鉴于接受的答案,OP 显然打算以第一种方式解释它。对于任何以另一种方式阅读问题的人,请尝试

SELECT `table_schema` 
FROM `information_schema`.`tables` 
WHERE `table_name` = 'whatever';

回答by Lam

SELECT COLUMN_NAME, TABLE_NAME,table_schema
FROM INFORMATION_SCHEMA.COLUMNS;