Introduction to MySQL method of deleting table data with foreign key constraints

Introduction to MySQL method of deleting table data with foreign key constraints

When deleting a table or a piece of data in MySQL,

[Err] 1451 -Cannot delete or update a parent row:
 aforeignkeyconstraintfails(...)

This is because a foreign key association is set in MySQL, which makes it impossible to update or delete data. This can be avoided by setting the FOREIGN_KEY_CHECKS variable.

To disable foreign key constraints, we can use:

SETFOREIGN_KEY_CHECKS=0;

Then delete the data

To enable foreign key constraints, we can use:

SETFOREIGN_KEY_CHECKS=1;

To view the current value of FOREIGN_KEY_CHECKS, use the following command:

SELECT @@FOREIGN_KEY_CHECKS;

Summarize

The above is all the content of this article about how to delete table data with foreign key constraints in MySQL. I hope it will be helpful to everyone. Interested friends can refer to: How to delete table data in MySQL, Analysis of MySQL variable declaration and stored procedure, Several important MySQL variables, Detailed explanation of MySQL prepare principle, etc. If you have any questions, please leave a message at any time and the editor will reply to you in time.

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
  • Simple implementation of ignoring foreign key constraints when deleting MySQL tables
  • How to disable foreign key constraint checking in MySQL child tables
  • How to create and delete foreign key constraints in MySQL

<<:  A Preliminary Study on Vue Unit Testing

>>:  How to increase your web performance by 3 times by turning on a parameter in Nginx

Recommend

MySQL string splitting example (string extraction without separator)

String extraction without delimiters Question Req...

Three ways to prevent MySQL from inserting duplicate data

Create a new table CREATE TABLE `person` ( `id` i...

Steps to export the fields and related attributes of MySQL tables

Need to export the fields and properties of the t...

Summary of various postures of MySQL privilege escalation

Table of contents 1. Write Webshell into outfile ...

JavaScript to achieve fixed sidebar

Use javascript to implement a fixed sidebar, for ...

Using cursor loop to read temporary table in Mysql stored procedure

cursor A cursor is a method used to view or proce...

In-depth explanation of binlog in MySQL 8.0

1 Introduction Binary log records SQL statements ...

Solution to the timeout problem when installing docker-compose with PIP

1: Installation command pip install docker-compos...

Detailed explanation of MySQL master-slave database construction method

This article describes how to build a MySQL maste...

Summary of knowledge points about events module in Node.js

Through the study and application of Node, we kno...

Simple web page code used in NetEase blog

How to use the code in NetEase Blog: First log in...

What qualities should a good advertisement have?

Some people say that doing advertising is like bei...

Docker connection mongodb implementation process and code examples

After the container is started Log in to admin fi...

Detailed steps to install xml extension in php under linux

Installing XML extension in PHP Linux 1. Enter th...