简体   繁体   English

制作Mysql数据库(外键)

[英]making Mysql Database (Foreign keys)

I have a task where I need to make a Database like social networks.我有一个任务,我需要制作一个像社交网络一样的数据库。 I need to have 3 tables users, friendships, and status.我需要有 3 个表用户、友谊和状态。

My problem is that I need to have a relationship with table friendships and table users.我的问题是我需要与表朋友和表用户建立关系。

I tried to do the task but I just don't get it.我试图完成任务,但我就是不明白。 Also table status needs to have a relationship with table users (user_id ).表状态也需要与表用户(user_id)有关系。 I only know that table friendship needs to have 2 foreign keys.我只知道表友谊需要有2个外键。

Here is also an EER diagram of what is I tried to do.这也是我尝试做的事情的 EER 图。

在此处输入图片说明

You want two foreign keys on two different columns of friendships pointing to users :您需要两个不同的friendships列上的两个外键指向users

create table friendships (
    request_sender   int not null,
    request_receiver int not null,
    ...
    constraint request_sender_users_fk  
        foreign key (request_sender)
        references users(user_id),
    constraint request_sender_receiver_fk  
        foreign key (request_receiver)
        references users(user_id)
);

Note: I don't think that it makes sense to have three different columns to store the status of the "friendship request", as in your ERD: friendship_accepted , friendship_declined and friendship_on_hold .注意:我认为使用三个不同的列来存储“友谊请求”的状态是没有意义的,就像在您的 ERD 中一样: friendship_acceptedfriendship_declinedfriendship_on_hold Presumably a relation can only have one status at a time, so a single column should be good enough, with en enum or the-like.据推测,一个关系一次只能有一个状态,所以单列应该足够好,带有 en enum之类的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM