In a scenario where we have customers, vendors, & branches which all have common attributes such as an address, which is a better solution and why?
Solution 1:
-
Customer table
-
Vendor table
-
Branch table
-
Address table with nullable foreign keys for CustomerID, VendorID, BranchID
Solution 2:
-
Entity table
-
Customer table with EntityID
-
Vendor table with EntityID
-
Branch table with EntityID
-
Address table with EntityID and a EntityType flag of ‘C’, ‘V’, or ‘B’
Solution 3 (suggested by AJC):
-
Customer table
-
Vendor table
-
Branch table
-
Address table
-
CustomerAddress xref table between Customer and Address
-
VendorAddress xref table between Vendor and Address
-
BranchAddress xref table between Branch and Address
Solution 4 (suggested by 9000)
-
Customer table
-
Vendor table
-
Branch table
-
CustomerAddress with FK to Customer
-
VendorAddress with FK to Vendor
-
BranchAddress with FK to Branch
-
vwAddress which UNION ALLs each of the above address tables, and includes a type flag (‘B’,’C’,’V’)
Notes:
Each customer, vendor, or branch could have multiple addresses, but should have at least one.
If an “entity” is both a customer and a vendor, it could have separate addresses for each role.
Would like to know if a customer is also a vendor.
C) None of the above.
The correct way to do it would be to have an Address Table and a CustomerAddress, VendorAddress and BranchAddress respectively to join each C V B entity with one or many addresses.
For attributes that are one-to-one, obviously there is no need for the extra tables, you just add the Id to the main table (AddressId)…