MySQL int(M)的意義(轉)

昨天寫sql文件時把之前一直不是很明白的地方弄明白了,就是在設置int型的時候,須要設置int(M),之前知道這個M最大是255,可是到底應該設置多少並無在乎。html

  查了下官方manual 有這樣的語句:mysql

     M indicates the maximum display width for integer types. The maximum legal display width is 255.sql

     這個M 就是maximum display width。那什麼是maximum display width?看了下面的例子很容易說明了,注意zerofill code

 

 mysql> create table b ( b int (4)); 
Query OK, 0 rows affected (0.25 sec)
htm

mysql> insert into b values ( 12345 ); 
Query OK, 1 row affected (0.00 sec)
get

mysql> select * from b; 
+-------+
| b     |
+-------+
| 12345 |
+-------+
1 row in set (0.00 sec)
table

mysql> alter table b change b b int(11); 
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0
class

mysql> select * from b; 
+-------+
| b     |
+-------+
12345 |
+-------+
1 row in set (0.00 sec)
select

mysql> alter table b change b b int(11) zerofill ; 
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0
im

mysql> select * from b ;
+-------------+
| b           |
+-------------+
| 000000
 12345 |
+-------------+
1 row in set (0.00 sec)

mysql> alter table b change b b int(4) zerofill ; 
Query OK, 1 row affected (0.08 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from b ;
+-------+
| b     |
+-------+
| 10000 |
+-------+
1 row in set (0.00 sec)

mysql> alter table b change b b int(6) zerofill ; 
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from b; 
+--------+
| b      |
+--------+
| 0
 12345 |
+--------+
1 row in set (0.00 sec)

     以上的例子說明了,這個M 的表示顯示寬度,他跟着zerofill 一塊兒纔有意義。就算前面設置的M的值比數值實際的長度小對數據也沒有任何影響。

相關文章
相關標籤/搜索