在RDBMS中建模好友和关注者 [英] Modeling Friends and Followers in an RDBMS
本文介绍了在RDBMS中建模好友和关注者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
~~~~
A用户可以拥有零到很多朋友。
用户可以拥有零到多个追踪者。
朋友和关注者都是用户本身。
~~~~~
为此建模最好的方法是什么?
谢谢!
解决方案
用户(UserId,...)
订阅(订阅者,发布商)
友谊(FirstUser,SecondUser)
CREATE TABLE用户(
UserID int not null主键,
)
CREATE TABLE订阅(
订阅者int not null引用用户(UserID),
发布者int not null引用用户(UserID),
约束ck_NotEqual检查(订阅者<> Publisher)
)
CREATE TABLE友谊(
FirstUser int not null引用用户(UserID),
SecondUser int not null引用用户(UserID),
约束ck_Order检查(FirstUser< SecondUser) - 由于友谊反映
)
I'm trying to decide on the best way to model a relationship of records in a relational database. It's the classic friend/follow model:
~~~~
A User can have zero to many friends.
A User can have zero to many followers.
Friends and followers are both Users themselves.
~~~~~
What's the best way to model this?
Thanks!
解决方案
Users (UserId, ...)
Subscription (Subscriber, Publisher)
Friendship (FirstUser, SecondUser)
CREATE TABLE Users (
UserID int not null primary key,
...
)
CREATE TABLE Subscription (
Subscriber int not null references Users(UserID),
Publisher int not null references Users(UserID),
constraint ck_NotEqual check (Subscriber <> Publisher)
)
CREATE TABLE Friendship (
FirstUser int not null references Users(UserID),
SecondUser int not null references Users(UserID),
constraint ck_Order check (FirstUser < SecondUser) -- since friendship is reflective
)
这篇关于在RDBMS中建模好友和关注者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文