mysql查看表结构的几种方式

在实际的开发中,我们肯定经常要查看表结构的,特别是遇到自己不是最初开发的项目的时候,通过表结构,大概就能看出表里存什么数据,每个字段代表什么意思。实际上有很多查看表结构的方式,下面就拿rails里面的表schema_migrations介绍一下我自己常用的命令:

desc table_name;

mysql> desc schema_migrations;
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| version | varchar(255) | NO   | PRI | NULL    |       |
+---------+--------------+------+-----+---------+-------+
1 row in set (0.00 sec)

它是我们最常用的命令,这个命令也是describe schema_migrations的缩写。

show fields from table_name;

mysql> show fields from schema_migrations;
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| version | varchar(255) | NO   | PRI | NULL    |       |
+---------+--------------+------+-----+---------+-------+
1 row in set (0.01 sec)

和describe命令达到的效果一样。

在它的基础上就有一个更强大的命令:

show full fields from table_name


mysql> show full fields from schema_migrations;
+---------+--------------+-----------------+------+-----+---------+-------+---------------------------------+---------+
| Field   | Type         | Collation       | Null | Key | Default | Extra | Privileges                      | Comment |
+---------+--------------+-----------------+------+-----+---------+-------+---------------------------------+---------+
| version | varchar(255) | utf8_general_ci | NO   | PRI | NULL    |       | select,insert,update,references |         |
+---------+--------------+-----------------+------+-----+---------+-------+---------------------------------+---------+
1 row in set (0.01 sec)

每个字段的详细信息也就出来了,但是遗憾的是索引没有处理,说到查看索引,很明显就会想到下面的命令:

show index from table_name


mysql> show index from schema_migrations;
+-------------------+------------+--------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table             | Non_unique | Key_name                 | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------------------+------------+--------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| schema_migrations |          0 | unique_schema_migrations |            1 | version     | A         |          21 |     NULL | NULL   |      | BTREE      |         |               |
+-------------------+------------+--------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

这个命令和 show keys from table_name得到的效果一样。

实际上有个终极命令,既可以看到本身的字段又可以看到索引等其他信息,还能得到完整的创建表结构的命令,那就是:

show create table table_name


mysql> show create table schema_migrations;
+-------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table             | Create Table                                                                                                                                                   |
+-------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| schema_migrations | CREATE TABLE `schema_migrations` (
  `version` varchar(255) NOT NULL,
  UNIQUE KEY `unique_schema_migrations` (`version`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

通过命令执行的结果可以看到创建表的sql语句,字段名,索引,搜索引擎,编码方式等信息,是不是很强大。







  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值