views:

90

answers:

5

Hi,

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

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.

Which is parent and which is child in this case?

+1  A: 

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

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!

David M
+5  A: 

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.

anon
A: 

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.

Felix Kling
Thanks. Easy explanation and thus this is the Product Material table.
dotnetdev
You can interpret it that way, but you will often be wrong. Take the case of two human parents with a single offspring :-)
anon
In relational databases, I don't think I've ever seen the term used to mean anything other than "one parent, many children", so it seems safe to interpret the term that way, in that context.
Jason Orendorff
A: 

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

Randy

Randy Minder
+1  A: 

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.

Guffa