Search code examples
databaseentity-relationship

Mapping n-ary relationships to relational tables


What be the correct way to map an n-ary relationship like the one I included? Would I do something like this?

Store(Store, Address, Phone Number)

Market(Product ID, Product Name, Product Type, Price)

Sales(Order ID, Quantity, Customer Name)

Inventory(Product ID, Quantity)

It is my understanding you take the primary key from each side that has "many" and include the other attributes as foreign keys. So, would you do the manages table like this?

Manages(Order ID, Product ID, Store Name)

enter image description here


Solution

  • based on the image ... you can also put INVENTORY_ID , STORE_NAME and PRODUCT_ID inside sales table ... so sales would become something like this :

    Sales(Order ID, Quantity, Customer Name, INVENTORY_ID, PRODUCT_ID, STORE_NAME)

    but since it makes the table a bit messy ... we can extract the relation and make a new table for it and would become something like this ...

    Manages(Order ID, STORE_NAME, PRODUCT_ID, INVENTORY_ID) and remember all primary_key of this table is combination of all the columns.