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 Introduction to Anaconda 1. Dow...
The installation of compressed packages has chang...
iOS 1. URL scheme This solution is basically for ...
First create a directory cd /etc/nginx mkdir ssl ...
1. To download the MySQL database, visit the offi...
Usage scenarios For existing servers A and B, if ...
The specific code is as follows: /*Scroll bar wid...
Prerequisite: Mac, zsh installed, mysql downloade...
This article shares the specific code of JavaScri...
Copy code The code is as follows: <style type=...
JSON is a lightweight data exchange format that u...
Recently I've been working on a framework tha...
There are two versions of MySQL database manageme...
This article shares the specific code of writing ...
MySQL downloads for all platforms are available a...