Understanding natural join in Relational algebra

2020-04-01 08:43发布

问题:

I have seen that natural join is just combination of selection and cartesian product but in various websites its said to be as combination of projection and cartesian product and both are completely different so which definition should corresponds to it more exactly?

回答1:

There are many different versions of "relational algebra" that differ even in their notion of "relation". There's no one PRODUCT or NATURAL JOIN.

Some versions of the relational algebra have relation headings that are lists of attribute names. PRODUCT outputs an attribute for every input list element. If there's a NATURAL JOIN then its result will be like first doing PRODUCT, then RESTRICTing/SELECTing on equality of pairs of same-named attributes, then PROJECTing out one attribute of each pair. They give the same result when there are no shared attribute names. PRODUCT works for any two inputs but NATURAL JOIN might be undefined when an input has duplicate attribute names.

Some versions of the relational algebra have relation headings that are sets of attribute names. (Elements are unordered & unique.) The result of NATURAL JOIN has a heading that is the union of the input headings. (Tuples have one copy each of the attribute names common to both inputs & one copy each of the attribute names unique to one input.) It returns all tuples with that heading that can be made by combining a tuple from each input table. That is regardless of how many common attribute names there are, including zero. PRODUCT is defined only when the inputs share no attribute names but otherwise acts like NATURAL JOIN. PRODUCT's role is to confirm that you expect that there are no shared attribute names. When all the column names are common, that is a kind of INTERSECTION.

All this is regardless of PKs, UNIQUE, FKs & other constraints.