mysql alter table if_MySQL 5.7.18:外键约束和ALTER TABLE CHANGE COLUMN从NULL到NOT NULL

以下sql脚本适用于MySQL 5.16.17及更早版本,但不适用于我的MysqL 5.7.18安装之一(另一个,在Docker容器中启动的MysqL 5.7.18也可以)

drop table if exists bar;

drop table if exists foo;

create table foo (foo_id int not null primary key,description varchar(32));

insert into foo values ("1","foo-one");

insert into foo values ("2","foo-two");

create table bar (bar_id int not null primary key,foo_id int null,description varchar(32),foreign key (foo_id) references foo(foo_id));

insert into bar values ("1","1","bar-one");

insert into bar values ("2","bar-two");

alter table bar change column foo_id foo_id int not null;

错误消息是:

Error Code: 1832. Cannot change column 'foo_id': used in a foreign key constraint 'bar_ibfk_1'

问题似乎是将外键约束从NULL更改为NOT NULL.

我知道我可以将最后一个语句包装在“SET foreign_key_checks …”调用中,但我感兴趣的是在这种情况下是否有任何系统变量或配置设置会影响MysqL的行为,因为我无法解释两个5.7.18实例之间的不同行为.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值