Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Followers/following database structure

Tags:

My website has a followers/following system (like Twitter's). My dilemma is creating the database structure to handle who's following who.

What I came up with was creating a table like this:

 id  |  user_id  |  followers |  following
  1  |    20     |  23,58,84  |  11,156,27
  2  |    21     |  72,35,14  |  6,98,44,12
 ... |   ...     |    ...     |     ...

Basically, I was thinking that each user would have a row with columns for their followers and the users they're following. The followers and people they're following would have their user id's separated by commas.

Is this an effective way of handling it? If not, what's the best alternative?

like image 621
Burrows Avatar asked Nov 01 '13 19:11

Burrows


People also ask

How do you store followers and following in database?

Instead, use a join table. That's a separate table where each row has a user id and a follower id. This allows things to be stored in one place, allows indexing and joining, and also allows you to add additional columns to that row, for example to show when the following relationship started.

What is a follower database?

The follower database feature allows you to attach a database located in a different cluster to your Azure Data Explorer cluster. The follower database is attached in read-only mode, making it possible to view the data and run queries on the data that was ingested into the leader database.

What is database structure example?

The first is an RDBMS, or a relational database management system. Examples of RDBMSs include SQL Server, MySQL, and PostgreSQL. The second and most important tool is SQL (structured query language). This is the language we use to define the tables, columns, indexes, and other artifacts in our database.


1 Answers

That's the worst way to do it. It's against normalization. Have 2 seperate tables. Users and User_Followers. Users will store user information. User_Followers will be like this:

id | user_id | follower_id
1  | 20      | 45
2  | 20      | 53
3  | 32      | 20

User_Id and Follower_Id's will be foreign keys referring the Id column in the Users table.

like image 161
regulus Avatar answered Sep 28 '22 02:09

regulus