I understand you have a structure of folders, each with a single
parent folder. You want to be able to delete one specific folder and
then have all folders directly or indirectly under that folder removed
at the same time.

This is easy to do using a foreign key with an "on delete"
clause. Declare a table like this:

CREATE TABLE gallery (

  gallery_id                    int,

  parent_gallery_id             int,

  PRIMARY KEY ( gallery_id ),

  FOREIGN KEY ( parent_gallery_id ) REFERENCES gallery ( gallery_id )


The foreign key clause establishes a relationship between two records
in the gallery table, a reference from on gallery to its parent. The
"on delete cascade" part says that when the gallery record referenced
from this record (i.e. the parent) is deleted, this gallery record is
to be deleted too. This will be done recursively by the database
system backend. 

To delete a subtree of galleries, you simply delete the record for the
topmost folder, the database backend will take care of the rest. 

I'm not sure how well foreign key and "on delete" is supported by
MySQL, but this is something PostgreSQL has been doing well for a long


Geir Pedersen

PHP Database Mailing List (
To unsubscribe, visit:

Reply via email to