MySQL 8.4Lab-verified · 2026-11-30

MySQL ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constraint

TRUNCATE was run on a parent table that another table's foreign key points to.

MySQL 8.4.13
1701 (42000): Cannot truncate a table referenced in a foreign key constraint (`lab_a`.`orders`, CONSTRAINT `orders_ibfk_1`)

Jump to a cause

  1. 01TRUNCATE on the parent while child rows exist
  2. 02TRUNCATE on the parent even though the child is empty

Lab-verifiedReproduced and fixed on MySQL 8.4.13 · 2026-11-30 · container image mysql:8.4

InnoDB refuses TRUNCATE TABLE on any table referenced by a foreign key. It does not look at the data: the error appears even when the child table has no rows. The message names the child table and constraint, e.g. (lab_a.orders, CONSTRAINT orders_ibfk_1).

Below are two cases reproduced on MySQL 8.4 with the fixes that were actually run. The message stays in English with Korean or Japanese lc_messages.

01TRUNCATE on the parent while child rows exist

orders.customer_id references customers.id, and orders had 2 rows. TRUNCATE TABLE customers failed with ERROR 1701 naming orders_ibfk_1.

To empty both tables: SET FOREIGN_KEY_CHECKS = 0, TRUNCATE TABLE orders, TRUNCATE TABLE customers, SET FOREIGN_KEY_CHECKS = 1. Both tables then had 0 rows.

information_schema.REFERENTIAL_CONSTRAINTS showed which foreign key blocks it: orders_ibfk_1 on orders referencing customers.

Setup
Setuprun as lab_a
CREATE TABLE customers (id INT PRIMARY KEY, name VARCHAR(30));
CREATE TABLE orders (id INT PRIMARY KEY, customer_id INT, FOREIGN KEY (customer_id) REFERENCES customers (id));
INSERT INTO customers VALUES (1, 'Kim'), (2, 'Lee');
INSERT INTO orders VALUES (10, 1), (11, 2);
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 0 rows affected (0.02 sec)

Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
TRUNCATE TABLE customers;
✕ ERROR
ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constraint (`lab_a`.`orders`, CONSTRAINT `orders_ibfk_1`)
Checkrun as root
SELECT CONSTRAINT_NAME, TABLE_NAME, REFERENCED_TABLE_NAME FROM information_schema.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = 'lab_a';
OUTPUT
+-----------------+------------+-----------------------+
| CONSTRAINT_NAME | TABLE_NAME | REFERENCED_TABLE_NAME |
+-----------------+------------+-----------------------+
| orders_ibfk_1   | orders     | customers             |
+-----------------+------------+-----------------------+
1 row in set (0.00 sec)

FOREIGN_KEY_CHECKS = 0 here is for the current session only. Truncate the child tables too, or the remaining rows will point to parents that no longer exist, and set it back to 1 right after.

After the fixrun as lab_a
SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE TABLE orders;
TRUNCATE TABLE customers;
SET FOREIGN_KEY_CHECKS = 1;
SELECT (SELECT COUNT(*) FROM customers) AS customers, (SELECT COUNT(*) FROM orders) AS orders;
✓ SUCCESS
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.03 sec)

Query OK, 0 rows affected (0.02 sec)

Query OK, 0 rows affected (0.00 sec)

+-----------+--------+
| customers | orders |
+-----------+--------+
|         0 |      0 |
+-----------+--------+
1 row in set (0.00 sec)

02TRUNCATE on the parent even though the child is empty

SELECT COUNT(*) FROM orders returned 0, but TRUNCATE TABLE customers still failed with ERROR 1701. The check is on the foreign key definition, not on data.

DELETE FROM customers worked (2 rows affected) and the count became 0, because DELETE checks rows one by one and no order referenced them.

information_schema.REFERENTIAL_CONSTRAINTS showed which foreign key blocks it: orders_ibfk_1 on orders referencing customers.

Setup
Setuprun as lab_a
CREATE TABLE customers (id INT PRIMARY KEY, name VARCHAR(30));
CREATE TABLE orders (id INT PRIMARY KEY, customer_id INT, FOREIGN KEY (customer_id) REFERENCES customers (id));
INSERT INTO customers VALUES (1, 'Kim'), (2, 'Lee');
OUTPUT
Query OK, 0 rows affected (0.02 sec)

Query OK, 0 rows affected (0.02 sec)

Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0
Reproducerun as lab_a
SELECT COUNT(*) FROM orders;
TRUNCATE TABLE customers;
✕ ERROR
+----------+
| COUNT(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)

ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constraint (`lab_a`.`orders`, CONSTRAINT `orders_ibfk_1`)
Checkrun as root
SELECT CONSTRAINT_NAME, TABLE_NAME, REFERENCED_TABLE_NAME FROM information_schema.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = 'lab_a';
OUTPUT
+-----------------+------------+-----------------------+
| CONSTRAINT_NAME | TABLE_NAME | REFERENCED_TABLE_NAME |
+-----------------+------------+-----------------------+
| orders_ibfk_1   | orders     | customers             |
+-----------------+------------+-----------------------+
1 row in set (0.00 sec)
After the fixrun as lab_a
DELETE FROM customers;
SELECT COUNT(*) FROM customers;
✓ SUCCESS
Query OK, 2 rows affected (0.01 sec)

+----------+
| COUNT(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)

FAQ

Why can't I truncate a table with a foreign key when the child table is empty?

InnoDB blocks TRUNCATE on any table that a foreign key references, regardless of data. Here the child had 0 rows and TRUNCATE still failed. Use DELETE instead.

Is SET FOREIGN_KEY_CHECKS = 0 safe for TRUNCATE?

It works for the session, as shown here. Truncate the child tables in the same step so no orphan rows remain, and set it back to 1.

What is the difference between DELETE and TRUNCATE here?

DELETE checks each row against the foreign key, so it succeeds when no child row references it. TRUNCATE is refused whenever a foreign key points at the table.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary users created for the test, each with full rights on a database of the same name (its default database); root is the administrator. MySQL 8.4.13