数据库设计 - 应该避免一对一的关系吗? [英] Database Design - Should one-to-one relationships be avoided?

查看:127
本文介绍了数据库设计 - 应该避免一对一的关系吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

为了简单起见,我会直接提出问题:避免数据库设计中的一对一关系,这个可以接受?

For the sake of simplicity, I'll ask the question straight out: should one-to-one relationships in database design be avoided or this acceptable?

我知道这个项目的所有属性都可以在一个表中托管,但我感觉当通过ORM将我的数据库设计转换为业务对象时,混杂具有不必要属性的实体。

I know all of the attributes of this "item" can be all hosted in ONE table, but I feel when converting my database design into business objects via an ORM, it clutters the entity with unnecessary properties.

通过UI,希望这将画一个更好的图片,我有一个主窗体所有必要的属性。我将有一个按钮,将允许用户点击它,它会带来一个新的形式,以附加额外的属性。不超过1个条目可以与主表单(实体)相关联,即它是0..1结束关系。

Via the UI, hopefully this will paint a better picture, I have a main form with all of the necessary attributes. I will have a button that will allow the user to click on it and it will bring up a new form to attach extra attributes. No more than 1 entry can be affiliated with the main form (entity), i.e. it is a 0..1 end relationship.

任何建议将不胜感激。 p>

Any advice will be appreciated.

推荐答案

不,1:1的关系可以完全合理。

No, a 1:1 relationship can totally make sense.

假设一个实体可以选择性地拥有一个具有完整属性的桶 - 一些实体有这些属性,另一些实体没有属性。

Imagine an entity that optionally has a bucket full of attributes - some of your entities have those, others don't.

您可以将所有属性作为列包含在实体表 - 但在这种情况下,大量的列将对于大量的条目结束为空。

You can either include all those attributes as columns into your entity table - but in that case, lots of columns would end up empty for a significant number of the entries.

或者:您可以将那些可选属性单独的表,与基本实体表建立1:1(或更确切地说,0:1 )关系,并且只有存储在那里的东西,如果你的实体真的有这些属性。

Or: you can put those "optional" attributes into a separate table, set up a 1:1 (or rather: 0:1) relationship with the base entity table, and only store stuff in there if your entity in question really has those attributes.

决定是否将某些属性外包到单独的表中的主要标准是:

The main criteria to decide whether to "outsource" some attributes into a separate table would be:


  • 这涉及多少属性?如果它只是一两个 - 不去长度把这些在单独的表。但是如果你说的是8,10,15,那么请考虑一下

  • how many attributes does this concern? If it's just one or two - don't go to lengths to put these in separate tables. But if you're talking about 8, 10, 15 - then consider it

有多少基本实体可能有这些可选属性?再次:如果95%的所有实体总是具有所有这些属性,那么这是没有意义的做这个额外的步骤。如果只有一半或更少的实体将具有这些属性 - >我肯定会考虑这样的表

how many of the base entities might have those optional attributes? Again: if 95% of all entities will always have all those attributes anyway, then it doesn't make sense to do this extra step. If only half or less of your entities will have those attributes -> I would definitely consider such a table

这篇关于数据库设计 - 应该避免一对一的关系吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