簡體   English   中英

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

[英]How to DESCRIBE a TABLE in SQL ? in 8.0 version?

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

描述夢想;

(顯示錯誤)

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)

請參見上面的示例。

如何在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  |     |         |       |

觀看演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM