Error 1452 MySQL - Cannot add or update Child Row
When you are trying to insert values into a table where foreign keys are defined, you need to make sure the specific values you enter for foreign key columns are available in the parent table.
In your case, when inserting values into the TruckMake
and TruckModel
tables, for TruckMakeID
column, you are using values with the prefix TMI
. But when inserting values into the Truck
table, for the column TruckMakeID
, you are using values with the prefix TM
(I
is missing). That's the reason for the error since values with prefix TM
followed by a number are not available in the TruckModel
table.
Since the data is not added to the Truck table, there are errors when trying to insert values to Allocation
table since there is a foreign key defined for TruckVINNum
referred from the Truck
table.
Receving error #1452 - Cannot add or update a child row
It looks like you are trying to enforce the relationship but don't match one or more of the values that should be in the parent table. The data that relates to each other must exist in both tables, otherwise you get the error message. You'll need to clean up the data in the tables before you can enforce the relationship.
MySQL Error 1452 when inserting data
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.To know more Go to this link
So your error Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails
essentially means that, you are trying to add a row to your property
table for which no matching row (intid)
is present in interiors
table.
You must first insert the row to your interiors
table.
MySQL Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails
I found the problem, I had to add the constraint
ALTER TABLE employee ADD CONSTRAINT employee_fk2 FOREIGN KEY(DeptID) REFERENCES department(DeptID) ON UPDATE CASCADE ON DELETE RESTRICT;
only after I had created both tables and inserted all the values. Thank you everyone for your suggestions.
Related Topics
SQL Server Query Xml Attribute for an Element Value
Unique Constraint Controlled by a Bit Column
SQL Server - Group Records by N Minutes Interval
Identifying Transitive Dependencies
SQL Server Performance for Alter Table Alter Column Change Data Type
SQL Server 2008: Bulk Datatype Change
Running Sum in Access Query with Group By
How to Extract Values from Column and Update Result in Another Column
Concat the Second Column Value If the First Column Value Is Same
Syntax Error When Using Row_Number in SQLite3
Reverse- Geocoding: How to Determine the City Closest to a (Lat,Lon) with Bigquery SQL
Amazon Redshift - Lateral Column Alias Reference
Why Does Nvl Always Evaluate 2Nd Parameter
SQL Count of Open Orders Each Day Between Two Dates
How to Fill Missing Dates and Values in Partitioned Data
Search If Number Is Contained Within an Expression Like: 1-3,5,10-15,20