Categories: DBMS

What is Relational Algebra?

Relational Algebra is a procedural query language, which takes Relation as input and generates relation as output. SQL and relational databases primarily have their theoretical underpinnings in relational algebra.

In relational algebra, the input typically consists of a collection of one or more relations, and the output typically consists of another relation that illustrates the outcome of a query or operation on the relations in the input. Because it contains the information that the user requested for a specific query or operation, the output relation is frequently referred to as a “result set” or “temporary table.”

The following are relational algebra’s fundamental operations:

  • Select
  • Project
  • Union
  • Set Different
  • Cartesian product
  • Rename
Select Operation (σ):

With this operation, rows from a relation that meet a predetermined condition are chosen. The relation’s attributes are used to express the condition as a logical formula.

Notation : σp(r)

Where σ stands for selection predicate and r stands for relation. p is a prepositional logic formula which may use connectors like and, or, and not. These terms may use relational operators like − =, ≠, ≥, < ,  >,  ≤.

For instance,

σsubject = "math" and marks >45(Result)

This will return tuples(rows) from the Result table with the information of students for math subject, where marks are more than 45.

Project Operation (∏):

With this operation, particular relational columns (attributes) are chosen. Only the attributes that were chosen will be present in the resulting relation.

Notation − ∏A1, A2, An (r)

Where A1, A2 , An are attribute names of relation r.

Note: Duplicate rows are automatically eliminated, as relation is a set.

For instance,

∏ name, roll (Result)  

This statement will output only the name and Roll number of all the students in Result table.

Union Operation (∪):

This operation combines two relations (with the same schema) and removes duplicates.

Notation : r U s

Where r and s are either database relations or relation result set (temporary relation).

For a union operation to be valid, the following conditions must hold −

  • r and s must have the same number of attributes.
  • Attribute domains must be compatible.
  • Duplicate tuples are automatically eliminated.

example,

∏ name (Singers) ∪ ∏ name (Dancers)

Above operation will give us names of Students who are both dancers and singers, eliminating repetition.

Set Difference (-):

This operation finds the difference between two relations (with the same schema). The resulting relation contains all the tuples in the first relation that are not in the second relation.

Notation : rs

Finds all the tuples that are present in r but not in s.

example,

∏ name (Singers) - ∏ name (Dancers)

Above operation will give us names of Students who are singers but not dancers, eliminating repetition.

Cartesian Product (X):

Each tuple from one relation is combined with each tuple from another relation using this operation. All the attributes from both input relations are included in the schema of the resulting relation.

Notation − r Χ s

Where r and s are relations and their output will be defined as −

r Χ s = { q t | q ∈ r and t ∈ s}

Example,

σsubject = 'math'(Result Χ Registered)

For the above query to work, both Registered and Result should have the attribute subject.

Rename Operation (ρ):

This operation renames the attributes of a relation. The resulting relation has the same tuples as the original relation, but the attribute names are changed.

Notationρ x (E)

Where the result of expression E is saved with name of x.

Example:

ρ(STUDENT1, STUDENT)

We can use the rename operator to rename STUDENT relation to STUDENT1.

Note: also read about Basic Concepts of RDBMS

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago

Binary Search Tree (BST)

A Binary Search Tree (BST) is a type of binary tree that satisfies the following…

1 year ago