5.relation Algebra
5.relation Algebra
5.relation Algebra
DBMS
CS-116
• Relational algebra is the basic set of operations for the relational model
• Relational algebra is a procedural query language that works on relational
model. The purpose of a query language is to retrieve data from database
or perform various operations such as insert, update, delete on the data.
When I say that relational algebra is a procedural query language, it means
that it tells what data to be retrieved and how to be retrieved.
• These operations enable a user to specify basic retrieval requests (or
queries)
• The result of an operation is a new relation, which may have been formed
from one or more input relations
– This property makes the algebra “closed” (all objects in relational
algebra are relations)
attributes
(or columns)
customer_name customer_street customer_city
customer
Procedural language
Six basic operators
◦ select:
◦ project:
◦ union:
◦ set difference: –
◦ Cartesian product: x
◦ rename:
The operators take one or two relations as inputs and produce
a new relation as a result.
Notation: p(r)
p is called the selection predicate
Defined as:
Example of selection:
branch_name=“Perryridge”(account)
8/10/2021 Faculty Name - Jyoti Snehi 21
Select Operation
Relation r
A B C D
1 7
5 7
12 3
23 10
1 7
23 10
Relation r: A B C
10 1
20 1
30 1
40 2
A,C (r) A C A C
1 1
1 = 1
1 2
2
8/10/2021 Faculty Name - Jyoti Snehi 28
8/10/2021 Faculty Name - Jyoti Snehi 29
Set Operations
Notation: r s
Defined as:
r s = {t | t r or t s}
For r s to be valid.
1. r, s must have the same arity (same number of attributes)
2. The attribute domains must be compatible (example: 2nd column
of r deals with the same type of values as does the 2nd
column of s)
Example: to find all customers with either an account or a loan
Relations r, s:
A B A B
1 2
2 3
1 s
r
A B
1
r s:
2
1
3
8/10/2021 Faculty Name - Jyoti Snehi 33
Intersection
• Notation r – s
• Defined as:
r – s = {t | t r and t s}
Relations r, s:
A B A B
1 2
2 3
1 s
r
r – s:
A B
1
1
• Notation r x s
• Defined as:
r x s = {t q | t r and q s}
• Assume that attributes of r(R) and s(S) are disjoint. (That is, R
S = ).
• If attributes of r(R) and s(S) are not disjoint, then renaming must
be used.
• GP-27
1 10 a
10 a
2 20 b
r 10 b
s
r x s:
A B C D E
1 10 a
1 10 a
1 20 b
1 10 b
2 10 a
2 10 a
2 20 b
2 10 b
A B C D E
1 10 a
2 10 a
Faculty Name - Jyoti Snehi 2 20 b
8/10/2021 48
Rename Operation
x ( A ,A
1
(E )
2 ,...,An )
returns the result of expression E under the name X, and with the
attributes renamed to A1 , A2 , …., An .
Find the loan number for each loan of an amount greater than
$1200
Find the names of all customers who have a loan and an account at
bank.
Find the names of all customers who have a loan at the Perryridge
branch.
customer_name (branch_name=“Perryridge”
(borrower.loan_number = loan.loan_number(borrower x loan)))
Find the names of all customers who have a loan at the Perryridge
branch.
Query 1
Query 2
customer_name(loan.loan_number = borrower.loan_number (
(branch_name = “Perryridge” (loan)) x borrower))
balance(account) - account.balance
(account.balance < d.balance (account x d (account)))
• Notation: r s
• Defined as:
• r s = { t | t r and t s }
• Assume:
– r, s have the same arity
– attributes of r and s are compatible
• Note: r s = r – (r – s)
• Relation r, s:
A B A B
1 2
2 3
1
r s
• rs
A B
2
• Join
• Division
A B C D B D E
1 a 1 a
2 a 3 a
4 b 1 a
1 a 2 b
2 b 3 b
r s
r s
A B C D E
1 a
1 a
1 a
1 a
2 b
8/10/2021 Faculty Name - Jyoti Snehi 72
Natural Join
• 5
Relation loan
Relation borrower
customer_name loan_number
Jones L-170
Smith L-230
Hayes L-155
Query 2
customer_name, branch_name (depositor account)
temp(branch_name) ({(“Downtown” ), (“Uptown” )})
Note that Query 2 uses a constant relation.
F1 ,F2 ,..., Fn
(E )
E is any relational-algebra expression
Each of F , F , …, F are are arithmetic expressions
1 2 n
Relation r:
A B C
7
7
3
10
27
branch_name sum(balance)
Perryridge 1300
Brighton 1500
Redwood 700
2) g sum(salary)(instructor)
3) g average(salary)(instructor)
4) g count(ID)(instructor)
5) g count-distinct(ID)( semester=“Spring” ᶺ year = 2010(teaches))