Domanda

Sto cercando di decidere il modo migliore per modellare un rapporto di record in un database relazionale. E 'il modello amico / follow classica:

~~~~

Un utente può avere zero a molti amici.
Un utente può avere zero a molti seguaci.

Amici e seguaci sono entrambi gli utenti stessi.

~~~~~

Qual è il modo migliore per modellare questo?

Grazie!

È stato utile?

Soluzione

Gli utenti (UserId, ...)
Abbonamento (Subscriber, Publisher)
Amicizia (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
)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top