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
Table of contents Preface text parameter example ...
Preface Adding drag and drop functionality is a g...
This article shows you how to use CSS to create a...
Create a simple Spring boot web project Use the i...
In Nginx, there are some advanced scenarios where...
An error message appears when MySQL is started in...
Chinese characters cannot be input in lower versio...
This article mainly introduces the example analys...
1. A static page means that there are only HTML ta...
1. Prepare a new disk and format it with the same...
MySQL password is correct but cannot log in local...
Let's take a look at ufw (Uncomplicated Firew...
Look at the code: Copy code The code is as follows...
Ansible is a new automated operation and maintena...
Get the number of connections --- Get the maximum...