Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You have at least these five options for modeling the type hierarchy you describe:</p> <ul> <li><p><a href="http://martinfowler.com/eaaCatalog/singleTableInheritance.html" rel="noreferrer">Single Table Inheritance</a>: one table for all Product types, with enough columns to store all attributes of all types. This means <em>a lot</em> of columns, most of which are NULL on any given row.</p></li> <li><p><a href="http://martinfowler.com/eaaCatalog/classTableInheritance.html" rel="noreferrer">Class Table Inheritance</a>: one table for Products, storing attributes common to all product types. Then one table per product type, storing attributes specific to that product type.</p></li> <li><p><a href="http://martinfowler.com/eaaCatalog/concreteTableInheritance.html" rel="noreferrer">Concrete Table Inheritance</a>: no table for common Products attributes. Instead, one table per product type, storing both common product attributes, and product-specific attributes.</p></li> <li><p><a href="http://martinfowler.com/eaaCatalog/serializedLOB.html" rel="noreferrer">Serialized LOB</a>: 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.</p></li> <li><p><a href="http://en.wikipedia.org/wiki/Entity-Attribute-Value_model" rel="noreferrer">Entity-Attribute-Value</a>: 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 <a href="https://stackoverflow.com/questions/tagged/eav">eav tag</a> on StackOverflow for some of the pitfalls.</p></li> </ul> <p>I have written more about this in a presentation, <a href="http://www.slideshare.net/billkarwin/extensible-data-modeling" rel="noreferrer">Extensible Data Modeling</a>.</p> <hr> <p>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 <a href="http://en.wikipedia.org/wiki/Tanstaafl" rel="noreferrer">TANSTAAFL</a>. Here are some of the disadvantages of EAV:</p> <ul> <li>No way to make a column mandatory (equivalent of <code>NOT NULL</code>).</li> <li>No way to use SQL data types to validate entries.</li> <li>No way to ensure that attribute names are spelled consistently.</li> <li>No way to put a foreign key on the values of any given attribute, e.g. for a lookup table.</li> <li>Fetching results in a conventional tabular layout is complex and expensive, because to get attributes from multiple rows you need to do <code>JOIN</code> for each attribute.</li> </ul> <p>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.</p> <p>And in most cases, it's an 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.</p> <p>I'd use EAV only if <em>every row</em> 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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload