Errors
Error 1452
This error occurs when we try to add or update a row in a child table, but the referenced parent row does not exist.
Example: we have a table users and a table meals.
- The
userstable has a columnid(Primary Key). - The
mealstable has a columnuser_id(Foreign Key) that referencesusers.id.
In relational databases, foreign keys define a parent-child relationship between two tables.
- Parent table: The table that owns the primary key (e.g.,
users) - Child table: The table that contains a foreign key pointing to the parent (e.g.,
meals)
This error will occur if you try to insert a row into meals with a user_id that does not exist in the users table.
INSERT INTO meals (user_id) VALUES (1);
will only work if in the table users we have a row which contains id = 1.