将int字段改为decimal类型时出现1264 Out of range value的问题分析和处理
来源:互联网 发布:水公主为什么离开网络 编辑:程序博客网 时间:2024/05/22 03:03
有开发同事在验证环境对某个表将int字段改成decimal(10,4)后,提示 1264 Out of range value报错,咨询dba这个为什么会报错,对数据有什么影响?
我收到这问题后,第一反应就是由于字段类型转换,长度不够发生了数据溢出。
于是,下面模拟该问题进行测试:
1.建测试表
CREATE TABLE `zeng` (
`_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
`a` int(32) DEFAULT NULL,
PRIMARY KEY (`_id`)
) ENGINE=InnoDB AUTO_INCREMENT=28129 DEFAULT CHARSET=utf8 ;
2.准备测试用数据
insert into zeng(a) values (70000);
insert into zeng(a) values (800000);
insert into zeng(a) values (900000);
insert into zeng(a) values (9000000);
insert into zeng(a) values (99999999);
3.修改字段类型
alter table zeng modify column a decimal(10,4) unsigned;
dba@192.168.74.60:3306 : >alter table zeng modify column a decimal(10,4) unsigned;
Query OK, 5 rows affected, 2 warnings (0.05 sec)
Records: 5 Duplicates: 0 Warnings: 2
4.查看warnings
dba@192.168.74.60:3306 >show warnings;
+---------+------+--------------------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------------------+
| Warning | 1264 | Out of range value for column 'a' at row 4 |
| Warning | 1264 | Out of range value for column 'a' at row 5 |
+---------+------+--------------------------------------------+
2 rows in set (0.00 sec)
5.查询类型更改后的数据
dba@192.168.74.60:3306:>select * from zeng;
+-------+-------------+
| _id | a |
+-------+-------------+
| 28129 | 70000.0000 |
| 28130 | 800000.0000 |
| 28131 | 900000.0000 |
| 28132 | 999999.9999 |
| 28133 | 999999.9999 |
+-------+-------------+
5 rows in set (0.00 sec)
第四和第五行的a字段记录发生了溢出。
decimal(10,4)的最大值是999999.9999,decimal(10,4)中的10表示全部位数为10(整数位+小数位),4表示小数位,70000能用decimal(10,4)来表示,表示为70000.0000。
show warnings看到out of range表示超过了这个值 ,建议生产环境在对一个表更改字段前,先对这个表的数据create table as select 来备份一份,若出现out of range时,
该记录的数据也全部变成了9。在更改前可以查询字段a值的最大长度:
select max(length(a)) from zeng;
已确保decimal(M,N)中M-N>=max(length(a))才不会产生溢出。
- 将int字段改为decimal类型时出现1264 Out of range value的问题分析和处理
- 将int字段改为decimal类型后出现1264 Out of range value的问题分析和处理之二
- Java中long类型直接赋值出现The literal xxxxxxxxxx of type int is out of range 问题的原因和解决方法
- 插入数据库 出现 Data truncation: Out of range value adjusted for column 'id'....的问题
- h2 DB的一个问题,关于类型--Numeric value out of range
- MySQL BIGINT UNSIGNED value is out of range... 的问题和解决
- mysql字段类型decimal和int类型运算,结果精度问题
- An out-of-range value
- mysql报错:out of range value for column问题
- [MySQL学习]STRICT_ALL_TABLES对应的OUT of RANGE VALUE FOR COLUMN和DATA truncated FOR COLUMN
- Mysql常见的两个报错信息Date too long和Out of range value
- BIGINT UNSIGNED value is out of range in..的解决方法
- 手机字段存储报错 :Warning Code : 1264 Out of range value for column 'buyer_tpl' at row 1
- 手机字段存储报错 :Warning Code : 1264 Out of range value for column 'buyer_tpl' at row 1
- 手机字段存储报错 :Warning Code : 1264 Out of range value for column ‘buyer_tpl’ at row 1
- MySQL5错误 #1264 "Out of range value adjusted for column ..."
- #1264 - Out of range value adjusted for column 'ID 【转】
- Oracle 将普通字段类型改为Clob类型的方法
- 运维开发详解
- Linux系统编程第一课: CentOS7下who命令的实现
- 2016暴利项目 蜜桃影院APP源码开源版分享
- mysql存储过程递归删除外键关联表
- ZeroMQ(java)之Router与Dealer运行原理
- 将int字段改为decimal类型时出现1264 Out of range value的问题分析和处理
- iOS cell高度自适应 - 教你写出优雅的table view
- TextView实现时钟效果
- 最短路径算法—Dijkstra(迪杰斯特拉)算法分析与实现(C/C++)及其他(待写)
- WKWebView JS互调(高级篇)
- 用Node.js实现Restful风格webservice
- REST简介
- mac office 2016 破解资源与攻略
- 理解RESTful架构