Relational Algebra P
Relational Algebra P
language that can assist its users to query the database instances. There
are two kinds of query languages − relational algebra and relational
calculus.
Relational Algebra
Relational algebra is a 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.
Select
Project
Union
Set different
Cartesian product
Rename
Notation − σp(r)
For example −
σsubject (Books)
= "database"
Output − Selects tuples from books where subject is 'database'.
Output − Selects tuples from books where subject is 'database' and 'price'
is 450.
σsubject (Books)
= "database" and price = "450" or year > "2010"
Output − Selects tuples from books where subject is 'database' and 'price'
is 450 or those books published after 2010.
For example −
Selects and projects columns named as subject and author from the
relation Books.
r ∪ s = { t | t ∈ r or t ∈ s}
Notation − r U s
Output − Projects the names of the authors who have either written a book
or an article or both.
Notation − r − s
Finds all the tuples that are present in r but not in s.
Output − Provides the name of authors who have written books but not
articles.
Notation − r Χ s
r Χ s = { q t | q ∈ r and t ∈ s}
Output − Yields a relation, which shows all the books and articles written
by tutorialspoint.
Set intersection
Assignment
Natural join
Notation
R1 ⋈θ R2
R1 and R2 are relations having attributes (A1, A2, .., An) and (B1, B2,..
,Bn) such that the attributes don’t have anything in common, that is R1 ∩
R2 = Φ.
Student
SID Name Std
101 Alex 10
102 Maria 11
Subjects
Class Subject
10 Math
10 English
11 Music
11 Sports
Student_Detail −
Student_detail
Equijoin
When Theta join uses only equality comparison operator, it is said to be
equijoin. The above example corresponds to equijoin.
Natural Join ( ⋈)
Natural join does not use any comparison operator. It does not concatenate
the way a Cartesian product does. We can perform a Natural Join only if
there is at least one common attribute that exists between two relations. In
addition, the attributes must have the same name and domain.
Courses
CS01 Database CS
ME01 Mechanics ME
EE01 Electronics EE
HoD
Dept Head
CS Alex
ME Maya
EE Mira
Courses ⋈ HoD
Outer Joins
Theta Join, Equijoin, and Natural Join are called inner joins. An inner join
includes only those tuples with matching attributes and the rest are
discarded in the resulting relation. Therefore, we need to use outer joins to
include all the tuples from the participating relations in the resulting
relation. There are three kinds of outer joins − left outer join, right outer
join, and full outer join.
Left
A B
100 Database
101 Mechanics
102 Electronics
Right
A B
100 Alex
102 Maya
104 Mira
Courses HoD
A B C D
A B C D
Courses HoD
A B C D