0% completed
An e-commerce platform is a digital marketplace where customers can browse, purchase, and review products. The platform involves multiple entities like users, products, orders, payments, and delivery management. Designing a robust database is critical for handling large-scale operations efficiently.
In this case study, we’ll model an e-commerce platform’s database using Entity-Relationship (ER) diagrams and then map it to a relational schema.
For our case study, let's outline the key functionalities that the HMS should support:
We will build the ER diagram for the Hospital Management System through the following four steps.
Entities represent objects or concepts in the system that have data stored about them.
1. User and Address
2. Product and Category
3. Product and Inventory
4. User and Order
5. Order and Order_Item
6. Order and Payment
7. Order and Delivery
8. Product and Review
9. User and Review
10. Order and Coupon
11. Product and Order_Item
Here is the final ER diagram.
When converting the ER diagram into a relational schema for our Ecommerce Platform, we need to carefully handle various components to ensure data integrity and optimal performance. Here are the key considerations:
Entity Tables and Attributes:
Foreign Keys and Relationships:
Here is the final relational model for the Ecommerce Platform.
Now, let's translate the ER model into SQL tables.
CREATE TABLE User ( User_ID INT PRIMARY KEY, Full_Name VARCHAR(100), Email VARCHAR(100), Phone_Number VARCHAR(15), Password VARCHAR(50), Date_Joined DATE );
CREATE TABLE Address ( Address_ID INT PRIMARY KEY, User_ID INT, Street VARCHAR(255), City VARCHAR(50), Country VARCHAR(50), Postal_Code VARCHAR(10), Address_Type VARCHAR(20), FOREIGN KEY (User_ID) REFERENCES User(User_ID) );
CREATE TABLE Product ( Product_ID INT PRIMARY KEY, Name VARCHAR(100), Description TEXT, Price DECIMAL(10, 2), Category_ID INT, FOREIGN KEY (Category_ID) REFERENCES Category(Category_ID) );
CREATE TABLE Category ( Category_ID INT PRIMARY KEY, Name VARCHAR(100), Description TEXT );
CREATE TABLE Inventory ( Inventory_ID INT PRIMARY KEY, Product_ID INT, Quantity_In_Stock INT, Reorder_Level INT, FOREIGN KEY (Product_ID) REFERENCES Product(Product_ID) );
CREATE TABLE Order ( Order_ID INT PRIMARY KEY, Order_Date DATE, User_ID INT, Coupon_ID INT Total_Amount DECIMAL(10, 2), Status VARCHAR(20), FOREIGN KEY (User_ID) REFERENCES User(User_ID), FOREIGN KEY (Coupon_ID) REFERENCES User(Coupon_ID), );
CREATE TABLE Order_Item ( Order_Item_ID INT PRIMARY KEY, Order_ID INT, Product_ID INT, Quantity INT, Subtotal DECIMAL(10, 2), FOREIGN KEY (Order_ID) REFERENCES Order(Order_ID), FOREIGN KEY (Product_ID) REFERENCES Product(Product_ID) );
CREATE TABLE Payment ( Payment_ID INT PRIMARY KEY, Order_ID INT, Payment_Date DATE, Payment_Method VARCHAR(50), Payment_Status VARCHAR(20), FOREIGN KEY (Order_ID) REFERENCES Order(Order_ID) );
CREATE TABLE Review ( Review_ID INT PRIMARY KEY, User_ID INT, Product_ID INT, Rating INT, Review_Text TEXT, Review_Date DATE, FOREIGN KEY (User_ID) REFERENCES User(User_ID), FOREIGN KEY (Product_ID) REFERENCES Product(Product_ID) );
CREATE TABLE Delivery ( Delivery_ID INT PRIMARY KEY, Order_ID INT, Delivery_Status VARCHAR(20), Delivery_Date DATE, Delivery_Partner VARCHAR(100), FOREIGN KEY (Order_ID) REFERENCES Order(Order_ID) );
CREATE TABLE Coupon ( Coupon_ID INT PRIMARY KEY, Code VARCHAR(50), Discount_Percentage DECIMAL(5, 2), Expiry_Date DATE, Maximum_Usage INT );
.....
.....
.....