以下 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", "1", "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实例之间的不同行为。
SQL_MODE
变量的值。请参见db-fiddle。 - wchiquito