简体   繁体   English

ER图-避免一对一关系

[英]ER diagram - avoiding one-to-one relationship

I've been working on an ER diagram for university project. 我一直在为大学项目设计ER图。 It is about transport company. 关于运输公司。 That company does particular jobs for other companies and for each job, there are three types of documents needed, and those documents have unique identifiers among other documents of the same kind. 该公司为其他公司做特定的工作,并且每项工作都需要三种类型的文档,并且这些文档在同类的其他文档中具有唯一的标识符。 So what I did is made these types of documents as separate entities. 因此,我所做的就是将这些类型的文档作为单独的实体。 Now when I want to join them(call them Doc1, Doc2, Doc3) into one entity(call it Job), they are basically made only for that one job and for no other. 现在,当我想将它们(称为Doc1,Doc2,Doc3)加入一个实体(称为Job)时,它们基本上仅是为该一项工作而没有的。 Also, this job has only one of each of these documents, so therefore it looks like relationships between documents and job are one-to-one. 另外,此作业只有这些文档中的每一个,因此看起来文档与作业之间的关系是一对一的。 However, when the professor was teaching us ER models, he told that we should always avoid drawing one-to-one relationships(that there should be a way to make these documents kind of attributes of job). 但是,当教授教我们ER模型时,他告诉我们应该避免绘制一对一的关系(应该有一种方法使这些文档具有工作的属性)。 So what I want to know is - is it correct to draw the identifiers of these documents as attributes of job, and then make them as foreign keys referencing corresponding fields in documents' table(in relations model)? 所以我想知道的是-将这些文档的标识符绘制为job属性,然后使其成为引用文档表(在关系模型中)的相应字段的外键是否正确? Or is there any other, more elegant way to connect them somehow avoiding these one-to-one relationships? 还是有其他更优雅的方式以某种方式将它们连接起来,从而避免了一对一的关系? Also, if I do it this way, I guess I should make all 3 columns representing documents' identifiers UNIQUE in Job table, right? 另外,如果我这样做,我想我应该在Job表中使代表文档标识符的所有3列都变为UNIQUE,对吗? So that I avoid making two jobs having, for example, same Doc1? 这样我就避免进行两个具有相同Doc1的作业? Thank you! 谢谢!

One-to-one relationships are to be avoided, because they signal that the entities joined by the relationship are actually one. 应避免一对一的关系,因为它们表示该关系所连接的实体实际上是一个实体。 However, in the case specified here, the relationship is not one-to-one. 但是,在此处指定的情况下,关系不是一对一的。 Instead it is "one to zero or one", also known as "one-to-one optional". 相反,它是“一对零或一个”,也称为“一对一可选”。

An example is the relationship between a Home and a Lot. 一个例子是房屋与地块之间的关系。 The Home must be located on a Lot, and only one Home can be located on any given Lot, but the Lot can exist before the Home is built. 房屋必须位于一个地块上,并且任何给定地块上只能放置一个房屋,但是该地块在建造房屋之前可以存在。 If you are modelling this relationship, you would have a "one to zero or one" relationship between Lot and Home. 如果要对此关系建模,则Lot与Home之间将具有“一对零或一个”关系。 It would be shown like this: 它将显示如下:

在此处输入图片说明

In your case you have three separate dependencies, so it would look like: 在您的情况下,您具有三个独立的依赖项,因此如下所示:

在此处输入图片说明

Physically, these relationships may be represented in two ways: 从物理上讲,这些关系可以两种方式表示:

  1. A nullable foreign key in the "one" row (Lot, in my example above), or “一个”行中的可为空的外键(在上面的示例中为“ Lot”),或者
  2. A non-nullable foreign key in the "zero or one" row (Home, in my example above) “零或一个”行中的非空外键(在上面的示例中为Home)

You can choose the approach that is most comfortable and efficient for you, depending on the direction in which your application usually navigates. 您可以根据应用程序通常的浏览方向来选择最舒适,最有效的方法。

You may decide to have the database enforce the uniqueness constraint (the fact that only one Home can be on a Lot). 您可能决定让数据库强制执行唯一性约束(事实上,一个地块上只能有一个房屋)。 In some databases, a null value participates in uniqueness constraints (in other words, a unique index can only have one Null entry). 在某些数据库中,空值会参与唯一性约束(换句话说,唯一索引只能有一个Null条目)。 In such a database, you would be constrained to the second approach. 在这样的数据库中,您将不得不使用第二种方法。 In MySQL, this is not the case; 在MySQL中,情况并非如此。 a uniqueness constraint ignores null values, so you can choose either approach. 唯一性约束会忽略空值,因此您可以选择其中一种方法。 The second approach is more common. 第二种方法更常见。

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

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