update 表中DECIMAL字段报ERROR 1264 (22003): Out of range value for column

【 使用环境 】生产环境
【 OB or 其他组件 】OB
【 使用版本 】5.7.25-OceanBase_CE-v4.1.0.0
【问题描述】
update 表中DECIMAL字段报ERROR 1264 (22003): Out of range value for column
【复现路径】
CREATE TABLE TEST (
ID varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
NAME varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
NUM decimal(2,2) DEFAULT NULL,
PRIMARY KEY (ID)
);

insert into test (id,name)
select ‘01’ as id,
‘测试1’ as name
union all
select ‘02’ as id,
‘测试2’ as name;

执行:
update oa.test set NUM = 1 where id = ‘01’
报 ERROR 1264 (22003): Out of range value for column ‘NUM’ at row 1

这个是符合预期的。

decimal(2, 2) 中第一个 2 的含义是说这个 decimal 整数位+小数位一共最多有两位有效数字,第二个 2 的含义是说这个 decimal 小数点儿后最多有两位有效数字,这样整数位最多就只有 2 - 2 = 0 位有效数字。所以这个 decimal(2, 2) 只能存绝对值小于 1 的小数,不能有整数位的。详见:https://www.oceanbase.com/docs/common-oceanbase-database-cn-1000000000222527

以下是原生 mysql 的行为:

mysql> create table t1(c1 decimal(2, 2));
Query OK, 0 rows affected (0.02 sec)

mysql> insert into t1 values(0.12);
Query OK, 1 row affected (0.01 sec)

mysql> insert into t1 values(1);
ERROR 1264 (22003): Out of range value for column 'c1' at row 1
1 个赞