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 : r − s
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
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Leave a Comment