DBMS UNIT 3
Relational Algebra
Prepared By:
Ms. Barkha Namdev
Relational algebra
Relational algebra is a procedural query language.
It gives a step by step process to obtain the result
of the query. It uses operators to perform queries.
1. Select
The select operation selects tuples that satisfy a
given predicate.
The notation is − σp(r)
Here σ stands for the selection predicate
while r stands for the relation. p refers to the
prepositional logic formula that may use
connectors such as or, and, and not. Also, these
terms may make use of relational operators such
as − =, ≠, ≥, < , >, ≤.
Example
1. σsubject = “information”(Novels)
The output would be − Selecting tuples from the
novels wherever the subject happens to be
‘information’.
2. σsubject = “information” and cost =
“150”(Novels)
The output would be − Selecting tuples from the
novels wherever the subject happens to be
‘information’ and the ‘price’ is 150.
Example
3. σsubject = “information” and cost = “150” or
year > “2015”(Novels)
The output would be − Selecting tuples from the
novels wherever the subject happens to be
‘information’ and the ‘price’ is 150 or those novels
have been published after 2015.
2.Project Operation (or ∏)
It projects those column(s) that satisfy any
given predicate.
Here B1, B2 , An refer to the attribute
names of the relation r.
The notation is − ∏B1, B2, Bn (r)
Remember that duplicate rows are
eliminated automatically, since relation is a
set.
2.Project Operation (or ∏)
Example
∏subject, writer (Novels)
The output would be − Selecting and
projecting columns named as writer as well
as the subject from the relation Novels.
3. Union Operation:
Suppose there are two tuples R and S. The
union operation contains all the tuples that are
either in R or S or both in R & S.
It eliminates the duplicate tuples. It is denoted
by ∪.
Notation: R ∪ S
A union operation must hold the following
condition:
R and S must have the attribute of the same
number.
Duplicate tuples are eliminated automatically.
DEPOSITOR
BORROW
CUSTOMER_NA ACCOUNT_NO CUSTOMER_ LOAN_NO
ME NAME
Johnson A-101 Jones L-17
Smith A-121 Smith L-23
Mayes A-321 Hayes L-15
Turner A-176 Jackson L-14
Johnson A-273 Curry L-93
Jones A-472 Smith L-11
Lindsay A-284 Williams L-17
Union Operation
Input:
∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTO
MER_NAME (DEPOSITOR)
Output: CUSTOMER_NAME
Johnson
Smith
Hayes
Turner
Jones
Lindsay
Jackson
Curry
Williams
4. Set Intersection:
Suppose there are two tuples R and S. The
set intersection operation contains all tuples
that are in both R & S.
It is denoted by intersection ∩.
Notation: R ∩ S
∏ CUSTOMER_NAME (BORROW) ∩ ∏ CUSTO
MER_NAME (DEPOSITOR)
Output:
CUSTOMER_NAME
Smith
Jones
5. Set Difference:
Suppose there are two tuples R and S. The
set intersection operation contains all tuples
that are in R but not in S.
It is denoted by intersection minus (-).
Notation: R - S
Input:
∏ CUSTOMER_NAME (BORROW) - ∏ CUSTOM
ER_NAME (DEPOSITOR)
Output:
Set Difference:
CUSTOMER_NAME
Jackson
Hayes
Willians
Curry
5. Cartesian product
The Cartesian product is used to combine
each row in one table with each row in the
other table. It is also known as a cross
product.
It is denoted by X.
Notation: E X D
Employee
Department
DEPT_NO DEPT_NAM
EMP_ID EMP_NA EMP_DEP E
ME T
A Marketing
1 Smith A
B Sales
2 Harry C
C Legal
3 John B
Input:
EMPLOYEE X DEPARTMENT
output
EMP_ID EMP_NAME EMP_DEPT DEPT_NO DEPT_NAM
E
1 Smith A A Marketing
1 Smith A B Sales
1 Smith A C Legal
2 Harry C A Marketing
2 Harry C B Sales
2 Harry C C Legal
3 John B A Marketing
3 John B B Sales
3 John B C Legal
7. Rename Operation:
The rename operation is used to rename
the output relation. It is denoted by rho (ρ).
Example: We can use the rename operator
to rename STUDENT relation to STUDENT1.
ρ(STUDENT1, STUDENT)
Relational Calculus
Relational Calculus, as compared to Relational
Algebra, refers to a non-procedural type of query
language. It means that it would tell you what to
do but would never explain how to do it.
The relational calculus exists in these two forms:
Tuple Relational Calculus (or TRC)
Domain Relational Calculus (or DRC)
Tuple Relational Calculus (or TRC)
The tuple relational calculus is specified to select the
tuples in a relation.
The result of the relation can have one or more tuples.
Notation:
A Query in the tuple relational calculus is expressed
as following notation
{T | P (T)} or {T | Condition (T)}
Where
T is the resulting tuples
P(T) is the condition used to fetch T.
Tuple Relational Calculus (or TRC)
Example
{ T.name | Author(T) AND T.article = 'database' }
Output: This query selects the tuples from the
AUTHOR relation. It returns a tuple with 'name'
from Author who has written an article on
'database'.
Domain Relational Calculus (DRC)
The second form of relation is known as Domain
relational calculus. In domain relational calculus,
filtering variable uses the domain of attributes.
Notation:
{ a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}
Where
a1, a2 are attributes
P stands for formula built by inner
attributes
Domain Relational Calculus (DRC)
For example:
{< article, page, subject > | ∈ javatpoint ∧ subject
= 'database'}
Output: This query will yield the article, page, and
subject from the relational javatpoint, where the
subject is a database.