I have seen an interview question before, which is about table design. How to design the friend relationship table? In the user table, use a field separated by commas to store it or store it in two tables in association. It seems that both are feasible
Should be a many-to-many relationship. A user can have multiple friends. You can also be added as friends by multiple users. Many-to-many relationships, in relational databases, are generally implemented using intermediate tables. At this time, the intermediate table usually only stores user IDs and friend IDs. But for the convenience of business implementation, you can add verification pass, friend group ID, sorting number, etc. to the intermediate table. The difference between this many-to-many intermediate table and the general many-to-many is that the association table of this is itself. That is, the many-to-many relationship between the user table and the user table.
There should also be a grouping field
In fact, there is no need to overcomplicate things, just progress slowly according to your needs.
This is the table structure of my follow function, you can refer to it.
Non-relational database
When I was designing here, I considered the function of groups, so I also converted the friend relationship between two people into a group
Three tables should appear
One is the user table
One is the group table
The other is the user-group correspondence table
Determined through three tables
As far as I know, Weibo’s attention is designed this way
I have seen an interview question before, which is about table design. How to design the friend relationship table? In the user table, use a field separated by commas to store it or store it in two tables in association. It seems that both are feasible
This is enough
But querying is also a problem. Isn’t redis very suitable for this task?
Maybe you can add a flag field to indicate whether you are a two-way friend
Should be a many-to-many relationship.
A user can have multiple friends.
You can also be added as friends by multiple users.
Many-to-many relationships, in relational databases, are generally implemented using intermediate tables.
At this time, the intermediate table usually only stores user IDs and friend IDs. But for the convenience of business implementation, you can add verification pass, friend group ID, sorting number, etc. to the intermediate table.
The difference between this many-to-many intermediate table and the general many-to-many is that the association table of this is itself. That is, the many-to-many relationship between the user table and the user table.