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

Method example of safely getting deep objects of Object in Js

Table of contents Preface text parameter example ...

How to use vue.js to implement drag and drop function

Preface Adding drag and drop functionality is a g...

CSS sample code with search navigation bar

This article shows you how to use CSS to create a...

How to start a Java program in docker

Create a simple Spring boot web project Use the i...

Nginx dynamically forwards to upstream according to the path in the URL

In Nginx, there are some advanced scenarios where...

MySQL startup error InnoDB: Unable to lock/ibdata1 error

An error message appears when MySQL is started in...

Bug of Chinese input garbled characters in flex program Firefox

Chinese characters cannot be input in lower versio...

Docker deployment springboot project example analysis

This article mainly introduces the example analys...

Description of the execution mechanisms of static pages and dynamic pages

1. A static page means that there are only HTML ta...

How to migrate mysql storage location to a new disk

1. Prepare a new disk and format it with the same...

MySQL password is correct but cannot log in locally -1045

MySQL password is correct but cannot log in local...

Introduction to ufw firewall in Linux

Let's take a look at ufw (Uncomplicated Firew...

IE6 space bug fix method

Look at the code: Copy code The code is as follows...

Ansible automated operation and maintenance deployment method for Linux system

Ansible is a new automated operation and maintena...

How to set and get the number of Mysql connections

Get the number of connections --- Get the maximum...