首页 > 代码库 > pt-online-schema-change和默认值关系

pt-online-schema-change和默认值关系

在使用pt-online-schema-change会遇到如下的错误导致表修改失败:

Copying rows caused a MySQL error 1364,Message: Field ‘XXX‘ doesn‘t have a default value

 

例子1:

#修改的SQL:ALTER TABLE `live_comment` ADD `sourcefrom` VARCHAR(15) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT ‘来源标志‘ AFTER `ip`, ADD `type` INT UNSIGNED NOT NULL DEFAULT ‘0‘ COMMENT ‘评论类型‘ AFTER `sourcefrom`, ADD INDEX (`type`) 

错误信息为:

`sportslive`.`live_comment` was not altered.Error copying rows from `sportslive`.`live_comment` to `sportslive`.`_live_comment_new`: Copying rows caused a MySQL error 1364:    Level: Warning     Code: 1364  Message: Field ‘sourcefrom‘ doesn‘t have a default value

对应的表结构为:

CREATE TABLE `sportslive`.`_live_comment_new` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `live_id` int(11) NOT NULL DEFAULT ‘0‘,  `content` varchar(200) NOT NULL DEFAULT ‘‘,  `reply_uid` bigint(20) NOT NULL DEFAULT ‘0‘,  `reply_username` varchar(30) NOT NULL DEFAULT ‘‘,  `reply_content` varchar(200) NOT NULL DEFAULT ‘‘,  `reply_profile_pic` varchar(100) NOT NULL DEFAULT ‘‘,  `parent_id` int(11) NOT NULL,  `uid` bigint(20) NOT NULL DEFAULT ‘0‘,  `username` varchar(30) NOT NULL DEFAULT ‘‘,  `profile_pic` varchar(100) NOT NULL DEFAULT ‘‘,  `addtime` datetime NOT NULL,  `ip` varchar(15) NOT NULL DEFAULT ‘‘,  `prior` tinyint(4) NOT NULL DEFAULT ‘0‘ COMMENT ‘xxxx‘,  PRIMARY KEY (`id`),  KEY `live_id` (`live_id`)) ENGINE=InnoDB AUTO_INCREMENT=296173 DEFAULT CHARSET=utf8

 

例子2:

修改的SQL:

ALTER TABLE `mb_post_spider_1209` CHANGE `category` `category` VARCHAR(8) NOT NULL COMMENT ‘‘, CHANGE `reposts_count` `reposts_count` INT(11) UNSIGNED NOT NULL DEFAULT ‘0‘ COMMENT ‘‘, CHANGE `heat` `heat` DECIMAL(8,5) UNSIGNED NOT NULL DEFAULT ‘0.00000‘ COMMENT ‘‘, CHANGE `spider_date` `spider_date` INT(8) UNSIGNED NOT NULL DEFAULT ‘0‘ COMMENT ‘抓取时间‘, CHANGE `main_cat` `main_cat` TINYINT(1) NOT NULL COMMENT ‘‘, CHANGE `sub_cat` `sub_cat` TINYINT(1) NOT NULL COMMENT ‘‘, CHANGE `state` `state` TINYINT(1) UNSIGNED NOT NULL DEFAULT ‘0‘ COMMENT ‘‘, CHANGE `bid` `bid` BIGINT(20) UNSIGNED NOT NULL DEFAULT ‘0‘ COMMENT ‘‘, CHANGE `bname` `bname` VARCHAR(32) NOT NULL COMMENT ‘‘

Warning信息如下:

Error copying rows from `mb_content`.`mb_post_spider_1209` to `mb_content`.`_mb_post_spider_1209_new`: Copying rows caused a MySQL error 1048:    Level: Warning     Code: 1048  Message: Column ‘bname‘ cannot be null

http://www.mysqlperformanceblog.com/2012/06/21/pt-online-schema-change-and-default-values/