Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database Design Question - Categories / Subcategories

I have a question for how I would design a few tables in my database. I have a table to track Categories and one for Subcategories:

TABLE Category
    CategoryID INT
    Description NVARCHAR(500)

TABLE Subcategory
    SubcategoryID INT
    CategoryID INT
    Description NVARCHAR(500)

A category might be something like Electronics, and its Subcategories might be DVD Players, Televisions, etc.

I have another table that is going to be referencing the Category/Subcategory. Does it need to reference the SubcategoryID?

TABLE Product
    SubcategoryID INT  -- should this be subcategory?

Is there a better way to do this or is this the right way? I'm not much of a database design guy. I'm using SQL Server 2008 R2 if that matters.

like image 819
Dismissile Avatar asked Mar 21 '11 21:03

Dismissile


People also ask

What is category in database?

Categories are child tables of a parent table. For example, an employee record (the parent) might contain a job-type column; category tables for that column might include engineer, technician, and administrator.

What is database design and its types?

Database design is the organization of data according to a database model. The designer determines what data must be stored and how the data elements interrelate. With this information, they can begin to fit the data to the database model. Database management system manages the data accordingly.


1 Answers

As long as Sub-Categories are never repeated in a different Category, and especially if they have different attributes, then your proposed method is good.

The one problem can come when you are adding/editing Products, and you don't have a field for Category, even though you probably want a control where the user can edit the Category.

like image 161
Judah Sali Avatar answered Sep 19 '22 11:09

Judah Sali