DB Lesson 4 Class Note Relational Algebara Part 1
DB Lesson 4 Class Note Relational Algebara Part 1
IT2306 – Database
Systems
Relational Algebra
part 1 – Traditional Set of Operations
Follow us on
@AcademyofGigaNerds
@GigaNerds
Benefits of RA Expressions:
• Formal and unambiguous: RA expressions provide a clear and well-defined way to
express queries, machine-readable and easier to understand for database systems.
• Foundation for SQL: The concepts of RA form the basis for the more user-friendly
Structured Query Language (SQL) used in everyday database interaction.
• Building complex queries: By nesting RA operations, you can construct intricate queries
that involve filtering, combining, and extracting data from multiple relations.
In essence, RA expressions offer a powerful way to specify retrieval requests and manipulate data
in relational databases. Their formal nature, closure property, and connection to SQL make them a
valuable tool.
1. Union (U): This operation combines the tuples from two relations, resulting in a new
relation containing all distinct tuples (no duplicates) present in either original relation.
2. Intersection (∩): This operation identifies tuples that exist in both relations. The resulting
relation will only contain tuples that are common to both input relations.
3. Set Difference (-): This operation finds tuples present in one relation
but not in the other. The resulting relation will contain tuples that exist in the first relation but
not in the second.
4. Cartesian Product (X): This operation creates a new relation by combining all possible
combinations of tuples from two relations. The resulting relation will have all attributes
(columns) from both original relations, with each row being a combination of one tuple from
each input relation.
3|Page ACADEMY OF GIGA NERD
Semester 02 - DB Lesson 4 - Relational Algebra
Union compatible
Union compatibility guarantees that the structures of the relations involved in a union operation
are aligned, allowing for a seamless combination of their rows into a new relation.
• Eliminating Duplicates: The resulting relation only includes unique tuples. If a tuple
appears in both R and S, it will only be present once in the union.
• Maximum Number of Tuples: The resulting union relation will contain a maximum of
(I + J) tuples, where I and J represent the number of tuples in relations R and S,
respectively. This is because, in the worst-case scenario, no tuples overlap (no duplicates),
and all tuples from both relations are included in the union.
2. Intersection (R ∩ S):
3.
4.
5.
6.
7.
8.
9.
10.
Concept:
• The intersection of two relations R and S, denoted by R ∩ S, results in a new relation that
contains all tuples (rows) present in both R and S.
• It identifies the common elements (data points) that satisfy the condition of being in both
relations.
Requirements:
• Union Compatibility: For a valid intersection, both relations (R and S) must be union
compatible. This means they must have the same number of attributes (columns) with the
same data types in each corresponding position. Imagine tables with different structures;
you can't meaningfully compare rows across them.
• Matching Attributes: There must be at least one attribute in both relations that allows for
comparison to determine if tuples belong in the intersection. This is similar to having a
common ground for comparison in set theory.
Result:
• The resulting relation from the intersection will have the same schema (attribute structure)
as the original relations (assuming they were union compatible).
• It will only contain tuples where the values in the matching attributes from both relations are
equal.
Example:
This query will only return customers who have a corresponding order in the Orders table based
on the matching CustID attribute.
Comparison with Set Difference:
• Intersection focuses on finding common elements (tuples) in both relations.
• Set difference, on the other hand, identifies elements (tuples) present in one relation but not
the other.
3. Set difference
• Alternative Names: Set difference is also known as the MINUS operation or EXCEPT
operation.
• Symbol: It's denoted by the minus sign (-) between two relations (R - S).
• Result: The result of the set difference includes all tuples (rows) that are present in the first
relation (R) but not in the second relation (S). Essentially, it removes the duplicates or
common elements between R and S, leaving only the unique elements present in R.
• Union Compatibility: For the set difference operation to be valid, both relations (R and S)
must be union compatible. This means they must have the same number of attributes
(columns) with the same data types in each corresponding position.
Concept:
• The Cartesian product of two relations R and S, denoted by R x S, results in a new relation
containing all possible combinations of tuples from R and S.
• Imagine creating a new table by taking every row from the first table (R) and pairing it with
every row from the second table (S).
Result:
• The resulting relation will have a schema (attribute structure) that combines the attributes of
both R and S.
• Each new tuple (row) will consist of values from a corresponding row in R and a
corresponding row in S.
• The number of rows in the resulting relation will be the product of the number of rows in the
original relations (R x S = number of rows in R * number of rows in S). This can lead to a
significant increase in data compared to the original relations.
Applications:
• The Cartesian product is often used as a starting point for more complex relational algebra
operations like selections and joins.
• It can help create new relations that combine data from multiple tables for further analysis.
Important Points:
• While the Cartesian product can be useful, it can sometimes lead to a large number of
irrelevant combinations.
• Using selection or join operations after the Cartesian product can help filter the results to
focus on specific combinations of interest.
𝜎 Emp_no=D_Emp_no (EMP_DEPS)