Fix SQL Error -532: Relationship Constraint on Row Deletion

Updated

SQL error message about a relationship constraint

SQL error -532 is a Db2 code, not a MySQL one. MySQL has no error -532 and never emits it. If you are on MySQL and a DELETE against a parent table is being refused, the error you actually get is:

ERROR 1451 (23000) at line 1: Cannot delete or update a parent row: a foreign key constraint fails (`hw_fk_demo`.`hw_fk_faculty`, CONSTRAINT `fk_faculty_dept` FOREIGN KEY (`department_num`) REFERENCES `hw_fk_department` (`department_num`))

The message already names the child table and the constraint. Delete or reassign the rows in that child table first, and the parent delete succeeds. Everything on this page was run on MySQL 8.4.10. To see the same error codes and delete-rule results on a MySQL 8 server of your own, run sql-error-532-relationship-constraint-restricts-deletion.sh; it works in a scratch schema, empties its tables afterwards and prints the command that drops the schema.

MySQL’s equivalent error codes: 1451 and 1452

MySQL’s 1216 (Cannot add or update a child row) is not the equivalent of Db2’s -532. It is the opposite direction — it fires when you insert or update a child row pointing at a parent that does not exist, not when you delete a parent. And InnoDB does not raise 1216 or 1217; it raises the extended variants that include the constraint detail. From perror on this server:

MySQL error code MY-001216 (ER_NO_REFERENCED_ROW): Cannot add or update a child row: a foreign key constraint fails
MySQL error code MY-001217 (ER_ROW_IS_REFERENCED): Cannot delete or update a parent row: a foreign key constraint fails
MySQL error code MY-001451 (ER_ROW_IS_REFERENCED_2): Cannot delete or update a parent row: a foreign key constraint fails%.192s
MySQL error code MY-001452 (ER_NO_REFERENCED_ROW_2): Cannot add or update a child row: a foreign key constraint fails%.192s

So on an InnoDB table: deleting a referenced parent gives 1451, inserting an orphan child gives 1452, and SQLSTATE is 23000, not Db2’s 23504.

A CREATE TABLE that writes REFERENCES Department_num, a column name where the table name belongs, is not valid SQL in any engine; MySQL 8.4.10 rejects it with:

ERROR 1239 (42000) at line 1: Incorrect foreign key definition for 'foreign key without name': Key reference and table reference don't match

Finding the rows that are actually blocking you

The error names the constraint but not the offending rows. Two queries get you there. First, list every child table pointing at your parent, with its delete rule:

SELECT rc.CONSTRAINT_NAME, kcu.TABLE_NAME AS child_table, kcu.COLUMN_NAME AS child_col,
       kcu.REFERENCED_TABLE_NAME AS parent_table, rc.DELETE_RULE
FROM information_schema.REFERENTIAL_CONSTRAINTS rc
JOIN information_schema.KEY_COLUMN_USAGE kcu
  ON kcu.CONSTRAINT_SCHEMA = rc.CONSTRAINT_SCHEMA
 AND kcu.CONSTRAINT_NAME   = rc.CONSTRAINT_NAME
WHERE rc.CONSTRAINT_SCHEMA = DATABASE()
  AND kcu.REFERENCED_TABLE_NAME = 'hw_fk_department';
+-----------------+---------------+----------------+------------------+-------------+
| CONSTRAINT_NAME | child_table   | child_col      | parent_table     | DELETE_RULE |
+-----------------+---------------+----------------+------------------+-------------+
| fk_faculty_dept | hw_fk_faculty | department_num | hw_fk_department | NO ACTION   |
+-----------------+---------------+----------------+------------------+-------------+

Note NO ACTION: that is what a foreign key with no explicit ON DELETE clause reports. If you write ON DELETE RESTRICT, the catalog stores RESTRICT — but MySQL executes the two identically, so a “NO ACTION” row is not deferred to commit time the way the SQL standard describes.

Then select the blocking rows using the child column the first query gave you:

SELECT * FROM hw_fk_faculty WHERE department_num = 1;
+-------------+--------------+----------------+
| faculty_num | faculty_name | department_num |
+-------------+--------------+----------------+
|          10 | Ada          |              1 |
|          11 | Grace        |              1 |
+-------------+--------------+----------------+

The four delete rules, measured

