Relations are sets so we can apply set operators. However, we want the results to be relations.
The two operands and must be type-compatible (except for CROSS PROD.).
- Same number of attributes.
- Same domain for attributes.
Properties of operations:
- Both UNION (Relational Operation) and INTERSECTION (Relational Operation) are commutative:
- They can also both be treated as -ary operations applicable to any number of relations as both are associative operations:
UNION
UNION (Relational Operation)
The binary operation UNION, denoted by , creates a relation that includes all tuples that includes all tuples from either operand.
The result of is a relation that includes all tuples that are either in , in , or in both. Duplicate tuples are eliminated.
Examples:
Link to original
- Retrieve the SSNs of all employees who work in dept 5. or directly supervise an employee who works in dept 5.
INTERSECTION
INTERSECTION (Relational Operation)
The binary operation INTERSECTION, denoted by , creates a relation that includes all tuples that are in both operands.
The result of is a relation that includes all tuples that are both in and .
Link to original
DIFFERENCE
DIFFERENCE (Relational Operation)
The binary operation SET DIFFERENCE (also called MINUS or EXCEPT), denoted by , creates a relation that includes all the tuples in the first operand that are not in the second operand.
The result of is a relation that includes all tuples that are in but not .
Link to original
CARTESIAN PRODUCT
CARTESIAN PRODUCT (Relational Operation)
The binary operation CARTESIAN PRODUCT (or CROSS PRODUCT), denoted by , creates a relation that combines tuples from two operands in a combinatorial fashion.
The result of is a relation with degree + attributes: . The resulting relation state has one tuple for each combination of tuples - one from and one from .
Operands do not have to be type-compatible.
Generally, cross product is not meaningful but it can precede other operations. To keep only combinations where the DEPARTMENT is related to the EMPLOYEE, we can add a SELECT operation:
Link to original