Guide To Design Database For Social Network System In MySQL

By bhagwatchouhan
Guide To Design Database For Social Network System In MySQL

This tutorial provides the complete steps to design a database schema of the Social Network System to manage the users, friends, followers, groups, and messages.

The Entity Relationship Diagram or visual database design is shown below.

 

Social Network System Database Design

Social Network System Database

You can also visit the popular tutorials including How To Install MySQL 8 on Ubuntu 20.04 LTS, How To Install MySQL 8 on Windows, How To Install MySQL Workbench On Ubuntu, How To Install MySQL 8 With Workbench On Windows 10, RBAC Database in MySql, Blog Database in MySql, Quiz Database in MySQL, Poll & Survey Database in MySQL, Online Shopping Cart Database in MySQL, Inventory Database in MySQL, and Learn Basic SQL Queries In MySQL.

Social Network System Database

The very first step is to create the Social Network System Database. It can be created using the query as shown below.

CREATE SCHEMA `sns` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

I have used the character set utf8mb4 to support a wide range of characters.

User Table

In this section, we will design the User Table to store user information. Below mentioned is the description of all the columns of the User Table.

IdThe unique id to identify the user.
First NameThe first name of the user.
Middle NameThe middle name of the user.
Last NameThe last name of the user.
MobileThe mobile number of the user. It can be used for login and registration purposes.
EmailThe email of the user. It can be used for login and registration purposes.
Password HashThe password hash generated by the appropriate algorithm. We must avoid storing plain or encrypted passwords.
Registered AtThis column can be used to calculate the life of the user with the application.
Last LoginIt can be used to identify the last login of the user.
IntroThe brief introduction of the User.
ProfileUser details.

The User Table with the appropriate constraints is shown below.

CREATE TABLE `sns`.`user` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`firstName` VARCHAR(50) NULL DEFAULT NULL,
`middleName` VARCHAR(50) NULL DEFAULT NULL,
`lastName` VARCHAR(50) NULL DEFAULT NULL,
`username` VARCHAR(50) NULL DEFAULT NULL,
`mobile` VARCHAR(15) NULL,
`email` VARCHAR(50) NULL,
`passwordHash` VARCHAR(32) NOT NULL,
`registeredAt` DATETIME NOT NULL,
`lastLogin` DATETIME NULL DEFAULT NULL,
`intro` TINYTEXT NULL DEFAULT NULL,
`profile` TEXT NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `uq_username` (`username` ASC),
UNIQUE INDEX `uq_mobile` (`mobile` ASC),
UNIQUE INDEX `uq_email` (`email` ASC) );

User Friend Table

In this section, we will design the User Friend Table to store the user friends. The friend status can be used to track the friendship status and type can be used to specify the type of friendship. Below mentioned is the description of all the columns of the User Friend Table.

IdThe unique id to identify the friendship.
Source IdThe user id to identify the user who initiated the friendship.
Target IdThe user id of the friend.
TypeThe type to classify friends. It can be School, College, or Acquaintance.
StatusThe status can be New, Rejected, or Active.
Created AtIt stores the date and time at which the friend request was initiated.
Updated AtIt stores the date and time at which the friend request was updated.
NotesIt stores the notes specific to the friendship.

The User Friend Table with the appropriate constraints is shown below.

CREATE TABLE `sns`.`user_friend` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`sourceId` BIGINT NOT NULL,
`targetId` BIGINT NOT NULL,
`type` SMALLINT NOT NULL DEFAULT 0,
`status` SMALLINT NOT NULL DEFAULT 0,
`createdAt` DATETIME NOT NULL,
`updatedAt` DATETIME NULL DEFAULT NULL,
`notes` TEXT NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `idx_friend_source` (`sourceId` ASC),
CONSTRAINT `fk_friend_source`
FOREIGN KEY (`sourceId`)
REFERENCES `sns`.`user` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);

ALTER TABLE `sns`.`user_friend`
ADD INDEX `idx_friend_target` (`targetId` ASC);
ALTER TABLE `sns`.`user_friend`
ADD CONSTRAINT `fk_friend_target`
FOREIGN KEY (`targetId`)
REFERENCES `sns`.`user` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;

