Common one-to-many table for multiple entities

后端 未结 4 1387
梦谈多话
梦谈多话 2020-12-10 07:13

Suppose I have two tables, Customer and Vendor. I want to have a common address table for customer and vendor addresses. Customers and Vendors can both have one to many ad

4条回答
  •  生来不讨喜
    2020-12-10 07:56

    I'd say the missing piece of the puzzle is the "is a" relationship that is often overlooked in data modeling; this is distinct from the familiar "has a" relationship. An "is a" relationship is similar to an inheritance relationship in a object oriented design. To model this you'll need a base table that represents the common attributes of vendors and customers. For example, we could call the base table "Organizations":

    Organizations       Vendors               Customers
    --------------      ---------------------  ---------------------
    OrganizationID(PK)  OrganizationID(FK/PK)  OrganizationID(FK/PK)
    AddressID1(FK)
    AddressID2(FK)
    

    In this example Vendor "is a" organization, and Customer "is a" organization, whereas an organization "has a" address. The Organizations, Vendors, and Customers tables share a common key and a common key sequence enforced by referential integrity.

提交回复
热议问题