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的值比数值实际的长度小对数据也没有任何影响。

相关文章
相关标签/搜索