Hmm - looks like the basic question has already been answered. But your setup bugs me slightly...
I think I d restructure your tables a little bit (Assuming internationalization is not an issue). Among other things, I don t feel there s a fundamental difference between the concepts of Category and Gallery . Now, there may be super and sub categories/galleries, and images may belong to more than one at once (ie, from the Renniasance, by Michaelangelo, sculpture, etc). It s similar to the concept of tags.
我或许会修改你的表格,以便:
Gallery (or Category, if you prefer)
=============
id -- autoincrement
name -- varchar(50) or something, unique
parent -- fk reference to another Gallery.id row, optional
Image
==========
id -- autoincrement
name -- varchar(50) or similar, non-unique
path -- store as URI/URL, unique
description -- varchar(128) or similar
Gallery_Image
===============
galleryId -- fk reference to Gallery.id
imageId -- fk reference to Image.id
-- the pair is unique
Related_Gallery -- optional table
================
galleryId -- fk reference to Gallery.id
relationship -- code, or fk reference to other table
relatedId -- fk reference to Gallery.id
-- entire row should be unique
-- somewhat tricky to use.