Skip to content

RESTRICT

CREATE DATABASE TestDB;
USE TestDB;
CREATE TABLE Employees (
id INT PRIMARY KEY,
name VARCHAR(30),
department_id INT,
FOREIGN KEY(department_id) REFERENCES Departments(id)
ON DELETE RESTRICT
);
INSERT INTO Employees
VALUES(1, 'John Doe', 1),
(2, 'Jane Doe', 2),
(3, 'Bill Smith', NULL);
CREATE TABLE Departments (
id INT PRIMARY KEY,
department_name VARCHAR(30)
);
DROP TABLE Departments;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails

The fOREIGN KEY constraint with RESTRICT option prevents from deleting the connected data in “Departments” table. So trying to drop the “Departments” table, which is referenced by “Employees” table, leads to an error.