Mysql 简明教程
MySQL - DESCRIBE Tables
描述 MySQL 表是指检索其定义或结构。当我们描述一个表时,它基本上包括现有的字段、它们的数据类型和对它们定义的任何约束。
Describing a MySQL table refers to retrieving its definition or structure. When we describe a table, it basically includes the fields present, their datatypes, and if any constraints defined on them.
我们可以使用以下 SQL 语句获取有关表结构的信息 −
We can get the information about the table structure using the following SQL statements −
-
DESCRIBE Statement
-
DESC Statement
-
SHOW COLUMNS Statement
-
EXPLAIN Statement
所有这些语句都用于相同目的。让我们在这个教程中逐一详细了解它们。
All these statements are used for the same purpose. Let us learn about them in detail, one by one, in this tutorial.
DESCRIBE Statement
MySQL DESCRIBE 语句用于检索与表相关的的信息,其中包括字段名称、字段数据类型和约束(如果存在)。该语句是 SHOW columns 语句的简写(它们都从表中检索相同的信息)。
The MySQL DESCRIBE statement is used to retrieve a table-related information, which consists of field names, field data types, and constraints (if any). This statement is a shortcut for the SHOW columns statement (they both retrieve the same information from a table).
除了检索表的定义外,此语句还可用于获取表中特定字段的信息。
Apart from retrieving a table’s definition, this statement can be used to get the information of a particular field in a table.
Syntax
以下是 MySQL DESCRIBE 语句的语法 −
Following is the syntax of MySQL DESCRIBE statement −
DESCRIBE table_name [col_name | wild];
Example
在以下示例中,我们使用 CREATE TABLE 语句创建一个名为 CUSTOMERS 的表 −
In the following example, we are creating a table named CUSTOMERS using the CREATE TABLE statement −
CREATE TABLE CUSTOMERS (
ID INT AUTO_INCREMENT,
NAME VARCHAR(20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
现在,执行以下查询以获取有关 CUSTOMERS 表的列的信息 −
Now, execute the following query to get the information about columns of the CUSTOMERS table −
DESCRIBE CUSTOMERS;
Describing a specific column
默认情况下,DESCRIBE 语句提供指定表中所有列的信息。但您还可通过指定该列的名称来检索表中特定列的信息。
By default, the DESCRIBE statement provides information about all the columns in the specified table. But you can also retrieve information about a particular column of a table by specifying the name of that column.
例如,以下查询显示了我们在前一个示例中创建的 CUSTOMERS 表的 NAME 列的信息。
For example, the following query displays information about NAME column of CUSTOMERS table, which we created in the previous example.
DESCRIBE CUSTOMERS NAME;
DESC Statement
我们还可使用 MySQL DESC 语句来获取表信息,而不是使用 DESCRIBE。它们给出相同的结果,因此 DESC 只是 DESCRIBE 语句的快捷方式。
We can also retrieve the table information using the MySQL DESC statement instead of DESCRIBE. They both give the same results, so DESC is just a shortcut for DESCRIBE statement.
Syntax
以下为 MySQL DESC 语句的语法 −
Following is the syntax of the MySQL DESC statement −
DESC table_name [col_name | wild];
Example
在此示例中,我们正在尝试使用 DESC 语句获取 CUSTOMERS 表的信息。
In this example, we are trying to get the information of CUSTOMERS table using DESC statement.
DESC CUSTOMERS;
以下是 CUSTOMERS 表的列信息 −
Following is the columns information of CUSTOMERS table −
Describing a specific column
我们还可以获取给定表中特定列的信息,类似于使用 DESCRIBE。使用 DESC,而不是 DESCRIBE。
We can also get the information of a specific column in a given table, similar to using DESCRIBE. Instead of DESCRIBE, we use DESC.
例如,以下查询显示 CUSTOMERS 表的 NAME 列的信息。
For example, the following query displays information about NAME column of CUSTOMERS table.
DESC CUSTOMERS NAME;
SHOW COLUMNS Statement
MySQL SHOW COLUMNS 语句用于显示表中所有列的信息。DESCRIBE 语句是此语句的快捷方式。
The MySQL SHOW COLUMNS Statement is used to display the information of all the columns present in a table. The DESCRIBE statement is a shortcut for this statement.
Note: 此语句不会显示特定字段的信息。
Note: This statement will not display information of a specific field.
EXPLAIN Statement
MySQL EXPLAIN 语句是 DESCRIBE 语句的同义词,它检索表结构的信息,如列名、列数据类型和约束(如果有)。
The MySQL EXPLAIN Statement is a synonym of DESCRIBE Statement which retrieves the information of a table’s structure such as column names, column data types, and constraints (if any).
Describe Tables in Different Formats
可以使用 explain_type 选项以各种格式检索信息。此选项的值可以是 TRADITIONAL、JSON 和 TREE。
You can retrieve the information in various formats using the explain_type option. The value to this option can be TRADITIONAL, JSON and, TREE.
Syntax
以下是描述不同格式表的语法 −
Following is the syntax to describe tables in different formats −
{EXPLAIN | DESCRIBE | DESC}
explain_type: { FORMAT = format_name } select_statement
Example
在以下示例中,我们将 CUSTOMERS 表格式描述为 TRADITIONAL 。
In the following example, we are describing the CUSTOMERS table format as TRADITIONAL.
EXPLAIN FORMAT = TRADITIONAL SELECT * FROM CUSTOMERS;
Example
在此,我们将 CUSTOMERS 表格式描述为 JSON 。
Here, we are describing the CUSTOMERS table format as JSON.
EXPLAIN FORMAT = JSON SELECT * FROM CUSTOMERS;
Describing Table Using a Client Program
除了使用 MySQL 查询从 MySQL 数据库描述表外,我们还可使用客户端程序对表执行 DESCRIBE TABLE 操作。
In addition to describe a table from MySQL Database using the MySQL query, we can also perform the DESCRIBE TABLE operation on a table using a client program.