Relational Algebra

Relational Algebra

Introduction

Relational algebra is a procedural query language used to query and manipulate data in relational databases.

It provides a foundation for SQL, and defines operations on relations (or tables) to produce new relations.

The key features are:

  • Operates on one or more relations.
  • Outputs a new relation.
  • Uses a mathematical approach.

It is very important because it:

  • Forms the theoretical foundation of relational databases.
  • Helps in query optimization.
  • Translates user-friendly SQL queries into executable instructions.
  • Promotes a deeper understanding of database operations.

Query Processing

Query processing steps include:

  1. Parsing and translation
  2. Optimization
  3. Evaluation

Basic Relational Algebra Operations

Operation Symbol Example
SELECT σ\sigma σ\sigma Status = “Active”(Customer)
PROJECTION π\pi Π\Pi CustomerName, Status (Customer)
RENAME ρ\rho ρ\rho newname/{new_name} / {old_name}
UNION \cup ABA \cup B gives all tuples in table AA and BB.
INTERSECTION \cap ABA \cap B gives all tuples both in AA and BB.
DIFFERENCE - ABA - B gives tuples in AA but not in BB.
CARTESIAN PRODUCT ×\times A×BA \times B gives combinations of columns in AA and BB.
JOIN \Join None

Modification of Database

Operations of modifying databases:

  • Deletion
  • Insertion
  • Updating

These operations are expressed using the assignment operator \gets.

Deletion

rrE r \gets r - E

Example:

CustomerCustomer(σ  Status = ‘Active’(Customer)) \text{Customer} \gets \text{Customer} - (\sigma \; \text{Status = `Active'(Customer)})

Insertion

rrE r \gets r \cup E

Example:

CustomerCustomer{1,Name,Active} Customer \gets Customer \cup \{1, `\text{Name}', `\text{Active}'\}

Updating

Updating can be expressed by a sequence of deletion and insertion operations.

Last updated on