Set Operations in Relational Algebra: Union, Intersection, and Set Difference
Set Operations in Relational Algebra
A comprehensive guide to Union, Intersection, and Set Difference in relational algebra.
Introduction
Relational algebra supports three primary set operations: Union, Intersection, and Set Difference. These operations allow you to combine or compare data from two relations, provided they meet the criteria for union compatibility.
Union (∪)
Definition: Union combines all tuples from two relations, eliminating duplicates to produce the final result.
Conditions:
- The two relations must have the same number of attributes.
- The corresponding attributes must have the same data types.
Example:
Relation R: emp_id | name 101 | Alice 102 | Bob 103 | Carol Relation S: emp_id | name 101 | Alice 104 | David 105 | Eve Union Result: emp_id | name 101 | Alice 102 | Bob 103 | Carol 104 | David 105 | Eve
Intersection (∩)
Definition: Intersection returns tuples that are present in both relations.
Conditions:
- The two relations must be union-compatible.
Example:
Intersection Result: emp_id | name 101 | Alice
Set Difference (−)
Definition: Set Difference returns tuples present in one relation but not in the other.
Example:
Set Difference (R − S): emp_id | name 102 | Bob 103 | Carol
Union Compatibility
Union compatibility is a requirement for set operations in relational algebra. For two relations to be union-compatible:
- They must have the same number of attributes.
- The corresponding attributes must have the same data types.
Example:
R1(a, b): Integer, String R2(c, d): Integer, String Since R1 and R2 have the same attribute count and data types, they are union-compatible.
Properties of Set Operations
- Commutativity: Union and Intersection are commutative.
- Associativity: Both operations are associative.
- Idempotency: Performing Union or Intersection with the same relation does not change the result.
Reducing Intersection Using Union and Set Difference
Intersection can be expressed using Union and Set Difference as follows:
R ∩ S = (R ∪ S) − (R − S) − (S − R)
Example:
Union: Combines all tuples. Subtract tuples exclusive to each relation to find the intersection.
Conclusion
- Union: Combines tuples from both relations, removing duplicates.
- Intersection: Retrieves tuples common to both relations.
- Set Difference: Finds tuples present in one relation but not in the other.