0% completed
Instagram is a platform where users share content, interact with others through likes, comments, and messages, and manage their profiles. This guide will model its core functionalities in a step-by-step fashion, ensuring efficient and scalable database design.
In this case study, we’ll design a database for Instagram using Entity-Relationship (ER) modeling and map it to a relational schema. By the end, you’ll have a clear understanding of how Instagram’s core functionalities are structured through database design.
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 Post
2. Post and Comment
3. Post and Like
4. User Follows Other Users
5. User and Comment
6. User and Like
7. User and Direct_Message
8. User and Notification
9. User and Group
10. User and Profile_Info
11. User and Profile_Dashboard
Here is the final ER diagram.
When converting the ER diagram into a relational schema for an instagram, we need to carefully handle various components to ensure data integrity and optimal performance. Here are the key considerations:
Entity Tables and Attributes:
Member
for the Group
entity.Foreign Keys and Relationships:
Handling Many-to-Many Relationships:
Here is the final relational schema diagram for Instagram.
Now, let's translate the ER model into SQL tables.
CREATE TABLE User ( User_ID INT PRIMARY KEY, Username VARCHAR(50) UNIQUE, Full_Name VARCHAR(100), Email VARCHAR(100), Phone_Number VARCHAR(15), Date_Joined DATE, Profile_Picture VARCHAR(255) );
CREATE TABLE Post ( Post_ID INT PRIMARY KEY, Caption TEXT, Post_Date DATE, User_ID INT, FOREIGN KEY (User_ID) REFERENCES User(User_ID) );
CREATE TABLE Comment ( Comment_ID INT PRIMARY KEY, Text TEXT, Comment_Date DATE, User_ID INT, Post_ID INT, FOREIGN KEY (User_ID) REFERENCES User(User_ID), FOREIGN KEY (Post_ID) REFERENCES Post(Post_ID) );
CREATE TABLE Like ( Like_ID INT PRIMARY KEY, User_ID INT, Post_ID INT, FOREIGN KEY (User_ID) REFERENCES User(User_ID), FOREIGN KEY (Post_ID) REFERENCES Post(Post_ID) );
CREATE TABLE Follow ( Follow_ID INT PRIMARY KEY, Follower_ID INT, Followed_ID INT, FOREIGN KEY (Follower_ID) REFERENCES User(User_ID), FOREIGN KEY (Followed_ID) REFERENCES User(User_ID) );
CREATE TABLE Direct_Message ( Message_ID INT PRIMARY KEY, Message_Text TEXT, Sent_Date DATE, Sender_ID INT, Recipient_ID INT, FOREIGN KEY (Sender_ID) REFERENCES User(User_ID), FOREIGN KEY (Recipient_ID) REFERENCES User(User_ID) );
CREATE TABLE Notification ( Notification_ID INT PRIMARY KEY, Notification_Text TEXT, Notification_Date DATE, User_ID INT, FOREIGN KEY (User_ID) REFERENCES User(User_ID) );
CREATE TABLE Group ( Group_ID INT PRIMARY KEY, Group_Name VARCHAR(100), Description TEXT, Created_By INT, FOREIGN KEY (Created_By) REFERENCES User(User_ID) );
CREATE TABLE Group_Member ( Group_ID INT, User_ID INT, Join_Date DATE, PRIMARY KEY (Group_ID, User_ID), FOREIGN KEY (Group_ID) REFERENCES Group(Group_ID), FOREIGN KEY (User_ID) REFERENCES User(User_ID) );
CREATE TABLE Profile_Info ( User_ID INT PRIMARY KEY, Bio TEXT, Website VARCHAR(100), Number_of_Posts INT, Number_of_Followers INT, Number_of_Following INT, FOREIGN KEY (User_ID) REFERENCES User(User_ID) );
CREATE TABLE Profile_Dashboard ( User_ID INT PRIMARY KEY, Total_Likes INT, Total_Comments INT, Total_Groups_Joined INT, Total_Posts_Shared INT, FOREIGN KEY (User_ID) REFERENCES User(User_ID) );
.....
.....
.....