关系数据库设计:用户列表

时间:2015-02-11 17:48:57

标签: php mysql database-design

背景

我正在设计一个数据库,允许在我的网站上注册的人创建列表。这些列表将由用户组成。该功能类似于Twitter Lists。我使用的是PHP和MySQL。

到目前为止我的尝试

到目前为止,我已经提出了两个表格:

USERS (user_id,first_name,last_name)

LISTS (list_id,title,description)

我现在意识到我需要第三个表来连接这两个,但我正在努力想出一个应该有的字段列表。我的第一个想法是我需要idlist_iduser_id字段,但我不确定这是否是正确的做法。

我的问题

我需要在第三个表格中创建哪些字段才能连接我的USERSLISTS表格?

2 个答案:

答案 0 :(得分:0)

我相信,你需要:

Creator ID - To store the id of the user that created the list;

List ID - To store the id of the List;

User ID - To store the id of the user in that list;

答案 1 :(得分:0)

你所追求的是一个数据透视表。 您最终会得到您指定的三个表格。

USERS(
    user_id integer not null primary key,
    first_name text not null,
    last_name text not null
 )

LISTS(
    list_id integer not null primary key,
    title text not null,
    description text,
    created_by integer references USERS
)

USERS_LISTS (
    user integer not null references USERS,
    list integer not null references LISTS,
    unique (user, list)
)

USER_LISTS是您需要的多种粘合剂。 <{1}}字段是不必要的。