关于数据库设计
我需要一些关于数据库设计的想法。我有大约 5 个字段用于用户的基本信息,例如姓名、电子邮件、性别等。 然后我想要大约 5 个字段用于可选信息,例如信使 ID。 以及 1 个可选文本字段,用于显示有关用户的信息。 我应该只创建一个包含所有字段的表格,还是应该为 5 个可选字段创建单独的表格以避免冗余等? 谢谢。
I need some idea about my database design. I have about 5 fields for basic information of user, such as name, email, gender etc.
Then I want to have about 5 fields for optional information such as messenger id's.
And 1 optional text field for info about user.
Should i create only one tabel with all fields all together or i should create separate table for the 5 optional fields in order to avoid redundancy etc?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我会坚持只用一张桌子。
添加另一个表只会使事情变得更加复杂,并且您只会获得很少的磁盘空间。
我真的不明白这有什么多余的;)
I'll stick with only one table.
Adding another table would only makes thins more complicated and you will only gain really little disk space.
And I really don't see how this can be redundant in any way ;)
我认为你绝对应该坚持使用一张桌子。由于所有信息都与用户相关,并且不反映任何其他逻辑模型(例如文章、博客文章等),因此您可以安全地将所有内容保留在一个位置,即使它们是可选的。
I think that you should definately stick with one table. Since all information is relevant to a user and do not reflect any other logical model (like an article, blog post or such), you can safely keep everything in one place, even if they are optional.
我只会为其他字段创建一个表。但不是 5 个字段,而是与基表和键/对值信息的外键关系。类似于:
最终您可能需要一个
additional_info
表来保存额外信息的可能有效值:信使、额外电子邮件等等。但这取决于你。我不会打扰。I would create only one table for additional fields. But not with 5 fields but a foreign key relation to base table and key/pair value info. Something like:
Eventually you might want an
additional_info
table to hold possible valid values for extra info: messenger, extra email, whatever. But that is up to you. I wouldn't bother.这取决于有多少人将拥有所有这些可选信息以及您是否计划添加更多字段。如果您认为将来要添加更多字段,则使用 EAV 模式将该信息移动到元表可能会很有用:http://en.wikipedia.org/wiki/Entity-attribute-value_model
因此,如果您不确定,您的表将类似于
使用 user_id 字段您的元表,您可以将其链接到您的用户表,并根据需要添加尽可能多的稀疏使用的可选字段。
注意:仅当您有许多稀疏填充的可选字段时,此方法才有效。否则将其放在一个字段中
It depends on how many people will be having all of that optional information and whether you plan on adding more fields. If you think you're going to add more fields in the future, it might be useful to move that information to a meta table using the EAV pattern : http://en.wikipedia.org/wiki/Entity-attribute-value_model
So, if you're unsure, your table would be like
Using the user_id field in your meta table, you can link it to your user table and add as many sparsely used optional fields as you want.
Note : This pays off ONLY if you have many sparsely populated optional fields. Otherwise have it in one field
我建议为此使用单个表。数据库非常擅长优化空列的空间。
将此表拆分为两个或多个表是垂直分区的一个示例,在这种情况下很可能是过早优化的情况。但是,当您有只需要在某些时候查询的列(例如)时,此技术可能会很有用。大的二进制 blob。
I would suggest using a single table for this. Databases are very good at optimizing away space for empty columns.
Splitting this table out into two or more tables is an example of vertical partitioning and in this case is likely to be a case of premature optimization. However, this technique can be useful when you have columns that you only need to query some of the time, eg. large binary blobs.