[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

database design question categories and sub categories

Posted on 2008-10-09
5
Medium Priority
?
1,342 Views
Last Modified: 2008-10-09
I am writing a product listing app that will list prodcucts in differing categories and sub categories.

Currently a prodcut can belong to one category

so I have    *producttable*
                   id
                   productname
                   categoryid

that relates to
                  *categorytable*
                   categoryid
                   categoryname

Now "some" categories will need SUB categories so what is the best approach to model this in database design?

Thanks

Now some
0
Comment
Question by:mugsey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 5

Accepted Solution

by:
Cvijo123 earned 1200 total points
ID: 22677405
when u create your category table put all categories inside and have parent ID inside

somethin like

categoryID
categoryParentID
categoryName

so for root category u will have

categoryID     categoryParentID     categoryName
1                              0                              first
2                              0                              second
3                              1                             sub category from first
4                              2                             sub category from second
5                              3                             sub category from (sub category from first)



0
 
LVL 23

Assisted Solution

by:Racim BOUDJAKDJI
Racim BOUDJAKDJI earned 400 total points
ID: 22677474
something like...

category:name
product:product_name, categoryg
lineage:child_category, parent_category

HTH
0
 
LVL 5

Assisted Solution

by:varungd
varungd earned 400 total points
ID: 22677480
Create a sub category table including
ID    SubCategoryName   ParentCategoryId
0
 

Author Comment

by:mugsey
ID: 22677870
OK thanks guys

So would my products table be..

                  *producttable*
                   id
                   productname
                   categoryid

then just have a productcategory table like this?

categoryID     categoryParentID     categoryName
1                              0                              first
2                              0                              second
3                              1                             sub category from first
4                              2                             sub category from second
5                              3                             sub category from (sub category from first)


0
 
LVL 1

Expert Comment

by:Bolter99
ID: 22779524
What code are you going to use to display the data?

Also, Only problem with that... you will only have one level sub categories. Unless you create a loop within a loop to iterate through all the categories one by one. If that is all you need/want. Then i guess that would do the trick.

Columns:
CategoryID
ParentID
Name

Should do the trick. Just remember all ParentID's are refrences to the first column to index them later. Good luck.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
What we learned in Webroot's webinar on multi-vector protection.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question