How to design a relational database for user following other users

database-design

I want to design my relational schema to store information about users following other users. So if user1 is following user2 the schema will be as below

user_follow
------------
|user1|user2| -> Column names
------------
|U_Nm1|U_Nm2| -> tuple
------------

So in this way if user1 is following 100 other users I will have 100 rows just for a single user. If I have lots of users following lots of other users my table size will grow enormously. Is there any other way I can design this? I know it is better to use NoSQL for such requirements but I am restricted to use Relational DB.

Best Answer

I know it is better to use NoSQL for such requirements

I'm not sure why you'd think that. After all, question data is very relational in nature.

Here's what I'd do:

User table

  • UserId (primary key)
  • UserName
  • ... etc ...

Following table (join table)

  • FollowingUserId (foreign key to UserTable.UserId)
  • FollowedUserId (foreign key to UserTable.UserId)

Utilizing this join table you'll be able to create a "following" relationship between users. And sure, if a user follows 100 users, then yes there will be 100 rows in the join table for that single user. That is small in reality, though, and with optimizations there would be negligible impact.

Related Topic