• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

mysql整数数据类型深入解析_MySQL

mysql 搞代码 4年前 (2022-01-09) 17次浏览 已收录 0个评论

gaodaima.com此处我们给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) | 本文来源gaodaimacom搞#^代%!码&网(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呢,原因是我们没有给他设定无符号类型的。
gaodaima.com


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:mysql整数数据类型深入解析_MySQL
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址