Relational database systems are expected to be equipped with a query language that can assist its users to query the database instances.
The main application of relational algebra is providing a theoretical foundation for relational databases, particularly query languages for such databases, chief among which is SQL.
Relational algebra is a theoretical procedural query language, which takes instances of relations as input and yields instances of relations as output. It uses operators to perform queries. An operator can be either unary or binary. They accept relations as their input and yield relations as their output. Relational algebra is performed recursively on a relation and intermediate results are also considered relations.
The fundamental operations of relational algebra are as follows −
1. Select Operation (σ)
It selects tuples that satisfy the given predicate from a relation.
Notation − σp(r)
Where σ stands for selection predicate and r stands for relation. p is prepositional logic formula which may use connectors like and, or, and not. These terms may use relational operators like − =, ≠, ≥, < , >, ≤.
For example −
σsubject = "database"(Books)
Output − Selects tuples from books where subject is ‘database’.
σsubject = "database" and price = "450"(Books)
Output − Selects tuples from books where subject is ‘database’ and ‘price’ is 450.
σsubject = "database" and price = "450" or year > "2010"(Books)
Output − Selects tuples from books where subject is ‘database’ and ‘price’ is 450 or those books published after 2010.
2. Project Operation (∏)
It projects column(s) that satisfy a given predicate.
Notation − ∏A1, A2, An (r)
Where A1, A2 , An are attribute names of relation r.
Duplicate rows are automatically eliminated, as relation is a set.
For example −
∏subject, author (Books)
◦Output: Selects and projects columns named as subject and author from the relation Books.
3. Union Operation (∪)
It performs binary union between two given relations and is defined as −
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.
For Example: ∏ author (Books) ∪ ∏ author (Articles)
Output − Projects the names of the authors who have either written a book or an article or both.
4. Set Difference (−)
The result of set difference operation is tuples, which are present in one relation but are not in the second relation.
Notation − r − s
Finds all the tuples that are present in r but not in s.
For Example: ∏ author (Books) − ∏ author (Articles)
Output − Provides the name of authors who have written books but not articles.
5. Cartesian Product (Χ)
Combines information of two different relations into one.
Notation − r Χ s
Where r and s are relations, and their output will be defined as −
σauthor = ‘nishant'(Books Χ Articles)
Output − Yields a relation, which shows all the books and articles written by nishant.
6. Rename Operation (ρ)
The results of relational algebra are also relations but without any name. The rename operation allows us to rename the output relation. ‘rename’ operation is denoted with small Greek letter rho ρ.
Notation − ρ x (E)
Where the result of expression E is saved with name of x.