Relational Algebra Expression Equivalence: Identifying the Non-Equivalent Query
Consider a schema with two relations, '(A, B)' and '(C, D)', where all values are integers. Make no assumptions about keys. Consider the following four relational algebra expressions:
(a) πA,B (R ⋈ S) (b) πA,R.B(R × S) (c) R ∩ (πA(R) × πB(S)) (d) R ⋈ πB(S)
One of the four expressions of relational algebra is not equivalent to the other three. Which query can produce a different answer? Give the simplest database instance to illustrate your answer.
The query that can produce a different answer is (c) R ∩ (πA(R) × πB(S)).
To illustrate this, let's consider a simple database instance:
R: +---+---+ | A | B | +---+---+ | 1 | 2 | | 2 | 3 | | 3 | 4 | +---+---+
S: +---+---+ | C | D | +---+---+ | 2 | 5 | | 3 | 6 | | 4 | 7 | +---+---+
Now, let's evaluate each of the four expressions:
(a) πA,B (R ⋈ S): This expression joins R and S on their common attribute B, and then projects the attributes A and B. The result would be an empty relation since there are no matching values of B in R and S.
(b) πA,R.B(R × S): This expression performs a Cartesian product of R and S, and then projects the attributes A and R.B. The result would be the following relation:
+---+---+ | A | B | +---+---+ | 1 | 2 | | 1 | 3 | | 1 | 4 | | 2 | 2 | | 2 | 3 | | 2 | 4 | | 3 | 2 | | 3 | 3 | | 3 | 4 | +---+---+
(c) R ∩ (πA(R) × πB(S)): This expression first projects the attribute A from R, and then projects the attribute B from S. It then takes the Cartesian product of these two projections and intersects it with R. The result would be the following relation:
+---+---+ | A | B | +---+---+ | 1 | 2 | | 2 | 3 | +---+---+
(d) R ⋈ πB(S): This expression projects the attribute B from S, and then performs a natural join between R and the projection on the common attribute B. The result would be the following relation:
+---+---+---+ | A | B | D | +---+---+---+ | 1 | 2 | 5 | | 2 | 3 | 6 | | 3 | 4 | 7 | +---+---+---+
As we can see, the result of expression (c) is different from the other three expressions.
原文地址: https://www.cveoy.top/t/topic/bvKS 著作权归作者所有。请勿转载和采集!