标签:unsigned sql_mod 插入数据 表结构 str 引擎 字段 tran bsp
一、tinyint类型说明
id tinyint(M) [UNSIGNED] [ZEROFILL] 字段名 字段类型(显示长度,生产中创建表时不指定) [无符号] [前导填充] unsigned: 01:tinyint(M)后面加上unsigned后,就是无符号,只能插入正整数,范围就是0~255; 02:1字节就是8位,那么2的8次方-1就是255; 03:因为加了unsigned就是正整数,也就是无符号,所以范围是0~255; 04:255的长度是3,当不指定长度时,例如:tinyint unsigned其显示长度就是3,当插入 的正整数长度不够3时,不会自动补充,也就是1显示1;255显示255; zerofill: 01:加上zerofill,会把unsigned给带上,就是无符号,只能插入正整数,范围是0~255; 02:1字节就是8位,那么2的8次方-1就是255; 03:因为会带上unsigned参数,只能插入正整数,也就是无符号,所以范围是0~255; 04:255的长度是3,,不指定长度时,例如:tinyint zerofill其显示长度就是3,不管 你插入的正整数长度达不到3时,用0前导填充;例:1显示001;255显示255; 不加unsigned和zerofill 不在tinyint后加上unsigned或zerofill,其默认是有符号,也就可插入正整数和负整数,且 最大的正整数和负整数的长度是3,但是因为要显示符号("-"),所以显示长度是4;虽然显示的 长度是4,但对于插入的数据的长度达不到4时,也不会自动前导零填充; 特别说明: 01:在为字段指定类型时,一般不指定数据类型的长度,例如:tinyint或tinyint unsigned 02:当插入数据若超出范围,能否插入成功,这个得根据sql_mode中是否开启严格模式,即
sql_mode参数中是否包含STRICT_TRANS_TABLES变量;
二、实践环境说明与准备
-- 数据库版本和默认的存储引擎 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【加unsigned参数】
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 tinyint UNSIGNED -> ); Query OK, 0 rows affected (0.06 sec) -- PS -- id字段类型是tinyint unsigned,范围就是0~255,其长度是3;因为255的长度是3; -- 查看test1表的表结构 mysql> desc test1; +-------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+-------+ | id | tinyint(3) unsigned | YES | | NULL | | +-------+---------------------+------+-----+---------+-------+ 1 row in set (0.03 sec) -- 测试插入范围0~255范围整数和不在该范围内的整数 mysql> insert into test1 values(-1); # 插入数值-1,错误(不在0~255范围内) ERROR 1264 (22003): Out of range value for column ‘id‘ at row 1 mysql> insert into test1 values(0); # 插入数值0,正常(在0~255范围内) Query OK, 1 row affected (0.06 sec) mysql> insert into test1 values(255); # 插入数值255,正常(在0~255范围内) Query OK, 1 row affected (0.05 sec) mysql> insert into test1 values(256); # 插入数值256,错误(不在0~255范围内) ERROR 1264 (22003): Out of range value for column ‘id‘ at row 1 mysql> select * from test1; +------+ | id | +------+ | 0 | | 255 | +------+ 2 rows in set (0.00 sec)
SQL_MODE中未开启严格模式,即SQL_MODE参数中不包含STRICT_TRANS_TABLES参数
-- 设置会话模式下sql_mode中不包含strit_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 tinyint unsigned -> ); Query OK, 0 rows affected (0.00 sec) -- PS -- id字段类型是tinyint unsigned,范围就是0~255,其长度是3;因为255的长度是3; -- 查看test11表的表结构 mysql> desc test11; +-------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+-------+ | id | tinyint(3) unsigned | YES | | NULL | | +-------+---------------------+------+-----+---------+-------+ 1 row in set (0.00 sec) -- 测试插入范围0~255范围整数和不在该范围内的整数 mysql> insert into test11(id) values(-1); Query OK, 1 row affected, 1 warning (0.00 sec) -- 不在范围内,插入没有报错(因为sql_mode中没有开启严格模式) -- 插入到表中的数据不是-1,而是0 mysql> insert into test11(id) values(0); Query OK, 1 row affected (0.01 sec) -- 没报错,因为在范围内,插入到表中的也是0 mysql> insert into test11(id) values(255); Query OK, 1 row affected (0.01 sec) -- 没报错,因为在范围内,插入到表中的也是255 mysql> insert into test11(id) values(256); Query OK, 1 row affected, 1 warning (0.00 sec) -- 不在范围内,插入没有报错(因为sql_mode中没有开启严格模式) -- 插入到表中的数据不是256,而是255 mysql> select * from test11; +------+ | id | +------+ | 0 | | 0 | | 255 | | 255 | +------+ 4 rows in set (0.00 sec)
四、测试2【加zerofill参数】
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,会前导零填充,同时还会带上unsigned) mysql> CREATE TABLE IF NOT EXISTS test2( -> id tinyint zerofill -> ); Query OK, 0 rows affected (0.01 sec) -- PS -- id字段类型是tinyint zerofill,范围就是0~255,其长度是3;因为255的长度是3; -- 查看test2表的表结构 mysql> desc test2; +-------+------------------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------------------+------+-----+---------+-------+ | id | tinyint(3) unsigned zerofill | YES | | NULL | | +-------+------------------------------+------+-----+---------+-------+ 1 row in set (0.00 sec) -- 测试插入范围0~255范围整数和不在该范围内的整数 mysql> insert into test2 values(-1); # 插入数值-1,错误(不在0~255范围内) ERROR 1264 (22003): Out of range value for column ‘id‘ at row 1 mysql> insert into test2 values(0); # 插入数值0,正常(在0~255范围内) Query OK, 1 row affected (0.06 sec) mysql> insert into test2 values(255); # 插入数值255,正常(在0~255范围内) Query OK, 1 row affected (0.05 sec) mysql> insert into test2 values(256); # 插入数值256,错误(不在0~255范围内) ERROR 1264 (22003): Out of range value for column ‘id‘ at row 1 mysql> select * from test2; +------+ | id | +------+ | 000 | | 255 | +------+ 2 rows in set (0.00 sec)
SQL_MODE中未开启严格模式,即SQL_MODE参数中不包含STRICT_TRANS_TABLES参数
-- 设置会话模式下sql_mode中不包含strit_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 tinyint zerofill -> ); Query OK, 0 rows affected (0.00 sec) -- PS -- id字段类型是tinyint unsigned,范围就是0~255,其长度是3;因为255的长度是3; -- 查看test22表的表结构 mysql> desc test22; +-------+------------------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------------------+------+-----+---------+-------+ | id | tinyint(3) unsigned zerofill | YES | | NULL | | +-------+------------------------------+------+-----+---------+-------+ 1 row in set (0.00 sec) -- 测试插入范围0~255范围整数和不在该范围内的整数 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) -- 没报错,因为在范围内,插入到表中的也是0 mysql> insert into test22(id) values(255); Query OK, 1 row affected (0.01 sec) -- 没报错,因为在范围内,插入到表中的也是255 mysql> insert into test22(id) values(256); Query OK, 1 row affected, 1 warning (0.00 sec) -- 不在范围,插入没报错(sql_mode中没有开启严格模式) -- 但到表中的数据不是256,而是255 mysql> select * from test22; +------+ | id | +------+ | 000 | | 000 | | 255 | | 255 | +------+ 4 rows in set (0.00 sec)
五、测试3【不加zerofill和unsigned参数】
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 test3( -> id tinyint -> ); Query OK, 0 rows affected (0.06 sec) -- PS -- id字段的类型为tinyint,其范围是-128至127,其显示长度是4,因为要显示符号("-") -- 查看test3表的表结构 mysql> desc test3; +-------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------+------+-----+---------+-------+ | id | tinyint(4) | YES | | NULL | | +-------+------------+------+-----+---------+-------+ 1 row in set (0.01 sec) -- 测试插入-128~127范围的整数和不在该范围内的整数 mysql> insert into test3(id) values(-129); # 插入数值-129,错误,不在范围内 ERROR 1264 (22003): Out of range value for column ‘id‘ at row 1 mysql> insert into test3(id) values(-128); # 插入数值-128,正确,在范围内 Query OK, 1 row affected (0.00 sec) mysql> insert into test3(id) values(127); # 插入数值127,正确,在范围内 Query OK, 1 row affected (0.01 sec) mysql> insert into test3(id) values(128); # 插入数值128,错误,不在范围内 ERROR 1264 (22003): Out of range value for column ‘id‘ at row 1 mysql> select * from test3; +------+ | id | +------+ | -128 | | 127 | +------+ 2 rows in set (0.00 sec)
SQL_MODE中未开启严格模式,即SQL_MODE参数中不包含STRICT_TRANS_TABLES参数
-- 设置会话模式下sql_mode中不包含strit_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表(不加unsigned和zerofill) mysql> CREATE TABLE test33( -> id tinyint -> ); Query OK, 0 rows affected (0.00 sec)
-- PS -- id字段的类型为tinyint,其范围是-128至127,其显示长度是4,因为要显示符号("-") -- 查看test33表的表结构 mysql> desc test33; +-------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------+------+-----+---------+-------+ | id | tinyint(4) | YES | | NULL | | +-------+------------+------+-----+---------+-------+ 1 row in set (0.00 sec) -- 测试插入-128~127范围的整数和不在该范围内的整数 mysql> insert into test33(id) values(-129); Query OK, 1 row affected, 1 warning (0.00 sec) -- 不在范围内,插入未报错,因为sql_mode中没有开启严格模式 -- 插入到表中的不是-129,而是-128; mysql> insert into test33(id) values(-128); Query OK, 1 row affected (0.01 sec) -- 在范围内,插入不报错,插入的是多少就是多少 mysql> insert into test33(id) values(127); Query OK, 1 row affected (0.00 sec) -- 在范围内,插入不报错,插入的是多少就是多少 mysql> insert into test33(id) values(128); Query OK, 1 row affected, 1 warning (0.01 sec) -- 不在范围内,插入未报错,因为sql_mode中没有开启严格模式 -- 插入到表中的不是128,而是127; mysql> select * from test33; +------+ | id | +------+ | -128 | | -128 | | 127 | | 127 | +------+ 4 rows in set (0.00 sec)
标签:unsigned sql_mod 插入数据 表结构 str 引擎 字段 tran bsp
原文地址:https://www.cnblogs.com/chenliangc/p/11107091.html