+--------+---------+-----------+
| id | title | parent_id |
+--------+---------+-----------+
| 1 | Lvl-1 | null |
+--------+---------+-----------+
| 2 | Lvl-11 | 1 |
+--------+---------+-----------+
| 3 | Lvl-111 | 2 |
+--------+---------+-----------+
| 4 | Lvl-12 | 4 |
+--------+---------+-----------+
What I’m trying to do is, when I delete the row with id 1, it will delete all its child rows (the rows with id 2 and 3 in the example table). The row with ID 2 should be deleted because its parent_id is 1, and the row with ID 3 should be deleted because its parent_id is 2.
I’m using the MyISAM engine. Is it possible to delete the row and all child rows with just one query?
No, not with myISAM. If you set this up with constraints in InnoDB, it may work.
But by doing this you’re using a table-oriented data store to manage a hierarchical data structure. You would be wise to handle this kind of thing explicitly in your application program.