The main question: "How to store heterogeneous subtypes of the parent class?". There are several options - the one you have chosen is a bit hybrid, which is not necessarily a bad thing.
The best description I've seen in this thread is in Craig Larman's book, Using UML and Templates, although he writes from an object-oriented, not database, perspective.
First of all: the way you set up the “options” may not be the way you want — it means that the “price” and “stock” are moving together, while they are separate separate pieces of data. I would think about expanding them into my own tables - "variant_price" and "variant_stock".
Secondly, the option you have chosen to represent functions is usually called "Object Attribute Value" or EAV. He got a big advantage by allowing him to store data without knowing his schema during development, but makes any boolean queries a huge pain - imagine that you are looking for all red XL shirts.
There are 3 alternatives in the relational world (this is based on Larman's book):
subtype for each option. Thus, you create the table "variant_tshirt" with size, color, etc. and "variant_trouser" with size, color, inside the leg, etc. This keeps the tables beautiful and self-describing, but makes your SQL a huge mess - it has to change for each subtype.
separate table with all possible columns: in this case you have one table with all possible fields for all subtypes. Here, your SQL remains much simpler - but the table becomes a huge mess, and you depend on your client application to “know” that trousers have legs inside the attribute, and t-shirts do not.
a common attribute table with subtypes that store their unique values in their own tables. In this model, if you only have trousers and T-shirts, you have a table of “options” with size and color, and also a table of “pants” with inside legs.
Each option has advantages and disadvantages - especially in a situation where you do not know in advance which subtypes you will need, the first option is the easiest at the end of the database, but it creates a bit of a mess for the client code.
Outside of SQL, you can use XML - using XPath, you can easily execute logical queries or NoSQL, but NoSQL will not be my favorite here, most of them are conceptually based on key value relationships that make logical queries quite difficult.
source share