Search code examples
database-designdata-modelingmultiple-inheritanceclass-diagramerd

How to Model Inheritance


I am a bit confused in choosing an approach to model the following relationships.

Scenario:

  1. The system has a User.

  2. The User can perform 2 functions :

    • He Can be a normal User who can buy products listed on the website.
    • He Can Subscribe to Sell Products on the website.
    • He Can Subscribe for the Delivery Service to Delivery his orders placed
      from the website
    • He can Subscribe for Both i.e. Sell Products as well as subscribe for Delivery Service.

How can i model the user who can do both the functions of a seller as well as place delivery requests.

                               User
                                 |
                                 |
         _________________________________________________
        |                                                 |
      Seller (ONLY)                                   Delivery (ONLY)

                      **SELLER AND DELIVERY** 

Solution

  • You have two options how to implement the class hierarchy formed by Seller and DeliverySubscriber being subtypes of User. If your subclasses/subtables do not have many additional properties/columns, one would normally use the Single Table Inheritance approach with just one table User and a type (or category) column for discriminating between users who are sellers and users who are delivery subscribers. Otherwise, if there are many additional properties/columns, it's better to use the Joined Table Inheritance approach, where subtables (representing subclasses) are joined to their supertable via their primary key being also a foreign key referencing the supertable. You can read more about these two forms of implementing class hierarchies with tables in my book chapter Subtyping and Inheritance.