2
以下SQL腳本適用於MySQL 5.16.17及更早版本,但不適用於我的MySQL 5.7.18安裝中的一個(另一個一個時,MySQL 5.7.18在泊塢窗容器推出,是OK以及)MySQL 5.7.18:外鍵約束和ALTER TABLE CHANGE COLUMN從NULL到NOT NULL
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](https://www.db-fiddle.com/f/xtPue5iZnMoHhR5S7cBQKZ/2)。 – wchiquito
的sql_mode在5.7.18本地安裝: NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION 的sql_mode在5.7.18泊塢容器: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION (是的,我禁用了本地STRICT_TRANS_TABLES看看這是否有任何效果,但無濟於事) – ndeuma
使用db-fiddle時,代碼片斷失敗,並且使用5.6和5.7運行。有趣... – ndeuma