How to disable foreign key constraint checking in MySQL child tables

How to disable foreign key constraint checking in MySQL child tables

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:
  • Specific method to add foreign key constraints in mysql
  • MySQL foreign key constraint (FOREIGN KEY) case explanation
  • Summary of MySQL foreign key constraints and table relationships
  • Example explanation of MySQL foreign key constraints
  • Detailed explanation of MySQL foreign key constraints
  • Introduction to MySQL method of deleting table data with foreign key constraints
  • Simple implementation of ignoring foreign key constraints when deleting MySQL tables
  • How to create and delete foreign key constraints in MySQL

<<:  MySQL foreign key constraint disable and enable commands

>>:  Detailed introduction to nobody user and nologin in Unix/Linux system

Recommend

MySQL slow_log table cannot be modified to innodb engine detailed explanation

background Getting the slow query log from mysql....

Detailed explanation of the concept of docker container layers

Table of contents 01 Container consistency 02 Con...

JavaScript to achieve lottery effect

This article shares the specific code of JavaScri...

Axios project with 77.9K GitHub repository: What are the things worth learning?

Table of contents Preface 1. Introduction to Axio...

Sample code for cool breathing effect using CSS3+JavaScript

A simple cool effect achieved with CSS3 animation...

Linux Cron scheduled execution of PHP code with parameters

1. Still use PHP script to execute. Command line ...

How to deploy Go web applications using Docker

Table of contents Why do we need Docker? Docker d...

Example of integrating Kafka with Nginx

background nginx-kafka-module is a plug-in for ng...

Docker Getting Started Installation Tutorial (Beginner Edition)

Doccer Introduction: Docker is a container-relate...

Solution to the problem of not finding Tomcat configuration in Intelli Idea

I joined a new company these two days. The compan...

How to encapsulate axios request with vue

In fact, it is very simple to encapsulate axios i...

Docker View Process, Memory, and Cup Consumption

Docker view process, memory, cup consumption Star...