Borie. operate Dovvred. Opralin,
selubtond =~) Ue sein oy)
Prjerkion( 2) G INTERSECT ( /)
- Gross Prod (x) Lapavaszov (4+)
Onion, .
Rename
Beer optoence
DEEPLY PPE PPE yayRelational Algebra & Calculus
‘A query language is a language in which user requests to retrieve some information ftom the database, The
query languages are considered as higher level languages than programming languages. Query languages are of
two types,
Procedural Language :
Non-Procedural Language fommohert> elo —
1. In procedural language, the user has to describe the specific procedure to retrieve the information from J
the database. ye db,
vohat: ee
“ ote lp?
Example: The Relational Algebra is a procedural languaze—> * He Ae & @ 4]
Geo Hava, Crt, Pelabional fig?
2. In non-procedural language, the user retrieves the information front the database without describing the
specific procedure to retrieve it.
«procedure to retrieve it,
Example: The Tuple Relational Calculus and the Domain Relational Calculus are non-procedural
Page Proudunasdl ve
: « mt at
Relational Algebra popumateat ope Condacative "a Selling abo
ee- SQL
The relational algebra is a procedural query language. It consists of a set of operations that take one or two
relations (tables) a8 input and produce a new relation, on the request of the user to retrieve the specific
information, as the output, |
‘The relational algebra contains the following operations,
1) Selection 2) Projection 3) Union 4) Rename
5) Set-Difference 6) Cartesian product 7) Intersection 8) Join
9) Divide 10) Assignment
The Selection, Projection and Rename operations are called unary operations because they operate only on one
relation The other operations ropetwey ot pairs bf ralations and are therelore called binary operations q
Fis ae .
aay cette, Ses CSL Se
Sorte cons Od Ae bal doa
SE — some |Ws
a Opp (shalt)
Frama (Fates (Sadie) «
1) The Selection (operation: 5 sed to potive datafrom dupli
‘elation (output) is ereated from another existing relation by selecting only rows requested by the user that satv
| ® specified condition, The lower greek letter ‘sigma 1 used to denote selection operation
nyi®
J) General Syntax: Selection condition ( relation name) | 7 (sduk Cond)
z ALS Compassion Dw two athibule.
sample: Find the customer details who are living in Hyderabad city from customer relation. 4
Rebate) wee ede
city = ‘Hyderabad’ ( customer ) eid COMP =
The selection operation uses the column names in specifying the selection condition Selection condition
are same as the int tement of any programming languages, selection condition uses —
relational operator(< > <= >= !=__ 1115 possible to combine several conditions imto a large condition using the,
Jojgical Gonnectives “and” represented by‘ ° and ‘or’ represented by * * orev px C= 2(EP)
Example: te
Customer relation,
city =*Hyderabad" — eustomer_id > 1000 ( customer )
Find the customer details who are living in Hyderabad city and whose customet_id ts greater than 1000 -—
2) The Projection ( —) operation:
‘The projection is a relational algebra operation that creates a new relation by deleting columns from on
existing relation i e., a new relation is created from another existing relation by selecing only thos
Columns requested by the user from projection and is denoted by letter pi (
. fone ei tk ae
The Selection operation eliminates unwanted rows whereas the projection operation eliminates uawa’
columns, he projection operation extracts specified columns from a table.
Find the customer names (not all customer details) who are living isi Hyderabad city from cu:
a 2 Ee . e 5
customer_name ( city =*Hyderabad’ ( customer ) )In the above example, the selection operation is performed first. Next, the projection
relation on the customer_name column is carried out. Thus, inste ‘Of all custoiner details of
in Hyderabad city, we can display only the customer names of cusfomers living in Hyderabad city
The above example is also known as relational algebra expression because we are combining two of
‘ations (i¢., selection and projection) mito Ome t-Fhe-sae-
‘more relational algebra operations (i¢., selection and projection) #1 OMe ume 3
Example: Find the customer names (not all customer details) from customer relation,
cM |. customer_name (customer )
The above stated query lists all customer names in the customer relation and this is not called as
relational algebra expression because itis performing only one relational algebra operation
3) The Set Operations: ( Union, Intersection, Set-Difference, Cartesian product )
7
i) Union * ©) * Operation: oF
‘The union denoted by °°) ILis a relational algebra operation that creates a union or combination of two
relations, The result of this operation, denoted by d ()( bis a relation that includes all tuples that all either in d or
inb or in both d and b, where duplicate tuples are eliminated.
Example: Find the customer_id of all customers in the bank who have either an account of 4 loan or both,
customer_id (depositor) | U customer_id ( borrower )
‘To solve the above query, first find the customers with an account in the bank. That is |. customer id
(depositor ), Then, we have to find all customers with a loan in the bank, |" customer_id ( borrower ). Now, to
answer the above query, we need the union of these two sets, that is, all customer names that appear in either or
both of the two relations by [) customer. id ( depositor ) /)°)~f2) customer_id ( borrower )
Ifsome customers A, B and C are both depositors ax well as borrowers, then in the resulting relation, their
customer ids will occu only once because duplicate values are eliminated,Therefore. for a union operation d
to be valid, we require that two conditions to be satisfied,
4) The relations depositor and borrower must have same number of attributes / columns.
fy The domains of i* attribute of depositor relation and the i* attribute of borrower relation must be
alli.
{| same,
| + The Intersection ‘ ”<" Operation:
The intersection operation denoted by" ‘"_’ It is a relational algebra operation that finds tuples that aré
both relations. The result of this operation, denoted by db, is a relation that includes all tuples common in b
depositor and borrower relations.
Example: Find the customer_id of all customers in the bank who have both an account and a loan.
customer_id ( depositor ) customer_id ( borrower }
‘The resulting relation of this query, lists all common customer ids of customers who have both an acco
and a loan, Therefore, for an intersection operation db to be valid, it requires that two conditions to h
isfied as was the case of union operation stated above
The set-difference operation denoted by’ \"' It ix relational algebra operation that finds tuples that
in one relation but are not in another.
‘Example:
© customer _id ( depositor ) ‘customer, id ( borrower )
F ‘The resulting relation for this query, lists the customer ids of all customers who have an account but
foan, Therefore a difference operation db 10 be valid, it requires that two conditions to be satisfied os was c
> pe a Gadunn- must lee Jay fn>" Cross-product (or) Cartesian Product * X ‘Operation:
ip The Cartesian-product operation denoted by a cross °X’(’ Itisa relational algebra operation which allows
>" combine information from who relations into one felation
; Assume that there are al tuple in borrower relation and n2 tuples in loan relation. Then, the result of this
‘operation, denoted by r= borrower X loan, is a relation ‘r’ that includes all the tuples formed by each possible pair
‘of tuples one from the borrower relation and one from the loan relation. Thus, “f° is a large relation containing n)
* n2 tuples
The drawback of the Cartesian-product is that same attribute name wall repeat, |
Example: Find the customer_id of all customers in the bank who have loan > 10,000. |
customer_id ( “borrowerJoan_no= loan.loan_no ((_) borrower.Joan_no= ( borrower X
Joan )))
That is, get customer_id from borrower relation and loan_amount from loan relation First, find Cartesian
juct of borrower X loan, so that the new relation contains both customer_id, loan_amoount with each
‘combination Now, select the amount, by {) —bloan_ampunt > 10000,
So, if any customer have taken the loan, then borrower loan_no= loan.loan_no should be selected as their
entries of loan_no matches in both relation.
4) The Renaming * 1” Operation:
‘The Rename operation is denoted by tho °L.". It is a relational algebra operation which is used to give the
yew names to the relation algebra expression, Thus, we can apply the rename operation to a relation ‘borrower" to
‘get the same relation under a new name. Given a relation ‘customer’, then the expression returns the same relation
“customer” under a new name ‘x’.
x (customer )
After performed this operation, Now there are two relations, one with customer name and second with
x° name. The ‘rename’ operation is useful when we want to compare the values among same column attribute in
Ja relation.
Example: Find the largest account balance in the bankaccountbalance ( account.balance > d.balance ( accoant Xd (account) ))
If we want to find the largest account balance in the bank, Thea we have to compare the values among
same column (balance) with each other m a same relation account, which ts not possi:
So, we rename the relation with a new name'd’, Now, we have two relations of account, one with account
ame and second with ‘d’ name. Now we can compare the balance attribute values with each other im separate
relations
5) The Joins * 04” Operation:
‘The jon operation, denoted by jom ° D<")" Itxs relational algebra operation, which is used fo combine
(join) two relations like Cartesian-product but finally removes duphcate attnibutes and makes the operations
(selection, projection, .) very simple. In simple words, we can say that join connects relabons oa columns
containing comparable informanon
There are three types of jours
1) Natural Jomn
in) Outer Jon
131) Theta Join (or) Conditional Joan
i) Natural Join:
‘The natural jomn wa binary operation that allows ws to combine two different relations into one relation
sand makes the sam column i two different relations into only one-cobunin in the resulung relation. Suppose
we have relations with following schemas, which contasn data on full-time employees
‘employee ( emp_name, street, city} and
cmployee_works(eup_name, branch name, salary)==
[ane [Redmond [37000
VUUEUe VV eeLY ole
Mf we want to gencrate a single relation with all the information (emp_name, street, city, branch _name and
) about full-time employees. then, a possible approach would be to use the natural-join operation as follows.
employee OS employee_works
result of this expression is the relation,
=
~
=
result of Natural join
We have lost street and city information about Smith, since wples describing smith % absent in
works. Similarly, we have lst branch name and salary information about Gites, since the tuple
‘Gales is absent front the employee relation. Now, we can easily perform select or reject query oo newExample: Find the employee names and city who have salary details.
emp_name, salary, city (employe DC employee_works }
‘The join operation selects all employees with salary details, from where we can easily project
‘employee names, cities and salaries. Natural Join operation results in some loss of information.
ii) Outer Join:
phoraenone
The drawback of natural join operation is some loss of information. To overcome the drawback of
join, we use outer-join operation. The outer-join operation 1s of three types,
a) Left outer-join ¢
b) Right outer-join ( pa)
¢) Full outer-join( pg
a) Left Outer-join:
enene
i
The left outer-join takes all tuples in left relation that did not match with any tuples in right relation,
the tuples with null values for all other columns from right relation and adds them to the result of natural join
follows,
‘The relations are,
pa anats
[Raimond 5a00
‘employee_works relation‘The result of this expression is the relation,
Carrotville
Valley
ae a
,~
result of Left Outer-join
b) Right Outer-join:
‘The right outer-join takes all tuples in right relation that did not match with any tuples in left relation, adds
the tuples with null values for all other columns from left relation and adds them to the result of natural join as
follows,
The relations are,
branch_name] salary
Oy Mesa 15000
aFay Toran eT
[est [tes [eons ison —]
[Gates frull_———|null____[Rednand [25000 |
Laas —fearir —[Sants fren —| 000 —]
result of Right Outer-join
©) Full Outer-join:
The full outer-join operation does both of those operations, by adding tuples from left rclation that did not
match any tuples from the reight relations, as well as adds tuples from the right relation that did not match any
tuple from the left relation and adding them to the result of naturat join as follows,
The relations are,result of Full Outer-join
iii) Theta Join (or) Condition join:
“The theta join operation, denoted by symbol * Da |."". Itis an extension to the natural join operation
that. combines two relations into one relation with a selection condition ()
The theta join operation is expressed as employee Dd salary < 19000 employee_works and the resulting
is as follows,
employee Dd salary > 20000
mployee_works
‘There are two tuples selected because theit salary greater than 20000 (salary » 20000) The result of theta
join as follows,
The relations are,
MRR RRAR ARRAN EEC CCEE COO or re yeEEY YEThe tesult of this expression is the relation,
[emp_name [street [rity Thbranch_name [salary]
[Gates [oul [null [Redmond [>5000__|
[Watams [Seaview [Seat | Redmond [23000]
result of Theta Join (or) Condition Join
| 6) The Division “ > * Operation:
The division operation, denoted by * (-°", isa relational algebra operation that creates a new relation by
| selecting the rows in one relation that does not match rows in another relation
Let, Relation Ais (x1, x2, ...., xn, yl, y2,.... ym) and
Relation B is (yl, y2, .... ym),
Where, yl, y2,...,ym_ tuples are common to the both relations A and B with same domain
| compalsory.
Then, A 1! B= new relation with x1, x2, ...., xm tuples, Relation A and B represents the dividend and
devisor respectively. A tuple ‘t"is in a 2b, ifand only if two conditions are to be satisfied,
tisin “A-B(r)
for every tuple th in B, there is a tuple ta in A satisfying the following two things,
1. ta[B] = th{B)
2. ta[A-B]=t
‘Relational Calculus
| Relational calculus is an alternative to relational algebra, In contrast to the algebra, which is procedural
|| the relational calculus is non-procedural or declarative
It allows user to describe the set of answers without showing procedure about how they should b7
Relational calculus are of two types,
Tuple Relational Calculus (TRC)
Domain Relational Calculus (DRC)
Variables in TRC takes tuples (rows) as values and TRC had strong influence on SQL
Variables in DRC takes fields (attributes) as values and DRC had strong influence on QBE.
i) Tuple Relational Calculus (TRC):
‘The tuple relational calculus, is a non-procedural query language because it gives the
desired information without showing procedure about how they should be computed.
Aqquery in Tuple Relational Calculus (TRC) is expressed as { T | p(T) }
Where, T —_- tuple variable,
P(T) -‘p'is.a condition or formula that is true for ‘t’
In addition to that we use,
TIA] - to denote the value of tuple t on attribute A and
Tor - to denote that tuple tis in relation r
ULUVELUUELYYYYVYV EY o
‘samples:
4) Find all loan details in loan relation.
~s ft] t 00 toan }
“si This query gives all loan details such as loan_no, loan_date, Joan_amt for all loan table in a bank
) Find all loan details for Joan amount over 100000 in loan relation,
~ {t]t "loam >t floan_amt]> 100000). ~=~*~*~*~*~* : au: -
This query gives all loan details such as loan_no, loan_date, loan_amt for all loan over 100000 in a loan
>
n>
y
ots
Dm.fi) Domain Relational Calculus (DRC);
A Duple Relational Calculus (DRC) is a variable that comes in the range of the values of domain (data fg
types) of some columns (attributes),
A Domain Relational Caleulus query has the form,
(
| plat x2, sam >) |
Where, cach xi is either a domain variable or a constant and p(< x1, x2. ..... xn >) denotes a DRC
formula,
ADRC formula is defined in a manner that is very similar to the definition of a TRC formula The main
difference is that the variables are domain variables,
Examples:
1) Find all loan details in toan relation
{| Clan}
" This query gives all loan details such as loan_no, loan_dte, loan_amt for all Ioan table in a bank. Each
‘column is represented with an initials such as N-loan_no, D ~ loan_date, A~ loan_amt. The condition < N, D, A>
Joan ensures that the domain variables N, D, A are restricted to the column domain.
pressive power of Algebra and Calculus
The tuple relational calculus restricts to safe expressions and is equal in expressive power to relational
algebra. Thus, for every relational algebra expression, there is an equivalent expression in the tuple relational
calculus and for tuple relational calculus expression, there is an equivalent relational algebra expression
A safe TRC formula Q 1s a formula such that,
For any given I, the set of answers for Q contains only values that are in dom(Q, 1.
———~" For each sub expression of the form TR(p(R)) in O, if'a tuple f makes the formula true, then t contains
only constraints in dom(Q, 1).Ce
Vv
3) For each sub expression of the form | R¢p(R)) in Q. 1fa tuple r contains a constant that 1s not in
Q. 1, then rmust make the formula true
wwe
The expressive power of relational algebra 1s often used as a metric how powerful a relational database
y language 1s. Ifa query language can express all the querics that we can express in relational algebra, tt 1s
d to be relationally complete. A practical query language 1 expected to be relationally complete In addition,
cial query languages typically support features that allow us to express some queries that cannot be
pressed in relational algebra
wae
‘When the domain relational calculus is restricted to safe expression, it is equivalent in expressive power
to the tuple relational calculus restricted to safe expressions, All three of the following are oquivalent,
The relational algebra
‘The tuple relational calculus restricted to safe expression
‘The domain relational calculus restricted to safe expression
RAY
a
ALDARA Ann AAA
|