mysql中int(1)與int(10)的區別

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

普通大小的整數。帶符號的範圍是-2147483648到2147483647。無符號的範圍是0到4294967295。

INT(1) 和 INT(10)本身沒有區別,但是加上(M)值後,會有顯示寬度的設置。

如代碼所示:

mysql> create table test(id int(3));
Query OK, 0 rows affected (0.47 sec)
mysql> insert into test values(12);
Query OK, 1 row affected (0.12 sec)
mysql> insert into test values(1234);
Query OK, 1 row affected (0.10 sec)
mysql> select * from test;
+------+| id   |+------+|   12 || 1234 |+------+

再試一下。這下咱們加上zerofill。

mysql> create table test1(id int(3) zerofill);
Query OK, 0 rows affected (0.32 sec)
mysql> insert into test1 value(12);
Query OK, 1 row affected (0.07 sec)
mysql> insert into test1 value(1234);
Query OK, 1 row affected (0.05 sec)
mysql> select * from test1;
+------+| id   |+------+|  012 || 1234 |+------+

 這下注意12前面輸出多了個0,int(M) 的值多了個0,這就是顯示寬度的限制。而多出來的還會顯示出來。只是系統判定12顯示寬度不足,會補0來補全顯示寬度

但是要注意插入負數的時候:

沒有設置zerofill的時候負數正常顯示

mysql> insert into test value(-1234);
Query OK, 1 row affected (0.07 sec)
mysql> select * from test;
+-------+| id    |+-------+|    12 ||   123 || -1234 |+-------+3 rows in set (0.00 sec)

咱再來看看設置 zerofill的時候:

mysql> insert into test1 value(-1234);
Query OK, 1 row affected, 1 warning (0.11 sec)
mysql> select * from test1;
+------+| id   |+------+|  012 || 1234 ||  000 |+------+

 輸出爲000,插入是-1234 。顯示是000。

原來添加zerofill的時候系統會給自動添加上unsigned屬性。就是非負數。而設置的顯示寬度爲3位。所以就會輸出000。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章