1

Assuming I have a DB like this:

  • Folders (with "parent folder" column)
  • Files (with "folder" column)

Is there a way to delete all files in a folder that has sub folders in only one query?

Example:

  • Folders:
    • id,name,parent
    • 1, folder1, 0
    • 2, folder2, 1
    • 3, folder3, 2
  • Files:
    • name, folder
    • file1, 2

And I try to delete folder1. That single query should delete all files in folder2, and folder3 becasue folder2 is under folder1, and folder2 is under folder1.

** I know I can do this as a recursive script, but I want to educate myself more

Amit
  • 5,118
  • 6
  • 39
  • 82

1 Answers1

1

As suggested by @jarlh, a really nice solution is having a Foreign key, with on delete cascade.

Amit
  • 5,118
  • 6
  • 39
  • 82
  • This totally works, IFF you don't need to do any of your own bookkeeping DURING the cascaded delete. Since it now becomes a "db-managed recursive action", the only way to affect it's behavior is usually something like triggers, which a lot of times is less than an optimal solution. But it you don't have this requirement, this answer works, and it works quite well. – SlimsGhost Jan 31 '17 at 19:14