• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

How to Fix MySQL Error 1216 – SQLSTATE: 23000 (ER_NO_REFERENCED_ROW) Cannot add or update a child row: a foreign key constraint fails

December 20, 2023

How to Fix MySQL Error 1216 – SQLSTATE: 23000 (ER_NO_REFERENCED_ROW) Cannot add or update a child row: a foreign key constraint fails

MySQL is a popular open-source relational database management system used by many websites and applications. It provides a robust and efficient way to store and retrieve data. However, like any software, MySQL can encounter errors that can hinder its functionality. One such error is MySQL Error 1216, also known as SQLSTATE: 23000 (ER_NO_REFERENCED_ROW).

Understanding MySQL Error 1216

MySQL Error 1216 occurs when you try to add or update a row in a child table that violates a foreign key constraint. In other words, it means that the value you are trying to insert or update in the child table does not exist in the parent table. This error prevents the operation from being completed successfully.

Foreign key constraints are used to enforce referential integrity in a database. They ensure that the data in the child table is consistent with the data in the parent table. When a foreign key constraint is defined, MySQL checks if the value being inserted or updated in the child table exists in the parent table. If it doesn’t, MySQL throws the Error 1216.

Fixing MySQL Error 1216

To fix MySQL Error 1216, you need to ensure that the value you are trying to insert or update in the child table exists in the parent table. Here are some steps you can follow:

1. Check the Foreign Key Constraint

First, verify that the foreign key constraint is correctly defined. Make sure that the column in the child table references the primary key column in the parent table. Also, ensure that the data types of the columns match.

2. Check the Data in the Parent Table

Next, examine the data in the parent table. Look for any missing or incorrect values that could cause the foreign key constraint to fail. If necessary, add or update the data in the parent table to ensure that it aligns with the values in the child table.

3. Check for Orphaned Rows

An orphaned row is a row in the child table that references a non-existent row in the parent table. These orphaned rows can cause the foreign key constraint to fail. To identify and fix orphaned rows, you can use the following query:

SELECT * FROM child_table WHERE foreign_key_column NOT IN (SELECT primary_key_column FROM parent_table);

This query will return any rows in the child table that reference non-existent rows in the parent table. You can then delete or update these rows accordingly.

4. Disable Foreign Key Checks

If you need to perform bulk operations on the tables, you can temporarily disable foreign key checks. This can be done using the following commands:

SET FOREIGN_KEY_CHECKS = 0;

Perform your operations, and then re-enable foreign key checks using:

SET FOREIGN_KEY_CHECKS = 1;

Remember to use this approach with caution, as it can lead to inconsistent data if not handled properly.

Summary

MySQL Error 1216, also known as SQLSTATE: 23000 (ER_NO_REFERENCED_ROW), occurs when a foreign key constraint fails during an insert or update operation in a child table. To fix this error, you need to ensure that the value being inserted or updated in the child table exists in the parent table. Verify the foreign key constraint, check the data in the parent table, identify and fix orphaned rows, and consider disabling foreign key checks temporarily for bulk operations.

If you are experiencing MySQL Error 1216, our Hong Kong VPS Hosting services can help you resolve the issue and ensure the smooth functioning of your MySQL database.

Recent Posts

  • Hong Kong VPS vs Singapore VPS: Which Is Better for Your Asia Business in 2026?
  • How to Migrate from CentOS 8 to AlmaLinux or Rocky Linux Safely
  • CentOS Server Performance Tuning: Optimization Techniques for 2026
  • How to Configure SELinux in CentOS Without Breaking Your System (CentOS Stream 9/10 – 2026)
  • Managing Users and Permissions in CentOS Stream: Best Practices (CentOS Stream 9/10 – 2026)

Recent Comments

No comments to show.

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot