While retrieving foreign keys information from MySQL (for a code generator) I noticed this strange behavior. It looks like a major bug in MySQL 8.x. When I create a foreign key using REFERENCES the engine does not enforce it. For example:
create table p (
id int primary key not null
) engine=innodb;
create table q (
pid int references p (id)
) engine=innodb;
insert into q (pid) values (123); -- succeeds (!)
See example at DB Fiddle.
However, if I create the foreign key typing FOREIGN KEY (col) REFERENCES table (col) it works properly:
create table p (
id int primary key not null
) engine=innodb;
create table r (
pid int,
foreign key (pid) references p (id)
) engine=innodb;
insert into r (pid) values (456); -- fails, as expected
See running example at DB Fiddle.
If this is a bona fide major bug in MySQL, is there any way of disabling the bad syntax?
Note: I just verified that MariaDB presented the same bug until 10.4, but it seems it's fixed in 10.5.