昨天写sql文件时把以前一直不是很明白的地方弄明白了,就是在设置int型的时候,需要设置int(M),以前知道这个M最大是255,但是到底应该设置多少并没有在意。
查了下官方manual有这样的语句:
M
indicates the maximum display width for integer types. The maximum legal display width is 255.
这个M就是maximum display width。那什么是maximum display width?看了下面的例子很容易说明了,注意zerofill:
mysql> create table b ( b int (4));
Query OK, 0 rows affected (0.25 sec)
mysql> insert into b values (12345);
Query OK, 1 row affected (0.00 sec)
mysql> select * from b;
+-------+
| b |
+-------+
| 12345 |
+-------+
1 row in set (0.00 sec)
mysql> alter table b change b b int(11);
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from b;
+-------+
| b |
+-------+
| 12345 |
+-------+
1 row in set (0.00 sec)
mysql> alter table b change b b int(11) zerofill;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> select * from b;
+-------------+
| b |
+-------------+
| 00000012345 |
+-------------+
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 |
+--------+
| 012345 |
+--------+
1 row in set (0.00 sec)
以上的例子说明了,这个M的表示显示宽度,他跟着zerofill一起才有意义。就算前面设置的M的值比数值实际的长度小对数据也没有任何影响。
转载于:https://blog.51cto.com/harshing/1717163