views:

23

answers:

1

what would be proper way to delete albums hierarchical data with its media.

should i just start looping and find all the sub album id and delete thous and make query to delete all the media belonging to that album id.

that will be loads of query execution if data is alot or albums are alot for a user. what your device is on this.

my database structure:

**album**
album_id
name
parent_id

**media**
media_id
album_id
name
A: 
DELETE 
  FROM albums,
       media
 WHERE media.albumID = albums.albumID
   AND (albums.parentID = ? or albums.albumID = ?)

That is the simplest approach, assuming you don't have triggers or relations defined in your data. Without knowing more about your specific database structure its hard to give a better answer; however, as with other situations, you are better off doing this as a single SQL statement. Why? It preserves your data integrity, otherwise you have to issue locks on the various tables while your software manually deletes specific entries (which is also a solution, but barring knowing more, not one I'd recommend).

macabail
ya sorry, added the table structure too.
Basit
I've updated the fragment; the bind-params would be the same ID (that of the parent album you want to delete).
macabail