0% found this document useful (0 votes)
14 views11 pages

DB Lesson 4 Class Note Relational Algebara Part 1

The document discusses relational algebra, which provides operations for manipulating data in relational databases. It describes traditional set operations like union, intersection, difference, and cartesian product. It explains concepts like union compatibility and provides examples of how to apply operations like intersection and difference to relations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views11 pages

DB Lesson 4 Class Note Relational Algebara Part 1

The document discusses relational algebra, which provides operations for manipulating data in relational databases. It describes traditional set operations like union, intersection, difference, and cartesian product. It explains concepts like union compatibility and provides examples of how to apply operations like intersection and difference to relations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

SEMESTER 02

IT2306 – Database
Systems

Relational Algebra
part 1 – Traditional Set of Operations

Follow us on

@AcademyofGigaNerds
@GigaNerds

Contact us – 071 380 52 07


Semester 02 - DB Lesson 4 - Relational Algebra

Relational algebra (RA) provides a foundation for manipulating data in relational


databases. It uses operators to perform specific actions on relations (tables) and retrieve desired
results. While RA might seem a bit mathematical, it forms the basis for the more user-friendly
Structured Query Language (SQL) used in everyday database interaction.

Informally, relational algebra is a (high-level) procedural language.

Core Operations and Expressions:


• Basic Set of Operations: RA offers a core set of operators that act on relations (tables) to
achieve various tasks. These include filtering data (selection), combining tables (joins),
extracting specific attributes (projection), and performing set operations (union, intersection,
difference). These operations form the building blocks for complex queries.
• Relational Algebra Expressions: A sequence of these RA operations forms a relational
algebra expression. This expression defines a step-by-step process for manipulating data
and retrieving the desired results.
• Closure Property: The output of an RA operation is also a relation, it can be used as input
for another RA operation. This allows for the nesting of operations. You can build
complex queries by chaining multiple RA operations together, where the result of one
operation becomes the operand for the next.

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.

2|Page ACADEMY OF GIGA NERD


Semester 02 - DB Lesson 4 - Relational Algebra

Traditional set operations

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

1. Same Number of Attributes (Columns): Both


relations must have the same number of columns.
This ensures that the resulting union operation can
combine rows in a meaningful way.

2. Compatible Attribute Domains (Data Types): The


corresponding columns (with the same position
or order) in both relations must have compatible
data types. For example, you can't union a column
containing names (text) with another column
containing ages (numbers). The data types need to
be aligned for the values to be combined correctly.

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.

Union (U), Intersection (∩), and Difference (-)


• Union Compatibility Required: for these operations (union, intersection, and difference) to
be performed, the relations involved must be union-compatible.
o Same Number of Attributes
o Compatible Data Types:

4|Page ACADEMY OF GIGA NERD


Semester 02 - DB Lesson 4 - Relational Algebra

1. Union Operation (R ∪ S):

• Combining Rows: The union of relations R and S (denoted by R ∪ S) creates a new


relation containing all distinct tuples (rows) that exist in either R, S, or both.

• 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.

• Union Compatibility: As you correctly mentioned, R and S must be union compatible.


o Same Number of Attributes
o Compatible Data Types

• 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.

5|Page ACADEMY OF GIGA NERD


Semester 02 - DB Lesson 4 - Relational Algebra

2. Intersection (R ∩ S):

STUDENT ∩ INSTRUCTOR = STUDENT – (STUDENT - INSTRUCTOR)

STUDENT – (STUDENT - INSTRUCTOR)

3.
4.
5.
6.
7.
8.
9.
10.

11. Set Difference

In relational algebra, intersection operates similarly to set theory's intersection but


specifically applies to relations (tables) in a database.

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.

6|Page ACADEMY OF GIGA NERD


Semester 02 - DB Lesson 4 - Relational Algebra

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:

Consider two relations:

• Customers (CustID, Name, City)


• Orders (OrderID, CustID, Product)
Both relations have a matching attribute CustID. We can perform an intersection to find customers
who placed orders:
SQL
Customers ∩ Orders = (SELECT c.CustID, c.Name, c.City FROM Customers c
INNER JOIN Orders o ON c.CustID = o.CustID);

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.

7|Page ACADEMY OF GIGA NERD


Semester 02 - DB Lesson 4 - Relational Algebra

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.

8|Page ACADEMY OF GIGA NERD


Semester 02 - DB Lesson 4 - Relational Algebra

9|Page ACADEMY OF GIGA NERD


Semester 02 - DB Lesson 4 - Relational Algebra
Cartesian Product
The Cartesian product, also known as the cross product, is another fundamental operation in
relational algebra. It combines rows (tuples) from two relations to create a new relation. Here's a
breakdown of the concept:

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.

10 | P a g e ACADEMY OF GIGA NERD


Semester 02 - DB Lesson 4 - Relational Algebra
EMPNAMES = > 𝜋 Fname, Lname, Emp_no (𝜎 Gender=‘F’ (Employee))
EMPNAMES

DEPS => 𝜋 Emp_no, Dependent_name (Dependent)


DEPS

EMP_DEPS => EMPNAMES x DEPS


EMP_DEPS

𝜎 Emp_no=D_Emp_no (EMP_DEPS)

11 | P a g e ACADEMY OF GIGA NERD

You might also like