How to design a product table for many kinds of product where each product has many parameters

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

 

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

  • 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.less

  • 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.ide

  • 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.post

  • 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.fetch

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


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:ui

  • 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.this

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是多餘的。類表繼承將是個人首選。

相關文章
相關標籤/搜索