https://stackoverflow.com/questions/695752/how-to-design-a-product-table-for-many-kinds-of-product-where-each-product-has-m

You have at least these five options for modeling the type hierarchy you describe:

  • Single Table Inheritance: one table for all Product types, with enough columns to store all attributes of all types. This means a lot of columns, most of which are NULL on any given row.

  • Class Table Inheritance: one table for Products, storing attributes common to all product types. Then one table per product type, storing attributes specific to that product type.

  • Concrete Table Inheritance: no table for common Products attributes. Instead, one table per product type, storing both common product attributes, and product-specific attributes.

  • Serialized LOB: One table for Products, storing attributes common to all product types. One extra column stores a BLOB of semi-structured data, in XML, YAML, JSON, or some other format. This BLOB allows you to store the attributes specific to each product type. You can use fancy Design Patterns to describe this, such as Facade and Memento. But regardless you have a blob of attributes that can't be easily queried within SQL; you have to fetch the whole blob back to the application and sort it out there.

  • Entity-Attribute-Value: One table for Products, and one table that pivots attributes to rows, instead of columns. EAV is not a valid design with respect to the relational paradigm, but many people use it anyway. This is the "Properties Pattern" mentioned by another answer. See other questions with the eav tag on StackOverflow for some of the pitfalls.

I have written more about this in a presentation, Extensible Data Modeling.


Additional thoughts about EAV: Although many people seem to favor EAV, I don't. It seems like the most flexible solution, and therefore the best. However, keep in mind the adage TANSTAAFL. Here are some of the disadvantages of EAV:

  • No way to make a column mandatory (equivalent of NOT NULL).
  • No way to use SQL data types to validate entries.
  • No way to ensure that attribute names are spelled consistently.
  • No way to put a foreign key on the values of any given attribute, e.g. for a lookup table.
  • Fetching results in a conventional tabular layout is complex and expensive, because to get attributes from multiple rows you need to do JOIN for each attribute.

The degree of flexibility EAV gives you requires sacrifices in other areas, probably making your code as complex (or worse) than it would have been to solve the original problem in a more conventional way.

And in most cases, it's unnecessary to have that degree of flexibility. In the OP's question about product types, it's much simpler to create a table per product type for product-specific attributes, so you have some consistent structure enforced at least for entries of the same product type.

I'd use EAV only if every row must be permitted to potentially have a distinct set of attributes. When you have a finite set of product types, EAV is overkill. Class Table Inheritance would be my first choice.

关于EAV的补充想法:虽然很多人似乎都喜欢EAV,但我不喜欢。这似乎是最灵活的解决方案,因此也是最好的。但是,请记住TANSTAAFL这句格言。以下是EAV的一些缺点:

无法强制列(相当于NOT NULL)。

无法使用SQL数据类型验证条目。

无法确保属性名称拼写一致。

无法将外键放在任何给定属性的值上,例如对于查找表。

在传统的表格布局中获取结果是复杂而昂贵的,因为要从多行获取属性,需要对每个属性执行JOIN。

EAV为您提供的灵活性要求您在其他方面做出牺牲,这可能会使您的代码比用更传统的方法解决原始问题更复杂(或者更糟)。

在大多数情况下,没有必要有那么大的灵活性。在OP关于产品类型的问题中,为特定于产品的属性创建每个产品类型的表要简单得多,因此至少对相同产品类型的条目要强制执行一些一致的结构。

只有当每一行都必须被允许具有一组不同的属性时,我才会使用EAV。当你有一组有限的产品类型时,EAV是多余的。类表继承将是我的首选。

