文章目录
- 1.1 smallint说明
- 1.2 测试环境说明
- 1.3 加unsigned属性
- 1.3.1 SQL模式开启严格模式
- 1.3.2 SQL模式未开启严格模式
- 1.4 加zerofill属性
- 1.4.1 SQL模式开启严格模式
- 1.4.2 SQL模式未开启严格模式
- 1.5 不加unsigned和zerofill属性
- 1.5.1 SQL模式开启严格模式
- 1.5.2 SQL模式未开启严格模式
1.1 smallint说明
数据类型 | 显示长度 | 占用字节 | 有符号 | 无符号 |
smallint | 加上unsigned/zerofill:5 不加unsigned/zerofill:6 | 2(16bit) | -32768至32767 | 0至65535 |
## 格式
id smallint(M) [unsigned] [zerofill]
字段名 数据类型(显示长度,建表时不指定) 无符号 无符号且前导零填充
## 关于smallint最大数值是怎样得来的
smallint占用2字节,2字节占用16位,经过换算(2的16次方减1)就是65535
## 关于加上unsigned后的说明
加上unsigned后就是无符号(范围是0~65535的整数,因为是整数,不会有符号"-",所以就是无符号)
## 关于加上zerofill后的说明
zerofill会把unsigned属性也给带上,这样就是无符号(范围是0~65535,显示长度就是5),同时还会进
行前导零填充(没有达到显示长度的数值,例如:你插入1,显示的是00001)。
## 不加unsigned和zerofill的说明
字段后面不加上这两个属性中的任何一个,就表示是有符号(范围是-32768~32767,因为有符号"-",所有是有符号)。
1.2 测试环境说明
## 数据库版本和默认的存储引擎
mysql> select @@version,@@default_storage_engine;
+------------+--------------------------+
| @@version | @@default_storage_engine |
+------------+--------------------------+
| 5.7.28-log | InnoDB |
+------------+--------------------------+
1 row in set (0.00 sec)
## 创建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)
## 查看事务是否自动提交
mysql> select @@global.autocommit;
+---------------------+
| @@global.autocommit |
+---------------------+
| 1 |
+---------------------+
1 row in set (0.00 sec)
1.3 加unsigned属性
1.3.1 SQL模式开启严格模式
SQL_MODE中开启了严格模式,即SQL_MODE参数中包含STRICT_TRANS_TABLES参数
## 设置会话模式下sql_mode中包含strict_trans_tables变量
mysql> set session sql_mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION";
Query OK, 0 rows affected (0.00 sec)
mysql> select @@sql_mode\G
*************************** 1. row ***************************
@@sql_mode: STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
1 row in set (0.00 sec)
## 创建test1测试表(这里指定了UNSIGNED,也就是无符号)
mysql> CREATE TABLE IF NOT EXISTS test1(
-> id smallint UNSIGNED
-> )engine=innodb character set utf8 collate utf8_general_ci;
Query OK, 0 rows affected (0.03 sec)
## test1表的id字段指定了unsigned参数,那么id字段的范围就是0~65535
## 显示长度为smallint(5),因为65535的长度是5
## 查看test1表的表结构
mysql> desc test1;
+-------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------------------+------+-----+---------+-------+
| id | smallint(5) unsigned | YES | | NULL | |
+-------+----------------------+------+-----+---------+-------+
1 row in set (0.00 sec)
## 测试插入范围0~65535的整数和不在该范围内的整数
mysql> insert into test1(id) values(-1); # 插入数值-1,报错,不在范围内
ERROR 1264 (22003): Out of range value for column 'id' at row 1
mysql> insert into test1(id) values(0); # 插入数值0,正确,在范围内
Query OK, 1 row affected (0.00 sec)
mysql> insert into test1(id) values(65535); # 插入数值65535,正确,在范围内
Query OK, 1 row affected (0.01 sec)
mysql> insert into test1(id) values(65536); # 插入数值65536,报错,不在范围内
ERROR 1264 (22003): Out of range value for column 'id' at row 1
mysql> select * from test1;
+-------+
| id |
+-------+
| 0 |
| 65535 |
+-------+
2 rows in set (0.00 sec)
1.3.2 SQL模式未开启严格模式
SQL_MODE中未开启严格模式,即SQL_MODE参数中不包含STRICT_TRANS_TABLES参数
## 设置会话模式下sql_mode中不开启严格模式,即不包含strict_trans_tables变量
mysql> set session sql_mode="NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION";
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> select @@sql_mode\G
*************************** 1. row ***************************
@@sql_mode: NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
1 row in set (0.00 sec)
## 创建test11表(这里加了unsigned参数哈,也就是无符号)
mysql> create table if not exists test11(
-> id smallint unsigned
-> )engine=innodb character set utf8 collate utf8_general_ci;
Query OK, 0 rows affected (0.01 sec)
## test11表的id字段指定了unsigned参数,那么id字段的范围就是0~65535
## 显示长度为smallint(5),因为65535的长度是5
## 查看test11表的表结构
mysql> desc test11;
+-------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------------------+------+-----+---------+-------+
| id | smallint(5) unsigned | YES | | NULL | |
+-------+----------------------+------+-----+---------+-------+
1 row in set (0.00 sec)
## 测试插入范围0~65535的整数和不在该范围内的整数
mysql> insert into test11(id) values(-1);
Query OK, 1 row affected, 1 warning (0.01 sec)
## 不在范围内,插入未报错(因为sql_mode中没有开启严格模式)
## 插入到表中的数据不是-1,而是0
mysql> insert into test11(id) values(0);
Query OK, 1 row affected (0.00 sec)
## 在范围内,插入未报错,插入的是多少就是多少
mysql> insert into test11(id) values(65535);
Query OK, 1 row affected (0.00 sec)
## 在范围内,插入未报错,插入的是多少就是多少
mysql> insert into test11(id) values(65536);
Query OK, 1 row affected, 1 warning (0.00 sec)
## 不在范围内,插入未报错(因为sql_mode中没有开启严格模式)
## 插入到表中的数据不是65536,而是65535
mysql> select * from test11;
+-------+
| id |
+-------+
| 0 |
| 0 |
| 65535 |
| 65535 |
+-------+
4 rows in set (0.00 sec)
1.4 加zerofill属性
1.4.1 SQL模式开启严格模式
SQL_MODE中开启了严格模式,即SQL_MODE参数中包含STRICT_TRANS_TABLES参数
## 设置会话模式下sql_mode中包含strict_trans_tables变量
mysql> set session sql_mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION";
Query OK, 0 rows affected (0.00 sec)
mysql> select @@sql_mode\G
*************************** 1. row ***************************
@@sql_mode: STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
1 row in set (0.00 sec)
## 创建test2表,(这里指定了zerofill,也就是前导零填充)
mysql> create table if not exists test2(
-> id smallint zerofill
-> )engine=innodb character set utf8 collate utf8_general_ci;
Query OK, 0 rows affected (0.12 sec)
## id字段指定了zerofill参数,它会把unsigned参数也带上,那么id字段的范围0~65535;
## 显示长度是smallint(5),因为65535的长度是5;
## 查看test2表的表结构
mysql> desc test2;
+-------+-------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------------------------+------+-----+---------+-------+
| id | smallint(5) unsigned zerofill | YES | | NULL | |
+-------+-------------------------------+------+-----+---------+-------+
1 row in set (0.00 sec)
## 测试插入范围0~65535的整数和不在该范围内的整数
mysql> insert into test2(id) values(-1); # 插入数值-1,报错,不在范围内
ERROR 1264 (22003): Out of range value for column 'id' at row 1
mysql> insert into test2(id) values(0); # 插入数值0,正确,在范围内
Query OK, 1 row affected (0.00 sec)
mysql> insert into test2(id) values(65535); # 插入数值65535,正确,在范围内
Query OK, 1 row affected (0.01 sec)
mysql> insert into test2(id) values(65536); # 插入数值65536,报错,不在范围内
ERROR 1264 (22003): Out of range value for column 'id' at row 1
mysql> select * from test2;
+-------+
| id |
+-------+
| 00000 |
| 65535 |
+-------+
2 rows in set (0.00 sec)
1.4.2 SQL模式未开启严格模式
SQL_MODE中未开启严格模式,即SQL_MODE参数中不包含STRICT_TRANS_TABLES参数
## 设置会话模式下sql_mode中不开启严格模式,即不包含strict_trans_tables变量
mysql> set session sql_mode="NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION";
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> select @@sql_mode\G
*************************** 1. row ***************************
@@sql_mode: NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
1 row in set (0.00 sec)
## 创建test22表(这里加上zerofill参数,前导零填充,同时也会把unsigned参数也带上)
mysql> create table if not exists test22(
-> id smallint zerofill
-> )engine=innodb character set utf8 collate utf8_general_ci;
Query OK, 0 rows affected (0.01 sec)
## id字段指定了zerofill参数,它会把unsigned参数也带上,那么id字段的范围0~65535;
## 显示长度是smallint(5),因为65535的长度是5;
## 查看test22表的表结构
mysql> desc test22;
+-------+-------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------------------------+------+-----+---------+-------+
| id | smallint(5) unsigned zerofill | YES | | NULL | |
+-------+-------------------------------+------+-----+---------+-------+
1 row in set (0.01 sec)
## 测试插入范围0~65535的整数和不在该范围内的整数
mysql> insert into test22(id) values(-1);
Query OK, 1 row affected, 1 warning (0.00 sec)
## 不在范围内,插入未报错(因为sql_mode中未开启严格模式)
## 插入到表中的数据不是-1,而是0,但显示的时候会前导零填充,因为有zerofill参数
mysql> insert into test22(id) values(0);
Query OK, 1 row affected (0.01 sec)
## 在范围内,插入未报错,插入是什么就是什么,
## 但因为有zerofill参数,显示时就会前导零填充
mysql> insert into test22(id) values(65535);
Query OK, 1 row affected (0.00 sec)
## 在范围内,插入未报错,插入是什么就是什么
mysql> insert into test22(id) values(65536);
Query OK, 1 row affected, 1 warning (0.00 sec)
## 不在范围内,插入未报错(因为sql_mode中未开启严格模式)
## 插入到表中的数据不是65536,而是65535
mysql> select * from test22;
+-------+
| id |
+-------+
| 00000 |
| 00000 |
| 65535 |
| 65535 |
+-------+
4 rows in set (0.00 sec)
1.5 不加unsigned和zerofill属性
1.5.1 SQL模式开启严格模式
SQL_MODE中开启了严格模式,即SQL_MODE参数中包含STRICT_TRANS_TABLES参数
## 设置会话模式下sql_mode中包含strict_trans_tables变量
mysql> set session sql_mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION";
Query OK, 0 rows affected (0.00 sec)
mysql> select @@sql_mode\G
*************************** 1. row ***************************
@@sql_mode: STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
1 row in set (0.00 sec)
## 创建test3表(不加unsigned和zerofill)
mysql> create table if not exists test3(
-> id smallint
-> )engine=innodb character set utf8 collate utf8_general_ci;
Query OK, 0 rows affected (0.08 sec)
## test3表的id字段没指定unsigned和zerofill参数,那么id字段范围就是-32768~32767
## 因为要显示符号("-"),可以显示长度是smallint(6)
## 查看test3表的表结构
mysql> desc test3;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | smallint(6) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.01 sec)
## 测试插入-32768~32767范围内的整数和不在该范围内的整数
mysql> insert into test3(id) values(-32769); #插入数值-32769,错误,不在范围内
ERROR 1264 (22003): Out of range value for column 'id' at row 1
mysql> insert into test3(id) values(-32768); #插入数值-32768,正确,在范围内
Query OK, 1 row affected (0.01 sec)
mysql> insert into test3(id) values(32767); #插入数值32767,正确,在范围内
Query OK, 1 row affected (0.00 sec)
mysql> insert into test3(id) values(32768); #插入数值32768,错误,不在范围内
ERROR 1264 (22003): Out of range value for column 'id' at row 1
mysql> select * from test3;
+--------+
| id |
+--------+
| -32768 |
| 32767 |
+--------+
2 rows in set (0.00 sec)
1.5.2 SQL模式未开启严格模式
SQL_MODE中未开启严格模式,即SQL_MODE参数中不包含STRICT_TRANS_TABLES参数
## 设置会话模式下sql_mode中不开启严格模式,即不包含strict_trans_tables变量
mysql> set session sql_mode="NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION";
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> select @@sql_mode\G
*************************** 1. row ***************************
@@sql_mode: NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
1 row in set (0.00 sec)
## 创建test33表(不加zerofill和unsigned参数)
mysql> create table if not exists test33(
-> id smallint
-> )engine=innodb character set utf8 collate utf8_general_ci;
Query OK, 0 rows affected (0.00 sec)
## test33表的id字段没指定unsigned和zerofill参数,那么id字段范围就是-32768~32767
## 因为要显示符号("-"),可以显示长度是smallint(6)
## 查看test33表的表结构
mysql> desc test33;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | smallint(6) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)
## 测试插入-32768~32767范围内的整数和不在该范围内的整数
mysql> insert into test33(id) values(-32769);
Query OK, 1 row affected, 1 warning (0.00 sec)
## 不在范围内,插入未报错(因为sql_mode中没有开启严格格式)
## 插入的数据不是-32769,而是-32768
mysql> insert into test33(id) values(-32768);
Query OK, 1 row affected (0.01 sec)
## 在范围内,插入不会报错,插入的是什么就是什么
mysql> insert into test33(id) values(32767);
Query OK, 1 row affected (0.00 sec)
## 在范围内,插入不会报错,插入的是什么就是什么
mysql> insert into test33(id) values(32768);
Query OK, 1 row affected, 1 warning (0.00 sec)
## 不在范围内,插入未报错(因为sql_mode中没有开启严格格式)
## 插入的数据不是32768,而是32767
mysql> select * from test33;
+--------+
| id |
+--------+
| -32768 |
| -32768 |
| 32767 |
| 32767 |
+--------+
4 rows in set (0.00 sec)