Home > Database > Mysql Tutorial > Detailed explanation of the foreign key constraint inspection method of closing child tables in MySQL (picture)

Detailed explanation of the foreign key constraint inspection method of closing child tables in MySQL (picture)

黄舟
Release: 2017-03-18 14:01:26
Original
1440 people have browsed it

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);
Copy after login

Step one:

Insert a teacher

insert into teachers(teacherName) values('NameA');
Copy after login

Detailed explanation of the foreign key constraint inspection method of closing child tables in MySQL (picture)

##Insert a student:

insert into students(studentName,teacherID) values('NameB',100);--可以知道没有这个教师号、所以插入会出错。
Copy after login

Detailed explanation of the foreign key constraint inspection method of closing child tables in MySQL (picture)

But is there a way to insert an unreasonable piece of data? There are still ways

Step 2:

set foreign_key_checks = 0; That’s it.

insert into students(studentName,teacherID) values('NameB',100);
Copy after login

Detailed explanation of the foreign key constraint inspection method of closing child tables in MySQL (picture)

Step 3:

Set back to the default value and maintain foreign key constraints Prosecutor.

set foreign_key_checks =1;
Copy after login

Summary:

This essay is very messy. The main point I want to say is set foreign_key_checks =0; Foreign key constraints are of no use. At this time, inserts into the subtable that violate foreign key constraints can be performed.

Don’t use this unless absolutely necessary.

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template