2015-02-11 76 views
0

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

我设计一个数据库,让谁已登记在我的网站创建列表人的过程中来的。这些列表将由用户组成。该功能与Twitter Lists类似。我使用PHP和MySQL。

我尝试到目前为止

到目前为止,我想出了2个表:

USERS(USER_ID,名字,姓氏)

LISTS(LIST_ID,标题,说明)

我现在意识到我需要第三个表将连接两个,但我挣扎ling提出一个应该有的领域清单。我的第一个想法是我需要id,list_iduser_id领域,但我不确定这是否是正确的方法。

我的问题

我需要什么领域在我的第三个表,以便能够连接我USERSLISTS表以创建?

+0

旁注:建议您的所有表都包含一个与每个(公共字段)都有关系的字段,这将使查询更容易。 – 2015-02-11 17:50:38

+0

@ Fred-ii-不会为第三张桌子服务吗? – henrywright 2015-02-11 17:56:03

+0

我想会的。我总是喜欢让每个桌子都有一个共同的领域,但那只是我;-) – 2015-02-11 17:57:29

回答

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是您需要做这项工作的多种胶水。 id字段是不必要的。

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; 
+0

感谢Creator ID的想法,但是不是更好的做法是在LISTS表中创建creator_id吗? – henrywright 2015-02-11 18:06:05

+0

你完全正确。我的错误,创作者ID完全是不必要的。我相信David K-J的回答更合适 – 2015-02-11 18:25:50