Jump to content


Photo

relationship (!)


  • Please log in to reply
4 replies to this topic

#1 branquito

branquito
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 05 May 2006 - 09:43 AM

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

you have Categories of recipes,
then subCategories
and final Recipes, 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

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 05 May 2006 - 07:51 PM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 branquito

branquito
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 06 May 2006 - 10:47 AM

[!--quoteo(post=371671:date=May 5 2006, 02:51 PM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ May 5 2006, 02:51 PM) View Post[/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..

#4 neylitalo

neylitalo
  • Staff Alumni
  • Advanced Member
  • 1,854 posts
  • LocationMichigan, USA

Posted 06 May 2006 - 01:44 PM

[!--quoteo(post=371797:date=May 6 2006, 05:47 AM:name=branquito)--][div class=\'quotetop\']QUOTE(branquito @ May 6 2006, 05:47 AM) View Post[/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...

http://nealylitalo.net - My personal website, and home of The Netizen's Journal.

#5 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 06 May 2006 - 04:12 PM

Even if you had a subcategories table, you'd have the same "maintenance" issues; besides, it's not a big deal, and quite common.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users