0

So I have a DB (Tree) that looks like this:

enter image description here

So when I delete a questions I want to delete the whole tree linked to that questions but I don't know how to do this :(. Do you have any ideas?

I have this query but it deletes only the question and the questions linked to that question but it stops there.

DELETE FROM support_faq WHERE id=".$id_intrebare." OR parinte=".$id_intrebare
Alexandru Coman
  • 165
  • 1
  • 14

2 Answers2

1

Add a FOREIGN key with ON DELETE CASCADE option

for example

ALTER TABLE ChildTable ADD FOREIGN KEY (columnName_c) REFERENCES ParentTable(columnName_p) ON DELETE CASCADE;

Hardik Vinzava
  • 968
  • 10
  • 22
0

If you have only one table with loosely coupled parent and child relationship. You can write a procedure to delete your record recursively.

  1. Please follow the link to get to know how to write procedure in mysql
  2. Also it will let you know how you can use the loop

LINK

UmarKashmiri
  • 872
  • 8
  • 15