SQL 什么是亲子关系?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2020343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:59:35  来源:igfitidea点击:

What are Parent-Child relationships?

sqldatabase-design

提问by GurdeepS

What is the parent and what is the child in a sql relationship?

什么是父级,什么是 sql 关系中的子级?

In my case, I have a part (Say screw), and product material. For argument's sake, a product material (eg steel) can only belong to one part (but not in the real world). So this is 1:n. The part will have its pk as a fk in the ProductMaterial table.

就我而言,我有一个零件(比如螺丝钉)和产品材料。为论证起见,一种产品材料(例如钢)只能属于一个部分(但在现实世界中则不然)。所以这是1:n。该零件将在 ProductMaterial 表中将其 pk 作为 fk。

Which is parent and which is child in this case?

在这种情况下,哪个是父母,哪个是孩子?

采纳答案by Felix Kling

You can interpret a 1:n relationship in database this way: A child is always that model which holds the foreign key as this indicates where it belongs to.
Of course if you have self referencing models/tables you have to look at it in a different way.

您可以通过这种方式解释数据库中的 1:n 关系:子项始终是持有外键的模型,因为这表明它所属的位置。
当然,如果您有自引用模型/表,则必须以不同的方式看待它。

回答by

Relational databases such as SQL actually have no concept of parent/child relationships - that is an interpretation that you as a programmer put on the data. There are architectures that explicitly state and use such relationships, such as heirarchical (and to a certain extent OO) databases.

SQL 等关系数据库实际上没有父/子关系的概念——这是您作为程序员对数据的一种解释。有一些体系结构明确声明和使用这种关系,例如分层(以及在一定程度上是面向对象的)数据库。

回答by David M

In this case, Part is parent and ProductMaterial is child.

在这种情况下,Part 是父级,ProductMaterial 是子级。

A parent can have unlimited numbers of children (scary thought - 2 is enough for me!), whereas a child can have only a limited number of parents - and in DB terms, only 1!

父母可以拥有无​​限数量的孩子(可怕的想法 - 2 对我来说就足够了!),而孩子只能拥有有限数量的父母 - 在 DB 术语中,只有 1!

回答by Guffa

Usually in a one-to-many relationship, it's the "one" record that is the parent, and the "many" records that are the children.

通常在一对多关系中,“一个”记录是父记录,“多”记录是子记录。

Of course, in some cases it doesn't make any sense to talk about a parent-child relationship. In your example it makes some kind of sense at least. In other examples you may even find the opposite, where a child has many parents, but then it's not very useful to describe it that way.

当然,在某些情况下,谈论亲子关系没有任何意义。在你的例子中,它至少有某种意义。在其他示例中,您甚至可能会发现相反的情况,即一个孩子有很多父母,但是这样描述它并不是很有用。

回答by user3856719

I would say that any table which has a one to many relationship with one or more other tables can be considered a parent to those other tables. Self joining? An ambiguous term which I don't think anyone understands.

我会说任何与一个或多个其他表具有一对多关系的表都可以被视为这些其他表的父表。自加盟?一个模棱两可的术语,我认为没有人理解。

回答by SayedRakib

'Product' is parent and 'Product material' is child in Product_ProductMaterial relationship or association.

在 Product_ProductMaterial 关系或关联中,“产品”是父项,“产品材料”是子项。

If you delete a child, parent can live and continue life. If you delete parent, child become orphan or identityless which is not good. If A can not not be deleted before B is deleted, then A is parent and B is child.

如果你删除一个孩子,父母可以继续生活。如果删除父项,子项将成为孤儿或无身份,这并不好。如果在删除 B 之前不能删除 A,那么 A 是父级,B 是子级。

If I guess right, in your case in Product_ProductMaterial relationship, if you delete Product, ProductMaterial will be assigned to no one, become orphan, identity crisis. But if you delete ProductMaterial, Product can still be there, no need identity support.

如果我猜对了,在你的Product_ProductMaterial关系中,如果你删除Product,ProductMaterial将不会被分配给任何人,成为孤儿,身份危机。但是如果你删除了 ProductMaterial,Product 仍然可以存在,不需要身份支持。

Sorry if my wordings are not good.

对不起,如果我的措辞不好。

回答by Randy Minder

Another way to look at this, in addition to what David M. said, is in terms of an ORM implementation (such as Linq to SQL). You have two entities, Part and ProductMaterial. Each part entity has a set of ProductMaterial entities (children entities or an EntitySet). Each ProductMaterial entity has zero or one Part entity (parent entity or an EntityRef).

除了 David M. 所说的之外,另一种看待这个问题的方式是从 ORM 实现(例如 Linq to SQL)的角度来看。您有两个实体,Part 和 ProductMaterial。每个部件实体都有一组 ProductMaterial 实体(子实体或 EntitySet)。每个 ProductMaterial 实体都有零个或一个 Part 实体(父实体或 EntityRef)。

Randy

兰迪