User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 425,826 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,980 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser: Programming Forums
Views: 912 | Replies: 7 | Solved
Reply
Join Date: Apr 2007
Posts: 257
Reputation: greeny_1984 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 10
greeny_1984's Avatar
greeny_1984 greeny_1984 is offline Offline
Posting Whiz in Training

how to create database for business site containing no of subcategories

  #1  
May 19th, 2008
Hi everyone,
Ima working on a business site similar to alibaba.com.In that we have a main categories
named
agriculture,chemicals ....so on.
on clicking on agriculture we get one more subcategory containing different names line
plant products,beans so on..

on clicking on beans we get the companies selling beans.

similar to that we want to create a website containing categories,subcategories.
the problem is that we dont no exactly how many no of subcategories will be present.
I want dynamically create subcategories.Can any PLZ give me the database design for this problem.

waiting for u r replies
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2008
Posts: 295
Reputation: tesuji is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 41
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: how to create database for business site containing no of subcategories

  #2  
May 19th, 2008
Hi greeny,

a way to solve this completely independently from the number of categories and subcategories are (recursive) trees in SQL. There is a famous book: Transact-SQL Cookbook by Ales Spetic, Jonathan Gennick what has an extra chapter only dealing with that topic. They also explain how to traverse trees recursively by means of transact SQL.

Below table I once designed for a similar problem you are facing with is based on an example from this book. Our categories table (it slightly differs from below simple example) works great, and the table now has over 4000 categories and subcategories, still increasing.
  1. CREATE TABLE categories ( -- father of main knot is NULL
  2. ucat integer NULL, -- a father knot of a tree, it has many son knots
  3. lcat integer NOT NULL, -- the son knot. has only one father knot
  4. name varchar (30),
  5. PRIMARY KEY (lcat),
  6. FOREIGN KEY (ucat) REFERENCES categories); -- recursive definition
  7.  
  8. -- Some samples
  9. INSERT INTO categories VALUES (NULL,1,'Categories');
  10. INSERT INTO categories VALUES (1,2,'Software');
  11. INSERT INTO categories VALUES (1,3,'Hardware');
  12. INSERT INTO categories VALUES (1,4,'Network');
  13. INSERT INTO categories VALUES (2,5,'Linux');
  14. INSERT INTO categories VALUES (3,6,'PC');
  15. INSERT INTO categories VALUES (3,7,'Printer');
  16. INSERT INTO categories VALUES (6,8,'LCD');
  17. commit
  18.  
  19. -- for example select all subcategories of category hardware
  20. SELECT * FROM categories
  21. WHERE ucat = ALL (SELECT lcat FROM categories
  22. WHERE name = 'Hardware');
  23.  
  24. /* Result
  25. ucat lcat name
  26. ------------------
  27. 3 6 PC
  28. 3 7 Printer
  29. */

You may draw the tree to see how it works.
Happy treeing!

krs,
tesu
Last edited by tesuji : May 19th, 2008 at 6:48 am.
Reply With Quote  
Join Date: Apr 2007
Posts: 257
Reputation: greeny_1984 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 10
greeny_1984's Avatar
greeny_1984 greeny_1984 is offline Offline
Posting Whiz in Training

Re: how to create database for business site containing no of subcategories

  #3  
May 20th, 2008
Hi tesu,
Thanks for the reply.I have some doubts here in the example u gave.U gave code for a single category and single sub category.
Can u explain u r code for this example

Agriculture--->main category
|
V
plant products-->sub category
|
V
Beans-->sub sub category
|
V
green beans-->sub sub sub category
|
V
companies containg this product--->final result

waiting for u r reply
Reply With Quote  
Join Date: Apr 2008
Posts: 295
Reputation: tesuji is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 41
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: how to create database for business site containing no of subcategories

  #4  
May 20th, 2008
Hi greeny,

There is no doubt that your doubt tells me you didn't understood anything of my sample code
neither you have drawn the tree of the example categories tree. If you don't understand this code, especially the primary, foreign keys and the insert statements, you may freely ask me.

First, I would advice you that you try to draw the categories tree from the given insert statements. Then compare it with your green bean example. What will you assess then? Yes, this very plain categories tree is already much more complex than your green bean example.

krs,
tesu
Reply With Quote  
Join Date: Apr 2008
Posts: 295
Reputation: tesuji is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 41
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: how to create database for business site containing no of subcategories

  #5  
May 20th, 2008
Sorry, corretion:
Originally Posted by tesuji View Post
Hi greeny,
... Our categories table (it slightly differs from below simple example) works great, and the table now has over 4000 categories and subcategories, still increasing...
tesu

We don't have 4000, sorry, only about 400 categories!

Need a little help for drawing the sample tree: it consists of one main category, three sub categories, two sub sub categories, and one sub sub sub category.

krs,
tesu
Reply With Quote  
Join Date: Apr 2007
Posts: 257
Reputation: greeny_1984 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 10
greeny_1984's Avatar
greeny_1984 greeny_1984 is offline Offline
Posting Whiz in Training

Re: how to create database for business site containing no of subcategories

  #6  
May 22nd, 2008
Hi tesu,
Thanks for the replies.I have understood what u have said and added a column named level
to get the level of category.
CAT_ID
CAT_NAME
CAT_DESC
CAT_LEVEL
PARENT_CAT
and its working great.
Reply With Quote  
Join Date: Apr 2008
Posts: 295
Reputation: tesuji is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 41
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: how to create database for business site containing no of subcategories

  #7  
May 22nd, 2008
hi, I am glad that the categories tree now also works fine for you. You are absolutely right when adding a level because navigation is then sometimes easier. In my actual tree I have also a further category for categorizing the categories, you see? Additionally to the product data categorized by the leaf nodes there is a further table with attributes to gather common data of a product.

krs,
tesu
Reply With Quote  
Join Date: Apr 2008
Posts: 295
Reputation: tesuji is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 41
tesuji tesuji is offline Offline
Posting Whiz in Training

Re: how to create database for business site containing no of subcategories

  #8  
May 22nd, 2008
Originally Posted by greeny_1984 View Post
Hi tesu,
Thanks for the replies.I have understood what u have said and added a column named level
to get the level of category.
CAT_ID
CAT_NAME
CAT_DESC
CAT_LEVEL
PARENT_CAT
and its working great.



Just out of curiosity, please post the CREATE TABLE statement of your categories tree.

krs,
tesu
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb MS SQL Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the MS SQL Forum

All times are GMT -4. The time now is 4:25 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC