目标:区分int(5)与int的区别
准备:mysql 5.7.18,官方文档:https://dev.mysql.com/doc/refman/5.7/en/numeric-type-syntax.html
步骤1:
官方文档中的描述:
For integer data types, M indicates the maximum display width. The maximum display width is 255. Display width is unrelated to the range of values a type can store, as described in Section 11.1.6, “Numeric Type Attributes”.
对于整数类型,M表示【展示宽度】的最大值。上限是255。展示宽度与整数类型可以存储值的范围没有关系,如11.1.6中所描述的那样。
步骤2:
创建测试表
步骤3:
插入测试数据
步骤4:
查询数据
结论:
int(5) 中5与存储的数据长度或大小没有关系,验证 column: int1。
如果使用了zerofill 属性,且存储的整数长度不足5,则使用0进行补位,验证 column:zero_fill1。
如果没有使用zerofill 属性,则 int(M) 中的 M实际上是不生效的。验证 column: int2。
超过了M长度,zerofill 属性当然也不生效。验证 column: zero_fill2。
参考:
https://nexladder.com/blog/what-does-int11-means-in-mysql/#:~:text=most%20of%20the%20developer%20think,with%2011%20digits%20in%20length.&text=in%20int(11)%2C%2011,character%20that%20can%20be%20stored.