有关Mysql数据截断问题的处理方法
测试数据:
有一个表aritcle和另一个表article_comment,关联是article的id。
创建测试表及添加测试数据:
表1:
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
表2:
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`text` varchar(200) NOT NULL,
`article_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `art_id` (`article_id`),
CONSTRAINT `art_id` FOREIGN KEY (`article_id`) REFERENCES `article` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB;
添加测试数据:
insert into article values(12345678901,'name1');
insert into article_comment(text,article_id) values('text1',12345678901);
insert into article_comment(text,article_id) values('text2',12345678902);
查看数据:
1,article表
4294967295 name1
2,article_comment表
1 text1 4294967295
2 text2 4294967295
可以看出,本来第二个插入的评论想关联另一个文章,但是却关联到了第一篇文章,这是因为Mysql的Data Truncation截断的原因。
show warnings显示
Warning | 1265 | Data truncated for column 'article_id' at row 1
造成:
1,评论关联到错误的文章
2,同一篇文章关联到许多的评论(这会造成性能问题)
解决方法:
insert into article_comment(text,article_id) values('text1',12345678903);
这会报错:
[SQL] insert into article_comment(text,article_id) values('text1',12345678903);
[Err] 1264 - Out of range value for column 'article_id' at row 1
如此,便避免了上述问题的出现。
您可能感兴趣的文章:
有关Mysql数据截断问题的处理方法
mysql查询与插入数据单引号的问题分析
php截取中文字符串乱码如何解决呢
php程序员面试题及答案(简单型)
mysql中limit、order by和group by的用法实例解析
mysqld_multi配置MySQL多实例关闭不了
解决Lost connection to MySQL server during query错误方法
php分割GBK中文乱码的解决方法
db2和mysql的区别是什么
linux下mysql服务无法启动