If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > Database Design for navigation that could have multiple levels

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-24-06, 16:33
flamesburn flamesburn is offline
Registered User
 
Join Date: Aug 2006
Posts: 3
Database Design for navigation that could have multiple levels

Hello, I have never had to design a database with the following and am having problems thinking it out.

Basically I have a table called chapters, it is related to a table called sections. a section could be as low as it goes or it could go infinately down into sub sections.

I.e.

chapter 2, section 1 would be 2.1
chapter 2 section 1, subsection 1 would be 2.1.1
etc.

Is there a basic design I could do to minimize the amount of tables used. I.e. setting up just a section and chapter table, or will I have to add a subsection table as well. I hope this makes sense any help would be appreciated.
Reply With Quote
  #2 (permalink)  
Old 08-24-06, 17:20
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
Have a table for chapters.

Have a second table for sections. This table should have a SectionID, as well as a column for ParentSectionID. This is known as the adjacency model, is pretty easy to implement once you understand it.

Your other option is the nested set model, but I wouldn't recommend that to new data modelers.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #3 (permalink)  
Old 08-24-06, 17:44
flamesburn flamesburn is offline
Registered User
 
Join Date: Aug 2006
Posts: 3
Ok, I think I get it

so if there is no sub category a section of ID 1, would I still put ParentSectionID = 1 or would I leave it null?

Thanks for the help
Reply With Quote
  #4 (permalink)  
Old 08-24-06, 17:54
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 08-24-06, 18:25
flamesburn flamesburn is offline
Registered User
 
Join Date: Aug 2006
Posts: 3
great thanks for the help
Reply With Quote
  #6 (permalink)  
Old 08-26-06, 00:37
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
Quote:
Originally Posted by flamesburn
Ok, I think I get it

so if there is no sub category a section of ID 1, would I still put ParentSectionID = 1 or would I leave it null?

Thanks for the help
Either method allows you to identify the top-level records. They will result in slightly different code to handle them, so it is a matter of programming preference.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #7 (permalink)  
Old 01-25-12, 11:35
AndyJay AndyJay is offline
Registered User
 
Join Date: Feb 2008
Posts: 68
I came accross this old post and was very please to find the link to
Categories and Subcategories

I want to use this method for my product navigation on my site.
The only problem is that the sub menu (down1_name) categories are showing up in the (root_name) and well as the sub menu

Hope that makes sense!

Andy
Reply With Quote
  #8 (permalink)  
Old 01-25-12, 14:03
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by AndyJay View Post
The only problem is that the sub menu (down1_name) categories are showing up in the (root_name) and well as the sub menu
please show the exact query you ran
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 01-26-12, 04:08
AndyJay AndyJay is offline
Registered User
 
Join Date: Feb 2008
Posts: 68
Hi r937 (to the rescue again :-)

The query im using is:
Code:
SELECT     TOP (100) PERCENT root.Category AS root_name, down1.Category AS down1_name, down2.Category AS down2_name, 
                      down3.Category AS down3_name, root.Section
FROM         dbo.Categories AS root LEFT OUTER JOIN
                      dbo.Categories AS down1 ON down1.ParentID = root.CategoryID LEFT OUTER JOIN
                      dbo.Categories AS down2 ON down2.ParentID = down1.CategoryID LEFT OUTER JOIN
                      dbo.Categories AS down3 ON down3.ParentID = down2.CategoryID
WHERE     (root.Section = 1)
ORDER BY root_name, down1_name, down2_name, down3_name
I've attached two screen grabs to illustrate the tables and results more clearly.

Thanks again for your time....

Andy
Attached Thumbnails
Database Design for navigation that could have multiple levels-table_view.jpg   Database Design for navigation that could have multiple levels-results.jpg  
Reply With Quote
  #10 (permalink)  
Old 01-26-12, 04:16
AndyJay AndyJay is offline
Registered User
 
Join Date: Feb 2008
Posts: 68
I'm adding a screen grab of the categories table
Attached Thumbnails
Database Design for navigation that could have multiple levels-table.jpg  
Reply With Quote
  #11 (permalink)  
Old 01-26-12, 06:56
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
WHERE root.parentid=0 AND root.section=1
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #12 (permalink)  
Old 01-26-12, 09:13
AndyJay AndyJay is offline
Registered User
 
Join Date: Feb 2008
Posts: 68
Perferct r937

You're a star

Thank You - brill brill brill :-)

Andy
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On