•
•
•
•
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
![]() |
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
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
•
•
Join Date: Apr 2008
Posts: 295
Reputation:
Rep Power: 1
Solved Threads: 41
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.
You may draw the tree to see how it works.
Happy treeing!
krs,
tesu
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.
SQL Syntax (Toggle Plain Text)
CREATE TABLE categories ( -- father of main knot is NULL ucat integer NULL, -- a father knot of a tree, it has many son knots lcat integer NOT NULL, -- the son knot. has only one father knot name varchar (30), PRIMARY KEY (lcat), FOREIGN KEY (ucat) REFERENCES categories); -- recursive definition -- Some samples INSERT INTO categories VALUES (NULL,1,'Categories'); INSERT INTO categories VALUES (1,2,'Software'); INSERT INTO categories VALUES (1,3,'Hardware'); INSERT INTO categories VALUES (1,4,'Network'); INSERT INTO categories VALUES (2,5,'Linux'); INSERT INTO categories VALUES (3,6,'PC'); INSERT INTO categories VALUES (3,7,'Printer'); INSERT INTO categories VALUES (6,8,'LCD'); commit -- for example select all subcategories of category hardware SELECT * FROM categories WHERE ucat = ALL (SELECT lcat FROM categories WHERE name = 'Hardware'); /* Result ucat lcat name ------------------ 3 6 PC 3 7 Printer */
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.
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
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
•
•
Join Date: Apr 2008
Posts: 295
Reputation:
Rep Power: 1
Solved Threads: 41
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
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
•
•
Join Date: Apr 2008
Posts: 295
Reputation:
Rep Power: 1
Solved Threads: 41
Sorry, corretion:
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
•
•
•
•
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
•
•
Join Date: Apr 2008
Posts: 295
Reputation:
Rep Power: 1
Solved Threads: 41
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
krs,
tesu
•
•
Join Date: Apr 2008
Posts: 295
Reputation:
Rep Power: 1
Solved Threads: 41
•
•
•
•
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
![]() |
•
•
•
•
•
•
•
•
DaniWeb MS SQL Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
adsense advertising avatar backup blogging bt business connectivity consumers copyright cyber-squatting daniweb database development domains dos economy google hacker hardware ibm ibm. news india internet isp linux marketing medicine microsoft news pay-per-click pc red hat revenue search security server small business smb software sql strider sun survey technology trademark typo-squatting video web youtube
- Previous Thread: One column value to multiple rows
- Next Thread: can't convert data into char


Linear Mode