mysql - 类似QQ的好友关系表是怎么设计的?
迷茫
迷茫 2017-04-17 14:46:10
0
12
3350

请问一下类似QQ的好友关系表是怎么设计的?
难道只是简单的id,userId,friendId吗?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(12)
一个人流浪

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.

  UserRelationship:
    type: object
    properties:
      id:
        type: integer
        description: Id
      user_id:
        type: integer
        description: 用户Id
      target_user_id:
        type: integer
        description: 目标用户Id
黄舟

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

PHPzhong

This is enough

Peter_Zhu

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template