What causes 'cannot add or update a child row: a foreign key constraint fails' in sql?
Example:
INSERT INTO orders (user_id) VALUES (999);
You're trying to insert or update a row in a table with a foreign key value that doesn't exist in the parent table.
Solution:Ensure the foreign key value exists in the parent table before inserting or updating in the child table.
-- Assuming user 999 does not exist in 'users' table
INSERT INTO users (id) VALUES (999);
INSERT INTO orders (user_id) VALUES (999);