简体   繁体   English

关于数据库设计

[英]about database design

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. 我有大约5个字段,用于存储用户的基本信息,例如姓名,电子邮件,性别等。然后,我希望有大约5个字段,用于存储可选信息,例如Messenger ID。 And 1 optional text field for info about user. 和1个可选的文本字段,用于获取有关用户的信息。 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? 我应该只创建一个包含所有字段在一起的表,还是应该为5个可选字段创建单独的表,以避免冗余等? Thanks. 谢谢。

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. 由于所有信息都与用户相关,并且不反映任何其他逻辑模型(例如文章,博客文章等),因此即使它们是可选的,也可以将所有内容安全地保存在一个地方。

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. 但不是5个字段,而是与基表和键/对值信息的外键关系。 Something like: 就像是:

create table users (
    user_id integer,
    name varchar(200),
    -- the rest of the fields
)

create table users_additional_info (
    user_id integer references users(user_id) not null,
    ai_type varchar(10) not null, -- type of additional info: messenger, extra email
    ai_value varchar(200) not null
)

Eventually you might want an additional_info table to hold possible valid values for extra info: messenger, extra email, whatever. 最终,您可能想让additional_info表保存额外信息的可能有效值:Messenger,额外的电子邮件等。 But that is up to you. 但这取决于您。 I wouldn't bother. 我不会打扰的。

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 如果您认为将来要添加更多字段,则使用EAV模式将该信息移动到元表中可能会很有用: http : //en.wikipedia.org/wiki/Entity-attribute-value_model

So, if you're unsure, your table would be like 因此,如果您不确定,您的餐桌会像

User : id, name, email, gender, field1, field2
User_Meta : id, user_id, attribute, value

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. 使用元表中的user_id字段,可以将其链接到用户表,并根据需要添加尽可能多的稀疏使用的可选字段。

Note : This pays off ONLY if you have many sparsely populated optional fields. 注意:仅当您有许多稀疏填充的可选字段时,此方法才有意义。 Otherwise have it in one field 否则在一个领域

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. 大的二进制Blob。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM