给定关系模式下关系代数Q₁与Q₂不等价及Q₂无效的示例咨询
First, let’s recap the context: we have a relation R(A, C) (with A as the primary key), and two queries:
Q₁ = π_A (σ_{C<10}(R))Q₂ = σ_{C<10}(π_A(R))
The key issue here is that Q₂ is a completely invalid expression—it can’t even be executed, let alone produce a result equivalent to Q₁. Let’s use a concrete data example to break this down clearly.
Step 1: Sample Data for Relation R
Let’s populate R with real values to make the example tangible:
| A | C |
|---|---|
| 1 | 5 |
| 2 | 15 |
| 3 | 8 |
| 4 | 20 |
How Q₁ Works (Valid and Executable)
Q₁ follows the logical order: filter first, then project
- First, the selection operation
σ_{C<10}(R)runs: it keeps only rows whereCis less than 10. This gives us:A C 1 5 3 8 - Next, the projection
π_A(...)drops theCcolumn, leaving only theAvalues from the filtered rows:A 1 3
Why Q₂ Fails (Invalid Expression)
Q₂ reverses the order fatally: project first, then filter
- First,
π_A(R)runs: this operation keeps only theAcolumn from R, discardingCentirely. The intermediate result looks like this:A 1 2 3 4 - Now, we try to run
σ_{C<10}(...)on this result—but there’s a critical problem: this intermediate table has noCcolumn! The database has no way to check the conditionC<10because the attributeCwas already discarded by the earlier projection.
In short, Q₂ tries to filter on an attribute that no longer exists in the data set. Databases will throw an error if you attempt to execute Q₂, since the selection condition references a non-existent attribute. That’s why the two queries aren’t equivalent—one works as intended, the other can’t run at all.
内容的提问来源于stack exchange,提问作者user8779054

