数据库中一对一和一对多关系的区别
当数据库中存在一对一关系时,另一个表具有外键 ID(在本示例中)。在一对多关系中,表包含许多外键。
但是数据库知道这是一对一关系还是一对多关系吗?我在 ER 图中建立的关系是否只是为了指示在制作实际表时哪里应该有外键?
数据库中的一对一关系和一对多关系有什么区别?
When having a one-to-one relationship in a database the other table has a foreign key ID (in this example). And in a one-to-many relationship the table contains many foreign keys.
But does the database know whether this is a one-to-one or one-to-many relationship? Are the relationships that I make in an ER-Diagram only to indicate where there should be foreign keys when making the actual tables?
What is the difference between one-to-one and one-to-many relationship in a database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
从某种意义上说,我们谈论的所有关系对于数据库来说都是未知的,它们是我们为了更好地理解如何设计表而发明的结构。
一对一和一对多在表结构方面的最大区别在于,在一对一中可以(但不是必须)具有双向关系,这意味着表 A 可以有外键到表 B 中,并且表 B 可以具有指向表 A 中关联记录的外键。这对于一对多关系是不可能的。
一对一关系将一个表中的一条记录与另一表中的一条记录相关联。一对多关系将一个表中的一条记录与另一表中的许多记录相关联。
In a sense, all the relationships we talk about are not known to the database, they are constructs we have invented to better understand how to design the tables.
The big difference in terms of table structure between one-to-one and one-to-many is that in one-to-one it is possible (but not necessary) to have a bidirectional relationship, meaning table A can have a foreign key into table B, and table B can have a foreign key into the associated record in table A. This is not possible with a one-to-many relationship.
One-to-one relationships associate one record in one table with a single record in the other table. One-to-many relationships associate one record in one table with many records in the other table.
要启用一对一关系,您需要为外键添加唯一约束。每个表不可能有两个外键,因为不可能创建记录。
To enable one-to-one relationship you need to add unique constraint to foreign key. It is not possible to have two foreign keys for each table as it will be impossible to create records.
我无法理解实际的问题是什么。
您的分析在很大程度上是正确的,因为如果您有 2 个表,并且 table2 有表 1 的外键,则它可能是一对一或多对一。
你的句子“在一对多关系中,表包含许多外键。”
“多”方的表仍然包含一列作为外键,只是多行可以具有相同的外键值(许多行指向一个父级)。
另请注意,您可以将外键放在父表上,再放在子表上,而不是相反。这样,如果您想这样做,就可以防止一对多。另请注意,通过这种方式,多个父母可以共享一个孩子,这可能是也可能不是您想要的。
Im having trouble understanding what the actual question is.
Your analysis is for the most part correct, in that if you have a 2 tables, and table2 has a foreign key to table one, it could be either a one-to-one or a many-to-one.
Your sentence "And in a one-to-many relationship the table contains many foreign keys."
The table of the 'many' side still contains one column that is a foreign key, its just that more than one row can have the same foreign key value (many rows point to one parent).
Also note that you can put the foreign key on the parent table, to the child, instead of the other way around. In this way, you can prevent one-to-many if you want to do that. Also note that in this way, more than one parent can share a child, which might or might not be what you want.
1:1 与 1:m 的数据库级别等效是在外键列上拥有唯一索引。请注意,这仅适用于 1:1,不适用于 1:0..1,因为在评估唯一性时会考虑
null
。对于此限制有一些解决方法,但仅此而已。The database-level equivalent of a 1:1 vs. 1:m is having a unique index on the foreign key column. Note that this will only work for 1:1, NOT 1:0..1, as
null
is considered when evaluating uniqueness. There are workarounds for this restriction, but that's it at the basic level.类似地,一个产品只有一个产品代码,因此它是一对一的关系(产品 <-> ABC123),但一个客户可以购买多个产品,因此它是一个-对多关系(人<->>>产品)。
Similarly by example, a product has only one product code, so it's one-to-one relationship (product <-> ABC123), but a customer can purchase more than one product, so it's one-to-many relationship (person <->>>product).
好吧,你是对的,这种关系对你来说很重要,但对数据库本身来说并不重要。当您有两张表时,一张包含您的基本信息,另一张包含您的详细信息。对于这两张表,您就是您,因此它是一对一的关系,您无法将您的数据映射到其他人。
现在添加第三个表“城市”和您居住的城市的信息点之一 - 这是一对多的示例(可以使用一个城市,并且应该用于许多人)。
一对多/一对一只是显示您的表如何交互。并且一直以来,您想要“保存”表中的行/列而不是重复它们,您将使用与另一个表的一对多关系。或者多对多:)
well, you are right, this relation is important for you, but not for db itself. When you have two tables, one with your basic information, and another one with your detailed information.. for both tables you are you, so it is one-to-one relation, you can not map your data to somebody else.
Now add third table "cities" and one of your information points to city you live in - this is example of one-to-many (one city can be used, and should be used for many people).
one-to-many / one-to-one just show how your tables interact. And all the time, you want to "save" rows/columns in table not duplicating them you will use one-to-many relation with another table. Or many-to-many :)
假设您有一个包含两个属性 A 和 B 的表。如果 A 是候选键而 B 不是,则 A 和 B 之间的关系是一对多。如果 A 和 B 都是候选键,则关系为 1 比 1。
Let's assume you have a table with two attributes A and B. If A is a candidate key and B is not then the relationship between A and B is 1 to many. If both A and B are candidate keys then the relationship is 1 to 1.
给定表 A 和 B,如果
最好的方法是使 B 的主键也是引用 A 的外键。这也称为“每个类型继承的表”和“是一个”关系。还有其他方法可以强制使用唯一的外键,但使用主键可以使模式和 ER 图中的关系变得清晰。
当然,总会有其他情况,如果您的设计不满足上述两个标准,您将不得不使用另一种方法。
Given table A and B if
The best approach is to make the primary key of B also a foreign key referencing A. This is also called "Table per Type Inheritance" and the "is a" relationship. There are other ways to enforce a unique foreign key, but using the primary key makes the relationship clear in the schema and in ER diagrams.
Of course there are always other scenarios, and if your design doesn't meet both of the criteria above, you'll have to use another approach.
以下是本教程中的一个示例(使用 NET Core、实体框架): https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/complex-data-model ?view=aspnetcore-5.0#entity-diagram-showing-relationships
答案 内容
1.
Visual Studio 2019 中的
图
在 Microsoft SQL Server Management Studio (SSMS)
2.解释
关系如下:
int? ClassID
)的可为 null 的 id 值。讲师
- 中间表:课程分配
- 表 2:课程
.3. 图表代码
此外,看看上面的图表是如何以编程方式生成的,可能会有所帮助:
Here's an example from this tutorial (using NET Core, Entity Framework): https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/complex-data-model?view=aspnetcore-5.0#entity-diagram-showing-relationships
Answer Content
1. Diagrams
At Visual Studio 2019
At Microsoft SQL Server Management Studio (SSMS)
2. Explanation
Relationships are read as follows:
int? ClassID
in C#) inside the student class.Instructor
- Middle table:CourseAssignment
- Table 2:Course
.3. Diagram Code
Additionally, taking a look at how the above diagram is generated programmatically, could be of some help: