数据库设计问题 - 类别/子类别 [英] Database Design Question - Categories / Subcategories

查看:124
本文介绍了数据库设计问题 - 类别/子类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我如何在我的数据库中设计几个表。我有一个表来跟踪分类,一个子类别:

  TABLE类别
CategoryID INT
描述NVARCHAR (500)

TABLE子类别
子类别ID INT
类别ID INT
描述NVARCHAR(500)

类别可能是电子产品,其子类别可能是DVD播放器,电视机等。



我有另一个表将要引用类别/子类别。是否需要引用SubcategoryID?

  TABLE Product 
SubcategoryID INT - 应该是子类别?

有没有更好的方法来做这个或是正确的方法吗?我不是一个数据库设计的人。我正在使用SQL Server 2008 R2,如果这很重要。

解决方案

您的设计是合适的。我是一个数据库的人转向开发人员,所以我可以理解在一个表中有Category和SubCategory的倾向,但KISS不能出错。



除非极端性能或无限层次是一个要求(我猜不是),你很好去。



如果能够将多个子类别与产品相关联是一个要求对于@ Mikael来说,你需要一个这样的设置,通过一个连接/相交表Product_SubCategory创建一个多对多的关系:

  CREATE TABLE产品(ProductID int,Description nvarchar(100))
CREATE TABLE Product_SubCategory(ProductID int,SubCategoryID int)
CREATE TABLE SubCategory(SubCategoryID int,CategoryID int,Description nvarchar (100))
CREATE TABLE类别(CategoryID int,描述nvarchar(100))

希望有帮助...



Eric Tarasoff


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.

解决方案

Your design is appropriate. I'm a database guy turned developer, so I can understand the inclination to have Category and SubCategory in one table, but you can never go wrong by KISS.

Unless extreme performance or infinite hierarchy is a requirement (I'm guessing not), you're good to go.

If being able to associate multiple subcategories with a product is a requirement, to @Mikael's point, you would need a set-up like this which creates a many-to-many relationship via a join/intersect table, Product_SubCategory:

CREATE TABLE Product (ProductID int, Description nvarchar(100))
CREATE TABLE Product_SubCategory (ProductID int, SubCategoryID int)
CREATE TABLE SubCategory (SubCategoryID int, CategoryID int, Description nvarchar(100))
CREATE TABLE Category (CategoryID int, Description nvarchar(100))

Hope that helps...

Eric Tarasoff

这篇关于数据库设计问题 - 类别/子类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