将单个类型与多个类型相关
假设我是马克·扎克伯格,现在是 2003 年。
我正在构建一个社交网络网站 (thefacebook.com),我需要建议。
该网站的重点是用户配置文件,但它也支持企业、大学等的配置文件。我们将这些其他配置文件称为“页面配置文件”。
两种类型的配置文件(用户配置文件和页面配置文件)都支持电话号码。
我应该如何将这些电话号码存储在数据库中?
以下设计是否有意义:
phones(phone_id, phone_number);
users_phones(user_id, phone_id, ...);
pages_phones(page_id, phone_id, ...);
您会建议替代设计吗?
PS:我相信我对 Facebook 的事情很感兴趣,所以通过参与这个问题,你可能正在参与创造历史。
Pretend I'm Mark Zuckerberg and it's 2003.
I'm building a social network website (thefacebook.com) and I need an advice.
The emphasis of the site is on user profiles but it also supports profiles of businesses, universities, etc. We call these other profiles - page profiles.
Both types of profiles (user profiles and page profiles) support phone numbers.
How should I store these phone numbers in the database?
Does the following design make sense:
phones(phone_id, phone_number);
users_phones(user_id, phone_id, ...);
pages_phones(page_id, phone_id, ...);
Woud you suggest an alternative design?
PS: I believe I'm onto something big with this Facebook thing, so by participating in this question you might be taking part of building history.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许:
也就是说,你真的关心手机作为一个实体吗?或者电话号码仅仅是用户和页面的有用属性?
对于每个用户和每个页面的多个电话:
Maybe:
i.e. do you really care about phones as an entity? Or is a phone number merely a useful attribute of a user and of a page?
For multiple phones per user and per page:
在个人资料电话号码表中。
有关此类模式的更多信息。还有更多信息 。
In a table of profile phone numbers.
More information about this kind of pattern. And even more information.
有两个概念混淆了。
(1):您正在使用多个相似但不完全相同的实体(“配置文件”),即使您有默认实体或主配置文件。
某些属性或字段对于所有实体都是相同的。对于每种实体,某些属性或字段是不同的。这种场景或模式称为“泛化”,通常被转换为具有附加子表的主共享表:
http://en.wikipedia.org/wiki/Class_diagram#Generalization
配置文件类型 {profiletype_id, profiletype_name}
配置文件 {profile_id, profiletype_id, profiletype_name}
profile_user{profile_id, profiletype_firstname, profiletype_lastname, profiletype_ssn, ...}
profile_company{profile_id, profiletype_companyname, ...}
profile_rockband{profile_id, profiletype_bandname, ...}
(2):您有一个可以重复多次的字段,但是仍然有一个默认值。
通常;我将 1 或 2 个“默认”电话号码添加到主“配置文件”/“表”中,并为电话创建一个附加表。
个人资料 {profile_id、profiletype_id、profiletype_name、profiletype_defaultphonenumber}
电话 {phone_id、profile_id、phone_number}
There are 2 concepts that are mixing up.
(1): is that you are working with several similar but not exactly equal entities ("profiles"), even if you have a default entity or main profile.
Some properties or fields are the same for all entities. Some properties or fields are different for each kind of entity. This scenario or pattern is called "generalization", and usually is traslated into a main shared table with additional subtables:
http://en.wikipedia.org/wiki/Class_diagram#Generalization
profiletypes {profiletype_id, profiletype_name}
profile {profile_id, profiletype_id, profiletype_name}
profile_user{profile_id, profiletype_firstname, profiletype_lastname, profiletype_ssn, ...}
profile_company{profile_id, profiletype_companyname, ...}
profile_rockband{profile_id, profiletype_bandname, ...}
(2): You have a field that can be repeat several times, but still have one as default.
Usually; I add 1 or 2 "default" phone numbers to the main "profile" / "table", and create an additional table for phones.
profile {profile_id, profiletype_id, profiletype_name, profiletype_defaultphonenumber}
phones {phone_id, profile_id, phone_number}