mysql的tinyint(1)_Mysql中tinyint(1)和tinyint(4)到底有什么区别?

tinyint存储只是用一个字节,就是8位,只能存储2^8个数字,也就是256个数字,在mysql实现中,有符号是-128-127,无符号是0-255

tinyint后面的括号带的数字,以后称之为M,和存贮的值没有任何关系,只是在某些情况下和显示的宽度有关系

1. 常规测试

使用基本的表,插入基本的数据

f1d42cb06bbdad413623a722f564b179.png

查询后发现没有任何区别

bc998f5bdb2ebc3da2440019bc198746.png

2. 无符号建表,同时zerofill

建表的基本语句是

4daf0f057eb8e84953805ab11c9b711d.png

最后查询结果如下,比较明显

bb90b08f917024ab9af463062000fe7e.png

zerofill的整数字段必须无符号,这里可以看出M显示出了特定的宽度,不够的时候会填充0,多余了不作处理

官方文档的解释是,5.7英文版

这里摘抄其中重要的一段

MySQL supports an extension for optionally specifying the display width of integer data types in parentheses following the base keyword for the type. For example, INT(4) specifies an INT with a display width of four digits. This optional display width may be used by applications to display integer values having a width less than the width specified for the column by left-padding them with spaces. (That is, this width is present in the metadata returned with result sets. Whether it is used or not is up to the application.)

The display width does not constrain the range of values that can be stored in the column. Nor does it prevent values wider than the column display width from being displayed correctly. For example, a column specified as SMALLINT(3) has the usual SMALLINT range of -32768 to 32767, and values outside the range permitted by three digits are displayed in full using more than three digits.

When used in conjunction with the optional (nonstandard) attribute ZEROFILL, the default padding of spaces is replaced with zeros. For example, for a column declared as INT(4) ZEROFILL, a value of 5 is retrieved as 0005.

至此,也验证了我们的结论.

绝对和存储的值没有关系

mysql的console中也会忽略这些

无符号和zerofill的时候会填充0,显示成M对应的宽度

整数类型都一样,有默认的显示宽度

M作为元数据存储,推荐是显示的宽度,但是最终的解释权归程序所有

最后,我给出了我的建议,那就是M其实没用,tinyint默认是4,其余的也有默认值,以后程序开发中,涉及整形数字的M时,可以不必纠结,直接忽略,最后使用数据库默认的M值即可

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值