How to understand `u=r÷s`, the division operator, in relational algebra?

青春壹個敷衍的年華 提交于 2019-12-01 08:27:11

An intuitive property of the division operator of the relational algebra is simply that it is the inverse of the cartesian product. For example, if you have two relations R and S, then, if U is a relation defined as the cartesian product of them:

U = R x S

the division is the operator such that:

U ÷ R = S

and:

U ÷ S = R

So, you can think of the result of U ÷ R as: “the projection of U that, multiplied by R, produces U”, and of the operation ÷, as the operation that finds all the “parts” of U that are combined with all the tuples of R.

However, in order to be useful, we want that this operation can be applied to any couple of relations, that is, we want to divide a relation which is not the result of a cartesian product. For this, the formal definition is more complex.

So, supposing that we have two relations R and S with attributes respectively A and B, their division can be defined as:

R ÷ S = πA-B(R) - πA-B((πA-B(R) x S) - R)

that can be read in this way:

  1. πA-B(R) x S: project R over the attributes of R which are not in S, and multiply (cartesian product) this relation with S. This produces a relation with the attributes A of R and with rows all the possible combinations of rows of S and the projection of R;

  2. From the previous result subtract all the tuples originally in R, that is, perform (πA-B(R) x S) - R. In this way we obtain the “extra” tuples, that is the tuples in the cartesian product that were not present in the original relation.

  3. Finally, subtract from the original relation those extra tuples (but, again, perform this operation only on the attributes of R which are not present in S). So, the final operation is: πA-B(R) - πA-B(the result of step 2).

So, coming to your example, the projection of r on D is equal to:

(D)
d1
d2
d3 
d4

and the cartesian product with s is:

(A,  B,  D)
 a1  b1  d1
 a1  b1  d2
 a1  b1  d3
 a1  b1  d4

Now we can remove from this set the tuples that were also in the original relation r, i.e. the first two tuples and the last one, so that we obtain the following result:

(A,  B,  D)
 a1  b1  d3

And finally, we can remove the previous tuples (projected on D), from the original relation (again projected on D), that is, we remove:

(D)
d3

from:

(D)
d1
d2
d3
d4

and we obtain the following result, which is the final result of the division:

(D)
d1
d2
d4

Finally, we could double check the result by multiplying it with the original relation s (which is composed only by the tuple (a1, b1)):

(A  B  D)
 a1 b1 d1
 a1 b1 d2
 a1 b1 d4

And looking at the rows of the original relation r, you can see this fact, that should give you an important insight on the meaning of the division operator:

the only values of the column D in r that are present together with (a1, b1) (the only tuple of s), are d1, d2 and d4.

You can also see another example in Wikipedia, and for a detailed explanation of the division, together with its transformation is SQL, you could look at these slides.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!