根據官方文檔描述,int(M)中的M表示數據顯示的寬度,與實際存儲的長度無關。
1、也就是int(3)和int(11)能夠存儲的數據是一樣的,都是從-2147483648
到2147483647(或者0-
4294967295
)。
2、int(M)只有聯合zerofill參數才能有意義,否則int(3)和int(11)沒有任何區別。
下面用實例來證明上述兩句話:
1、創建測試表,具有int(3)、int(11)、int三個字段
create table test_int(id int(3) unsigned not null,uid int(11) unsigned not null,uuid int unsigned not null );
下面插入int無符號能夠存儲的最大值:
insert into test_int values(4294967295,4294967295,4294967295);
(product)root@localhost [a]> select * from test_int;
+------------+------------+------------+
| id | uid | uuid |
+------------+------------+------------+
| 4294967295 | 4294967295 | 4294967295 |
+------------+------------+------------+
1 row in set (0.00 sec)
【結論1】:通過上述實驗,對於沒有加上zerofill參數
的int、int(3)、int(11)無論在存儲上還是在顯示上都毫無區別。
2、創建測試表,具有int(3)、int(11)、int三個字段同時加上zerofill參數
(product)root@localhost [a]> create table test_int1(id int(3) unsigned zerofill not null,uid int(11) unsigned zerofill not null,uuid int unsigned zerofill not null ); Query OK, 0 rows affected (0.14 sec) (product)root@localhost [a]> insert into test_int1 values(4294967295,4294967295,4294967295); Query OK, 1 row affected (0.03 sec) (product)root@localhost [a]> insert into test_int1 values(1,4294967295,110000); Query OK, 1 row affected (0.00 sec) (product)root@localhost [a]> select * from test_int1; +------------+-------------+------------+ | id | uid | uuid | +------------+-------------+------------+ | 4294967295 | 04294967295 | 4294967295 | | 001 | 04294967295 | 0000110000 | +------------+-------------+------------+ 2 rows in set (0.00 sec)
【結論2】:通過上述實驗,對於加上zerofill參數
的int、int(3)、int(11),不足M寬度的,用0補充,否則不影響顯示。