Jump to content

[SOLVED] Setting up Table Structure for multiple Categories & Subcategories


moagrius

Recommended Posts

Hi,

 

What's the best approach for table structure when using multiple levels of categories and subcategories for each item?

 

For example, say I a table "Items", and each is to have an FK to a category, and I have multiple "top-level" categories like "Clothes", "Shoes" and "Jewelry".

 

Within "Clothes" there's "Tops", "Bottoms", and "Dresses".

 

Within "Tops" there's "Tees", "Tanks", and "Jackets".

 

Within "Jackets" there's "Sleeved" and "Sleeveless".

 

etc.

 

My first instinct is to create a single "Category" table, and have each Category use a Key to another row in the same table...

 

So if "Clothes" has a PK ID of 1, then "Tops" would have a "Parent Key" of 1, and an PK ID of say 3, so "Jackets" would have a Parent Key of 3, and a PK ID of say 10, and Sleeves would have a Parent Key of 3, and a PK ID of say 22...

 

Is this approach wise, or even feasible?  If so, how would retrieval work - to get all "Tops" how would I query it?  I presume there's some kind of "self-join" operation I'd need to use...?

 

"select * from Items [some kind of self join] where Items.Parent_Key = 3"

 

But would that grab "Sleeveless", since it's Parent Key is not 3, but it's parent category's parent category Parent Key is...?

 

Sorry if this is stated poorly - I'm having trouble getting my head around it.

 

Any input appreciated.

 

TYIA.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.