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

Docker uses the nsenter tool to enter the container

When using Docker containers, it is more convenie...

Five ways to achieve automatic page jump in HTML

In the previous article, we introduced three comm...

The use of v-model in vue3 components and in-depth explanation

Table of contents Use two-way binding data in v-m...

Use Docker to run multiple PHP versions on the server

PHP7 has been out for quite some time, and it is ...

How to build a DHCP server in Linux

Table of contents 1. Basic knowledge: 2. DHCP ser...

How to create a table in mysql and add field comments

Directly post code and examples #Write comments w...

Implementation of Vue counter

Table of contents 1. Implementation of counter 2....

The difference between key and index in MySQL

Let's look at the code first: ALTER TABLE rep...

JS implementation of carousel example

This article shares the specific code of JS to im...

How InnoDB cleverly implements transaction isolation levels

Preface In the previous article Detailed Explanat...

Vue el-date-picker dynamic limit time range case detailed explanation

There are two situations 1. Start time and end ti...

JS implements request dispatcher

Table of contents Abstraction and reuse Serial Se...

Vue globally introduces scss (mixin)

Table of contents 1. mixin.scss 2. Single file us...

Example code of how to create a collapsed header effect using only CSS

Collapsed headers are a great solution for displa...