首页  >  文章  >  数据库  >  mysql如何查询表字所有字段?

mysql如何查询表字所有字段?

青灯夜游
青灯夜游原创
2020-09-30 15:52:2415413浏览

mysql查询表字所有字段的方法:使用“SHOW FROM”语句配合FULL关键字来查询,语法“SHOW FULL COLUMNS FROM table_name”,可以显示指定数据表的所有字段信息。

mysql如何查询表字所有字段?

mysql查询表字所有字段

1、查看所有表名:

show tables [from db_name];

2、查看字段信息

SHOW FULL COLUMNS FROM table_name

获取以下信息
Field :字段名
Type:字段类型
Collation:字符集(mysql 5.0以上有)
Null :是否可以为NULL
Key:索引(PRI,unique,index)
Default:缺省值
Extra:额外(是否 auto_increment)
Privileges:权限
Comment:备注(mysql 5.0以上有)

mysql> create table teacher  # 创建teacher表
    -> (
    -> Id int (5) auto_increment not null primary key,
    -> name char(10) not null,
    -> address varchar(50) default 'No.1 Mid school',
    -> year date
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| teacher          |
+------------------+
1 row in set (0.00 sec)

mysql> show full columns from teacher;  # 显示teacher表的所有字段
+---------+-------------+-------------------+------+-----+-----------------+----------------+---------------------------------+---------+
| Field   | Type        | Collation         | Null | Key | Default         | Extra          | Privileges                      | Comment |
+---------+-------------+-------------------+------+-----+-----------------+----------------+---------------------------------+---------+
| Id      | int(5)      | NULL              | NO   | PRI | NULL            | auto_increment | select,insert,update,references |         |
| name    | char(10)    | latin1_swedish_ci | NO   |     | NULL            |                | select,insert,update,references |         |
| address | varchar(50) | latin1_swedish_ci | YES  |     | No.1 Mid school |                | select,insert,update,references |         |
| year    | date        | NULL              | YES  |     | NULL            |                | select,insert,update,references |         |
+---------+-------------+-------------------+------+-----+-----------------+----------------+---------------------------------+---------+
4 rows in set (0.01 sec)

mysql> drop table teacher;  # 删除teacher表
Query OK, 0 rows affected (0.03 sec)

mysql> show tables;
Empty set (0.00 sec)

mysql>

推荐教程:mysql视频教程

以上是mysql如何查询表字所有字段?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn