标签:sql_mod 包含 mysql image bsp 范围 for value code
一、bigint类型说明
id bigint(M) [UNSIGNED] [ZEROFILL] 字段名 字段类型(显示长度,创建表时不指定长度) [无符号] [前导填充] unsigned 01:bigint(M)后面加上unsigned,就是无符号,只可插入正整数,其bigint的范围是 0至18446744073709551615; 02:8字节就是64位,那么2的64次方减1就是18446744073709551615; 03:因为加了unsigned就是正整数,也就是无符号,所以范围:0至18446744073709551615; 04:18446744073709551615长度是20;不指定bigint长度,例如:bigint unsigned,显示 长度就是bigint(20),当插入的正整数长度达不到长度20时,不会自动补充,也就1显示1; 18446744073709551615显示的就是18446744073709551615;
zerofill 01:bigint(M)后面加上zerofill参数,会把unsigned带上,也就是无符号,只可插入正整数, 其范围是0至18446744073709551615; 02:8字节就是64位,那么2的64次方减1就是18446744073709551615; 03:因为会把unsigned带上,所以就是正整数,也就是无符号,范围是:0至18446744073709551615; 04:18446744073709551615长度是20,不指定长度时,例如:bigint zerofill,其显示 长度就是20,当你插入的正整数够不够达不到长度20时,都前导0填,如下所示: 1显示00000000000000000001;18446744073709551615显示18446744073709551615;
不加unsigned和zerofill 不在bigint后加上unsigned或zerofill,其默认是有符号,也就可以插入正整数和负整数, 且负整数和正整数的最大数的长度是20,但是因为要显示符号("-"),所以长度是bigint(21), 虽然显示长度是21,但对于插入的整数长度达不到长度21时,不会自动前导零填充;
特别说明 01:在为字段指定类型时,一般不指定数据类型的长度,例如:bigint或bigint 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 bigint unsigned -> ); Query OK, 0 rows affected (0.01 sec) -- PS -- id字段类型是bigint,且加上了unsigned,其范围是0至18446744073709551615; -- 显示的长度是bigint(20),因为18446744073709551615的长度是20 -- 查看test1表的表结构 mysql> desc test1; +-------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+-------+ | id | bigint(20) unsigned | YES | | NULL | | +-------+---------------------+------+-----+---------+-------+ 1 row in set (0.00 sec) -- 测试插入0~18446744073709551615范围的整数和不在该范围内的整数 mysql> insert into test1(id) values(-1); # 错误,不在范围内 ERROR 1264 (22003): Out of range value for column ‘id‘ at row 1 mysql> insert into test1(id) values(0); # 正确,在范围内 Query OK, 1 row affected (0.00 sec) mysql> insert into test1(id) values(18446744073709551615); # 正确,在范围内 Query OK, 1 row affected (0.01 sec) mysql> insert into test1(id) values(18446744073709551616); # 错误,不在范围内 ERROR 1264 (22003): Out of range value for column ‘id‘ at row 1 mysql> select * from test1; +----------------------+ | id | +----------------------+ | 0 | | 18446744073709551615 | +----------------------+ 2 rows in set (0.00 sec)
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 bigint unsigned -> ); Query OK, 0 rows affected (0.00 sec) -- PS -- id字段类型是bigint,且加上了unsigned,其范围是0至18446744073709551615; -- 显示的长度是bigint(20),因为18446744073709551615的长度是20 -- 查看test11表的表结构 mysql> desc test11; +-------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+-------+ | id | bigint(20) unsigned | YES | | NULL | | +-------+---------------------+------+-----+---------+-------+ 1 row in set (0.00 sec) -- 测试插入0~18446744073709551615范围的整数和不在该范围内的整数 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.00 sec) -- 在范围内,不报错,插入的是什么就是什么 mysql> insert into test11(id) values(18446744073709551615); Query OK, 1 row affected (0.01 sec) -- 在范围内,不报错,插入的是什么就是什么 mysql> insert into test11(id) values(18446744073709551616); Query OK, 1 row affected, 1 warning (0.00 sec) -- 不在范围内,但未报错(因为sql_mode中未开启严格模式) -- 插入到表中的数据不是18446744073709551616,而是18446744073709551615 mysql> select * from test11; +----------------------+ | id | +----------------------+ | 0 | | 0 | | 18446744073709551615 | | 18446744073709551615 | +----------------------+ 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参数,也就是前导0填充) mysql> create table if not exists test2( -> id bigint zerofill -> ); Query OK, 0 rows affected (0.01 sec) -- PS -- id字段的类型为bigint zerofill参数,会把unsigned参数也带上,范围是0至 -- 18446744073709551615; -- 其显示长度是bigint(20),因为18446744073709551615的长度是20 -- 查看test2表的表结构 mysql> desc test2; +-------+------------------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------------------+------+-----+---------+-------+ | id | bigint(20) unsigned zerofill | YES | | NULL | | +-------+------------------------------+------+-----+---------+-------+ 1 row in set (0.01 sec) -- 测试插入0~18446744073709551615范围的整数和不在该范围内的整数 mysql> insert into test2(id) values(-1); # 错误,不在范围内 ERROR 1264 (22003): Out of range value for column ‘id‘ at row 1 mysql> insert into test2(id) values(0); # 正确,在范围内 Query OK, 1 row affected (0.00 sec) mysql> insert into test2(id) values(18446744073709551615); # 正确,在范围内 Query OK, 1 row affected (0.01 sec) mysql> insert into test2(id) values(18446744073709551616); # 错误,不在范围内 ERROR 1264 (22003): Out of range value for column ‘id‘ at row 1 mysql> select * from test2; +----------------------+ | id | +----------------------+ | 00000000000000000000 | | 18446744073709551615 | +----------------------+ 2 rows in set (0.00 sec)
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参数,也就是前导0填充) mysql> create table if not exists test22( -> id bigint zerofill -> ); Query OK, 0 rows affected (0.00 sec) -- PS -- id字段的类型为bigint zerofill参数,会把unsigned参数也带上,范围是0至 -- 18446744073709551615; -- 其显示长度是bigint(20),因为18446744073709551615的长度是20 -- 查看test22表的表结构 mysql> desc test22; +-------+------------------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------------------+------+-----+---------+-------+ | id | bigint(20) unsigned zerofill | YES | | NULL | | +-------+------------------------------+------+-----+---------+-------+ 1 row in set (0.00 sec) -- 测试插入0~18446744073709551615范围的整数和不在该范围内的整数 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.00 sec) -- 在范围内,不会报错,插入的数据昌0,因zerofill参数,但会前导零填充 mysql> insert into test22(id) values(18446744073709551615); Query OK, 1 row affected (0.00 sec) -- 在范围内,不会报错,插入的数据是最大值了,所以不会前导零填充 mysql> insert into test22(id) values(18446744073709551616); Query OK, 1 row affected, 1 warning (0.00 sec) -- 不在范围内,但未报错(因为sql_mode中未开启严格模式) -- 插入到表中的数据不是18446744073709551616,而是18446744073709551615 mysql> select * from test22; +----------------------+ | id | +----------------------+ | 00000000000000000000 | | 00000000000000000000 | | 18446744073709551615 | | 18446744073709551615 | +----------------------+ 4 rows in set (0.00 sec)
五、测试3【不加unsigned和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) -- 创建表test3(不指定unsigned和zerofill参数,也就是有符号) mysql> create table if not exists test3( -> id bigint -> ); Query OK, 0 rows affected (0.00 sec) -- PS -- id字段的类型是bigint,没有加unsigned和zerofill参数控制,其范围 -- 是:-9223372036854775808至9223372036854775807 -- 其显示长度是bigint(21),因为要显示符号("-"),所以是21; -- 查看test3表的表结构 mysql> desc test3; +-------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------+------+-----+---------+-------+ | id | bigint(21) | YES | | NULL | | +-------+------------+------+-----+---------+-------+ 1 row in set (0.00 sec) -- 测试插入-9223372036854775808至9223372036854775807范围的整数和不在该范围内的整数 mysql> insert into test3(id) values(-9223372036854775809); # 错误,不在范围内 ERROR 1264 (22003): Out of range value for column ‘id‘ at row 1 mysql> insert into test3(id) values(-9223372036854775808); # 正确,在范围内 Query OK, 1 row affected (0.00 sec) mysql> insert into test3(id) values(9223372036854775807); # 正确,在范围内 Query OK, 1 row affected (0.00 sec) mysql> insert into test3(id) values(9223372036854775808); # 错误,不在范围内 ERROR 1264 (22003): Out of range value for column ‘id‘ at row 1 mysql> select * from test3; +----------------------+ | id | +----------------------+ | -9223372036854775808 | | 9223372036854775807 | +----------------------+ 2 rows in set (0.00 sec)
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表(不指定unsigned和zerofill参数) mysql> create table if not exists test33( -> id bigint -> ); Query OK, 0 rows affected (0.01 sec) -- PS -- id字段的类型是bigint,没有加unsigned和zerofill参数控制,其范围 -- 是:-9223372036854775808至9223372036854775807 -- 其显示长度是bigint(21),因为要显示符号("-"),所以是21; -- 查看test33表的表结构 mysql> desc test33; +-------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------+------+-----+---------+-------+ | id | bigint(20) | YES | | NULL | | +-------+------------+------+-----+---------+-------+ 1 row in set (0.00 sec) -- 测试插入-9223372036854775808至9223372036854775807范围的整数和不在该范围内的整数 mysql> insert into test33(id) values(-9223372036854775809); Query OK, 1 row affected, 1 warning (0.00 sec) -- 不在范围内,但未报错(因为sql_mode中没有开启严格模式) -- 插入到表中数据不是-9223372036854775809,而是-9223372036854775808 mysql> insert into test33(id) values(-9223372036854775808); Query OK, 1 row affected (0.00 sec) -- 在范围内,不报错,插入的是什么就是什么 mysql> insert into test33(id) values(9223372036854775807); Query OK, 1 row affected (0.01 sec) -- 在范围内,不报错,插入的是什么就是什么 mysql> insert into test33(id) values(9223372036854775808); Query OK, 1 row affected, 1 warning (0.00 sec) -- 不在范围内,但未报错(因为sql_mode中没有开启严格模式) -- 插入到表中数据不是9223372036854775808,而是9223372036854775807 mysql> select * from test33; +----------------------+ | id | +----------------------+ | -9223372036854775808 | | -9223372036854775808 | | 9223372036854775807 | | 9223372036854775807 | +----------------------+ 4 rows in set (0.00 sec)
标签:sql_mod 包含 mysql image bsp 范围 for value code
原文地址:https://www.cnblogs.com/chenliangc/p/11610093.html