ALTER TABLE `sns`.`user_friend` ADD UNIQUE `uq_friend`(`sourceId`, `targetId`);

User Follower Table

In this section, we will design the User Follower Table to store the user followers. The follower type can be used to specify the type of follower among Like, Dislike, or Follow. Below mentioned is the description of all the columns of the User Follower Table.

IdThe unique id to identify the follower.
Source IdThe user id to identify the follower user.
Target IdThe user id to identify the following user.
TypeThe type to classify followers. It can be Like, Dislike, or Follow.
Created AtIt stores the date and time at which the follower was created.
Updated AtIt stores the date and time at which the follower was updated.

The User Follower Table with the appropriate constraints is shown below.

CREATE TABLE `sns`.`user_follower` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`sourceId` BIGINT NOT NULL,
`targetId` BIGINT NOT NULL,
`type` SMALLINT NOT NULL DEFAULT 0,
`createdAt` DATETIME NOT NULL,
`updatedAt` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `idx_ufollower_source` (`sourceId` ASC),
CONSTRAINT `fk_ufollower_source`
FOREIGN KEY (`sourceId`)
REFERENCES `sns`.`user` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);

ALTER TABLE `sns`.`user_follower`
ADD INDEX `idx_ufollower_target` (`targetId` ASC);
ALTER TABLE `sns`.`user_follower`
ADD CONSTRAINT `fk_ufollower_target`
FOREIGN KEY (`targetId`)
REFERENCES `sns`.`user` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;


ALTER TABLE `sns`.`user_follower` ADD UNIQUE `uq_ufollower`(`sourceId`, `targetId`, `type`);

User Message Table

In this section, we will design the User Message Table to store the user chat messages. Below mentioned is the description of all the columns of the User Message Table.

IdThe unique id to identify the message.
Source IdThe user id to identify the sender.
Target IdThe user id to identify the receiver.
MessageThe message body.
Created AtIt stores the date and time at which the message was created.
Updated AtIt stores the date and time at which the message was updated.

The User Message Table with the appropriate constraints is shown below.

CREATE TABLE `sns`.`user_message` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`sourceId` BIGINT NOT NULL,
`targetId` BIGINT NOT NULL,
`message` TINYTEXT NULL DEFAULT NULL,
`createdAt` DATETIME NOT NULL,
`updatedAt` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `idx_umessage_source` (`sourceId` ASC),
CONSTRAINT `fk_umessage_source`
FOREIGN KEY (`sourceId`)
REFERENCES `sns`.`user` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);

ALTER TABLE `sns`.`user_message`
ADD INDEX `idx_umessage_target` (`targetId` ASC);
ALTER TABLE `sns`.`user_message`
ADD CONSTRAINT `fk_umessage_target`
FOREIGN KEY (`targetId`)
REFERENCES `sns`.`user` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;

User Post Table

In this section, we will design the User Post Table to store the user posts. The sender might be required to allow the other users with appropriate permissions to post on the user wall. Below mentioned is the description of all the columns of the User Post Table.

IdThe unique id to identify the post.
User IdThe user id to identify the corresponding user.
Sender IdThe sender id to identify the corresponding sender.
MessageThe message body.
Created AtIt stores the date and time at which the post was created.
Updated AtIt stores the date and time at which the post was updated.

The User Post Table with the appropriate constraints is shown below.

CREATE TABLE `sns`.`user_post` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`userId` BIGINT NOT NULL,
`senderId` BIGINT NOT NULL,
`message` TINYTEXT NULL DEFAULT NULL,
`createdAt` DATETIME NOT NULL,
`updatedAt` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `idx_upost_user` (`userId` ASC),
CONSTRAINT `fk_upost_user`
FOREIGN KEY (`userId`)
REFERENCES `sns`.`user` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);

ALTER TABLE `sns`.`user_post`
ADD INDEX `idx_upost_sender` (`senderId` ASC);
ALTER TABLE `sns`.`user_post`
ADD CONSTRAINT `fk_upost_sender`
FOREIGN KEY (`senderId`)
REFERENCES `sns`.`user` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;

Group Table

In this section, we will design the Group Table to store the group data. Below mentioned is the description of all the columns of the Group Table.

