更新时间:2025-09-12 GMT+08:00
DESCRIBE
功能描述
DESCRIBE显示指定表中列的信息,同时也适用于视图。
显示的列信息包括:
- Field:列名。
- Type:列数据类型。
- Null:列值是否可以为NULL,可以为YES,否则为NO。
- Key:列是否被索引,取值范围:
- PRI:表示该列被主键索引。
- UNI:表示该列是唯一索引中的第一列。
- MUL:表示该列是非唯一索引中的第一列。
- NULL:表示该列没有被索引,或者不是唯一索引或非唯一索引的第一列。
- Default:列默认值。
- Extra:列的附加信息,取值范围:
- auto_increment:表示该列为自增列。
- on update CURRENT_TIMESTAMP:表示该列具有ON UPDATE约束。
- VIRTUAL GENERATED:表示该列为虚拟生成列。
- STORED GENERATED:表示该列为存储生成列。
注意事项
- DESCRIBE是SHOW COLUMNS语法的快捷键,同时DESCRIBE还可以显示视图的信息。
- 默认情况下,DESCRIBE显示表中所有列的信息。若给出表中列的名称,在这种情况下该语句仅显示指定列的信息;若给出带有%或_的通配符的字符串,在这种情况下该语句仅显示名称与字符串匹配的列的输出。除非字符串包含空格或其他特殊字符,否则无需将字符串括在引号内。
语法格式
{DESCRIBE | DESC}
tbl_name [col_name | wild]
参数说明
wild
一个模式字符串,它可以包含%和_通配符字符。
示例
-- 创建表t01 m_db=# CREATE TABLE t01 ( c1 int AUTO_INCREMENT COMMENT 'this is c1', c2 int NOT NULL CHECK (c2 > 0), c3 text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL COMMENT 'this is c3', c4 int DEFAULT 5 COMMENT 'this is c4', c5 datetime ON UPDATE current_timestamp COMMENT 'this is c5', c6 int GENERATED ALWAYS AS (c2 + c4) STORED COMMENT 'this is c6', c7 int UNIQUE COMMENT 'this is c7', c8 int, c9 text CHARSET utf8mb4 COLLATE utf8mb4_bin COMMENT 'this is c9', c10 int COMMENT 'this is c10', c11 int COMMENT 'this is c11', c12 int COMMENT 'this is c12', CONSTRAINT PRIMARY KEY (c1) USING btree COMMENT 'this is primary key', CONSTRAINT FOREIGN KEY (c8) REFERENCES t01(c1), CONSTRAINT CHECK (c10 > 0), CONSTRAINT UNIQUE (c11) COMMENT 'this is unique constraint', c13 text COMMENT 'this is c13', c14 int COMMENT 'this is c14', INDEX USING btree (c13(10)) COMMENT 'this is index', INDEX (c13(10),c14) USING btree COMMENT 'this is index too' ); -- 显示列信息 m_db=# DESCRIBE t01; Field | Type | Null | Key | Default | Extra -------+----------+------+-----+---------+----------------------------- c1 | integer | NO | PRI | | auto_increment c2 | integer | NO | | | c3 | text | YES | | | c4 | integer | YES | | 5 | c5 | datetime | YES | | | on update CURRENT_TIMESTAMP c6 | integer | YES | | | STORED GENERATED c7 | integer | YES | UNI | | c8 | integer | YES | | | c9 | text | YES | | | c10 | integer | YES | | | c11 | integer | YES | UNI | | c12 | integer | YES | | | c13 | text | YES | MUL | | c14 | integer | YES | | | (14 rows) -- 模糊匹配列信息 m_db=# DESCRIBE t01 c_; Field | Type | Null | Key | Default | Extra -------+----------+------+-----+---------+----------------------------- c1 | integer | NO | PRI | | auto_increment c2 | integer | NO | | | c3 | text | YES | | | c4 | integer | YES | | 5 | c5 | datetime | YES | | | on update CURRENT_TIMESTAMP c6 | integer | YES | | | STORED GENERATED c7 | integer | YES | UNI | | c8 | integer | YES | | | c9 | text | YES | | | (9 rows) m_db=# DESCRIBE t01 c%; Field | Type | Null | Key | Default | Extra -------+----------+------+-----+---------+----------------------------- c1 | integer | NO | PRI | | auto_increment c2 | integer | NO | | | c3 | text | YES | | | c4 | integer | YES | | 5 | c5 | datetime | YES | | | on update CURRENT_TIMESTAMP c6 | integer | YES | | | STORED GENERATED c7 | integer | YES | UNI | | c8 | integer | YES | | | c9 | text | YES | | | c10 | integer | YES | | | c11 | integer | YES | UNI | | c12 | integer | YES | | | c13 | text | YES | MUL | | c14 | integer | YES | | | (14 rows) m_db=# DROP TABLE t01;
父主题: D