I am trying to create a e-commerce/shop system, I've decided to choose the class table inheritance design in SQL design based on this great answer.
Now I've got following table structure:
product ( id [PK], name, description )
product_shirt ( id [PK], product_id [FK], color, size, stock, price )
product_ring ( id [PK], product_id [FK], size, stock, price )
// maybe coming more
This works for representation of the products on a website. But if I wanted to place an order, of a few products how would this work?
If I had one table for my products I could assign the product_id
as foreign key to a relation table, but with multiple tables this seems not possible anymore. Is it even possible with class table inheritance?
I've looked around alot, most answers/tutorial seem to concentrate on the representation of the products, but not on the order of a customer.
Drop fields
product_id
fromproduct_shirt
andproduct_ring
, and make theirid
fields both primary key and foreign key toproduct.id
.Your
order_item
table would contain a foreign key toproduct.id
.When you need to pull information about products in a given order, make a query with a JOIN to
product
only. When you need the full details of a specific product, also JOIN with eitherproduct_shirt
orproduct_ring
depending on the actual product type.Examples: