Jump to content

Archived

This topic is now archived and is closed to further replies.

branquito

relationship (!)

Recommended Posts

let's say you have a book of recipes, and you want to make database to hold this information..

you have [b]Categories[/b] of recipes,
then [b]subCategories[/b]
and final [b]Recipes[/b], so nothing complicated..
BUT
some categories do not have subcategories, only recipes that belong to them..

so i know you relate categories to subcategories as one to many, and subcategories to recipes as one to many or many to many (as one recipe may exist in more than one subcategory) and that's all ok..
but HOW TO relate now categories that do not have subcategories to recipes table..when in recipes table is needed field from subcategories (enforce referential integrity)..

thank you very much
Branko

Share this post


Link to post
Share on other sites
That's the wrong way to think about it -- you only need two tables, recipes & categories. That relationship is simple; then, simply store a parent_category_id in the categories table, and have the top-level ones have a NULL parent. That's it.

Share this post


Link to post
Share on other sites
[!--quoteo(post=371671:date=May 5 2006, 02:51 PM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ May 5 2006, 02:51 PM) [snapback]371671[/snapback][/div][div class=\'quotemain\'][!--quotec--]
That's the wrong way to think about it -- you only need two tables, recipes & categories. That relationship is simple; then, simply store a parent_category_id in the categories table, and have the top-level ones have a NULL parent. That's it.
[/quote]
yes ,but then you would have repeating parent_category_id field values for all recipes that belong to same category, and that way it would be hard to maintain database in future..

Share this post


Link to post
Share on other sites
[!--quoteo(post=371797:date=May 6 2006, 05:47 AM:name=branquito)--][div class=\'quotetop\']QUOTE(branquito @ May 6 2006, 05:47 AM) [snapback]371797[/snapback][/div][div class=\'quotemain\'][!--quotec--]
yes ,but then you would have repeating parent_category_id field values for all recipes that belong to same category, and that way it would be hard to maintain database in future.. [/quote]

Yes, you would have repeating parent_category_id field values - but how does that make it hard to maintain your database? That's actually very standard database design...

Share this post


Link to post
Share on other sites
Even if you had a subcategories table, you'd have the same "maintenance" issues; besides, it's not a big deal, and quite common.

Share this post


Link to post
Share on other sites

×

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.