How to temporarily disable foreign key constraints in SQL

Foreign key constraints in SQL are essential for maintaining data integrity and ensuring referential integrity between related tables. However, there are situations when we may need to temporarily disable these constraints to perform certain data operations or maintenance tasks. Disabling foreign key constraints allows us to modify or delete data without triggering constraint violations.

Why disable foreign key constraints

Foreign key constraints establish relationships between tables, ensuring that the data in the referencing table corresponds to valid entries in the referenced table. While these constraints are crucial for maintaining data integrity, certain scenarios may require their temporary deactivation:

  • Data loading: When bulk importing data into a table with foreign key constraints, it can be faster and more efficient to disable the constraints temporarily. This prevents the database from checking constraints for every individual row, resulting in faster data loading.

  • Data cleanup: During data cleanup or migration tasks, we may need to modify or delete records across multiple related tables. Disabling foreign key constraints allows us to perform these operations without constraint violations.

  • Performance optimization: In complex queries involving multiple tables, temporarily disabling foreign key constraints can improve query performance by avoiding unnecessary constraint checks.

Methods to temporarily disable foreign key constraints

There are different methods to temporarily disable foreign key constraints based on the SQL database management system we are using. Let’s explore some common methods:

SQL Server (Microsoft SQL Server)

To disable all foreign key constraints in SQL Server, we can use the following SQL commands:

-- Disabling all the foreign key constraints
EXEC sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT all';

We can re-enable the foreign key constraints after performing the necessary operations using the following command:

-- Re-enabling all the foreign key constraints
EXEC sp_MSforeachtable 'ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all';

MySQL

In MySQL, we can temporarily disable foreign key checks using:

-- Disabling the foreign key checks
SET FOREIGN_KEY_CHECKS = 0;

We can use the following method to re-enable foreign key checks after performing the data operations:

-- Enabling the foreign key checks
SET FOREIGN_KEY_CHECKS = 1;

PostgreSQL

In PostgreSQL, we can temporarily disable foreign key constraints using:

-- Disabling the foreign key constraints
SET CONSTRAINTS ALL DEFERRED;

To re-enable foreign key constraints:

-- Enabling foreign key constraints
SET CONSTRAINTS ALL IMMEDIATE;

Code example

Let’s demonstrate how we can disable foreign key constraints temporarily in MySQL.

-- Creating the 'universities' table
CREATE TABLE universities(
university_id INT PRIMARY KEY AUTO_INCREMENT,
university_name VARCHAR(255) NOT NULL
);
-- Creating the 'departments' table
CREATE TABLE departments(
department_id INT PRIMARY KEY AUTO_INCREMENT,
department_name VARCHAR(255),
university_id INT,
FOREIGN KEY(university_id)
REFERENCES universities(university_id)
);
-- Disabling foreign key checks
SET FOREIGN_KEY_CHECKS = 0;
-- Inserting a row with a non-existing university_id
INSERT INTO departments(department_name, university_id)
VALUES('IT', 1);
-- Enabling foreign key checks
SET FOREIGN_KEY_CHECKS = 1;
-- Now, we can retrieve the data from the 'depratments' table
SELECT * FROM departments;

Note: Comment out line 17 and click the “Run” button, a Cannot add or update a child row: a foreign key constraint fails error will occur. This shows that we cannot add data or update a table without disabling the foreign key constraints.

Code explanation

  • Lines 2–5: Create a table universities with two columns: university_id which is of integer data type and set as the primary key with auto-increment, and university_name which is of varchar data type and is required (should not be NULL).

  • Lines 8–14: Define the creation of the departments table. This table includes three columns: department_id of integer data type as the primary key with auto-increment, department_name of varchar data type to store the department’s name, and university_id of integer data type. The university_id column is linked to the university_id column in the universities table through a foreign key constraint.

  • Lines 17–24: Involves the insertion of data into the departments table. However, foreign key checks are temporarily disabled using SET FOREIGN_KEY_CHECKS = 0;. This permits the insertion of a row with a university_id that doesn’t correspond to any existing university. In this case, a department named IT is inserted with university_id 11, which doesn’t match any university in the universities table. Afterward, foreign key checks are re-enabled using SET FOREIGN_KEY_CHECKS = 1;.

  • Line 27: We execute a query to retrieve all data from the departments table. This allows us to examine the data we’ve inserted into the table, including the row with the non-matching university_id.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved