branquito Posted May 5, 2006 Share Posted May 5, 2006 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..BUTsome 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 muchBranko Quote Link to comment https://forums.phpfreaks.com/topic/9119-relationship/ Share on other sites More sharing options...
fenway Posted May 5, 2006 Share Posted May 5, 2006 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 Link to comment https://forums.phpfreaks.com/topic/9119-relationship/#findComment-33703 Share on other sites More sharing options...
branquito Posted May 6, 2006 Author Share Posted May 6, 2006 [!--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.. Quote Link to comment https://forums.phpfreaks.com/topic/9119-relationship/#findComment-33821 Share on other sites More sharing options...
neylitalo Posted May 6, 2006 Share Posted May 6, 2006 [!--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... Quote Link to comment https://forums.phpfreaks.com/topic/9119-relationship/#findComment-33837 Share on other sites More sharing options...
fenway Posted May 6, 2006 Share Posted May 6, 2006 Even if you had a subcategories table, you'd have the same "maintenance" issues; besides, it's not a big deal, and quite common. Quote Link to comment https://forums.phpfreaks.com/topic/9119-relationship/#findComment-33854 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.