Relational Algebra
Relational Algebra
ALGEBRA &
BASIC SQL
LEARNING
OBJECTIVE
1. Data structure
• Tables (relations), rows, columns
2. Data integrity constraints
• Mechanisms for implementing business rules that maintain integrity of
manipulated data
3. Data manipulation
• Powerful SQL operations for retrieving and modifying data
• Relational algebra is the foundation for SQL operations
Presentation title 4
RELATIONAL ALGEBRA
Relational Algebra: A collection of algebraic operators that
• Are defined on relations;
• Produce relations as results; and
• Can be combined to form complex algebraic expressions.
• Can be grouped into:
• Unary operators: operate on one relation only (select, project, name)
• Operators from set theory: merge the elements of two sets in various
ways (union, cross product, difference)
• Derived operators – join operators (intersection, join, natural join)
Presentation title 5
RELATIONAL ALGEBRA
What is algebra, anyway? Expressions created by combining operands and
operators that satisfy some laws
Operands (values) Expressions
− Variables, constants − Combine operations with parenthesis (explicit)
− Closure property − OR using either precedence (implied)
Operators Laws
+ “Addition” − Identify semantically equivalent expressions
* “Multiplication” − Commutativity, associativity, etc.
- “Subtraction”
/: “Division”
Presentation title 6
Relational algebra:
Presentation title 10
Relational algebra:
Presentation title 17
SQL query:
Presentation title 18
• Notation: rnew_name(R)
RELATIONAL ALGEBRA:
OPERATORS FROM SET THEORY
• UNION (υ)
• INTERSECTION (∩),
• DIFFERENCE (-)
• CARTESIAN PRODUCT (x)
Presentation title 26
RELATIONAL ALGEBRA:
UNION OPERATION
• UNION is symbolized by ∪ symbol.
• It includes all tuples that are in tables A or in B. It R1 R2
also eliminates duplicate tuples.
• For a union operation to be valid, the following
conditions must hold –
R1 R1 ∪ R2 R2
• R and S must be the same number of attributes.
• Attribute domains need to be compatible.
• Duplicate tuples should be automatically removed.
Presentation title 27
RELATIONAL ALGEBRA:
UNION OPERATION
Presentation title 28
RELATIONAL ALGEBRA:
UNION OPERATION
Union operation
that requires
renaming entities
Presentation title 29
RELATIONAL ALGEBRA:
UNION OPERATION
Union operation
that requires
renaming
attributes
Presentation title 30
RELATIONAL ALGEBRA:
INTERSECTION OPERATION
RELATIONAL ALGEBRA:
INTERSECTION OPERATION
Intersection:
Example
Presentation title 32
RELATIONAL ALGEBRA:
DIFFERENCE OPERATION
• Denoted by the symbol “–”.
• The result of A – B, is a relation which
includes all tuples that are in A but not in B.
• The attribute name of A has to match with
the attribute name in B.
• The two-operand relations A and B should
be either compatible or Union compatible.
• The result should be a defined relation
consisting of the tuples that are in relation
A, but not in B.
Presentation title 33
RELATIONAL ALGEBRA:
DIFFERENCE OPERATION
Set
Difference:
Example
Presentation title 34
RELATIONAL ALGEBRA:
CARTESIAN PRODUCT OPERATION
RELATIONAL ALGEBRA:
CARTESIAN PRODUCT OPERATION
Cartesian
Product:
Example