CREATE DATABASE SocialMediaAnalytics (SQL Project)
CREATE DATABASE SocialMediaAnalytics (SQL Project)
+
USE SocialMediaAnalytics;
2. Tables Creation
Table: Users
sql
Copy code
CREATE TABLE Users (
UserID INT AUTO_INCREMENT PRIMARY KEY,
UserName VARCHAR(50) NOT NULL,
Email VARCHAR(100) UNIQUE NOT NULL,
DateOfBirth DATE,
JoinDate TIMESTAMP DEFAULT
CURRENT_TIMESTAMP
);
Table: Posts
sql
Copy code
CREATE TABLE Posts (
PostID INT AUTO_INCREMENT PRIMARY KEY,
UserID INT NOT NULL,
Content TEXT NOT NULL,
PostDate TIMESTAMP DEFAULT
CURRENT_TIMESTAMP,
FOREIGN KEY (UserID) REFERENCES
Users(UserID)
);
Table: Likes
sql
Copy code
CREATE TABLE Likes (
LikeID INT AUTO_INCREMENT PRIMARY KEY,
UserID INT NOT NULL,
PostID INT NOT NULL,
LikeDate TIMESTAMP DEFAULT
CURRENT_TIMESTAMP,
FOREIGN KEY (UserID) REFERENCES
Users(UserID),
FOREIGN KEY (PostID) REFERENCES
Posts(PostID)
);
Table: Comments
sql
Copy code
CREATE TABLE Comments (
CommentID INT AUTO_INCREMENT PRIMARY
KEY,
PostID INT NOT NULL,
UserID INT NOT NULL,
CommentText TEXT NOT NULL,
CommentDate TIMESTAMP DEFAULT
CURRENT_TIMESTAMP,
FOREIGN KEY (UserID) REFERENCES
Users(UserID),
FOREIGN KEY (PostID) REFERENCES
Posts(PostID)
);
3. Insert Data
Insert into Users
sql
Copy code
INSERT INTO Users (UserName, Email, DateOfBirth)
VALUES
('Ali Khan', '[email protected]', '1995-05-10'),
('Sara Malik', '[email protected]', '1998-09-20'),
('Usman Javed', '[email protected]', '2000-01-15');
Insert into Posts
sql
Copy code
INSERT INTO Posts (UserID, Content)
VALUES
(1, 'My first post on this platform!'),
(2, 'Loving this social media project.'),
(3, 'This is a great day to start coding!');
Insert into Likes
sql
Copy code
INSERT INTO Likes (UserID, PostID)
VALUES
(2, 1),
(3, 1),
(1, 2);
Insert into Comments
sql
Copy code
INSERT INTO Comments (PostID, UserID,
CommentText)
VALUES
(1, 2, 'Great post, Ali!'),
(1, 3, 'Nice to see you here.'),
(2, 1, 'Thank you, Sara!');
4. Logical Queries
Retrieve All Posts with User Information
sql
Copy code
SELECT Posts.PostID, Users.UserName, Posts.Content,
Posts.PostDate
FROM Posts
JOIN Users ON Posts.UserID = Users.UserID;
Count Total Likes on Each Post
sql
Copy code
SELECT Posts.PostID, Posts.Content,
COUNT(Likes.LikeID) AS TotalLikes
FROM Posts
LEFT JOIN Likes ON Posts.PostID = Likes.PostID
GROUP BY Posts.PostID;
Fetch All Comments on a Specific Post
sql
Copy code
SELECT Comments.CommentText, Users.UserName,
Comments.CommentDate
FROM Comments
JOIN Users ON Comments.UserID = Users.UserID
WHERE Comments.PostID = 1;
5. Update Data
Update a User's Email
sql
Copy code
UPDATE Users
SET Email = '[email protected]'
WHERE UserName = 'Ali Khan';
Update a Post's Content
sql
Copy code
UPDATE Posts
SET Content = 'Updated content of my first post!'
WHERE PostID = 1;
6. Delete Data
Delete a Like
sql
Copy code
DELETE FROM Likes
WHERE LikeID = 1;
Delete a User (and cascade to related posts, likes,
comments if foreign keys are set with ON DELETE
CASCADE)
sql
Copy code
DELETE FROM Users
WHERE UserID = 3;
7. Drop Tables
Drop the Likes Table
sql
Copy code
DROP TABLE Likes;
Drop the Entire Database
sql
Copy code
DROP DATABASE SocialMediaAnalytics;
1. Database Creation
sql
Copy code
CREATE DATABASE SocialMediaAnalytics;
USE SocialMediaAnalytics;
2. Simplified Tables
Table: Users
Columns: UserID, UserName, Email
sql
Copy code
CREATE TABLE Users (
UserID INT AUTO_INCREMENT PRIMARY KEY,
UserName VARCHAR(50),
Email VARCHAR(100)
);
Table: Posts
Columns: PostID, UserID, Content
sql
Copy code
CREATE TABLE Posts (
PostID INT AUTO_INCREMENT PRIMARY KEY,
UserID INT,
Content TEXT,
FOREIGN KEY (UserID) REFERENCES
Users(UserID)
);
Table: Likes
Columns: LikeID, UserID, PostID
sql
Copy code
CREATE TABLE Likes (
LikeID INT AUTO_INCREMENT PRIMARY KEY,
UserID INT,
PostID INT,
FOREIGN KEY (UserID) REFERENCES
Users(UserID),
FOREIGN KEY (PostID) REFERENCES
Posts(PostID)
);
Table: Comments
Columns: CommentID, PostID, UserID,
CommentText
sql
Copy code
CREATE TABLE Comments (
CommentID INT AUTO_INCREMENT PRIMARY
KEY,
PostID INT,
UserID INT,
CommentText TEXT,
FOREIGN KEY (UserID) REFERENCES
Users(UserID),
FOREIGN KEY (PostID) REFERENCES
Posts(PostID)
);
3. Insert Data
Insert into Users
sql
Copy code
INSERT INTO Users (UserName, Email)
VALUES
('Ali', '[email protected]'),
('Sara', '[email protected]'),
('Usman', '[email protected]');
Insert into Posts
sql
Copy code
INSERT INTO Posts (UserID, Content)
VALUES
(1, 'Hello world!'),
(2, 'This is my first post.'),
(3, 'Loving this platform!');
Insert into Likes
sql
Copy code
INSERT INTO Likes (UserID, PostID)
VALUES
(2, 1),
(3, 1),
(1, 2);
Insert into Comments
sql
Copy code
INSERT INTO Comments (PostID, UserID,
CommentText)
VALUES
(1, 2, 'Great post!'),
(1, 3, 'Keep it up!'),
(2, 1, 'Thanks for sharing.');
4. Logical Queries
Retrieve All Posts
sql
Copy code
SELECT Posts.PostID, Users.UserName, Posts.Content
FROM Posts
JOIN Users ON Posts.UserID = Users.UserID;
Count Total Likes on Each Post
sql
Copy code
SELECT PostID, COUNT(*) AS TotalLikes
FROM Likes
GROUP BY PostID;
Fetch Comments on a Specific Post
sql
Copy code
SELECT CommentText, UserID
FROM Comments
WHERE PostID = 1;
5. Update Data
Update a User's Email
sql
Copy code
UPDATE Users
SET Email = '[email protected]'
WHERE UserName = 'Ali';
Update a Post's Content
sql
Copy code
UPDATE Posts
SET Content = 'Updated post content!'
WHERE PostID = 1;
6. Delete Data
Delete a Like
sql
Copy code
DELETE FROM Likes
WHERE LikeID = 1;
Delete a User
sql
Copy code
DELETE FROM Users
WHERE UserID = 3;
7. Drop Tables
Drop the Likes Table
sql
Copy code
DROP TABLE Likes;
Drop the Entire Database
sql
Copy code
DROP DATABASE SocialMediaAnalytics;
1. Users Table
Purpose:
This table stores the basic information about the users of
the social media platform.
Columns:
UserID: A unique number assigned to each user. It is
the primary key, meaning no two users can have the
same ID.
Example: 1, 2, 3.
UserName: The name of the user. This helps identify
who is using the platform.
Example: 'Ali', 'Sara', 'Usman'.
Email: The email address of the user. It’s optional for
now but useful for contact.
Example: '[email protected]'.
Example Table:
UserID UserName Email
1 Ali [email protected]
2 Sara [email protected]
3 Usman [email protected]
2. Posts Table
Purpose:
This table stores all the posts created by users on the
platform.
Columns:
PostID: A unique number for each post. It is the
primary key.
Example: 1, 2, 3.
UserID: The ID of the user who created the post. This
is a foreign key linked to the Users table.
Example: 1 (Ali's post), 2 (Sara's post).
Content: The text/content of the post created by the
user.
Example: 'Hello world!', 'This is my first post.'.
Relationships:
Each post is created by a user. The UserID column
connects the post to the specific user in the Users table.
Example Table:
PostID UserID Content
1 1 Hello world!
2 2 This is my first post.
3 3 Loving this platform!
3. Likes Table
Purpose:
This table records which users have liked which posts.
Columns:
LikeID: A unique number for each like. It is the
primary key.
Example: 1, 2, 3.
UserID: The ID of the user who liked the post. This is
a foreign key linked to the Users table.
Example: 2 (Sara liked), 3 (Usman liked).
PostID: The ID of the post that was liked. This is a
foreign key linked to the Posts table.
Example: 1 (Ali's post was liked).
Relationships:
Each like connects a user (UserID) to a post (PostID).
Example Table:
LikeID UserID PostID
1 2 1
2 3 1
3 1 2
4. Comments Table
Purpose:
This table stores all comments made by users on posts.
Columns:
CommentID: A unique number for each comment. It
is the primary key.
Example: 1, 2, 3.
PostID: The ID of the post that the comment belongs
to. This is a foreign key linked to the Posts table.
Example: 1 (Ali's post).
UserID: The ID of the user who made the comment.
This is a foreign key linked to the Users table.
Example: 2 (Sara commented).
CommentText: The actual text/content of the
comment.
Example: 'Great post!', 'Keep it up!'.
Relationships:
Each comment connects a user (UserID) to a post
(PostID).
Example Table:
CommentID PostID UserID CommentText
1 1 2 Great post!
2 1 3 Keep it up!
3 2 1 Thanks for sharing.
How These Tables Work Together
1. Users is the core table:
Every table (Posts, Likes, Comments) uses UserID to
identify who created or interacted with the content.
2. Posts table stores user content:
Each post is linked to a specific user via the UserID
column.
3. Likes table tracks user reactions:
It connects a user (UserID) to a post (PostID), showing
which user liked which post.
4. Comments table records feedback:
It links users and posts, storing what users have said
about specific posts.
Database Diagram
Here’s how these tables are related:
scss
Copy code
Users (UserID) ---> Posts (UserID)
Users (UserID) ---> Likes (UserID)
Users (UserID) ---> Comments (UserID)
Posts (PostID) ---> Likes (PostID)
Posts (PostID) ---> Comments (PostID)