码迷,mamicode.com
首页 > 数据库 > 详细

mysql中int(1)与int(10)的区别

时间:2016-10-16 21:41:17      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:

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。

mysql中int(1)与int(10)的区别

标签:

原文地址:http://www.cnblogs.com/wanghaokun/p/5967663.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!