Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 705
  • Last Modified:

How to manage subcategories?

Hello -

I need some help/advice on how to handle subcategories in my application without creating some maintenance nightmare.
I currently have the following tables, docs, MainCategories, SubCategories

Each document today, in the Docs table, is assigned to a MainCategory. The application lists the MainCategories which the end user can then select to see the assigned documents.

We would like to implement SubCategories into the mix for applicable MainCategories. This means that some docs would be assigned to a MainCategory and associated SubCategories if one or more is available. In many instances a single document would be assigned to the safety MainCategory, for example, and then the SubCategories of Warehouse, Order Pullers, Order Processors, etc. The safety MainCategory may have 20 *number from thin air* SubCategories associated with it.

Docs Table
------------
ID (AutoNumber)
Title (Text)
Detail (Text)
MainCategory (ID from MainCategories)
SubCategory (yet to be defined)

MainCategories Table
-------------------------
ID (AutoNumber)
Category (Text)
subCategory (Yes/No - determines if this category has sub categories associated with it)

SubCategories Table
------------------------
ID (AutoNumber)
MainCategory (ID from MainCategories)
SubCategory (Text)
Status (Yes/No - determines whether or not the subcategory is available to be assigned to)

Database is SQL 2000 and CF production version is 6.1 with 7 in testing

If I were storing only one main and sub category this would be no problem. I'm having trouble grasping what to do with multiple subcategories without adding additional fields to the Docs table. Is it possible for me to write a list of subcategory ID's to the Docs table (1,2,3,4,5) and then return this value and parse it so I can list the associated subcategories thus leading to the documents assigned to the subcategory?

I have done very little where lists and arrays are concerned so if you can offer some code along with any suggestions on how else this may/could be handled it would be greatly appreciated.

TIA
0
Activar
Asked:
Activar
  • 4
  • 3
  • 2
1 Solution
 
dgrafxCommented:
The concept would be this:
First, you have mainCategories.
Even though you may have mainCategories that contain no subCategories
You can Never have a subCategory that is not in a mainCategory

Then you have subCategories.
Even though you may have subCategories that contain no docs
You can Never have a doc that is not in a subCategory

Then you have docs - which are assigned to subCategories - not mainCategories.

Setup your relationships via diagrams from sql server enterprise mgr
a relationship enforcing db integrity will make your life easier.

Your tables in brief - I removed some irrelevant "stuff"
MainCategories Table
-------------------------
ID (AutoNumber)
Category (Text)
Sort (numeric value you set to define display order - a value of 0(zero) will "hide")

SubCategories Table
------------------------
ID (AutoNumber)
MainCategory (ID from MainCategories)
SubCategory (Text)
Sort (numeric value you set to define display order - a value of 0(zero) will "hide")

Docs Table
------------
ID (AutoNumber)
Title (Text)
Detail (Text)
SubCategory (ID from SubCategories)
Sort (numeric value you set to define display order - a value of 0(zero) will "hide")

ex. of how Sort would be used
  Select fields
  From MainCategories
  Where Sort > 0
  order by Sort
this could also be used for your "if available to assign subCategories to".
those will value of 0 could not be assigned subCategories
et cetera

good luck ...
 
0
 
ActivarAuthor Commented:
dgrafx,

Thanks for the comment.

>The concept would be this:
>First, you have mainCategories.
>Even though you may have mainCategories that contain no subCategories
>You can Never have a subCategory that is not in a mainCategory

True

>Then you have subCategories.
>Even though you may have subCategories that contain no docs
>You can Never have a doc that is not in a subCategory

I'm not understanding this

>Then you have docs - which are assigned to subCategories - not mainCategories.

Why? I'm guessing this is based on the table layout you are recommending.
0
 
ExpertAdminCommented:
I think you are making it more complicated than it needs to be. With relational databases, you don't have to do things like have a flag to tell if there are subcategories, and I think all of your categories (main and sub) can go in the same table. Consider this layout:


Docs Table
------------
ID (AutoNumber)
Title (varchar(100))
Detail (Text)

Categories Table
-------------------------
ID (AutoNumber)
Category (Text)

DocumentCategories
------------------------------
DocID (int)
CategoryID (int)
IsMain (bit)

The data for these tables will look like this:

>>>Docs Table                

ID        TITLE               DETAILS
===========================
1          TestDoc1     ...some details...
2          TestDoc2     ....details....


>>>Categories Table


ID        Category
=================
1         Category1
2         Category2
3         Category3

>>>DocumentCategories

DocID                 CategoryID       IsMain
===========================
1                                1                0
1                                2                1
2                                1                1
2                                2                0
2                                3                0


What the DocumentCategories does is tie documents to multiple categories through their ID feilds.

To get a list of documents and their categories, you could do this:

---------------------------------------------------
SELECT Documents.DocID,
           Documents.Title,
           Categories.ID AS CategoryID,
           Categories.Category,
           IsMain
FROM Documents
             INNER JOIN DocumentCategories
                       ON DocumentCategories.DocumentID = Documents.ID
             INNER JOIN Categories
                        ON Categories.ID = DocumentCategories.CategoryID
ORDER BY IsMain DESC
---------------------------------------------------

which would yeild this output:

DocID              Title             CategoryID     Category       IsMain
===========================================
1                TestDoc1             2                Category2          1
1                TestDoc1             1                Category1          0
2                TestDoc2             1                Category1          1
2                TestDoc2             2                Category2          0
2                TestDoc2             3                Category3          0

If you want to get the categories for a single document, just add the critieria:

---------------------------------------------------
SELECT Documents.DocID,
           Documents.Title,
           Categories.ID AS CategoryID,
           Categories.Category,
           IsMain
FROM Documents
             INNER JOIN DocumentCategories
                       ON DocumentCategories.DocumentID = Documents.ID
             INNER JOIN Categories
                        ON Categories.ID = DocumentCategories.CategoryID

WHERE Documents.ID = 2

ORDER BY IsMain DESC
---------------------------------------------------


Does this make sense?

M@
       




0
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 
ActivarAuthor Commented:
ExpertAdmin - that is looking pretty good. I'm gonna chew on it for a minute or two and try a few things.
I'll be posting back.

Thanks for the response.
0
 
dgrafxCommented:
as expertadmin says you could combine tables or all 3 for that matter.
this is a design preference.
To answer the question you asked in your last post:
This is database design 101
The concept is the entire reason for categories, subcategories, etc being "categorized"
Otherwise you could just throw everything into the db and let users have at it!
It is a system of order.

Docs Table
ID        TITLE   Subcategory
====================================
1        Doc1        5
2        Doc2        5
3        Doc3        6
4        Doc4        7

SubCats Table
ID        TITLE      Category
==============================
5        SubCat5         3
6        SubCat6         3
7        SubCat7         4

Cats Table
ID        TITLE        
========================
3        Cat3          
4        Cat4        
         

0
 
ActivarAuthor Commented:
dgrafx,

Thanks again for the response. Apparently I need DB 101, no argument there.
I also need the time and resources which are not available this is why I look often to the Experts on this site.

0
 
dgrafxCommented:
good luck ...
0
 
ActivarAuthor Commented:
ExpertAdmin -

I've accepted your answer. Thank you very much for breaking it down for me.
0
 
ExpertAdminCommented:
Anytime.

M@
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now