0% found this document useful (0 votes)
12 views5 pages

ER Model To Relational Database

The document explains how to convert an ER model into a relational database using an e-commerce application as an example. It outlines relationships between entities such as customers, carts, and products, detailing one-to-one and many-to-many relationships and the use of foreign keys and junction tables. The document emphasizes the importance of primary keys and how they uniquely identify rows in the database tables.

Uploaded by

Prachi More
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

ER Model To Relational Database

The document explains how to convert an ER model into a relational database using an e-commerce application as an example. It outlines relationships between entities such as customers, carts, and products, detailing one-to-one and many-to-many relationships and the use of foreign keys and junction tables. The document emphasizes the importance of primary keys and how they uniquely identify rows in the database tables.

Uploaded by

Prachi More
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

10/18/24, 8:02 AM Revolutionizing the Job Market | NxtWave

ER Model to Relational Database


In the previous cheatsheet, we've learnt to build an ER model for a given scenario. Now, let's convert this ER model to Relational Databa
Let's consider the same e-commerce application.

E-commerce Application

In a typical e-commerce application,

Customer has only one cart. A cart belongs to only one customer

Customer can add products to cart

Cart contains multiple products

Customer can save multiple addresses in the application for further use like selecting delivery address

Entity Type to Table

Primary key: A minimal set of attributes (columns) in a table that uniquely identifies rows in a table.

In the following tables, all the ids are primary keys as they uniquely identify each row in the table.

https://learning.ccbp.in/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=514fa3b0-52eb-4e67-8e4f-21ac0618d716&s_id=c99aefb6-a4… 1/5
10/18/24, 8:02 AM Revolutionizing the Job Market | NxtWave

Relationships

Relation Between Customer and Address - One to Many Relationship

A customer can have multiple addresses.

An address is related to only one customer.

We store the primary key of a customer in the address table to denote that the addresses are related to a particular customer.
This new column/s in the table that refer to the primary key of another table is called Foreign Key.

https://learning.ccbp.in/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=514fa3b0-52eb-4e67-8e4f-21ac0618d716&s_id=c99aefb6-a4… 2/5
10/18/24, 8:02 AM Revolutionizing the Job Market | NxtWave

Here,

customer_id is the foreign key that stores id (primary key) of customers.

Relation Between Cart and Customer - One to One Relationship

A customer has only one cart.

A cart is related to only one customer.

This is similar to one-to-many relationship. But, we need to ensure that only one cart is associated to a customer

Relation Between Cart and Products - Many to Many Relationship

A cart can have many products.

A product can be in many carts.

Here, we cannot store either the primary key of a product in the cart table or vice versa.
To store the relationship between the cart and product tables, we use a Junction Table.

https://learning.ccbp.in/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=514fa3b0-52eb-4e67-8e4f-21ac0618d716&s_id=c99aefb6-a4… 3/5
10/18/24, 8:02 AM Revolutionizing the Job Market | NxtWave

Note

We store the properties related to a many-to-many relationship in the junction table. For example, quantity of each product in the
cart should be stored in the junction table cart_product

E-commerce Usecase: ER Model to Relational Database

Following ER model is represented as the below tables in the relational database.

ER Model

Relational Database

https://learning.ccbp.in/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=514fa3b0-52eb-4e67-8e4f-21ac0618d716&s_id=c99aefb6-a4… 4/5
10/18/24, 8:02 AM Revolutionizing the Job Market | NxtWave

https://learning.ccbp.in/course?c_id=5d03f5a4-a285-4e52-8a57-18f718c4859f&t_id=514fa3b0-52eb-4e67-8e4f-21ac0618d716&s_id=c99aefb6-a4… 5/5

You might also like