Sample Header Ad - 728x90

Solving supertype-subtype relationship without sacrificing data consistency in a relational database

7 votes
4 answers
3032 views
Trying to get better at designing databases, I noticed I'm always stuck trying to solve variations of the exact same problem. Here is an example using common requirements: 1. An online store sells different categories of product. 2. The system must be able to retrieve the list of all product categories, say food and furniture. 3. A customer may order any product and retrieve his order history. 4. System must store specific properties depending on the product category ; say the expiration_date and calories for any food product and manufacture_date for any furniture product. If it wasn't for requirement 4, the model could be quite straightforward: enter image description here Problem is trying to solve requirement 4. I thought of something like this: enter image description here In this approach, the relationships product-furniture and product-food are *supertype-subtype* (or superclass-subclass) associations; the primary key of the subtype is also a foreign key to the supertype primary key. However, this approach can not guarantee the category referenced via a foreign key to the product will be consistent with its actual subtype. For instance, nothing stops me from setting food category to a product tuple having a subtype row in the Furniture table. I read various articles about inheritance in modelling relational databases, especially this one and this one which were very helpful but didn't solve my problem for the reason mentioned above. But whatever model I come with, I'm never satisfied with the data consistency. How can I solve requirement 4 without sacrificing data consistency ? Am I going all wrong here ? If so, what would be the best way to solve this problem based on these requirements ?
Asked by user174739 (71 rep)
Mar 15, 2019, 02:41 PM
Last activity: Aug 3, 2024, 03:49 PM