【问题标题】:Database design for products with multiple categories and multiple sub-features多品类多子特征产品的数据库设计
【发布时间】:2014-06-10 20:38:23
【问题描述】:

假设您正在为网上商店设计一个可搜索的数据库,并且遇到如下情况:


每个product 将有多个不同的categories

例如,将 MARKER 视为product

例如:相同的 MARKER 可能与其category 具有以下所有三个:

  • 玩具
  • 学校文具
  • 艺术配饰

现在,每个category 都有一组对应的任意数量的参数(我们称它们为features),这些参数仅适用于该类别。

因此,对于它的三个product-category 表示中的每一个,上面的MARKER 将有多个不同的product-feature-names 和各自的product-feature-values

例如:我们可能有这些基于类别的feature-names 和特定产品的feature-values

  • PRODUCT-NAME::: CATEGORY-NAME::: Feature1-name = Feature1-value
  • MARKER::: TOY::: AgeGroup = 6-12, 有毒 = 没有, ChristmasSpecial = 是的
  • MARKER::: SCHOOL-STATIONERY::: BulkAvailability =
  • MARKER::: ART-ACCESSORY::: 使用 = 精细绘图, CompatibleSurface = 全部

对于这种情况,最好/最优化的设计是什么?


我的想法是使用三个表,但我不知道这对于以后检索数据是否最有效(也许这应该在两个表中完成,甚至只是一个?):

产品表

id,产品名称

类别表

id、fk_product_id、fk_category_name

特征表

id、fk_category_id、feature_name、feature_value

【问题讨论】:

    标签: mysql database schema


    【解决方案1】:

    让数据库说出是怎么回事。

    yn = yes,no
    age_range = 0-4,4-6,6-12,...
    use = Fine-Drawing,Canoeing,...
    surface_group = All,PaperOrSkin,PaperOrWall,...
    PRODUCT(p,n) -- product [p] is named [n]
    TOY(p,AgeGroup,Toxic,ChristmasSpecial,...)
        -- [p] is a toy for age range [AgeGroup] and whether it's toxic is [Toxic] and whether it's on Christmas special is [ChristmasSpecial] and ...
    SCHOOL-STATIONERY(p,BulkAvailability,...)
        -- [p] is school stationery and its bulk availability is [BulkAvailability] and ...
    ART-ACCESSORY(p,Use,CompatibleSurface,...)
        -- [p] is an art accessory with use [u] and is compatible with surfaces in surface group [CompatibleSurface] and ...
    

    SQL 查询结合了条件和表。查询的含义由上面给出的条件和表含义组合而成。

    在 EAV 和 OTLT 上查看 this stackoverflow post from today 或无数其他人。 Or this. 另外,学习一下数据库设计。也许开始here

    注意,如果是/否选项分开,事情会更简单:

    TOY(p,AgeGroup,...) -- [p] is a toy for age range [AgeGroup] and ...
    TOY-TOXIC(p) -- toy [p] is toxic
    TOY-XMAS-SPECIAL(p) -- toy [p] is on Christmas special
    SCHOOL-STATIONERY(p,...) -- [p] is school stationery and ...
    SCHOOL-STATIONERY-BULK-AVAILABLE(p) -- school stationery [p] is available in bulk
    

    可能您有时也不想为事物组添加标签,而只是简单地说明事物或范围内的第一个和最后一个事物:

    age = 0,1,2,...
    surface = Paper,Skin,Wall,...
    TOY(p,...) -- [p] is a toy and ...
    TOY(p,MinAge,MaxAge) -- [p] is a toy with age minimum [MinAge] and maximum [MaxAge]
    ART-ACCESSORY(p,Use,...) -- [p] is an art accessory with use [u] and ...
    ART-ACCESSORY-COMPATIBLE-SURFACE(p,CompatibleSurface) -- accessory [p] is compatible with surface [CompatibleSurface]
    

    您始终可以更改当前硬连线的信息(类型集和类型、列和表名称)。您始终可以编写甚至不知道什么是硬连线的通用查询,因为所有硬连线名称都是构成 DBMS 元数据的表中的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-27
      • 2019-07-14
      • 2017-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多