当前位置: 移动技术网 > IT编程>数据库>Mysql > mysql整数数据类型深入解析

mysql整数数据类型深入解析

2017年12月12日  | 移动技术网IT编程  | 我要评论

男款单肩包,旧口高中,r440指拨

此处我们给int char没有给出他们的宽度,系统默认会给它分配一个宽度。
m指示最大显示宽度。最大有效显示宽度是255。显示宽度与存储大小或类型包含的值的范围无关
我们来进行下试验
复制代码 代码如下:

mysql(root@localhost:test 03:19:00)>create table c (
-> id int not null,
-> name char not null);
query ok, 0 rows affected (0.25 sec)
mysql(root@localhost:test 03:19:34)>desc c;
+-------+---------+------+-----+---------+-------+
| field | type | null | key | default | extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | no | | null | |
| name | char(1) | no | | null | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

那么我们可以看到这里,系统会自动为我们的数据类型给出一个默认的宽带值,这里这个宽度值其实只有在zerofill的作用下才能起到一定的作用。在下面我们看下其他的默认值是多少,
复制代码 代码如下:

mysql(root@localhost:test 03:34:53)>alter table c modify id smallint;
query ok, 0 rows affected (0.05 sec)
records: 0 duplicates: 0 warnings: 0
mysql(root@localhost:test 03:39:39)>desc c;
+-------+-------------+------+-----+---------+-------+
| field | type | null | key | default | extra |
+-------+-------------+------+-----+---------+-------+
| id | smallint(6) | yes | | null | |
| name | varchar(10) | yes | | null | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql(root@localhost:test 03:39:44)>alter table c modify id bigint;
query ok, 4 rows affected (0.23 sec)
records: 4 duplicates: 0 warnings: 0
mysql(root@localhost:test 03:40:12)>desc c;
+-------+-------------+------+-----+---------+-------+
| field | type | null | key | default | extra |
+-------+-------------+------+-----+---------+-------+
| id | bigint(20) | yes | | null | |
| name | varchar(10) | yes | | null | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

这里我们再来看下当插入值大于数据类型的取值范围的情况:
复制代码 代码如下:

mysql(root@localhost:test 03:25:58)>insert into c values(300,'chen');
query ok, 1 row affected, 2 warnings (0.08 sec)
mysql(root@localhost:test 03:26:20)>show warnings;
+---------+------+---------------------------------------------+
| level | code | message |
+---------+------+---------------------------------------------+
| warning | 1264 | out of range value for column 'id' at row 1 |
| warning | 1265 | data truncated for column 'name' at row 1 |
+---------+------+---------------------------------------------+
2 rows in set (0.00 sec)
mysql(root@localhost:test 03:26:27)>select * from c;
+------+------+
| id | name |
+------+------+
| 127 | c |
+------+------+
1 row in set (0.02 sec)
mysql(root@localhost:test 03:26:40)>insert into c values(320,'chen');
query ok, 1 row affected, 2 warnings (0.05 sec)
mysql(root@localhost:test 03:26:53)>select * from c;
+------+------+
| id | name |
+------+------+
| 127 | c |
| 127 | c |
+------+------+
2 rows in set (0.00 sec)

这里的tinyint是占有一个字节,就是可以表示从0-255这个范围的整数,可是这里为什么直到127呢,原因是我们没有给他设定无符号类型的。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网