数据类型之【数值类型】的整数型 int
作者:互联网
1.1 int说明
类型 | 长度 | 不指定长度时长度为 | 占用字节 | 有符号 | 无符号 |
int | 10 | 11(实际长度还是10位) | 4(32bit) | -2147483648~2147483647 | 0~4294967295 |
id int(M) [UNSIGNED] [ZEROFILL]
字段名 字段类型(长度) [无符号] [前导填充]
unsigned:
01:int(M)后面加上unsigned,就是无符号(int的范围就是0~4294967295)
02:int(M)后面不加上unsigned,并且不加ZEROFILL参数,就是有符号(int的范围就是
-2147483648~2147483647)
zerofill:
01:进行前导零填充
02:int(M)加上zerofile后,同时也会把unsigned参数也带上(int范围0~4294967295)
1.2 int实践
1.2.1 环境准备
##创建chenliang库
mysql> create database if not exists chenliang;
Query OK, 1 row affected (0.03 sec)
mysql> show databases like "chenliang";
+----------------------+
| Database (chenliang) |
+----------------------+
| chenliang |
+----------------------+
1 row in set (0.03 sec)
##进入chenliang库
mysql> use chenliang;
Database changed
mysql> select database();
+------------+
| database() |
+------------+
| chenliang |
+------------+
1 row in set (0.01 sec)
1.2.2 加unsigned参数
##创建test1测试表(这里指定了UNSIGNED,也就是无符号)
mysql> create table if not exists test1(
-> id int(10) unsigned
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> desc test1;
+-------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| id | int(10) unsigned | YES | | NULL | |
+-------+------------------+------+-----+---------+-------+
1 row in set (0.01 sec)
**测试01:测试插入范围0~4294967295的整数以及超过4294967295的整数
mysql> insert into test1 values(0);
Query OK, 1 row affected (0.05 sec)
<=插入整数0,正确(没有超过范围0~4294967295)
mysql> insert into test1 values(4294967295);
Query OK, 1 row affected (0.01 sec)
<=插入数值4294967295,正确(没有超过范围0~4294967295)
mysql> insert into test1 values(4294967296);
ERROR 1264 (22003): Out of range value for column 'id' at row 1
<=插入数值4294967296,错误(超出范围0~4294967295)
mysql> select * from test1;
+------------+
| id |
+------------+
| 0 |
| 4294967295 |
+------------+
2 rows in set (0.00 sec)
**测试02:测试-1~-2147483648范围的数据是否能够正常插入
mysql> insert into test1 values(-1);
ERROR 1264 (22003): Out of range value for column 'id' at row 1
<=插入负数报错(因为建表时,id字段加了unsigned参数,int的范围为0~4294967295)
mysql> insert into test1 values(-2147483648);
ERROR 1264 (22003): Out of range value for column 'id' at row 1
^==插入负数报错(因为建表时,在id字段加了unsigned参数,int的范围为0~4294967295)
mysql> select * from test1;
+------------+
| id |
+------------+
| 0 |
| 4294967295 |
+------------+
2 rows in set (0.00 sec)
1.2.3 加zerofill参数
##创建test2表,(这里指定了zerofill,也就是前导零填充)
mysql> create table if not exists test2(
-> id int(10) zerofill
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> desc test2;
+-------+---------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------------+------+-----+---------+-------+
| id | int(10) unsigned zerofill | YES | | NULL | |
+-------+---------------------------+------+-----+---------+-------+
1 row in set (0.03 sec)
<=只指定unsigned,不会有zerofill
<=只指定zerofill时,会带上unsigned
**测试01:测试插入范围0~4294967295的整数和超过4294967295的整数
mysql> insert into test2 values(0);
Query OK, 1 row affected (0.04 sec)
<=插入整数0,在0~4294967295范围内,正确
mysql> insert into test2 values(4294967295);
Query OK, 1 row affected (0.02 sec)
<=插入整数4294967295,在0~4294967295范围内,正确
mysql> insert into test2 values(4294967296);
ERROR 1264 (22003): Out of range value for column 'id' at row 1
<=插入整数4294967296,不在0~4294967295范围内,错误
mysql> select * from test2;
+------------+
| id |
+------------+
| 0000000000 |
| 4294967295 |
+------------+
2 rows in set (0.02 sec)
**测试02:测试-1~-2147483648围的数据是否能够正常插入
mysql> insert into test2 values(-1);
ERROR 1264 (22003): Out of range value for column 'id' at row 1
<=插入负整数-1,错误(因为在id字段加了zerofill参数,它会把unsigned也带上,所以
id字段的范围为0~4294967295)
mysql> insert into test2 values(-2147483648);
ERROR 1264 (22003): Out of range value for column 'id' at row 1
<=插入负整数-2147483648,错误(因为在id字段加了zerofill参数,它会把unsigned也带
上,所以id字段的范围为0~4294967295)
mysql> select * from test2;
+------------+
| id |
+------------+
| 0000000000 |
| 4294967295 |
+------------+
2 rows in set (0.03 sec)
1.2.4 不加unsigned和zerofill参数
##创建test3表(不加unsigned和zerofill)
mysql> create table if not exists test3(
-> id int(10)
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> desc test3;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(10) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)1 row in set (0.01 sec)
**测试01:测试插入整数0~4294967295和超过4294967295的整数
mysql> insert into test3 values(0);
Query OK, 1 row affected (0.00 sec)
<=插入整数0,正确(在范围-2147483648~2147483647范围内)
mysql> insert into test3 values(2147483647);
Query OK, 1 row affected (0.02 sec)
<=插入整数2147483647,正确(在范围-2147483648~2147483647范围内)
mysql> insert into test3 values(2147483648);
ERROR 1264 (22003): Out of range value for column 'id' at row 1
<=插入整数2147483648,错误(不在范围-2147483648~2147483647范围内)
mysql> select * from test3;
+------------+
| id |
+------------+
| 0 |
| 2147483647 |
+------------+
2 rows in set (0.00 sec)
**测试02:测试插入负数-1~-2147483648和小于--2147483648的负数
mysql> INSERT INTO test3 values(-1);
Query OK, 1 row affected (0.10 sec)
<=插入负整数-1,正确(在范围-2147483648~2147483647范围内)
mysql> insert into test3 values(-2147483648);
Query OK, 1 row affected (0.00 sec)
<=插入负整数-2147483648,正确(在范围-2147483648~2147483647范围内)
mysql> insert into test3 values(-2147483649);
ERROR 1264 (22003): Out of range value for column 'id' at row 1
<=插入负整数-2147483649,错误(不在范围-2147483648~2147483647范围内)
mysql> select * from test3;
+-------------+
| id |
+-------------+
| 0 |
| 2147483647 |
| -1 |
| -2147483648 |
+-------------+
4 rows in set (0.00 sec)
1.3 int总结
格式:
id int(M) [UNSIGNED] [ZEROFILL]
字段名 字段类型(长度) [无符号] [前导填充]
unsigned:
01:int(M)后面加上unsigned后,就是无符号(int的范围就是0~4294967295)
02:int(M)后面不加上unsigned,并且不加zerofill参数,就是有符号(int的范围就
是-2147483648~2147483647)
zerofill:
01:进行前导零填充(插入数值1,字段显示的是0000000001,因为int的长度为10)
02:int(M)加上zerofile后,同时也会把unsigned参数也带上(int的范围为0~4294967295)
标签:int,数据类型,4294967295,整数型,sec,mysql,id,row 来源: http://blog.51cto.com/11576296/2344714