【发布时间】:2020-02-17 08:41:59
【问题描述】:
create table students (StudentId int primary key not null);
create table records (
RecordId int primary key not null,
StudentId int not null,
constraint fk foreign key(StudentId) references students(StudentId));
insert into students values (201810696);
Query OK, 1 row affected (0.120 sec)
insert into records values (1011, 20181313);
Query OK, 1 row affected (0.004 sec)
为什么它继续接受表学生中不存在的值?我已经在 phpmyadmin localhost 操作中将我的两个表上的存储引擎设置为 innoDb
顺便说一句,我正在使用最新版本的 wampp 并在 cmd 中编码
【问题讨论】:
-
哪个数据库引擎?
-
旧版本的 Innodb 不是默默地忽略 fk 的吗?
-
我已将其设置为 innoDb
-
我认为您应该仔细检查。使用
SHOW CREATE TABLE students\G和SHOW CREATE TABLE records\G,您将看到各自的ENGINE。 -
另一种可能性:
select @@foreign_key_checks;确认您已启用 FK 的强制执行。如果启用,这将返回1,否则返回0。
标签: mysql sql foreign-keys innodb