MySQL - DESCRIBE 语句
描述 MySQL 表是指检索其定义或结构。当我们描述一个表时,它基本上包括存在的字段、它们的数据类型以及对它们定义的任何约束。
我们可以使用以下 SQL 语句获取有关表结构的信息 -
DESCRIBE 语句
DESC 语句
SHOW COLUMNS 语句
EXPLAIN 语句
所有这些语句都用于相同的目的。在本教程中,我们将逐一详细了解它们。
DESCRIBE 语句
MySQL DESCRIBE 语句用于检索与表相关的信息,包括字段名称、字段数据类型和约束(如果有)。此语句是 SHOW columns 语句的快捷方式(它们都从表中检索相同的信息)。
除了检索表的定义之外,此语句还可用于获取表中特定字段的信息。
语法
以下是 MySQL DESCRIBE 语句的语法 -
DESCRIBE table_name [col_name | wild];
示例
在下面的示例中,我们使用 CREATE TABLE 语句创建一个名为 CUSTOMERS 的表 -
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 表的列信息 -
DESCRIBE CUSTOMERS;
输出
以下是 CUSTOMERS 表的列信息 -
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
ID | int | NO | PRI | NULL | auto_increment |
NAME | varchar(20) | NO | NULL | ||
AGE | int | NO | NULL | ||
ADDRESS | char(25) | YES | NULL | ||
SALARY | decimal(18, 2) | YES | NULL |
描述特定列
默认情况下,DESCRIBE 语句会提供指定表中所有列的信息。但您也可以通过指定列的名称来检索表中特定列的信息。
例如,以下查询显示我们在上一个示例中创建的 CUSTOMERS 表的 NAME 列的信息。
DESCRIBE CUSTOMERS NAME;
输出
以下是 CUSTOMERS 表中 NAME 列的描述 -
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
NAME | varchar(20) | NO | NULL |
DESC 语句
我们也可以使用 MySQL DESC 语句代替 DESCRIBE 语句来检索表信息。它们的结果相同,因此 DESC 只是 DESCRIBE 语句的快捷方式。
语法
以下是 MySQL DESC 语句的语法 -
DESC table_name [col_name | wild];
示例
在此示例中,我们尝试使用 DESC 语句获取 CUSTOMERS 表的信息。
DESC CUSTOMERS;
以下是 CUSTOMERS 表的列信息 -
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
ID | int | NO | PRI | NULL | auto_increment |
NAME | varchar(20) | NO | NULL | ||
AGE | int | NO | NULL | ||
ADDRESS | char(25) | YES | NULL | ||
SALARY | decimal(18,2) | YES | NULL |
描述特定列
我们也可以获取给定表中特定列的信息,类似于使用 DESCRIBE。我们使用 DESCRIBE 而不是 DESCRIBE。
例如,以下查询显示有关 CUSTOMERS 表的 NAME 列的信息。
DESC CUSTOMERS NAME;
输出
以下是 CUSTOMERS 表中 NAME 列的描述 -
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
NAME | varchar(20) | NO | NULL |
SHOW COLUMNS 语句
MySQL SHOW COLUMNS 语句用于显示表中所有列的信息。DESCRIBE 语句是此语句的快捷方式。
注意:此语句不会显示特定字段的信息。
语法
以下是 SHOW COLUMNS 语句的语法 -
SHOW COLUMNS FROM table_name;
示例
此处,我们使用 SHOW COLUMNS 语句检索同一个 CUSTOMERS 表的列信息。
SHOW COLUMNS FROM CUSTOMERS;
以下是 CUSTOMERS 表的列信息 -
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
ID | int | NO | PRI | NULL | auto_increment |
NAME | varchar(20) | NO | NULL | ||
AGE | int | NO | NULL | ||
ADDRESS | char(25) | YES | NULL | ||
SALARY | decimal(18,2) | YES | NULL |
EXPLAIN 语句
MySQL EXPLAIN 语句与 DESCRIBE 语句同义,后者用于检索表结构信息,例如列名、列数据类型和约束(如果有)。
语法
以下是 SHOW COLUMNS 语句的语法 -
EXPLAIN table_name;
示例
在下面的查询中,我们使用 EXPLAIN 语句检索 CUSTOMERS 表的列信息。
EXPLAIN CUSTOMERS;
以下是 CUSTOMERS 表的列信息 -
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
ID | int | NO | PRI | NULL | auto_increment |
NAME | varchar(20) | NO | NULL | ||
AGE | int | NO | NULL | ||
ADDRESS | char(25) | YES | NULL | ||
SALARY | decimal(18,2) | YES | NULL |
以不同格式描述表
您可以使用 explain_type 选项检索各种格式的信息。此选项的值可以是 TRADITIONAL、JSON 和 TREE。
语法
以下是以不同格式描述表的语法 -
{EXPLAIN | DESCRIBE | DESC} explain_type: { FORMAT = format_name } select_statement
示例
在下面的示例中,我们将 CUSTOMERS 表格式描述为 TRADITIONAL。
EXPLAIN FORMAT = TRADITIONAL SELECT * FROM CUSTOMERS;
输出
执行上述查询将产生以下输出 -
id | select_type | table | partitions | possible_keys |
---|---|---|---|---|
1 | SIMPLE | CUSTOMERS | NULL | NULL |
示例
此处,我们将 CUSTOMERS 表的格式描述为 JSON。
EXPLAIN FORMAT = JSON SELECT * FROM CUSTOMERS;
输出
执行上述查询将产生以下输出 -
解释 |
---|
{ "query_block": { "select_id": 1, "cost_info": { "query_cost": "0.95" }, "table": { "table_name": "CUSTOMERS", "access_type": "ALL", "rows_examined_per_scan": 7, "rows_produced_per_join": 7, "filtered": "100.00", "cost_info": { "read_cost": "0.25", "eval_cost": "0.70", "prefix_cost": "0.95", "data_read_per_join": "1K" }, "used_columns": [ "ID", "NAME", "AGE", "ADDRESS", "SALARY" ] } } } |
示例
在以下示例中,我们将 CUSTOMERS 表格式描述为 TREE。
EXPLAIN FORMAT = TREE SELECT * FROM CUSTOMERS;
输出
执行上述查询将产生以下输出 -
EXPLAIN |
---|
对 CUSTOMERS 进行表扫描 (cost=0.95 rows=7) |
使用客户端程序描述表
除了使用 MySQL 查询从 MySQL 数据库中描述表之外,我们还可以使用客户端程序对表执行 DESCRIBE TABLE 操作。
语法
以下是使用各种编程语言从 MySQL 数据库中描述表的语法 -
要通过 PHP 程序描述 MySQL 数据库中的表,我们需要使用 mysqli 函数 query() 执行 Describe Table 语句,如下所示:-
$sql="Describe Table_name"; $mysqli->query($sql);
要通过 Node.js 程序描述 MySQL 数据库中的表,我们需要使用 mysql2 库中的 query() 函数执行 Describe Table 语句,如下所示:-
sql="Describe Table_name"; con.query(sql);
要通过 Java 程序描述 MySQL 数据库中的表,我们需要使用 JDBC 函数 executeUpdate() 执行 Describe Table 语句,如下所示:-
String sql="Describe Table_name"; statement.executeUpdate(sql);
要通过 Python 程序描述 MySQL 数据库中的表,我们需要使用 MySQL Connector/Python 的 execute() 函数执行 Describe Table 语句,如下所示:-
sql="Describe Table_name"; cursorObj.execute(sql);
示例
以下是程序 -
$dbhost = 'localhost'; $dbuser = 'root'; $dbpass = 'password'; $dbname = 'TUTORIALS'; $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname); if ($mysqli->connect_errno) { printf("Connect failed: %s
", $mysqli->connect_error); exit(); } // printf('Connected successfully.
'); $sql = " DESCRIBE sales "; if ($q = $mysqli->query($sql)) { printf(" Table described successfully.
"); while ($row = mysqli_fetch_array($q)) { echo "{$row['Field']} - {$row['Type']} "; } } if ($mysqli->errno) { printf("table could not be described .
", $mysqli->error); } $mysqli->close();
输出
获得的输出如下 -
Table described successfully. ID - int ProductName - varchar(255) CustomerName - varchar(255) DispatchDate - date DeliveryTime - time Price - int Location - varchar(255)
var mysql = require('mysql2'); var con = mysql.createConnection({ host: "localhost", user: "root", password: "Nr5a0204@123" }); //连接到 MySQL con.connect(function (err) { if (err) throw err; console.log("Connected!"); console.log("--------------------------"); sql = "USE TUTORIALS" con.query(sql); sql = "CREATE TABLE SALES(ID INT, ProductName VARCHAR(255), CustomerName VARCHAR(255), DispatchDate date, DeliveryTime time, Price INT, Location VARCHAR(255));" con.query(sql); sql = "DESCRIBE SALES;" con.query(sql, function(err, result){ if (err) throw err console.log(result); }); });
输出
生成的输出如下 -
Connected! -------------------------- [ { Field: 'ID', Type: 'int', Null: 'YES', Key: '', Default: null, Extra: '' }, { Field: 'ProductName', Type: 'varchar(255)', Null: 'YES', Key: '', Default: null, Extra: '' }, { Field: 'CustomerName', Type: 'varchar(255)', Null: 'YES', Key: '', Default: null, Extra: '' }, { Field: 'DispatchDate', Type: 'date', Null: 'YES', Key: '', Default: null, Extra: '' }, { Field: 'DeliveryTime', Type: 'time', Null: 'YES', Key: '', Default: null, Extra: '' }, { Field: 'Price', Type: 'int', Null: 'YES', Key: '', Default: null, Extra: '' }, { Field: 'Location', Type: 'varchar(255)', Null: 'YES', Key: '', Default: null, Extra: '' } ]
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class DescribeTable { public static void main(String[] args){ String url = "jdbc:mysql://localhost:3306/TUTORIALS"; String username = "root"; String password = "password"; try{ Class.forName("com.mysql.cj.jdbc.Driver"); Connection connection = DriverManager.getConnection(url, username, password); Statement statement = connection.createStatement(); System.out.println("Connected successfully...!"); //Describe table details...! ResultSet resultSet = statement.executeQuery("DESCRIBE customers"); System.out.println("Following is the table description"); while(resultSet.next()) { System.out.print(resultSet.getNString(1)); System.out.println(); } connection.close(); } catch(Exception e){ System.out.println(e); } } }
输出
获得的输出如下所示 -
Connected successfully...! Following is the table description ID NAME AGE ADDRESS SALARY
import mysql.connector #建立连接 connection = mysql.connector.connect( host='localhost', user='root', password='password', database='tut' ) table_name = 'tutorials_tbl' #创建游标对象 cursorObj = connection.cursor() describe_table_query = f"DESCRIBE {table_name}" cursorObj.execute(describe_table_query) columns_info = cursorObj.fetchall() print(f"Description of table '{table_name}':") for column in columns_info: print(f"Column Name: {column[0]}, Type: {column[1]}, Null: {column[2]}, Key: {column[3]}, Default: {column[4]}, Extra: {column[5]}") cursorObj.close() connection.close()
输出
以下是上述代码的输出 -
Description of table 'tutorials_tbl': Column Name: tutorial_id, Type: b'int', Null: NO, Key: PRI, Default: None, Extra: auto_increment Column Name: tutorial_title, Type: b'varchar(100)', Null: NO, Key: , Default: None, Extra: Column Name: tutorial_author, Type: b'varchar(40)', Null: NO, Key: , Default: None, Extra: Column Name: submission_date, Type: b'date', Null: YES, Key: , Default: None, Extra: Column Name: tutorial_name, Type: b'varchar(20)', Null: YES, Key: , Default: None, Extra: