HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

How to design a relationship database table to store friendship relationship?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
designstoredatabasehowfriendshiptablerelationship

Problem

I want to design a table to store friendship relationship in my web project

It should satisfy at least the following 4 conditions:

who send the add-friend request e.g.(if A TO B then this column will be A)

who receive the add-friend request e.g.(if A TO B then this column will be B)

current status e.g.(0 denotes rejected whereas 1 denotes accepted or 2 denotes unprocessed

our friend relationship is bilateral

If any of you are experienced with this , any suggestion is welcomed

my current design (I think bad right now) is like this these are the columns

frienshipId  
fromUserId  
toUserId  
status  
requestTime

Solution

I'd create a table a lot like the one that you have. I'm using SQL Server data types and syntax, you may need to tweak depending on your platform.

CREATE TABLE FriendStatus
(FriendStatusId BIGINT PRIMARY KEY IDENTITY(1,1),
FromUserId BIGINT,
ToUserId BIGINT,
StatusId TINYINT,
SentTime DATETIME2,
ResponseTime DATETIME2);


Indexing of the table will be critical as the table grows to the tens and hundreds of millions.

Code Snippets

CREATE TABLE FriendStatus
(FriendStatusId BIGINT PRIMARY KEY IDENTITY(1,1),
FromUserId BIGINT,
ToUserId BIGINT,
StatusId TINYINT,
SentTime DATETIME2,
ResponseTime DATETIME2);

Context

StackExchange Database Administrators Q#1688, answer score: 9

Revisions (0)

No revisions yet.