测试环境:
MySQL 5.5.14/MySQL 5.6.36

测试脚本:

create table tb001(id int primary key,c1 int);
alter table tb001 modify id int null;
show create table tb001;

建表语句为:

CREATE TABLE `tb001` (
  `id` int(11) NOT NULL DEFAULT '0',
  `c1` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

上面ALTER命令想要将表主键ID列修改为null,执行完成未报错,但未修改成功。

查看BINLOG日志发现:

# at 696251545
#190326 10:51:55 server id 2817457  end_log_pos 696251653 	Query	thread_id=9585927	exec_time=0	error_code=0
SET TIMESTAMP=1553568715/*!*/;
create table tb001(id int primary key,c1 int)
/*!*/;
# at 696251653
#190326 10:52:00 server id 2817457  end_log_pos 696251752 	Query	thread_id=9585927	exec_time=0	error_code=0
SET TIMESTAMP=1553568720/*!*/;
alter table tb001 modify id int null
/*!*/;

发现该命令被记录到BINLOG日志中。

 

上面代码在MySQL 5.7版本中执行,会报错:

错误代码: 1171
All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead

 

如果主库为MySQL 5.5或MySQL 5.6,而从库为MySQL 5.7,上面操作会导致主从复制异常。

相关文章:

  • 2022-12-23
  • 2021-07-28
  • 2022-12-23
  • 2022-12-23
  • 2021-09-28
  • 2022-02-07
  • 2021-10-03
  • 2021-05-31
猜你喜欢
  • 2021-08-05
  • 2022-12-23
  • 2022-01-08
  • 2022-02-01
  • 2021-12-31
  • 2022-12-23
相关资源
相似解决方案