IdThe unique id to identify the group.
Created ByThe user id to identify the user who registered the group.
Updated ByThe user id to identify the user who updated the group.
TitleThe group title.
Meta TitleThe meta title to be used for browser title and SEO purposes.
SlugThe slug to form the unique URL.
SummaryThe summary to mention the key highlights.
StatusThe status of the group can be New, Approved, Active, or Blocked.
Created AtIt stores the date and time at which the group is created.
Updated AtIt stores the date and time at which the group is updated.
ProfileThe column used to store the profile details of the group.
ContentThe column used to store the additional details of the group.

It uses the column status to track the status of the group. The status can be either New, Approved, Active, or Blocked. The Group Table with the appropriate constraints is as shown below.

CREATE TABLE `sns`.`group` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`createdBy` BIGINT NOT NULL,
`updatedBy` BIGINT NOT NULL,
`title` VARCHAR(75) NOT NULL,
`metaTitle` VARCHAR(100) NULL,
`slug` VARCHAR(100) NOT NULL,
`summary` TINYTEXT NULL,
`status` SMALLINT NOT NULL DEFAULT 0,
`createdAt` DATETIME NOT NULL,
`updatedAt` DATETIME NULL DEFAULT NULL,
`profile` TEXT NULL DEFAULT NULL,
`content` TEXT NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `uq_slug` (`slug` ASC),
INDEX `idx_group_creator` (`createdBy` ASC),
CONSTRAINT `fk_group_creator`
FOREIGN KEY (`createdBy`)
REFERENCES `sns`.`user` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);

ALTER TABLE `sns`.`group`
ADD INDEX `idx_group_modifier` (`updatedBy` ASC);
ALTER TABLE `sns`.`group`
ADD CONSTRAINT `fk_group_modifier`
FOREIGN KEY (`updatedBy`)
REFERENCES `sns`.`user` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;

Group Meta

The Group Meta Table can be used to store additional information about groups including the group banner URL etc. Below mentioned is the description of all the columns of the Group Meta Table.

IdThe unique id to identify the group meta.
Group IdThe group id to identify the parent group.
KeyThe key identifying the meta.
ContentThe column used to store the group's metadata.

The Group Meta Table with the appropriate constraints is as shown below.

CREATE TABLE `sns`.`group_meta` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`groupId` BIGINT NOT NULL,
`key` VARCHAR(50) NOT NULL,
`content` TEXT NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `idx_meta_group` (`groupId` ASC),
UNIQUE INDEX `uq_meta_group` (`groupId` ASC, `key` ASC),
CONSTRAINT `fk_meta_group`
FOREIGN KEY (`groupId`)
REFERENCES `sns`.`group` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

Group Member Table

In this section, we will design the Group Member Table to store the group members. The member status can be used to track the membership status and member role can be used to identify the member privileges. Below mentioned is the description of all the columns of the Group Member Table.

IdThe unique id to identify the membership.
Group IdThe group id to identify the corresponding group.
User IdThe user id to identify the corresponding user.
Role IdThe role to check user privileges.
StatusThe status can be New, Rejected, Active, or Blocked.
Created AtIt stores the date and time at which the member request was initiated.
Updated AtIt stores the date and time at which the member was updated.
NotesIt stores the notes specific to the membership.

The Group Member Table with the appropriate constraints is shown below.

CREATE TABLE `sns`.`group_member` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`groupId` BIGINT NOT NULL,
`userId` BIGINT NOT NULL,
`type` SMALLINT NOT NULL DEFAULT 0,
`status` SMALLINT NOT NULL DEFAULT 0,
`createdAt` DATETIME NOT NULL,
`updatedAt` DATETIME NULL DEFAULT NULL,
`notes` TEXT NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `idx_member_group` (`groupId` ASC),
CONSTRAINT `fk_member_group`
FOREIGN KEY (`groupId`)
REFERENCES `sns`.`group` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);

ALTER TABLE `sns`.`group_member`
ADD INDEX `idx_member_user` (`userId` ASC);
ALTER TABLE `sns`.`group_member`
ADD CONSTRAINT `fk_member_user`
FOREIGN KEY (`userId`)
REFERENCES `sns`.`user` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;

ALTER TABLE `sns`.`group_member` ADD UNIQUE `uq_friend`(`groupId`, `userId`);

Group Follower Table

