关于数据库设计

发布于 2024-10-20 16:19:04 字数 162 浏览 2 评论 0原文

我需要一些关于数据库设计的想法。我有大约 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

白鸥掠海 2024-10-27 16:19:04

我会坚持只用一张桌子。

添加另一个表只会使事情变得更加复杂,并且您只会获得很少的磁盘空间。

我真的不明白这有什么多余的;)

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 ;)

不奢求什么 2024-10-27 16:19:04

我认为你绝对应该坚持使用一张桌子。由于所有信息都与用户相关,并且不反映任何其他逻辑模型(例如文章、博客文章等),因此您可以安全地将所有内容保留在一个位置,即使它们是可选的。

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.

红颜悴 2024-10-27 16:19:04

我只会为其他字段创建一个表。但不是 5 个字段,而是与基表和键/对值信息的外键关系。类似于:

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
)

最终您可能需要一个 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:

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. But that is up to you. I wouldn't bother.

薆情海 2024-10-27 16:19:04

这取决于有多少人将拥有所有这些可选信息以及您是否计划添加更多字段。如果您认为将来要添加更多字段,则使用 E​​AV 模式将该信息移动到元表可能会很有用:http://en.wikipedia.org/wiki/Entity-attribute-value_model

因此,如果您不确定,您的表将类似于

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

使用 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

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.

Note : This pays off ONLY if you have many sparsely populated optional fields. Otherwise have it in one field

煞人兵器 2024-10-27 16:19:04

我建议为此使用单个表。数据库非常擅长优化空列的空间。

将此表拆分为两个或多个表是垂直分区的一个示例,在这种情况下很可能是过早优化的情况。但是,当您有只需要在某些时候查询的列(例如)时,此技术可能会很有用。大的二进制 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文