Three child tables were built against one parent, each with a different rule, then the parent row each referenced was deleted:

  • ON DELETE RESTRICT — the delete fails with ERROR 1451 (23000), same as no clause at all.
  • ON DELETE CASCADE — the delete succeeds and takes the children with it. SELECT COUNT(*) on the child table returned 0 afterwards.
  • ON DELETE SET NULL — the delete succeeds and the child’s FK column becomes NULL. The child row survived as 300 | NULL.

SET NULL has a prerequisite: the child column must be nullable, and MySQL rejects the schema at CREATE TABLE time if it is not:

ERROR 1830 (HY000) at line 1: Column 'd' cannot be NOT NULL: needed in a foreign key constraint 'hw_fk_bad_ibfk_1' SET NULL

CASCADE is a real design decision, not a convenience. It means deleting one department silently deletes every faculty record under it, with no confirmation and no way to see the blast radius first. Prefer it where the child genuinely cannot exist without the parent, and prefer RESTRICT plus an explicit application-level cleanup everywhere else.

Do not reach for SET FOREIGN_KEY_CHECKS = 0

This is the fix most search results hand you, and it does not fix anything — it converts a loud error into silent corruption. Run on the same tables:

SET FOREIGN_KEY_CHECKS = 0;
DELETE FROM hw_fk_department WHERE department_num = 1;
SET FOREIGN_KEY_CHECKS = 1;

SELECT f.faculty_num, f.faculty_name, f.department_num
FROM hw_fk_faculty f
LEFT JOIN hw_fk_department d ON d.department_num = f.department_num
WHERE d.department_num IS NULL;
+-------------+--------------+----------------+
| faculty_num | faculty_name | department_num |
+-------------+--------------+----------------+
|          10 | Ada          |              1 |
|          11 | Grace        |              1 |
+-------------+--------------+----------------+

Turning the checks back on does not re-validate anything. Two rows now point at a department that does not exist, and CHECK TABLE is perfectly happy with them:

+--------------------------+-------+----------+----------+
| Table                    | Op    | Msg_type | Msg_text |
+--------------------------+-------+----------+----------+
| hw_fk_demo.hw_fk_faculty | check | status   | OK       |
+--------------------------+-------+----------+----------+

It gets worse if you later try to clean up. Dropping and re-adding the constraint to force a re-check succeeds at the drop and fails at the add:

ERROR 1452 (23000) at line 1: Cannot add or update a child row: a foreign key constraint fails (`hw_fk_demo`.`#sql-1b1c_c0f1`, CONSTRAINT `fk_faculty_dept` FOREIGN KEY (`department_num`) REFERENCES `hw_fk_department` (`department_num`))

Querying REFERENTIAL_CONSTRAINTS for that table afterwards returns nothing. The table is now unprotected and inconsistent, and it will stay that way until someone finds and repairs the orphans by hand. The only defensible use of FOREIGN_KEY_CHECKS = 0 is loading a dump whose tables arrive out of dependency order, where you know every reference will resolve by the end of the load.

The fix

Deal with the children, in one transaction so a crash between the two statements cannot leave orphans:

START TRANSACTION;
DELETE FROM hw_fk_faculty    WHERE department_num = 1;   -- or: UPDATE ... SET department_num = 2
DELETE FROM hw_fk_department WHERE department_num = 1;
COMMIT;

If that pattern is showing up all over your application code, that is the signal to put ON DELETE CASCADE in the schema instead — with the caveat above about what it costs you.

If you really are on Db2

No Db2 instance was available to test against, so this section is from IBM’s documentation rather than measured here. SQLCODE -532 with SQLSTATE 23504 means a parent row could not be deleted because a dependent row exists in a relationship whose delete rule is RESTRICT or NO ACTION. Db2’s message text identifies the constraint by name; you resolve it the same way — find the dependent rows through the named constraint, then delete or reassign them, or change the relationship’s delete rule to CASCADE or SET NULL. The MySQL codes above are the equivalents, not the same error.

Photo of author
Sudhir P. founded HeatWare.com in 1999 and has built and operated it full-stack ever since; it is now used by more than 88,000 people. He writes here about the PostgreSQL, MySQL, Linux and DevOps work that keeps it running. Articles are rewritten only after the commands in them have actually been run, and the verification scripts are published alongside them so anyone can check the claims. Reach him at blog@heatware.net.