The following editor will bring you an article on how to check the foreign key constraints of MySQL closed subtables. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.
Preparation:
Define a teacher table and a student table; reference the teacher table in the student table ID
create table teachers(teacherID int not null auto_increment primary key,teacherName varchar(8)); create table students(studentID int not null auto_increment primary key,teacherID int not null,studentName varchar(8), constraint fk_students_teacherID foreign key (teacherId) references teachers(teacherId) on delete no action on update cascade);
Step one:
Insert a teacher
insert into teachers(teacherName) values('NameA');
insert into students(studentName,teacherID) values('NameB',100);--可以知道没有这个教师号、所以插入会出错。
Step 2:
set foreign_key_checks = 0; That’s it.insert into students(studentName,teacherID) values('NameB',100);
Step 3:
Set back to the default value and maintain foreign key constraints Prosecutor.set foreign_key_checks =1;
Summary:
The above is the detailed content of Detailed explanation of the foreign key constraint inspection method of closing child tables in MySQL (picture). For more information, please follow other related articles on the PHP Chinese website!