本教程操作环境:windows7系统、mysql8版本、dell g3电脑。
创建完数据表之后,经常需要查看表结构(表信息)。在 mysql 中,可以使用 describe 和 show create table 命令来查看数据表的结构。
describe:以表格的形式展示表结构
describe/desc 语句会以表格的形式来展示表的字段信息,包括字段名、字段数据类型、是否为主键、是否有默认值等,语法格式如下:
describe 表名;
或简写成:
desc 表名;
【实例1】分别使用 describe 和 desc 查看表 tb_emp1 的表结构,sql 语句和运行结果如下:
mysql> describe tb_emp1;+--------+-------------+------+-----+---------+-------+| field | type | null | key | default | extra |+--------+-------------+------+-----+---------+-------+| id | int(11) | yes | | null | || name | varchar(25) | yes | | null | || deptid | int(11) | yes | | null | || salary | float | yes | | null | |+--------+-------------+------+-----+---------+-------+4 rows in set (0.14 sec)
mysql> desc tb_emp1;+--------+-------------+------+-----+---------+-------+| field | type | null | key | default | extra |+--------+-------------+------+-----+---------+-------+| id | int(11) | yes | | null | || name | varchar(25) | yes | | null | || deptid | int(11) | yes | | null | || salary | float | yes | | null | |+--------+-------------+------+-----+---------+-------+4 rows in set (0.14 sec)
其中,各个字段的含义如下:
null:表示该列是否可以存储 null 值。
key:表示该列是否已编制索引。pri 表示该列是表主键的一部分,uni 表示该列是 unique 索引的一部分,mul 表示在列中某个给定值允许出现多次。
default:表示该列是否有默认值,如果有,值是多少。
extra:表示可以获取的与给定列有关的附加信息,如 auto_increment 等。
show create table:以sql语句的形式展示表结构
show create table 命令会以 sql 语句的形式来展示表信息。和 describe 相比,show create table 展示的内容更加丰富,它可以查看表的存储引擎和字符编码;另外,你还可以通过\g或者\g参数来控制展示格式。
show create table 的语法格式如下:
show create table 表名;
在 show create table 语句的结尾处(分号前面)添加\g或者\g参数可以改变展示形式。
【实例2】使用 show create table 语句查看表 tb_emp1 的详细信息,一次使用\g结尾,一次不使用:
mysql> show create table tb_emp1;+---------+------------------------------------------------+| table | create table |+---------+------------------------------------------------+| tb_emp1 | create table `tb_emp1` ( `id` int(11) default null, `name` varchar(25) default null, `salary` float default null) engine=innodb default charset=gb2312 |+---------+------------------------------------------------+1 row in set (0.01 sec)
mysql> show create table tb_emp1 \g;+---------+------------------------------------------------+| table | create table |+---------+------------------------------------------------+| tb_emp1 | create table `tb_emp1` ( `id` int(11) default null, `name` varchar(25) default null, `salary` float default null) engine=innodb default charset=gb2312 |+---------+------------------------------------------------+1 row in set (0.00 sec)
show create table 使用\g结尾的 sql 语句和运行结果如下:
mysql> show create table tb_emp1\g*************************** 1. row *************************** table: tb_emp1create table: create table `tb_emp1` ( `id` int(11) default null, `name` varchar(25) default null, `deptid` int(11) default null, `salary` float default null) engine=innodb default charset=gb23121 row in set (0.03 sec)
【相关推荐:mysql视频教程】
以上就是mysql怎么查询表结构的详细内容。