Home > Mobile >  Relationship into table (ER-model into relational model)
Relationship into table (ER-model into relational model)

Time:01-02

I am trying to transform ER-model to Relational model and then to SQlite3 code.

In the ER-model I write the relationship for example:

Customer - has - Order

(Where Customer and Order are entities. Has is the relationship)

I know I need to transform the entities into tables and their attributes to fields of that table

But, should I create a table of the "has" relationship between "Order" and "Customer" -entities?

I should, According to this article: https://www.google.com/amp/s/www.geeksforgeeks.org/mapping-from-er-model-to-relational-model/amp/

CodePudding user response:

The question comes down to the relationship type (1-to-1, 1-to-many, or many-to-many).

Quick Answer: In general, we only need a relationship table** when the relationship type is many-to-many.

  • For 1-to-1 or 1-to-many, we don't have to and shouldn't create a relationship table.
  • For many-to-many, we must create a relationship table.

Examples:

For example, say the relationship is 1-to-many (one customer can have many orders). Then, we can add the entity's primary key (pk) on the "1" side as a foreign key (fk) on the "many" side.

Customer: (<customer_id_pk>, <other_fields>)

Order:    (<order_id_pk>, <other_fields>, <customer_id_fk>)

In another example, say the relationship is many-to-many (customers can have many orders, and many customers can share one order for whatever reason). Then, we will need another table to represent the relationship.

Customer: (<customer_id>, <other_fields>)

Order:    (<order_id>, <other_fields>)

Customer_Order_Relationship: (<customer_id>, <order_id>)
  • Related