Disable foreign key constraints in MySQL
In this article we will learn how to use FOREIGN_KEY_CHECKS in MySQL Workbench to temporarily turn off foreign key constraints in MySQL.
Disable foreign key constraints in MySQL
There are many situations where we temporarily turn off foreign keys. For example, loading data into parent and child tables in an arbitrary order.
In this case, we can use FOREIGN_KEY_CHECKS to turn off foreign key constraints in MySQL Server. To understand this, let's first create two tables and populate them.
Sample code:
# create a `student` table
CREATE TABLE student(
student_id INT NOT NULL PRIMARY KEY,
student_name VARCHAR(255) NOT NULL
);
# create a `course` table
CREATE TABLE course(
course_id INT NOT NULL PRIMARY KEY,
course_name VARCHAR(255),
student_id INT,
FOREIGN KEY(student_id)
REFERENCES student(student_id)
);
Example code:
# insert data into the `student` table
INSERT INTO student(student_id, student_name)
VALUES
(1, 'Maryam Taymor'),
(2, 'Mehvish Ashiq'),
(3, 'James Daniel'),
(4, 'Rahul Agarwal');
# insert data into the `course` table
INSERT INTO course(course_id, course_name, student_id)
VALUES
(1, 'Java Programming', 4),
(2, 'Data Science', 3),
(3, 'Computer Vision', 2),
(4, 'Python Programming', 1);
Use the SELECT command to see the current data in both tables.
SELECT * FROM student;
SELECT * FROM course;
Output (student table):
+------------+---------------+
| student_id | student_name |
+------------+---------------+
| 1 | Maryam Taymor |
| 2 | Mehvish Ashiq |
| 3 | James Daniel |
| 4 | Rahul Agarwal |
+------------+---------------+
4 rows in set (0.00 sec)
Output (course table):
+-----------+----------------------+------------+
| course_id | course_name | student_id |
+-----------+----------------------+------------+
| 1 | Java Programming | 4 |
| 2 | Data Science | 3 |
| 3 | Computer Vision | 2 |
| 4 | Python Programming | 1 |
+-----------+----------------------+------------+
4 rows in set (0.00 sec)
Set FOREIGN_KEY_CHECKS to turn off foreign keys in MySQL
Suppose we have another student who wants to enroll in Java Programming. This time, we need to insert the record into the child table (Courses table) first.
Sample code:
INSERT INTO course(course_id,course_name, student_id)
VALUES
(5, 'Java Programming', 5);
As soon as we execute the query to insert a record in the courses table, it generates the following error.
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`ms23`.`course`, CONSTRAINT `course_ibfk_1` FOREIGN KEY (`student_id`) REFERENCES `student` (`student_id`))
This happens because there is a foreign key in the Courses table. Therefore, we can disable the foreign key check as follows.
SET foreign_key_checks = 0;
Now, insert it again into the course table.
INSERT INTO course(course_id,course_name, student_id)
VALUES
(5, 'Java Programming', 5);
This time, the record is inserted successfully. Confirm the insertion using the SELECT command.
SELECT * from course;
Output (course table):
+-----------+----------------------+------------+
| course_id | course_name | student_id |
+-----------+----------------------+------------+
| 1 | Java Programming | 4 |
| 2 | Data Science | 3 |
| 3 | Computer Vision | 2 |
| 4 | Python Programming | 1 |
| 5 | Java Programming | 5 |
+-----------+----------------------+------------+
5 rows in set (0.00 sec)
Don't forget to set the value of FOREIGN_KEY_CHECKS to 1 to re-enable foreign key constraint checking.
SET foreign_key_checks = 1;
We can also use the same command in phpMyAdmin, but make sure to uncheck the options highlighted in the following screenshot.
Disable foreign key constraints in mysql - Disable foreign key in phpmyadmin
Remember that when we
FOREIGN_KEY_CHECKS
set the value of back to 1, it will not trigger any validation on the current data we inserted after turning off foreign key checking.
It only checks the database for new updates and additions. We can also disable foreign key constraints globally using FOREIGN_KEY_CHECKS.
Sample code:
# turn off foreign key constraints globally
SET GLOBAL FOREIGN_KEY_CHECKS=0;
# turn on foreign key constraints globally
SET GLOBAL FOREIGN_KEY_CHECKS=1;
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Display tables and database structure in MySQL
Publish Date:2025/04/23 Views:97 Category:MySQL
-
Today, we will learn about queries in MySQL that can display the table and database structure. We will use the mysqldump utility, DESCRIBE the , SHOW TABLES and SHOW CREATE TABLE the statements. We are using MySQL version 8.0.28 while writi
Select first row from MySQL table
Publish Date:2025/04/23 Views:112 Category:MySQL
-
Today, we will explore three scenarios and their solutions where we want to select the first row from a MySQL table. In the first scenario, we will learn to get the first row from a MySQL table where there are multiple instances of a partic
Insert timestamp into MySQL table
Publish Date:2025/04/23 Views:77 Category:MySQL
-
Today, we will learn how to TIMESTAMP insert date and time into a type column of a MySQL table according to the table definition. Create a MySQL table First, we will create the tables that we will use in this tutorial. Sample code: CREATE T
The difference between two tables in MySQL
Publish Date:2025/04/23 Views:102 Category:MySQL
-
In this article, we will learn how to find the difference between two tables in MySQL. The difference between two tables in MySQL We often need to compare two tables to find records in one table that have no matching records in the other ta
MySQL sorts data alphabetically
Publish Date:2025/04/23 Views:129 Category:MySQL
-
In this article, we will learn about various ways to sort data alphabetically in MySQL. Sort MySQL data alphabetically When you use the SELECT command to query data from a table, the rows in the result set are in arbitrary order. To order t
Display the current database in MySQL
Publish Date:2025/04/23 Views:199 Category:MySQL
-
This article focuses on the various queries that can be used to display the current database in MySQL. We will learn by using the Windows command line and MySQL Workbench. Display the current database in MySQL We can use the following query
Check if a database exists in MySQL
Publish Date:2025/04/23 Views:179 Category:MySQL
-
In this article, we will introduce many ways to check if a database exists in MySQL. Check if the database exists in MySQL The system schema is the schema that MySQL uses. It includes tables that contain data needed by a running MySQL serve
Get the sum of multiple columns in MySQL
Publish Date:2025/04/23 Views:125 Category:MySQL
-
In this article, we will learn how to sum multiple columns in MySQL. Sum multiple columns in MySQL You can use aggregate functions SUM() to calculate the total value in a collection. SUM() The function calculation does not consider NULL val
MySQL ForEach Loop
Publish Date:2025/04/23 Views:164 Category:MySQL
-
This article describes how to simulate a foreach loop in MySQL using INSERT, SELECT, WHERE, and JOIN in one statement. MySQL foreach loop To understand foreach loop simulation, let us create three tables with the following names and attribu