In this section, we will design the Group Follower Table to store the group followers. The follower type can be Like, Dislike, or Follow. Below mentioned is the description of all the columns of the Group Follower Table.

IdThe unique id to identify the follower.
Group IdThe group id to identify the corresponding group.
User IdThe user id to identify the corresponding user.
TypeThe follower type can be Like, Dislike, or Follow.
Created AtIt stores the date and time at which the follower was created.
Updated AtIt stores the date and time at which the follower was updated.

The Group Follower Table with the appropriate constraints is shown below.

CREATE TABLE `sns`.`group_follower` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`groupId` BIGINT NOT NULL,
`userId` BIGINT NOT NULL,
`type` SMALLINT NOT NULL DEFAULT 0,
`createdAt` DATETIME NOT NULL,
`updatedAt` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `idx_gfollower_group` (`groupId` ASC),
CONSTRAINT `fk_gfollower_group`
FOREIGN KEY (`groupId`)
REFERENCES `sns`.`group` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);

ALTER TABLE `sns`.`group_follower`
ADD INDEX `idx_gfollower_user` (`userId` ASC);
ALTER TABLE `sns`.`group_follower`
ADD CONSTRAINT `fk_gfollower_user`
FOREIGN KEY (`userId`)
REFERENCES `sns`.`user` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;

ALTER TABLE `sns`.`group_follower` ADD UNIQUE `uq_friend`(`groupId`, `userId`, `type`);

Group Message Table

In this section, we will design the Group Message Table to store the group chat messages. Below mentioned is the description of all the columns of the Group Message Table.

IdThe unique id to identify the message.
Group IdThe group id to identify the corresponding group.
User IdThe user id to identify the corresponding user.
MessageThe message body.
Created AtIt stores the date and time at which the message was created.
Updated AtIt stores the date and time at which the message was updated.

The Group Message Table with the appropriate constraints is shown below.

CREATE TABLE `sns`.`group_message` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`groupId` BIGINT NOT NULL,
`userId` BIGINT NOT NULL,
`message` TINYTEXT NULL DEFAULT NULL,
`createdAt` DATETIME NOT NULL,
`updatedAt` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `idx_gmessage_group` (`groupId` ASC),
CONSTRAINT `fk_gmessage_group`
FOREIGN KEY (`groupId`)
REFERENCES `sns`.`group` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);

ALTER TABLE `sns`.`group_message`
ADD INDEX `idx_gmessage_user` (`userId` ASC);
ALTER TABLE `sns`.`group_message`
ADD CONSTRAINT `fk_gmessage_user`
FOREIGN KEY (`userId`)
REFERENCES `sns`.`user` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;

Group Post Table

In this section, we will design the Group Post Table to store the group posts. The members having the appropriate role can post to the group. Below mentioned is the description of all the columns of the Group Post Table.

IdThe unique id to identify the post.
Group IdThe group id to identify the corresponding group.
User IdThe user id to identify the corresponding user.
MessageThe message body.
Created AtIt stores the date and time at which the post was created.
Updated AtIt stores the date and time at which the post was updated.

The Group Post Table with the appropriate constraints is shown below.

CREATE TABLE `sns`.`group_post` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
`groupId` BIGINT NOT NULL,
`userId` BIGINT NOT NULL,
`message` TINYTEXT NULL DEFAULT NULL,
`createdAt` DATETIME NOT NULL,
`updatedAt` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `idx_gpost_group` (`groupId` ASC),
CONSTRAINT `fk_gpost_group`
FOREIGN KEY (`groupId`)
REFERENCES `sns`.`group` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);

ALTER TABLE `sns`.`group_post`
ADD INDEX `idx_gpost_user` (`userId` ASC);
ALTER TABLE `sns`.`group_post`
ADD CONSTRAINT `fk_gpost_user`
FOREIGN KEY (`userId`)
REFERENCES `sns`.`user` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;

Summary

In this tutorial, we have discussed the database design of a Social Network System to manage the users, friends, followers, messages, and groups.

You may submit your comments to join the discussion. You may also be interested in designing the database of the Blog, Shopping Cart, and Poll & Survey applications. The complete database schema is also available on GitHub.

share on :

Profile picture for user bhagwatchouhan
bhagwatchouhan