How to design a product table for many kinds of product where each product has many parameters的更多相关文章

  1. react ,ant Design UI中table组件合并单元格并展开详情的问题

    需求:购物车订单列表,如图: 一:单元格合并 遇到这种你会怎么办呢?  单元格合并?  还是其他的方法? 下面是我的处理方式,就是在table 组件的columns上处理,这里拿商品举例,其余的类似, ...

  2. ant design pro---ProTable关闭Table上的提示信息

    toolBarRender={false} tableAlertRender={false}

  3. Managing Hierarchical Data in MySQL

    Managing Hierarchical Data in MySQL Introduction Most users at one time or another have dealt with h ...

  4. [转]FluentData

    本文来自:http://fluentdata.codeplex.com/wikipage?title=Fluency&referringTitle=Home Documentation  Fl ...

  5. Snowflake weakness and type2 fact table

    DimProduct DimSubcategory Dimcategory productpk subcategorypk categorypk sku subcategoryName categor ...

  6. Thinking In Design Pattern——MVP模式演绎

    原文<Thinking In Design Pattern——MVP模式演绎>不知为何丢失了,故重新整理了一遍. 目录 What Is MVP Domain Model StubRepos ...

  7. 数据库设计(二)Introduction to Database Design

    原文链接:http://www.datanamic.com/support/lt-dez005-introduction-db-modeling.html Introduction to Databa ...

  8. In-App Purchase Programming Guide----(三) ----Retrieving Product Information

    Retrieving Product Information In the first part of the purchase process, your app retrieves informa ...

  9. Entity Framework – (复数)Plural and (单数)Singular 表名Table names

    By default, the Entity Framework will assume that all of the names of your tables in your database a ...

随机推荐

  1. rabbitMQ消息队列1

    rabbitmq 消息队列 解耦 异步 优点:解决排队问题 缺点: 不能保证任务被及时的执行 应用场景:去哪儿, 同步 优点:保证任务被及时的执行 缺点:排队问题 大并发 Web nginx 1000 ...

  2. 比XGBOOST更快--LightGBM介绍

    xgboost的出现,让数据民工们告别了传统的机器学习算法们:RF.GBM.SVM.LASSO.........现在,微软推出了一个新的boosting框架,想要挑战xgboost的江湖地位.笔者尝试 ...

  3. ELK 日志管理系统,初次尝试记录

    简介: ELK 是一套开源的日志管理平台,主要包括三个组件,可以用于日志的收集.分析.存储和展示工作. ELK 成员:Elasticsearch .Logstash .Kibana( K4 ) ELK ...

  4. malloc realloc calloc free

    自上次发现自己对这几个C函数不熟悉,就打算抽空整理一下,也就现在吧.这几个函数都是跟堆内存打交道的,还有一个好玩的函数--alloca,它是跟栈内存打交道的,我想留在以后研究出好玩点的来,再专门为其写 ...

  5. lnline Hook初试

    简单解释下hook: 钩子(Hook),是Windows消息处理机制的一个平台,应用程序可以在上面设置子程以监视指定窗口的某种消息,而且所监视的窗口可以是其他进程所创建的.当消息到达后,在目标窗口处理 ...

  6. 【UVA1515 算法竞赛入门指南】 水塘【最小割】

    题意: 输入一个h行w列的字符矩阵,草地用“#”表示,洞用"."表示.你可以把草改成洞,每格花费为d,也可以把洞填上草,每格花费为f.最后还需要在草和洞之间修围栏,每条边花费为b. ...

  7. 解剖Nginx·自动脚本篇(4)工具型脚本系列

    目录 auto/have 向自动配置头文件追加可用宏定义(objs/ngx_auto_config.h) auto/nohave 向自动配置头文件追加不可用宏定义(objs/ngx_auto_conf ...

  8. shell判断文件类型和权限

    shell  判断文件类型. -d 文件 判断该文件是否存在,并且是否为目录(是目录为真) -e文件 判断该文件是否存在(存在为真) -f文件 判断该文件是否存在,并且是否为文件(是普通文件为真) - ...

  9. Java基础知识常识总结

    Java基础知识常识总结 1.面向对象的特征以及对他的理解? 封装,继承,多态,如果再增加一条就是抽象. 2.Object有几种方法,分别是什么? 在JDK1.5中的11种方法,分别有: toStri ...

  10. Opencv轮廓计数(学习)

    #include <iostream>#include <opencv2/opencv.hpp>#include <opencv2/xfeatures2d.hpp> ...