Prepare: Define a teacher table and a student table; reference the teacher table ID in the student table 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); first step: Insert a teacher insert into teachers(teacherName) values('NameA'); Insert a student: insert into students(studentName,teacherID) values('NameB',100);--You can know that there is no such teacher ID, so the insertion will fail. But is there any way to insert an unreasonable piece of data? There is still a way Step 2: set foreign_key_checks = 0; and that's it. insert into students(studentName,teacherID) values('NameB',100); Step 3: Set back to default value, keep foreign key constraint checking. set foreign_key_checks = 1; Summarize: This essay is very messy. The main point I want to make is that foreign key constraints are useless when set foreign_key_checks = 0;. At this time, you can insert into the child table that violates the foreign key constraint. Don't use this unless absolutely necessary. The above article about MySQL foreign key constraint checking method for closing child tables is all I want to share with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. You may also be interested in:
|
<<: MySQL foreign key constraint disable and enable commands
>>: Detailed introduction to nobody user and nologin in Unix/Linux system
When developing a web project, you need to instal...
background Getting the slow query log from mysql....
Table of contents 01 Container consistency 02 Con...
This article shares the specific code of JavaScri...
Table of contents Preface 1. Introduction to Axio...
Problem: The PHP program on one server cannot con...
A simple cool effect achieved with CSS3 animation...
1. Still use PHP script to execute. Command line ...
Table of contents Why do we need Docker? Docker d...
background nginx-kafka-module is a plug-in for ng...
Doccer Introduction: Docker is a container-relate...
I joined a new company these two days. The compan...
In fact, it is very simple to encapsulate axios i...
Docker view process, memory, cup consumption Star...
After a lot of trouble, I finally figured out the...