1. 程式人生 > 實用技巧 >mysql中int(5)的含義

mysql中int(5)的含義

目標:區分int(5)與int的區別

準備:mysql 5.7.18,官方文件:https://dev.mysql.com/doc/refman/5.7/en/numeric-type-syntax.html

步驟1:

官方文件中的描述:

INT[(M)] [UNSIGNED] [ZEROFILL]

For integer data types,Mindicates 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

Section11.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.