需要数据库表设计帮助
CREATE TABLE MemberProfile
(
memberID,
memberTypeID,
aboutMeID,
memberRegID,
memberProfileSettingsID,
lastLogin datetime,
PRIMARY KEY(memberID)
)
memberTypeID 代表基本会员、黄金会员、白银会员等
aboutMeID 代表向人们介绍该用户的帖子/话题。每个用户都会有“关于我”页面。属性有:标题、正文等。
memberRegID 代表用户名、密码、电子邮件、接收通知、isActive 等
memberProfileSettingsID 代表选项例如隐藏收藏列表、隐藏年龄等
这样的设计好吗?我不想将所有字段放在一张表“MemberProfile”中。
CREATE TABLE MemberProfile
(
memberID,
memberTypeID,
aboutMeID,
memberRegID,
memberProfileSettingsID,
lastLogin datetime,
PRIMARY KEY(memberID)
)
memberTypeID represents Basic Member, Gold Member, Silver Member, etc
aboutMeID represents a post/thread that would tell people about this user. Every user would have About Me page. Attributes are: title, body, etc.
memberRegID represents username, password, email, receive notification, isActive, etc
memberProfileSettingsID represents options such as hide favorite list, hide age, etc
Is this a good design? I didn't want to put all fields in one table "MemberProfile".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您与 MemberID 和所有属性之间存在强制的 1:1 关系,这意味着您可以将它们全部存储在同一个表 (memberprofile) 中。这样做将使编码变得更加容易。
要知道,很难强制执行真正的 1:1 关系,但可以使用触发器来完成。查看带有 [database-design] 标签的问题。最近有两个关于1:1关系的问题。
我对标识符有一些评论。如果您使用其他表来进行成员设置、登录信息等,则应该从 MemberProfile 中删除键,而使用这些表中的 MemberID,并使用外键引用 MemberProfile。这将让您直接使用这些表,而无需加入会员配置文件。
就像这样:
现在我想起来了,我认为 AboutMeID 不是 1:1。您的意思是链接到用户发布的论坛帖子来介绍自己吗?在这种情况下,基数为 1:0,因为您通常必须在创建帖子之前注册:)
您可以将其实现为 MemberProfile 中的 NULLable 列,或者实现为以 member_id 作为主键和论坛帖子 ID 的单独表。如果您期望大多数行最终都具有价值,我个人会选择可为空的列。
是否对 (1:1) 属性使用一个或多个表是一种实现选择。根据您的查询模式,它可能会影响性能好或坏。
It looks like you have a mandatory 1:1 relation with the MemberID and all of the attributes, which means that you could store them all in the same table (memberprofile). Doing so will make coding a lot easier.
Know that it is hard to enforce true 1:1 relations, but it can be done using triggers. Have a look at questions with the [database-design] tag. There are two recent questions on 1:1 relations.
I have some comments on the identifiers. If you go with additional tables for member settings,login information etcetera, you should drop the keys from MemberProfile, and instead use the MemberID in those tables, referencing MemberProfile with a foreign key. That will let you use those tables directly without joining to memberprofile.
Like this:
Now that I'm thinking of it, I think AboutMeID isn't 1:1. Do you mean to link to a forum post made by the user to introduce himself? In that case, it is 1:0 cardinality, because you typically have to register before creating a post :)
You can implement this as a NULLable column in MemberProfile, or as a separate table with member_id as primary key and the ID of the forum post. If you expect most rows to eventually have value I would personally go with the nullable column.
Whether to use one or several tables for your (1:1) attributes is an implementation choice. Depending on your query patterns, it can impact performance good or bad.
听起来 memberTypeID 是您选择创建的四个 ID 之间唯一的一对多关系。因此,建议像您所做的那样将其分解为相关表,以消除重复的元素(“第一范式”)。
其他三个似乎并不指一对多关系(它们中的每一个听起来都像如果它对于主表是唯一的),所以我会将其他三个的内容移回到主表中。
It sounds like memberTypeID is the only one-to-many relationship among the four ID's that you chose to create. So it's advisable to break that out to a related table as you've done, to eliminate repeated elements ('First Normal Form')
The other three don't seem to refer to one-to-many relationships (each of them sounds as if it'll be unique to the primary table) so I'd move the contents of the other three back into the primary table.