我的约束外键在我的数据库上不起作用


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 中编码

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 records values(1, 1);

我在mysql 5.6中尝试过这个,插入失败:

Cannot add or update a child row: a foreign key constraint fails (`db_9_cd6353`.`records`, CONSTRAINT `fk` FOREIGN KEY (`StudentId`) REFERENCES `students` (`StudentId`))

主表中必须有您遗漏的相应数据。

最新更新