I have a table in my front-end code where I show a series of exercises and next to each of them, there’s a button to be able to eliminate the exercise. The problem is that my exercises table is related to other tables:
CREATE TABLE exercises (
exercise_id INT,
PRIMARY KEY(exercise_id)
);
CREATE TABLE ex_tag(
exercise_id_fk INT,
tag_id_fk INT,
FOREIGN KEY(exercise_id_fk) REFERENCES exercises(exercise_id),
FOREIGN KEY(tag_id_fk) REFERENCES tags(tag_id)
);
CREATE TABLE solution(
exercise_id_fk INT,
solution_number INT,
FOREIGN KEY(exercise_id_fk) REFERENCES exercises(exercise_id)
);
And, sometimes, an entry in the following table is created. If the exercise is answered, it will be created, but if not, it will not be created.
CREATE TABLE answers(
exercise_id_fk INT,
student_id INT,
FOREIGN KEY(exercise_id_fk) REFERENCES exercises(exercise_id)
);
If the table 'exercises' had no relation to any table, this query would work:
DELETE FROM exercises WHERE exercise_id=$id
How can I change the query to make it possible to erase the selected exercise from all the tables that need it, with this single exercise_id?