Sub topic 5
Cartesian Product and Rename
Prerequisites and purpose
Assume you know relations, tuples, schemas, basic set operations, and selection in relational algebra. This chapter explains two operators that help you combine relations and avoid ambiguous attribute names: Cartesian product and rename.
Cartesian product
The Cartesian product of relations \(R\) and \(S\), written \(R \times S\), pairs every tuple of \(R\) with every tuple of \(S\).
If \(R\) has \(m\) tuples and \(S\) has \(n\) tuples, then \(R \times S\) has \(m \times n\) tuples. Its schema contains the attributes of both relations. Attribute names should be distinct, or qualified with their relation names.
Consider these relations:
| Student | Course |
|---|---|
| sid, sname | cid, cname |
The expression Student × Course pairs every student with every course. It does not check whether a student enrolled in a course. Therefore, its result can be much larger than the meaningful answer.
A product followed by a selection creates a conditional combination:
σ_{Student.sid = Enrolled.sid}(Student × Enrolled)This keeps only pairs whose student identifiers match. More generally, a theta join can be defined using product and selection:
R ⋈_condition S = σ_condition(R × S)For example:
Student ⋈_{Student.sid = Enrolled.sid} Enrolledis equivalent to:
σ_{Student.sid = Enrolled.sid}(Student × Enrolled)Rename operator
The rename operator, written \(\rho\), changes the name of a relation, its attributes, or both. Rename does not change the tuples.
ρ_{S(sid, sname, age)}(Student)This creates a renamed copy of Student, called S. Rename is essential when the same relation appears more than once in an expression. This is called a self-join.
For example, to find pairs of different students with the same age:
S1 ← ρ_{S1(sid1, sname1, age1)}(Student)
S2 ← ρ_{S2(sid2, sname2, age2)}(Student)
σ_{S1.age1 = S2.age2 ∧ S1.sid1 ≠ S2.sid2}(S1 × S2)The two renamed copies let us distinguish S1.age1 from S2.age2. Without rename, references to Student.age would be ambiguous.
GATE checks
For \(R \times S\), multiply tuple counts. For a selection after the product, count only pairs satisfying the condition. For a self-join, rename each copy before writing conditions.
Exercise
Relation R has 4 tuples and relation S has 3 tuples. How many tuples can R × S contain before any selection?
Show answer
It contains 4 × 3 = 12 tuples.