
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL error 1452 - Cannot add or update a child row: a foreign key constraint fails?
This error comes whenever we add a foreign key constraint between tables and insert records into the child table. Let us see an example.
Creating the child table.
mysql> create table ChildDemo -> ( -> id int, -> FKPK int -> ); Query OK, 0 rows affected (0.86 sec)
Creating the second table.
mysql> create table ParentDemo -> ( -> FKPK int, -> Name varchar(100) -> , -> primary key(FKPK) -> ); Query OK, 0 rows affected (0.57 sec)
To add foreign key constraint.
mysql> alter table ChildDemo add constraint ConstChild foreign key(FKPK) references ParentDemo(FKPK); Query OK, 0 rows affected (1.97 sec) Records: 0 Duplicates: 0 Warnings: 0
After creating foreign key constraint, whenever we insert records into the first table or child table, we will get the above error.
mysql> insert into ChildDemo values(1,3); ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`business`.`childdemo`, CONSTRAINT `ConstChild` FOREIGN KEY (`FKPK`) REFERENCES `parentdemo` (`fkpk`))
The error comes when you are trying to add a row for which no matching row in in the other table.
As stated
“Foreign key relationships involve a parent table that holds the central data values, and a child table with identical values pointing back to its parent. The FOREIGN KEY clause is specified in the child table. It will reject any INSERT or UPDATE operation that attempts to create a foreign key value in a child table if there is no a matching candidate key value in the parent table.”
- Related Articles
- MySQL error 1452 - Cannot add or a child row: a foreign key constraint fails
- Add constraint for on duplicate key update in MySQL
- How can we add a FOREIGN KEY constraint to the field of an existing MySQL table?
- In case of FOREIGN KEY constraint, what kind of relationship is there between MySQL parent and child tables?
- How can we remove FOREIGN KEY constraint from a column of an existing MySQL table?
- How can we assign FOREIGN KEY constraint on multiple columns?
- Get a list of Foreign Key constraints in MySQL
- How can we add FOREIGN KEY constraints to more than one fields of a MySQL table?
- MySQL Syntax to create Foreign Key?
- Update multiple columns of a single row MySQL?
- How to identify foreign key in MySQL DB?
- How do I add a check constraint to a table in MySQL?
- Add a positive integer constraint to an integer column in MySQL?
- How add a unique key constraint to a column of a table in a database using JDBC API?
- Update an entire row in MySQL?
