如何在SQL中描述表?在8.0版本中?

时间:2019-04-04 11:46:41

标签: mysql sql

CREATE TABLE dreams (
 dream_id INT PRIMARY KEY,
 name VARCHAR (20),
 type VARCHAR (10));

DESCRIBE的梦想;

(显示错误)

2 个答案:

答案 0 :(得分:1)

mysql> desc constitution;
+-------------------+--------------+------+-----+---------+----------------+
| Field             | Type         | Null | Key | Default | Extra          |
+-------------------+--------------+------+-----+---------+----------------+
| id                | int(2)       | NO   | PRI | NULL    | auto_increment |
| constitution_name | varchar(300) | NO   |     | NULL    |                |
+-------------------+--------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)

请参见上面的示例。

答案 1 :(得分:0)

  

如何在SQL中描述表

更多的SQL标准确认使用information_schema数据库和此视图的SQL查询。

与Pawan Tiwari答案提到的非标准desc MySQL子句的功能相同。

查询

SELECT 
    information_schema.COLUMNS.COLUMN_NAME AS 'Field'
    , information_schema.COLUMNS.COLUMN_TYPE AS 'Type'
    , information_schema.COLUMNS.IS_NULLABLE AS 'Null'
    , information_schema.COLUMNS.COLUMN_KEY AS 'Key'
    , information_schema.COLUMNS.COLUMN_DEFAULT AS 'Default'
    , information_schema.COLUMNS.EXTRA AS 'Extra'
FROM 
    information_schema.TABLES
INNER JOIN
    information_schema.COLUMNS ON information_schema.TABLES.TABLE_NAME =  information_schema.COLUMNS.TABLE_NAME
WHERE
    information_schema.TABLES.TABLE_NAME = 'dreams'

结果

| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| dream_id | int(11)     | NO   | PRI |         |       |
| name     | varchar(20) | YES  |     |         |       |
| type     | varchar(10) | YES  |     |         |       |

请参见demo