Mod 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 150

Dr.

Pradyumna Kumar Tripathy 1


Relational Algebra
• Query: A query is a statement requesting the retrival of
information.
• Query languages: Language in which user requests
information from the database. Allow manipulation
and retrieval of data from a database.
• There are two types of formal query languages for the
relational model.
– Procedural Query language. Describe the operation how
to get the desired information.(ex- Relational Algebra)
– Nonprocedural Query language : Describe what
information the result should contain, rather than how to
compute it. (ex- Relational Calculus)
• Tuple Relational Calculus(TRC)
• Domain Relational Calculus(DRC)

Dr. Pradyumna Kumar Tripathy 2


Relational Algebra

• The relational algebra is a procedural query


language.
• It consists of a set of operations that take one or
two relations as input and produce a new
relation as their result.
• These operations enable a user to specify basic
retrieval requests (or queries).
• It is used to tell the DBMS how to build a new
relation from one or more existing relation.
• It forms the basis of Query language.

Dr. Pradyumna Kumar Tripathy 3


Relational Algebra

• Six Basic operators:


– Select
– Project
– Cartesian Product or Cross-product
– Set-difference
– Union
– Intersection
• Additional operators
– Joins (natural, equi-join, theta join, outer-join)
– Division
– Renaming
• The relation algebra operators are classified into either
– Unary: Operate on a single relation(one operator)
– Binary : Operate on two relations(two operator)

Dr. Pradyumna Kumar Tripathy 4


Select Operation(σ)
• Symbol: σ(sigma)
• Unary operator
• Notation :  p(r) where p is called section condition or
predicate and r is a relation
• The selection condition acts as a filter
– Keeps only those tuples that satisfy the qualifying
condition
– Tuples satisfying the condition are selected whereas
the other tuples are discarded (filtered out)
• Selects a subset of rows or tuples from relation
according to a given selection condition or predicate.
• It is used as an expression to choose tuples which meet
the selection condition.
Dr. Pradyumna Kumar Tripathy 5
Dr. Pradyumna Kumar Tripathy 6
Example
emp
eno ename sal dno
1 Jones 1000 5

2 Smith 2500 1

3 Blake 3700 3

4 Xyp 2900 3

5 Pst 3785 5

Write the relational algebra expression to find the details of the emp ( eno, ename, sal,
dno) whose salary is 2500.
Ans: σ(sal = 2500)(emp) eno ename sal dno

2 Smith 2500 1

External Query: select * from emp where sal = 2500

Dr. Pradyumna Kumar Tripathy 7


Example
emp
eno ename sal dno
1 Jones 1000 5

2 Smith 2500 1

3 Blake 3700 3

4 Xyp 2900 3

5 Pst 3785 5

Write the relational algebra expression to find the details of the emp ( eno, ename, sal,
dno) of department 3.
Ans: σ(dno = 3)(emp) eno ename sal dno

3 Blake 3700 3
4 Xyp 2900 3

Dr. Pradyumna Kumar Tripathy 8


Example
emp
eno ename sal dno
1 Jones 1000 5

2 Smith 2500 1

3 Blake 3700 3

4 Xyp 2900 3

5 Pst 3785 5

Write the relational algebra expression to find the details of the emp ( eno, ename, sal,
dno) whose name is jones.
Ans: σ(ename = “jones”)(emp) eno ename sal dno

1 Jones 1000 5

Dr. Pradyumna Kumar Tripathy 9


Example
emp
eno ename sal dno We can combine more than one condition
1 Jones 1000 5 using AND(^), OR(v), NOT(¬)
2 Smith 2500 1

3 Blake 3700 3

4 Xyp 2900 3

5 Pst 3785 5

Write the relational algebra expression to find the details of the emp ( eno, ename, sal,
dno) whose sal > 3000 and dno = 5
Ans: σ(sal> 3000 ^ dno = 5)(emp) eno ename sal dno

5 Pst 3785 5

Dr. Pradyumna Kumar Tripathy 10


Example
r
A B C D
α α 1 7
α β 5 7
β β 2 3
β β 23 10

What is the output of the following relational algebra expression?


σ(A = B ^ D > 5) (r)

A B C D
α α 1 7
β β 23 10

Dr. Pradyumna Kumar Tripathy 11


Example

sid sname rating age


28 yuppy 9 35.0 sid sname rating age
31 lubber 8 55.5 28 yuppy 9 35.0
44
58
guppy
rusty
5
10
35.0
35.0
58 rusty 10 35.0
rating  (S2)
S2
8

Dr. Pradyumna Kumar Tripathy 12


Example

• σDNO=5 (σsalary>25000 (EMPLOYEE)) == σsalary>25000 (σDno=5 (EMPLOYEE)) ==


σDNO=5 AND salary>25000 (EMPLOYEE))

Dr. Pradyumna Kumar Tripathy 13


SELECT Operation Properties

• The SELECT operation  <selection condition>(R) produces a


relation S that has the same schema (same attributes) as R
• SELECT  is commutative:
•  <condition1>( < condition2> (R)) =  <condition2> ( < condition1> (R))
• Because of commutativity property, a cascade (sequence)
of SELECT operations may be applied in any order:
• <cond1>(<cond2> (<cond3> (R)) = <cond2> (<cond3> (<cond1> ( R)))
• A cascade of SELECT operations may be replaced by a single
selection with a conjunction of all the conditions:
• <cond1>(< cond2> (<cond3>(R)) =  <cond1> AND < cond2> AND < cond3>(R)))
• The number of tuples in the result of a SELECT is less
than (or equal to) the number of tuples in the input
relation R

Dr. Pradyumna Kumar Tripathy 14


Projection Operation(π)
• PROJECT Operation is denoted by  (pi)
• Unary operator
• This operation keeps certain columns (attributes) from a relation
and discards the other columns.
– PROJECT creates a vertical partitioning
• The list of specified columns (attributes) is kept in each tuple
• The other attributes in each tuple are discarded
• The general form of the project operation is:
(R)
–  (pi) is the symbol used to represent the project operation
–  are the desired list of attributes from relation R.
• The result is defined as relation of k columns obtained by
eliminating duplicate rows from the result and erasing the columns
that are not listed.
• Example: To list each employee( SSN, FNAME, LNAME, SALARY,
ADDRESS, DNO) first and last name and salary, the following is used:
LNAME, FNAME,SALARY(EMPLOYEE)

Dr. Pradyumna Kumar Tripathy 15


Example

S2

Dr. Pradyumna Kumar Tripathy 16


Example
r
A B C
α 10 1
α 20 1
β 30 1
β 40 1

What is the output of the following relational algebra expression?


π(A, C) (r)

A C
α 1
β 1

Dr. Pradyumna Kumar Tripathy 17


Example
emp
eno ename sal dno
1 Jones 1000 5

2 Smith 2500 1

3 Blake 3700 3

4 Xyp 2900 3

5 Pst 3785 5

Write the relational algebra expression to find employee name whose salary is greater
than 2500. ename
Ans: πename (σ(sal > 2500)(emp) )
Blake
Xyp
Pst

External Query: select ename from emp where sal > 2500

Dr. Pradyumna Kumar Tripathy 18


Example
emp
eno ename sal dno
1 Jones 1000 5

2 Smith 2500 1

3 Blake 3700 3

4 Xyp 2900 3

5 Pst 3785 5

Write the relational algebra expression to find employee no and salary whose salary is
greater than 3000 and dno = 3. ename sal
Ans: πename,sal (σ(sal > 3000s ^ dno=3)(emp) )
Blake 3700

Dr. Pradyumna Kumar Tripathy 19


Project Operation Properties

• The number of tuples in the result of


projection <list>(R) is always less or equal to
the number of tuples in R
– If the list of attributes includes a key of R, then the
number of tuples in the result of PROJECT is equal
to the number of tuples in R
• PROJECT is not commutative

Dr. Pradyumna Kumar Tripathy 20


Combination of Selection and Projection Operator

πSNO(σSTATE=‘NY’ (S))

O/P :

Dr. Pradyumna Kumar Tripathy 22


BASIC RELETIONAL ALGEBRA OPERATION FROM SET THEORY

NAME SYMBOL
UNION U
INTERSECTION ∩
SET DIFFERENCE -
CARTESIAN PRODUCT ×

Dr. Pradyumna Kumar Tripathy 24


Cartesian Product or Cross Product

• Denoted by x
• Let two relational schema r(A1, A2,….., An) and s(B1, B2,….., Bm)
• Notation r x s
• Defined as: r x s = {t q | t  r and q  s}
– Assumption is that the attributes of r and s are disjoint(r Ոs = Φ)
• Each row of r combined with each row of s
• Result is a relation Q with degree n + m attributes:
• Q(A1, A2,….., An, B1, B2,….., Bm), in that order.
• Hence, if r has nr tuples (denoted as |r| = nr ), and s has ns tuples,
then r x s will have nr * ns tuples or │r x s│= nr * ns
• Also known as cross join
• It is not a meaningful operation unless it is associated with some
condition.

Dr. Pradyumna Kumar Tripathy 25


Cartesian Product or Cross Product

R1 X S1

Dr. Pradyumna Kumar Tripathy 26


Cartesian Product or Cross Product

A B C D E
α 1 α 10 a A B C D E

 10 a
 1
α 1 β 10 a  10 a
α 1 β 20 b  2  20 b
r  10 b
α 1  10 b
r x s: A=C(r x s) s
β 2 α 10 a
β 2 β 10 a A B C D E
β 2 β 20 b α 1 α 10 a
β 2  20 b β 2 β 10 a
β 2 β 20 b
Dr. Pradyumna Kumar Tripathy 27
Cartesian Product or Cross Product

A B C D E
α 1 α 10 a A B C D E

 10 a
 1
α 1 β 10 a  10 a
α 1 β 20 b  2  20 b
r  10 b
α 1  10 b
r x s: A=C(r x s) s
β 2 α 10 a
β 2 β 10 a A B C D E
β 2 β 20 b α 1 α 10 a
β 2  20 b β 2 β 10 a
β 2 β 20 b
Dr. Pradyumna Kumar Tripathy 28
Cartesian Product or Cross Product

stud result
ID NAME ID1 CGPA

1 XYZ 1 9.2

2 PQR 2 8.5

Stud X result
ID NAME ID1 CGPA

1 XYZ 1 9.2

1 XYZ 2 8.5
Not Valid
2 PQR 1 9.2

2 PQR 2 8.5

Write the relational algebra expression to find student ID, name and CGPA.
Ans: πNAME,ID,CGPA (σ(ID = ID1)(stud x result) ) ID NAME CGPA

Cartesian product is meaningless unless it is associated with select 1 XYZ 9.2


condition.
2 PQR 8.5

Dr. Pradyumna Kumar Tripathy 29


stud result
ID NAME ID1 CGPA

1 XYZ 1 9.2

2 PQR 2 8.5

• Find the name of those student whose CGPA is >9.0


The relational algebra expression π NAME (σcgpa > 9.0( stud × result) ) produces
spurious tuples leading to incorrect result.
ID NAME ID1 CGPA
ID NAME ID1 CGPA NAME
1 XYZ 1 9.2
1 XYZ 1 9.2 XYZ
1 XYZ 2 8.5
2 PQR 1 9.2 PQR
2 PQR 1 9.2

2 PQR 2 8.5
π NAME (σcgpa > 9.0( stud × result) )
σcgpa >9.0( stud × result)
stud × result
Dr. Pradyumna Kumar Tripathy 30
stud result
ID NAME ID1 CGPA

1 XYZ 1 9.2

2 PQR 2 8.5

• The correct relational algebra expression to find the name of those student whose
CGPA is >9.0 is

π NAME (σ ID=ID1 (σcgpa >9.0( stud × result) )).

Dr. Pradyumna Kumar Tripathy 31


•Consider the following relational schema
Emp(eno,ename,dno,sal)
Dept(deptno,dname,dloc)
Que-Write the relational algebra expression to find the name all the
employees who works in MCA department. emp
eno ename sal dno
1 Smith 5000 10
1 Emp × Dept 2 Jones 6000 20
2 σdname = ‘MCA’ (Emp × Dept) 3 Blake 7000 10

3 σ dno = deptno (σdname = ‘MCA’ (Emp × Dept)) 4 Peter 8000 30

4 π ename (σ dno = deptno (σdname = ‘MCA’ (Emp × Dept)))


dept
deptno dname dloc
1 Emp dno =deptno Dept
10 MCA bbsr
2 σdname = ‘MCA’ (Emp dno = deptno Dept) 20 cs ctc
3-π ename (σdname = ‘MCA’ (Emp dno = deptno Dept)) 30 IT rkl

Dr. Pradyumna Kumar Tripathy 32


Emp dno =deptno Dept

eno Ename sal dno deptno dname dloc


1 Smith 5000 10 10 MCA bbsr
2 Jones 6000 20 20 cs ctc
emp
3 Blake 7000 10 10 MCA bbsr
eno Ename sal dno
4 Peter 8000 30 30 IT rkl
1 Smith 5000 10
2 Jones 6000 20
3 Blake 7000 10
σdname = ‘MCA’ (Emp dno = deptno Dept) 4 Peter 8000 30
eno Ename sal dno deptno dname dloc
1 Smith 5000 10 10 MCA bbsr
3 Blake 7000 10 10 MCA bbsr dept
deptno dname dloc
10 MCA bbsr
20 cs ctc
π ename (σdname = ‘MCA’ (Emp dno = deptno Dept)) 30 IT rkl

Ename
Smith
Blake

Dr. Pradyumna Kumar Tripathy 33


Join
• denoted by
• Notation: R <join condition>S.
• R p S ≡ σp (R x S)
• A general join condition is of the form
<condition> AND <condition> AND … AND <condition>

Join = Cartesian Product + Select


• The sequence of CARTESIAN PRODECT followed by SELECT is used quite
commonly to identify and select related tuples from two relations
• A special operation, called JOIN combines this sequence into a single
operation.
• The join condition must an attribute of R and S.
• Only combination of tuples satisfying the join condition only appear in the
result whereas in the cartesian product all combination of tuples are
included in the results.
• If degree of R = n and degree of S = m then degree of R p S = m+n

Dr. Pradyumna Kumar Tripathy 34


Types of Join
• Equi Join
• Natural Join
• Theta join
• Outer Join
– Left Outer Join
– Right Outer Join
– Full Outer Join

Dr. Pradyumna Kumar Tripathy 35


Equijoin

When the join condition c contains only comparison operator = then such type of
join is called as EQUIJOIN.

Ex.

Only = operator can be used in join condition c.

Equi join is also a theta join.

Dr. Pradyumna Kumar Tripathy 36


R1 R1.sid= s1.sid S1

sid Sname Rating age sid bid age


22 dustin 7 45.0 22 101 10/10/96
58 rusty 10 35 58 103 11/12/96

The result of equi join we have one or more pair of attributes that have identical
values in every tuple.
Dr. Pradyumna Kumar Tripathy 37
•Consider the following relational schema
Emp(eno,ename,dno,sal)
Dept(deptno,dname,dloc)
Que-Write the relational algebra expression to find the name and salary of all the
employees who belong to MCA department. emp
eno ename sal dno
1- Emp dno =deptno Dept 1 Smith 5000 10

2 σdname = ‘MCA’ (Emp dno = deptno Dept) 2 Jones 6000 20

3 π ename (σdname = ‘MCA’ (Emp dno = deptno Dept)) 3 Blake 7000 10


4 Peter 8000 30

Emp dno =deptno Dept


eno ename sal dno deptno dname dloc
dept
1 Smith 5000 10 10 MCA bbsr deptno dname dloc
10 MCA bbsr
2 Jones 6000 20 20 cs ctc
20 cs ctc
3 Blake 7000 10 10 MCA bbsr 30 IT rkl

4 Peter 8000 30 30 IT rkl

Dr. Pradyumna Kumar Tripathy 38


Natural Join Operation *

• Another variation of JOIN called NATURAL JOIN


• denoted by * or was created to get rid of the second (superfluous)
attribute of an EQUIJOIN condition.
– because one of each pair of attributes with identical values is
superfluous in equijoin
• Natural join is an equi join of two relation R and S over all
common attributes.
• The standard definition of natural join requires that the two join
attributes, or each pair of corresponding join attributes, have the same
name and domain in both relations
o –NATURAL
If this isJOIN doescase,
not the not use any of the operation
a renaming comparisonisoperators.
applied first.
o There should be at least one common attribute between two relations.
o One occurrence of each common attribute is eliminated from the result.
o It performs selection forming equality on those attributes which appear in both
relations and eliminates the duplicate attributes.
Dr. Pradyumna Kumar Tripathy 39
Natural Join

Q  R(A,B,C,D) * S(C,D,E)
The implicit join condition includes each pair of attributes with
the same name, “AND” ed together: R.C=S.C AND R.D=S.D. Result
keeps only one attribute of each such pair: Q(A,B,C,D,E)
Example:
r = (A, B, C, D)
s = (E, B, D)
t = (A, B, C, D, E)
t= r s is defined as:
r.A, r.B, r.C, r.D, s.E (r.B = s.B  r.D = s.D (r x s))
Dr. Pradyumna Kumar Tripathy 40
emp emp.deptno = dept.deptno dept

eno ename sal deptno deptno dname dloc

1 Smith 5000 10 10 MCA bbsr

2 Jones 6000 20 20 cs ctc


emp
3 Blake 7000 10 10 MCA bbsr eno ename sal deptno
1 Smith 5000 10
4 Peter 8000 30 30 IT rkl
2 Jones 6000 20
3 Blake 7000 10
emp * dept 4 Peter 8000 30
eno ename sal deptno dname dloc

1 Smith 5000 10 MCA bbsr dept


2 Jones 6000 20 cs ctc deptno dname dloc
10 MCA bbsr
3 Blake 7000 10 MCA bbsr 20 cs ctc
30 IT rkl
4 Peter 8000 30 IT rkl

Dr. Pradyumna Kumar Tripathy 41


Natural Join Operation – Example
• Relations r, s:
B D E A B C D E
A B C D
1 a α
α 1 α a
α 1 α a α
3 a 
 2  a
α 1 α a 
1 a 
 4  b
α 1  a α
2 b 
α 1  a
α 1  a 
3 b 
 2  b
 2  b 

r
s r *s

Dr. Pradyumna Kumar Tripathy 42


Example
branch (branch_name, branch_city, assets)
customer (customer_name, customer_street, customer_city)
account (account_number, branch_name, balance)
loan (loan_number, branch_name, amount)
depositor (customer_name, account_number)
borrower (customer_name, loan_number)
Q) Find all loans of amount over $1200

amount > 1200 (loan)


Q) Find the loan number for each loan amount greater than $1200

πloan_number (amount > 1200 (loan))

Q) Find the name, loan no and loan amount of all customers who have a loan at the bank

 customer_name, loan-number, amount (borrower borrower.loan_number=loan.loan_number loan)

Q) Find names of all customers who have both a loan and deposit account at the bank.

 customer_name (borrower borrower.customer_name=depositor.customer_name depositor)

Dr. Pradyumna Kumar Tripathy 43


branch (branch_name, branch_city, assets)
customer (customer_name, customer_street, customer_city)
account (account_number, branch_name, balance)
loan (loan_number, branch_name, amount)
depositor (customer_name, account_number)
borrower (customer_name, loan_number)

Q) Find names of all customers who have a loan at ‘DUMDUM’ branch.

customer_name (branch_name=“DUMDUM” (borrower borrower.loan_number = loan.loan_number loan))

Q) Find names of all customers who have an account at ‘Redwood’ branch.

customer_name (branch_name=“Redwood” (depositor depositor.account_number = accoount.account_number account))

Dr. Pradyumna Kumar Tripathy 44


Theta Join Operation
The Theta join operation is an extension to the cross-join operation that
allows us to combine a selection and a cartesian product into a single
operation.
It defines a relation that contains tuples satisfying the predicate from the
cartesian product of R and S.

s = σ
•Theta can be any general Boolean expression on r (r x s)
the attributes of R and S: θ
• R.Ai <S.Bj AND (R. Ak=S. Bl OR R. Ap<S. Bq)

where, Ɵ may be any of the


comparison operator <, ≤, >, ≥, =, ǂ

Equi join is special type of θ join where join condition uses only = as the comparison
operator.

Dr. Pradyumna Kumar Tripathy 45


Car Boat
Car_model Car_price Boat_model Boat_price
Car A 20,000 Boat 1 10,000
Car B 30,000 Boat 2 40,000
Car C 50,000 Boat 3 60,000

Car Car_price > Boat_price Boat)

Car_m Car_pr Boat_ Boat_


odel ice model price
Car A 20,000 Boat 1 10,000
Car B 30,000 Boat 1 10,000
Car C 50,000 Boat 1 10,000
Car C 50,000 Boat 2 40,000

Dr. Pradyumna Kumar Tripathy 46


•In NATURAL JOIN and EQUIJOIN, tuples without a matching (or
related) tuple are eliminated from the join result
• Tuples with null in the join attributes are also eliminated. This
amounts to loss of information.
•A set of operations, called OUTER joins, can be used when we want to
keep all the tuples in R, or all those in S, or all those in both relations in
the result of the join, regardless of whether or not they have matching
tuples in the other relation.
•It uses NULL Values.

Dr. Pradyumna Kumar Tripathy 47


Left Outer Join
Matching Tuple in natural join +Extra tuple in the left relation.
Takes all the tuples in left relation that did not match with any tuple in the right relation.
The attributes of the relation for the unmatched tules filled with NULL Values

The left outer join operation keeps every tuple in the first or left relation R in R S; if
no matching tuple is found in R, then the attributes of S in the natural join result are filled
or “padded” with null values.

Dr. Pradyumna Kumar Tripathy 48


Right Outer Join
Matching Tuple in natural join +Extra tuple in the right relation.
Takes all the tuples in right relation that did not match with any tuple in the right relation.
The attributes of the left relation for the unmatched tuple filled with NULL Values

The left outer join operation, keeps every tuple in the second or right relation S in the
result of R S. if no matching tuple is found in S, then the attributes of R in the natural
join result are filled or “padded” with null values.

Dr. Pradyumna Kumar Tripathy 49


Full Outer Join
Matching Tuple in natural join +Extra tuple in both left and right relation.
FULL OUTER JOIN = LEFT OUTER JOIN + RIGHT OUTER JOIN

keeps all tuples in both the left and the right relations when no matching tuples are
found, padding them with null values as needed.

Dr. Pradyumna Kumar Tripathy 50


Outer Join – Example
loan loan_number branch_name amount
L-170 Downtown 3000
L-230 Redwood 4000
L-260 Perryridge 1700

Relation borrower customer_name loan_number


Jones L-170
Smith L-230
Hayes L-155

loan * borrower
loan_number branch_name amount customer_name
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith

Dr. Pradyumna Kumar Tripathy 51


Outer Join – Example
loan loan_number branch_name amount
L-170 Downtown 3000
L-230 Redwood 4000
L-260 Perryridge 1700

Relation borrower customer_name loan_number


Jones L-170
Smith L-230
Hayes L-155
loan borrower
loan_number branch_name amount customer_name

L-170 Downtown 3000 Jones


L-230 Redwood 4000 Smith
L-260 Perryridge 1700 null

Dr. Pradyumna Kumar Tripathy 52


Outer Join – Example
loan loan_number branch_name amount
L-170 Downtown 3000
L-230 Redwood 4000
L-260 Perryridge 1700

Relation borrower customer_name loan_number


Jones L-170
Smith L-230
Hayes L-155

Right Outer Join


loan borrower

loan_number branch_name amount customer_name


L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-155 null null Hayes

Dr. Pradyumna Kumar Tripathy 53


Outer Join – Example
loan loan_number branch_name amount
L-170 Downtown 3000
L-230 Redwood 4000
L-260 Perryridge 1700

Relation borrower customer_name loan_number


Jones L-170
Smith L-230
Full Outer Join
Hayes L-155
loan borrower
loan_number branch_name amount customer_name
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-260 Perryridge 1700 null
L-155 null null Hayes

Dr. Pradyumna Kumar Tripathy 54


UNION, INTERSECTION, SET DIFFERENCE

• All of these operations take two input relations,


which must be union-compatible:
– Same number of fields.
– Corresponding’ fields have the same type.
• Two relation R1(A1, A2, ..., An) and R2(B1, B2, ..., Bn) are said to be
union or type compatible, if they satisfy following two conditions:
– Both the relations , i.e. R1 and R2 should be of same arity, i.e.
they should have same number of attributes.
– Domain of the similar attributes, i.e. domain of ith attribute of
R1 and domain of ith attribute of R2, should be same. (i.e.
dom(Ai)=dom(Bi) for i=1, 2, ..., n).
• The resulting relation for R1R2 (also for R1R2, or R1–
R2) has the same attribute names as the first operand
relation R1 (by convention)
Dr. Pradyumna Kumar Tripathy 55
• If R and S are two union compatible relation then the resultant relation P = R 𝖴 S is
a relation includes all tuples that are either in R or in S or in both R and S.
Duplicate tuples are eliminated.
• It is defined as R ꓴ S = { t | t ϵ R or t ϵ S}

R RꓴS
S
A B
A B A B
α 1
α 2 α 1
α 2
β 3 α 2
β 2
β 2
β 3

Dr. Pradyumna Kumar Tripathy 56


Account Loan o/p
Account_no Name Name
Jones
101 Jones Loan_no Name
Smith
103 Smith 103 Smith Leon
104 Leon 104 Leon Evan

107 Evan 106 Byran Drew

Smita
110 Drew 110 Drew
Byran
112 Smita

Write the relational algebra expression to find name of those customer who
have account or loan or both.
ΠName (Account) ꓴ ΠName (Loan)

Dr. Pradyumna Kumar Tripathy 57


Dr. Pradyumna Kumar Tripathy 58
• The result of this operation, denoted by R ∩ S,
is a relation that includes all tuples that are in
both R and S.
• It is defined as R Ո S = { t | t ϵ R and t ϵ S}

Dr. Pradyumna Kumar Tripathy 59


R R ՈS
S
A B
A B A B
α 1
α 2 α 2
α 2
β 3
β 2

Dr. Pradyumna Kumar Tripathy 60


Account Loan o/p
Account_no Name Name
Smith
101 Jones Loan_no Name
Leon
103 Smith 103 Smith Drew
104 Leon 104 Leon
107 Evan 106 Byran
110 Drew 110 Drew
112 Smita

Write the relational algebra expression to find name of those customer who
have both an account and loan.
ΠName (Account) ՈΠName (Loan)

Dr. Pradyumna Kumar Tripathy 61


• The result of this operation, denoted by R – S,
is a relation that includes all tuples that are in
R but not in S.
• It is defined as R - S = { t | t ϵ R and t ϵ S}

Dr. Pradyumna Kumar Tripathy 62


R R-S
S
A B
A B A B
α 1
α 2 α 1
α 2
β 3 β 2
β 2

Dr. Pradyumna Kumar Tripathy 63


Account Loan o/p
Account_no Name Name
Jones
101 Jones Loan_no Name
Evan
103 Smith 103 Smith Smita
104 Leon 104 Leon
107 Evan 106 Byran
110 Drew 110 Drew
112 Smita

Write the relational algebra expression to find name of those customer who
have but not taken a loan.
ΠName (Account) − ΠName (Loan)

Dr. Pradyumna Kumar Tripathy 64


Properties of UNION, INTERSECTION and SET DIFFERENCE

• Notice that both union and intersection are commutative


operations; that is
– R  S = S  R, and R  S = S  R
• Both union and intersection can be treated as n-ary
operations applicable to any number of relations as both are
associative operations; that is
– R  (S  T) = (R  S)  T
– (R  S)  T = R  (S  T)
• The minus operation is not commutative; that is, in general
– R–S≠S–R

Dr. Pradyumna Kumar Tripathy 65


ASSIGNMENTS

Query1-: Find the bid of red coloured boats

Πbid (σcolor = ‘Red’ (Boats))

Query 2: Find the name of sailors who have reserved boat number 2
Π sname (σbid = 2(Sailors * Reserve))

Query-3 Find the names of sailors who have reserved a red boat.
Πsname (σcolor = ‘Red’ (Boats *Reserve * Sailors) )
Query-4 Find the colors of the boats reserved by sailor ‘xyz’
Πcolor (σsname = ‘xyz’ (Sailors * Reserve * Boats) )

Dr. Pradyumna Kumar Tripathy 66


RENAME OPERATION (ρ)

• The RENAME operator is denoted by  (rho)


• The rename operation allows to name the
results of a relational algebra expression and
also inxdividual column.
• Notation:  x (E)
– Returns the expression E under the name xs emp
eno ename sal dno
A1 A2 A3 A4
1 Smith 5000 10
1 Smith 5000 10
2 Jones 6000 20
2 Jones 6000 20
3 Blake 7000 10
3 Blake 7000 10
4 Peter 8000 30
4 Peter 8000 30

 x(a1,a2,a3,a4) emp 68
Dr. Pradyumna Kumar Tripathy
RENAME OPERATION (ρ)

• In some cases, we may want to rename the attributes of a


relation or the relation name or both
– Useful when a query requires multiple operations
• Rename operation can be expressed by any of the
following forms:
– S (B1, B2, …, Bn )(R) changes both:
• the relation name to S, and
• the column (attribute) names to B1, B1, …..Bn
– S(R) changes:
• the relation name only to S
– (B1, B2, …, Bn )(R) changes:
• the column (attribute) names only to B1, B1, …..Bn

Dr. Pradyumna Kumar Tripathy 69


Relational Algebra Expressions

• We may want to apply several relational


algebra operations one after the other
– Either we can write the operations as a single
relational algebra expression by nesting the
operations, or
– We can apply one operation at a time and create
intermediate result relations.
• In the latter case, we must give names to the
relations that hold the intermediate results using
assignment operator ←(left arrow).

Dr. Pradyumna Kumar Tripathy 70


Storing Intermediate Results in Relations

OR

FNAME, LNAME, SALARY( DNO=5(EMPLOYEE))


(Single Relational Expression)

Dr. Pradyumna Kumar Tripathy 71


EMPLOYEE
Fname Mname Lname SSN Bdate Address Sex Salary SuperSSN Dno

Write relational algebra expression to display name of all employees and their
supervisors.
y ← ρd (emp)
z ← π emp.Fname,emp.Mname,emp.Lname,y.Fname,y.Mname,y.Lname (emp emp.ssn = y.SuperSSn y )

Dr. Pradyumna Kumar Tripathy 72


Generalized Projection
• Extends the projection operation by allowing arithmetic functions to
be used in the projection list.

 F , F ,...,
1 2 Fn
(E )
– where, E is any relational-algebra expression
– each of F1, F2, …, Fn are arithmetic expressions involving
constants and attributes in the schema of E.

Example :
Given relation Employee (emp_name, SSN, desgn, basic_sal, TA)
Find the total salary of each employee:
 emp_name, (basic_sal + TA) (Employee)

Dr. Pradyumna Kumar Tripathy 73


emp
eno sal ename
1 1000 xyz
2 5000 pqr
3 7000 abc

Write relational algebra expression to retrieve name and annual salary of employees.
ρ(ename,Annual Salary) (Π (ename, 12 * sal) (emp))

ename Annual Salary


xyz 12000
pqr 60000
abc 84000

Dr. Pradyumna Kumar Tripathy 74


Aggregate Functions
• Aggregation function takes a collection of values and returns a single
value as a result.
avg : average value
min : minimum value
max : maximum value
sum : sum of values
count : number of values

Syntax :
G1 ,G2 ,…,G 
E is any relational-algebra expression
– G1, G2 …, Gn is a list of attributes known as grouping attributes (can be empty)
– Each Fi is an aggregate function
– Each Ai is an attribute name
• The operator is pronounced as script F.
• The resulting relation has the grouping attributes plus one attribute for
each element in the function list.
• However, NULL values are not considered in the aggregate function
Dr. Pradyumna Kumar Tripathy 75
– g max( Salary) (EMPLOYEE) retrieves the maximum salary value
from the EMPLOYEE relation
– g min( Salary)(EMPLOYEE) retrieves the minimum Salary value
from the EMPLOYEE relation
– g sum( Salary)(EMPLOYEE) retrieves the sum of the Salary from
the EMPLOYEE relation
– g count( SSN),avg(Salary) (EMPLOYEE) computes the count (number)
of employees and their average salary

Dr. Pradyumna Kumar Tripathy 76


Aggregate Operation – Example
• Relation r :
A B C

  7
  7
  3
  10

g sum(c) (r) sum(c )

27

If no grouping attributes are specified, the functions are applied to all the tuples
in the relation, so the resulting relation has a single tuple only.
Dr. Pradyumna Kumar Tripathy 77
• Write the relational algebra expression to find
the maximum salary of employees.
emp
eno sal ename deptno
1 1000 xyz d1
2 5000 pqr d2
3 7000 abc d1

g max(sal) (emp) max(sal)


7000

Dr. Pradyumna Kumar Tripathy 78


• Write the relational algebra expression to
display the maximum salary of each
department.
emp
eno sal ename deptno
1 1000 xyz d1
2 5000 pqr d2
3 7000 abc d1

deptno max(sal)
deptno g max(sal) (emp) d1 7000
d2 5000

select max(sal) from emp group by deptno


Dr. Pradyumna Kumar Tripathy 79
Write RA expression to display deptno, avg salary and no of employees of each
department
SSN eno salary Dno
ρR(Dno, NO-of-Employees, Average-salary) (Dno g count(SSN),avg(salary) (employee)) E1 SMITH 30000 5
E2 Wang 40000 5

E3 John 25000 4
E4 Jenifer 43000 4
E5 Rames 38000 5
h
E6 Joyce 25000 5
E7 Ahmad 25000 4
E8 James 55000 1

g count(SSN),avg(salary) (employee)

Dr. Pradyumna Kumar Tripathy 80


Aggregate Operation – Example
• Relation account grouped by branch-name:

branch_name account_number balance


Perryridge A-102 400
Perryridge A-201 900
Brighton A-217 750
Brighton A-215 750
Redwood A-222 700

branch_name g sum(balance) (account)


branch_name sum(balance)
Perryridge 1300
Brighton 1500
Redwood 700

Dr. Pradyumna Kumar Tripathy 81


EMPLOYEE(SSN, Fname,Lname, salary,super_ssn,Dno)
DEPEDENT(ESSN, Dependent_name, Relationship)

• Display the names of all employees with two or


more dependents.
– T1(Ssn, No_of_dependents) ← Essng COUNT (Dependent_name)
(DEPENDENT)
– T2 ← σNo_of_dependents>2(T1)
– RESULT ← πLname, Fname(T2 * EMPLOYEE)

Dr. Pradyumna Kumar Tripathy 82


Examples : Aggregate Functions

Schema :

Query 1 :

Solution :

Dr. Pradyumna Kumar Tripathy 83


Examples : Aggregate Functions

Schema :

Query 3 :

Solution :

Dr. Pradyumna Kumar Tripathy 84


Modification of Database

• The content of database may be modified using following operations:

– Deletion

– Insertion

– Updating

• All these operations are expressed using the assignment operator.

Dr. Pradyumna Kumar Tripathy 85


where r is a relation and E is a relational algebra expression.

Dr. Pradyumna Kumar Tripathy 86


account
Deletion Examples
(account_number, branch_name, balance)
loan (loan_number, branch_name, amount)

• Delete all account records of Perryridge branch.

account  account –  branch_name = “Perryridge” (account )

Delete all loan records with amount in the range of 0 to 50

loan  loan –  amount  0 and amount  50 (loan)

Delete emp smith record from emp(eno, ename, sal, dno) relation
emp ← emp – σ ename = “smith” (emp)

Delete all employees record whose salary >= 5000nfrom emp(eno, ename, sal, dno)
relation
emp ← emp – σ sal ≥ 5000s (emp)

Dr. Pradyumna Kumar Tripathy 87


Insert
Syntax: r←rꓴE

where r is a relation and E is a relational algebra expression.

It is used to insert a new record into a relation.

Write the relational algebra expression to insert a emp tuple(305,”rama”,10000,10)


into emp(eno, ename, sal,dno).
emp ← emp ꓴ {305,”rama”,10000,10 }

Dr. Pradyumna Kumar Tripathy 88


Insertion Examples
• Insert information in the database specifying that Smith has $1200 in account
A-973 at the Perryridge branch. Consider the following relational schema.

account (account_number, branch_name, balance)


depositor (customer_name, account_number)

account  account  {(“A-973”, “Perryridge”, 1200)}


depositor  depositor  {(“Smith”, “A-973”)}

Dr. Pradyumna Kumar Tripathy 89


UPDATE
A mechanism to change a value in a tuple without charging all values in the tuple

Use the generalized projection operator to do this task


r ← Π F1,, F2,….., Fn (r)
Where each Fi is either the ith attribute or updated value of ith attribute of r.

Write a relational algebra expression to increase the salary of each


employee(eno,ename,sal,dno) by 30% .
employee ← Π eno, ename, sal + 0.3 * sal,dno (employee)

Dr. Pradyumna Kumar Tripathy 90


Update Examples
account (account_number, branch_name, balance)

• Make interest payments by increasing all balances by 5 percent.


account   account_number, branch_name, balance * 1.05 (account)

Pay all accounts with balances over $10,000 6 percent interest


and pay all others 5 percent

account   account_number, branch_name, balance * 1.06 ( BAL  10000 (account ))


  account_number, branch_name, balance * 1.05 (BAL  10000 (account))

Dr. Pradyumna Kumar Tripathy 91


Division Operation

• Division operator is denoted by


• Notation: r s
• Suited for the queries that include the phrase for all
• It is a binary operator.
• R(Z)  S(X), where X subset Z. Let Y = Z - X (and hence Z = X  Y);
that is, let Y be the set of attributes of R that are not attributes of S.
• The result of DIVISION is a relation T(Y) that includes a tuple t if
tuples tR appear in R with tR [Y] = t, and with
• tR [X] = ts for every tuple ts in S.
• For a tuple t to appear in the result T of the DIVISION, the values in
t must appear in R in combination with every tuple in S.

Dr. Pradyumna Kumar Tripathy 92


Dr. Pradyumna Kumar Tripathy 93
Dr. Pradyumna Kumar Tripathy 94
sno pno
s1 p1 pno pno pno
s1 p2 p2 p2 p1
s1 p3 p4 p2
s1 p4 B1
p4
s2 p1 B2
s2 p2 sno B3
s3 p2 s1
s4 p2 s2 sno
s4 p4 s3 s1 sno
s4 s4 s1

A A/B1 A/B2 A/B3


Dr. Pradyumna Kumar Tripathy 95
Solving the query using
Assignment operator ()

account (account_number, branch_name, balance)


depositor (customer_name, account_number)
branch (branch_name, branch_city, assets)

Query : Find all customers who have an account at all branches


located in Cuttack.

Solution : A  Account)
customer_name, branch_name (depositor *

B   branch_name ( branch_city = ”Cuttack” Branch)

Result  A  B

Dr. Pradyumna Kumar Tripathy 96


Consider the following relational schema.
EMPLOYEE(SSN, Fname,Lname, salary,super_ssn,Dno)
WORKS_ON(ESSN,Pno,Hours)

Retrieve the names of employees who work on all the projects that ‘John
Smith’ works on.
SMITH ← σ Fname=‘John’ AND Lname=‘Smith’ (EMPLOYEE)
SMITH_PNOS ← πPno(WORKS_ON Essn=Ssn SMITH)
SSN_PNOS ← πEssn, Pno(WORKS_ON)
y ← SSN_PNOS ÷ SMITH_PNOS
RESULT ← π Fname, Lname (y * EMPLOYEE)

Dr. Pradyumna Kumar Tripathy 97


• Consider the following relational schema.
EMPLOYEE(SSN, Fname,Lname, salary,super_ssn,Dno)
WORKS_ON(ESSN,Pno,Hours)
PROJECT(Pname,Pnumber,Plocation,Dnum)

• Find the names of employees who work on all


the projects controlled by department number
5.
DEPT5_PROJS ← ρ(Pno)(πPnumber(σDnum=5(PROJECT)))
EMP_PROJ ← ρ(ssn, Pno)(πEssn, Pno(WORKS_ON))
RESULT_EMP_SSNS ← EMP_PROJ ÷ DEPT5_PROJS
RESULT ← πLname, Fname(RESULT_EMP_SSNS * EMPLOYEE)

Dr. Pradyumna Kumar Tripathy 98


• Consider the following relational schema.

EMPLOYEE(SSN, Fname,Lname, salary,super_ssn,Dno)
DEPEDENT(ESSN, Dependent_name, Relationship)

Retrieve the names of employees who have no


dependents.
– ALL_EMPS   SSN(EMPLOYEE)
– EMPS_WITH_DEPS(SSN)   ESSN(DEPENDENT)
– EMPS_WITHOUT_DEPS  (ALL_EMPS - EMPS_WITH_DEPS)
– RESULT   LNAME, FNAME (EMPS_WITHOUT_DEPS * EMPLOYEE)

Dr. Pradyumna Kumar Tripathy 99


Example

• Consider the following relation schema:-

• employee (person-name, street, city)


• works (person-name, company-name, salary)
• company (company-name, city)
• manages (person-name, manager-name)

Dr. Pradyumna Kumar Tripathy 100


Example
• Find the names of all employees who work for First Bank Corporation

• Π person-name (σ company-name = “First Bank Corporation” (works))

• Find the names and cities of residence of all employees who work for
First Bank Corporation.

• Πperson-name, city (employee *( σcompany-name = “First Bank Corporation” works))


• ≡Πperson-name, city (employee x( σcompany-name = “First Bank Corporation” works))
• ≡ Πperson-name, city (σcompany-name = “First Bank Corporation” AND employee.person-name=works.person-name employee x
works)
• ≡ Πperson-name, city (σcompany-name = “First Bank Corporation” employee employee.person-name=works.person-name

works)

Dr. Pradyumna Kumar Tripathy 101


Example

• Find the names, street address, and cities of residence of all


employees who work for First Bank Corporation and earn more
than $10,000 .
• Πperson-name, street, city(σ(company-name = “First Bank Corporation” 𝖠 salary > 10000)
(works) * employee)

• Find the names of all employees in this database who live in the
same city as the company for which they work.
• Πperson-name (employee * works * company)

Dr. Pradyumna Kumar Tripathy 102


Example

• Give all employees of First Bank Corporation a 10 percent salary


raise.
• works ← Πperson-name,company-name,1.1∗salary(σ(company-name=“First Bank
Corporation”)(works)) 𝖴 (works − σcompany-name=“First Bank Corporation”(works))

Dr. Pradyumna Kumar Tripathy 103


Example

• Consider the following relational schema

Dr. Pradyumna Kumar Tripathy 104


Example

• Query 1. Retrieve the name and address of all employees


who work for the ‘Research’ department.
– RESEARCH_DEPT ← σDname=‘Research’(DEPARTMENT)
– RESEARCH_EMPS ← (RESEARCH_DEPT Dnumber=Dno EMPLOYEE)
– RESULT ← πFname, Lname, Address(RESEARCH_EMPS)

• Query-2 For every project located in ‘Stafford’, list the


project number, the controlling department number, and
the department manager’s last name, address, and birth
date.
– STAFFORD_PROJS ← σPlocation=‘Stafford’(PROJECT)
– CONTR_DEPTS ← (STAFFORD_PROJS Dnum=Dnumber DEPARTMENT)
– PROJ_DEPT_MGRS ← (CONTR_DEPTS Mgr_ssn=Ssn EMPLOYEE)
– RESULT ← πPnumber, Dnum, Lname, Address, Bdate(PROJ_DEPT_MGRS)

Dr. Pradyumna Kumar Tripathy 105


Example

• Q4

Dr. Pradyumna Kumar Tripathy 106


Example

• Query 5. Make a list of project numbers for


projects that involve an employee whose last
name is ‘Smith’, either as a worker or as a
manager of the department that controls the
project.
– SMITHS(Essn) ← πSsn (σLname=‘Smith’(EMPLOYEE))
– SMITH_WORKER_PROJS ← πPno(WORKS_ON * SMITHS)
– MGRS ← πLname, Dnumber(EMPLOYEE Ssn=Mgr_ssn DEPARTMENT)
– SMITH_MANAGED_DEPTS(Dnum) ← πDnumber (σLname=‘Smith’(MGRS))
– SMITH_MGR_PROJS(Pno) ← πPnumber(SMITH_MANAGED_DEPTS *
PROJECT)
– RESULT ← (SMITH_WORKER_PROJS 𝖴 SMITH_MGR_PROJS)

Dr. Pradyumna Kumar Tripathy 107


Relational Calculus

Dr. Pradyumna Kumar Tripathy 108


Relational Calculus

• Non-procedural query language for relational model


• It is up two types
– TRC (Tuple Relational calculus)
– DRC(Domain Relational Calculus)
• In a calculus expression, there is no order of operations to specify
how to retrieve the query result—a calculus expression specifies
only what information the result should contain.
– This is the main distinguishing feature between relational algebra and
relational calculus.
• A relational calculus expression creates a new relation.
• TRC: Variables range over tuples.
– Example: SQL
• DRC: Variables range over domain
– Example: Query-By-Example (QBE)

Dr. Pradyumna Kumar Tripathy 2


109
Relational Calculus

• This differs from relational algebra, where we


must write a sequence of operations to
specify a retrieval request in a particular
order of applying operations; hence relational
algebra can be considered as a procedural
way of stating a query.
• The order of relational algebra operations
used for query execution plan.

Dr. Pradyumna Kumar Tripathy 110


Tuple Relational Calculus(TRC)

• The tuple relational calculus is based on specifying a number of tuple


variables.
• A tuple variable is a variable whose values can be tuples of a relational
schema.
• Each tuple variable usually ranges over a particular database relation,
meaning that the variable may take as its value any individual tuple from
that relation.
• A simple tuple relational calculus query is of the form
{t | COND(t)}
– where t is a tuple variable and COND (t) is a conditional expression
involving t which evaluates to either true or false if a specific tuple is
substituted for t
– The result of such a query is the set of all tuples t that satisfy COND
(t).
– |(BAR) is read as such that

Dr. Pradyumna Kumar Tripathy 111


Tuple Relational Calculus(TRC)

• Consider the following sailor relation instance


• Find all sailors with a rating above 7.
– {S | S ϵ Sailors AND S.rating > 7}
• When this query is evaluated on an instance of the Sailors relation, the
tuple variable S is instantiated successively with each tuple, and the test
S.rating>7 is applied. The answer contains those instances of S that pass
this test.
sid sname Rating age
31 Lubber 8 55.5
32 Andy 8 25.5
58 Rusty 10 35.0
71 Zorba 10 16.0
74 Horatio 9 35.0
Sailors
Dr. Pradyumna Kumar Tripathy 112
Tuple Relational Calculus(TRC)

• Find sailor name and sailor id of all sailors


with a rating above 7
– {S.sid, S.sname | S ϵ Sailors AND S.rating > 7}
– S.sid references the attribute sid of the tuple variable S
– S,sname references the attribute sname of the tuple variable S
sid sname
31 Lubber
32 Andy
58 Rusty
71 Zorba
74 Horatio
Sailors Result
Dr. Pradyumna Kumar Tripathy 113
Tuple Relational Calculus(TRC)

• In a tuple relational expression following information


are specified
– For each tuple variable t, the range relation R of t. This
value is specified by a condition of the form t ϵ R .
– A condition to select particular combinations of tuples.
The condition is evaluated for every possible combination
of tuples to identify the selected combinations for which
the condition evaluates to TRUE.
– A set of attributes to be retrieved, the requested
attributes. The values of these attributes are retrieved for
each selected combination of tuples.

Dr. Pradyumna Kumar Tripathy 114


7
Tuple Relational Calculus(TRC)

• Consider the following relational schema

• Write the TRC Query to find all employees whose


salary is above $50,000
– {t | t ϵ EMPLOYEE AND t.Salary>50000}
• Write the TRC Query to Retrieve the birth date
and address of the employee (or employees)
whose name is John B. Smith.
– {t.Bdate, t.Address | t ϵ EMPLOYEE AND
t.Fname=‘John’ AND t.Minit=‘B’ AND t.Lname=‘Smith’}

Dr. Pradyumna Kumar Tripathy 115


TRC Expression and Formulas

• A general expression of the tuple relational


calculus is of the form {t1.Aj, t2.Ak, ... , tn.Am |
COND(t1, t2, ..., tn, tn+1, tn+2, ..., tn+m)}
– where t1, t2, ..., tn, tn+1, tn+2, ..., tn+m are tuple variables,
each Ai is an attribute of the relation on which ti
ranges, and COND is a condition or formula of the
tuple relational calculus.
• Relational calculus Formula consists of:
– Simple atomic formulas
– connected using logical connectives like
• AND, OR, NOT
• ˄, ˅ , ¬

Dr. Pradyumna Kumar Tripathy 116


TRC Expression and Formulas

• A atomic formula is one of the following


– ti ϵ R where R is a relation name and ti is a tuple variable.
identifies the range of the tuple variable ti as the relation
whose name is R.
– ti.A op tj.B, where op is one of the comparison operators
in the set {=, <, ≤, >, ≥, ≠}, ti and tj are tuple variables, A is
an attribute of the relation on which ti ranges, and B is an
attribute of the relation on which tj ranges.
– ti.A op C where op is one of the comparison operators in
the set {=, <, ≤, >, ≥, ≠}, ti is a tuple variable and C is a
constant
• Each of the atom evaluates to True or false for a
specific tuple.

Dr. Pradyumna Kumar Tripathy 117


TRC Expression and Formulas

• A formula (Boolean condition) is made up of one


or more atoms connected via the logical
operators AND, OR, and NOT and is defined
recursively by Rules 1 and 2 as follows:
– Rule 1: Every atom is a formula.(Atomic Formula)
– Rule 2: If F1 and F2 are formulas, then so are (F1 AND
F2), (F1 OR F2), NOT (F1), and NOT (F2).
• Example : ti.A op c AND tj.B where op is one of the
comparison operators , ti and tj are tuple variables, A is an
attribute of the relation on which ti ranges, B is an attribute
of the relation on which tj ranges, and c is a constant value.

Dr. Pradyumna Kumar Tripathy 118


TRC Expression and Formulas

Dr. Pradyumna Kumar Tripathy 118


Free And Bound Variable

• Two special symbols called quantifiers can


appear in formulas;
– universal quantifier ()
– existential quantifier ()
• A quantifiers t or t in the formula are said
to bind t in the formula and t is called as
bound variable
• Informally, a tuple variable t is bound if it
appears in an ( t) or ( t) clause
• The variable X that appears to the left of ‘|‘
are called as free variable in the formula.

Dr. Pradyumna Kumar Tripathy 119


Existential and Universal Quantifier
• If F is a formula, then so are ( t)(F) and ( t)(F), where t
is a tuple variable.
– The formula ( t)(F) is true if the formula F evaluates
to true for some (at least one) tuple assigned to t ;
otherwise ( t)(F) is false.
– The formula ( t)(F) is true if the formula F evaluates
to true for every tuple (in the universe) assigned to t;
otherwise ( t)(F) is false.

•  is called the universal or “for all” quantifier because


every tuple in “the universe of” tuples must make
quantified formula F true.
•  is called the existential or “there exists” quantifier
because any tuple that exists in “the universe of” tuples
may make F true to make the quantified formula true.

Dr. Pradyumna Kumar Tripathy 120


Example

•Consider the following relational schema


Sailors(sid, sname, rating, age)
Boat( bid, bname, color)
Reserves(sid, bid, day)

Dr. Pradyumna Kumar Tripathy 121


Selection and Projection
sid sname rating age
S1 28 yuppy 9 35.0
• Selection
S1 31 lubber 8 55.5
Find all sailors with rating above 8
S1 44 guppy 5 35.0
{S | S Sailors  S.rating > 8} S1 58 rusty 10 35.0
• Projection
Find names and ages of sailors with rating above 8.
sname age
{S | S1 Sailors(S1.rating > 8 S yuppy 35.0
 S.sname = S1.sname S rusty 35.0
 S.age = S1.age)}

S is a tuple variable of 2 fields (i.e. {S} is a projection of Sailors)


OR
{S.sname, s.age | S Sailors A N D S.rating > 8 }

Dr. Pradyumna Kumar Tripathy 122


Join
Find names of the sailors with a rating sid sname rating age
above 7 who’ve reserved boat #103 S 22 dustin 7 45.0
S 31 lubber 8 55.5
S 58 rusty 10 35.0
{S.name | SSailors A N D S.rating > 7 A N D
R(RReserves A N D R.sid = S.sid sid bid day
A N D R.bid = 103)} R 22 101 10/10/96
R 58 103 11/12/96

Note the use of  to find a tuple in Reserves that


`joins with’ the Sailors tuple under consideration.
{S1.sname | S( SSailors A N D S.rating > 7 A N D
R(RReserves A N D R.sid = S.sid
A N D R.bid = 103 A N D S1.sname = S.sname))}
Dr. Pradyumna Kumar Tripathy 123
Join(Continued)

Find sailors rated > 7 who’ve reserved a red boat

{S | SSailors A N D S.rating > 7 A N D


R(RReserves A N D R.sid = S.sid
A N D B(BBoats A N D
B.bid = R.bid A N D
B.color = ‘red’))}
Notice how the parentheses control the scope of each quantifier’s binding.

Dr. Pradyumna Kumar Tripathy 124


Join(Continued)

• Find the sailor name, boat id, and reservation


date for each reservation.
• {P |  R( R ϵ Reserves AND  S( S ϵ Sailors
AND R.sid = S.sid AND P.bid = R.bid AND P.day
= R.day AND P.sname = S.sname))}

Dr. Pradyumna Kumar Tripathy 125


DIVISION
•Recall the algebra expression A ÷ B…
A value x in A is disqualified if by attaching a y value from B, we obtain an xy tuple
that is not in A. (e.g: only give me A tuples that have a match in B.)

In calculus, use the  operator:

e.g. Find sailors who’ve reserved all boats:

{S | SSailors 
BBoats (RReserves
Find all sailors S such that… (S.sid = R.sid
For all tuples B in Boats…  B.bid = R.bid))}
There is at least one tuple in Reserves…
showing that sailor S has reserved B.
Dr. Pradyumna Kumar Tripathy 126
Example

• Consider the following relational schema

Dr. Pradyumna Kumar Tripathy 127


Example

• Write the trc query to retrieve the name and address of all employees
who work for the ‘Research’ department.
– {t.FNAME, t.LNAME, t.ADDRESS | t ϵ EMPLOYEE AND ( d) ( d ϵ
DEPARTMENT AND d.DNAME=‘Research’ AND d.DNUMBER=t.DNO) }
– In above query, t is the only free variable;
– If a tuple satisfies the conditions specified in the query, the attributes
FNAME, LNAME, and ADDRESS are retrieved for each such tuple.
– The conditions t ϵ EMPLOYEE and d ϵ DEPARTMENT specify the range
relations for t and d.
– The condition d.DNAME = ‘Research’ is a selection condition and
corresponds to a SELECT operation in the relational algebra, whereas
the condition d.DNUMBER = t.DNO is a JOIN condition.

Dr. Pradyumna Kumar Tripathy 128


Example

• Write the trc query to retrieve every project located in ‘Stafford’, list the
project number, the controlling department number, and the
department manager’s last name, birth date, and address.
• {p.Pnumber, p.Dnum, m.Lname, m.Bdate, m.Address | p ϵ PROJECT AND m
ϵ EMPLOYEE AND p.Plocation=‘Stafford’ AND ((∃d)(d ϵ DEPARTMENT AND
p.Dnum=d.Dnumber AND d.Mgr_ssn=m.Ssn))}

Dr. Pradyumna Kumar Tripathy 129


Example

• Retrieve the employee’s first and last name and the first and last name
of his or her supervisor
– {e.Fname, e.Lname, s.Fname, s.Lname | e ϵ EMPLOYEE AND s ϵ
EMPLOYEE AND e.Super_ssn=s.Ssn}

Dr. Pradyumna Kumar Tripathy 130


Example
• Find the names of employees who work for all the projects controlled by
department number 5.
– {e.LNAME, e.FNAME | e ϵ EMPLOYEE AND ( x)(not(x ϵ PROJECT) OR
not(x.DNUM=5) OR ( w)(w ϵ WORKS_ON and w.ESSN=e.SSN and
x.PNUMBER=w.PNO))}
• Exclude from the universal quantification all tuples that we are not interested in by
making the condition true for all such tuples.
• The above query consists of following component
– {e.Lname, e.Fname | e ϵ EMPLOYEE(e) AND F′}
– F′ = ((∀x)(NOT(x ϵ PROJECT) OR F1))
– F1 = NOT(x.Dnum=5) OR F2
– F2 = ((∃w)(WORKS_ON(w) AND w.Essn=e.Ssn AND x.Pnumber=w.Pno))
• not(PROJECT(x)) inside the universally quantified formula evaluates to true all
tuples x that are not in the PROJECT relation.
– The expression not(x.DNUM=5) evaluates to true all tuples x that are in the
project relation but are not controlled by department 5.
– Finally, we specify a condition that must hold on all the remaining tuples in R.
( ( w)(WORKS_ON(w) and w.ESSN=e.SSN and x.PNUMBER=w.PNO))

Dr. Pradyumna Kumar Tripathy 131


Example

• List the names of employees who have no


dependents.
– {e.Fname, e.Lname | e ϵ EMPLOYEE AND NOT
(∃d)(d ϵ DEPENDENT AND e.ssn=d.Essn)}
• List the names of managers who have at least
one dependent.
– {e.Fname, e.Lname | e ϵ EMPLOYEE AND
(∃d)(∃ρ)(d ϵ DEPARTMENT AND p ϵ DEPENDENT
AND e.Ssn=d.Mgr_ssn AND ρ.Essn=e.Ssn)}

Dr. Pradyumna Kumar Tripathy 132


Languages Based on TRC

• The language SQL is based on tuple calculus. It


uses the basic block structure to express the
queries in tuple calculus:
– SELECT <list of attributes>
– FROM <list of relations>
– WHERE <conditions>
• SELECT clause mentions the attributes being
projected, the FROM clause mentions the
relations needed in the query, and the WHERE
clause mentions the selection as well as the join
conditions.

Dr. Pradyumna Kumar Tripathy 133


Safe Expression

• A safe expression in relational calculus is one that is


guaranteed to yield a finite number of tuples as its result.
• Otherwise, the expression is called unsafe expression.
Example: {t | NOT (EMPLOYEE(t))}
include tuples (and hence values) from outside the
EMPLOYEE relation
• such values are not in the domain of the expression.
• It yields all tuples in the universe that are not EMPLOYEE
tuples, which are infinite number.
• An expression is said to be safe if all values in its result are
from the domain of the expression.

Dr. Pradyumna Kumar Tripathy 134


Domain Relational Calculus

Dr. Pradyumna Kumar Tripathy 135


Domain Relational Calculus(DRC)

• Another variation of relational calculus called the domain


relational calculus, or simply, domain calculus is equivalent to
tuple calculus and to relational algebra.
• The language called QBE (Query-By-Example) that is related to
domain calculus was developed almost concurrently to SQL at
• IBM Research, Yorktown Heights, New York.
– Domain calculus was thought of as a way to explain what QBE does.
• Domain calculus differs from tuple calculus in the type of variables
used in formulas:
– Rather than having variables range over tuples, the variables range
over single values from domains of attributes.
• To form a relation of degree n for a query result, we must have n
of these domain variables— one for each attribute.

Dr. Pradyumna Kumar Tripathy 136


Domain Relational Calculus(DRC)

• An expression of the domain calculus is of


the form
– { x1, x2, . . ., xn | COND(x1, x2, . . ., xn , xn+1, xn+2, . .
., xn+m )}
– where x1, x2, . . ., xn , xn+1, xn+2, . . ., xn+m are
domain variables that range over domains (of
attributes) and COND is a condition or formula of
the domain relational calculus.

Dr. Pradyumna Kumar Tripathy 137


DRC Formulas

• A drc formula is made up of atoms


• Atomic formula can be :
– ( x1, x2, . . ., xn ) ∈ R, where R is a relation with n
attribute and each xi is a domain variable
– xi op xj, where op is one of the comparison operator
{=, <, ≤, >, ≥, ≠} and xi and xj are domain variable
– xi op c or c op xj where op is one of the comparison
operator {=, <, ≤, >, ≥, ≠} and xi and xj are domain
variable and c is a constant
• Atoms evaluate to either TRUE or FALSE for a
specific set of values

Dr. Pradyumna Kumar Tripathy 138


Free and Bound Variable

• The use of quantifiers ∃ X and ∀ X in a formula


is said to bind X.
– A variable that is not bound is free.
• There is an important restriction: the variables
x1, x2, . . ., xn that appear to the left of ‘|’ must
be the only free variables in the formula p(...).

Dr. Pradyumna Kumar Tripathy 139


Examples

• Consider the following relational schema


Sailors(sid, sname, rating, age)
Boat( bid, bname, color)
Reserves(sid, bid, day)

Dr. Pradyumna Kumar Tripathy 140


Examples

• Find all sailors with a rating above 7.


– {I,N, T, A | (I, N, T, A) ϵ Sailors AND T > 7}
– The condition (I, N, T, A) ϵ Sailors binds the
domain variable I, N, T and A to the fields of any
sailors tuple
– Every tuple that satisfies T> 7 is in the answer
• Find name of all sailors with a rating above 7.
– {N| (I)(T)(A) ((I,N,R,A)Sailors  T > 7)}

Dr. Pradyumna Kumar Tripathy 141


Examples

• Find the sailors with rating >7 who have reserved


boat 103.
– {I,N, T, A | (I, N, T, A) ϵ Sailors AND T > 7 AND
(S)(B)(D)((S,B,D) ϵ Reservations AND I = S AND B =
103) }
• Find the name of sailors who have reserved boat
103
– {N| (I)(T)(A) ((I,N,R,A)Sailors  T > 7) AND
(S)(B)(D)((S,B,D) ϵ Reservations AND I = S AND B =
103) }
– The sname field is retained in the answer and only N is
the free variable

Dr. Pradyumna Kumar Tripathy 142


Examples

• Find the names of sailors who have reserved a


red boat
– {N| (I)(T)(A) ((I,N,R,A)Sailors ) AND
(S)(B)(D)((S,B,D) ϵ Reservations AND I = S AND
(X)(Y)(Z)(X,Y,Z) ϵ Boats AND X = B AND Z=
‘red’))) }

Dr. Pradyumna Kumar Tripathy 143


Examples

• Find sailors who have reserved all boats


– {I,N, T, A | (I, N, T, A) ϵ Sailors AND (X)(Y) (Z)
( NOT((X,Y,Z)ϵ Boats)) OR (S)(B)(D)
((S,B,D) ϵ Reservations AND I = S AND B =X )) }
• Find all sailors such that for each 3-tuple(X,Y,
Z) either it is not a tuple in Boats or there is a
tuple in Reserves showing that sailor has
reserved it.

Dr. Pradyumna Kumar Tripathy 144


Example

• Consider the following relational schema

Dr. Pradyumna Kumar Tripathy 145


Example

• List the birth date and address of the employee


whose name is ‘John B. Smith’
– {u, v | (∃q) (∃r) (∃s) (∃t) (∃w) (∃x) (∃y) (∃z)
((qrstuvwxyz) ϵ Employee AND q=‘John’ AND r=‘B’
AND s=‘Smith’)}
• Retrieve the name and address of all employees
who work for the ‘Research’ department.
– {q,r, s, v | (∃t) (∃w) (∃x) (∃y) (∃z) ((qrstuvwxyz) ϵ
Employee AND (∃l) (∃m) (∃n) (∃o) ((lmno) ϵ DEPARTMENT
AND l=‘Research’ AND m=z))}

Dr. Pradyumna Kumar Tripathy 146


Example

• List the names of employees who have


dependents.
– {q,r, s| (∃t) (∃u) (∃v)(∃w) (∃x) (∃y) (∃z) ((qrstuvwxyz)
ϵ Employee AND (∃l) (∃m (∃n)(∃o) (∃p)((lmnop) ϵ
DEPENDENT AND l = t))}
• List the names of employees who have no
dependents.
– {q,r, s| (∃t) (∃u) (∃v)(∃w) (∃x) (∃y) (∃z)
( (qrstuvwxyz) ϵ Employee AND (NOT(∃l) NOT(∃m)
NOT(∃n) NOT(∃o) NOT(∃p)((lmnop) ϵ DEPENDENT
AND l = t))}

Dr. Pradyumna Kumar Tripathy 147


Example

• List the names of managers who have at least one


dependent
– {q,r, s| (∃t) (∃u) (∃v)(∃w) (∃x) (∃y) (∃z) ((qrstuvwxyz) ϵ
EMPLOYEE AND (∃a) (∃b) (∃c) (∃d) ((abcd) ϵ DEPARTMENT
AND (∃l) (∃m (∃n)(∃o) (∃p)((lmnop) ϵ DEPENDENT AND t =
c AND l = t))}
• For every project located in ‘Stafford’, list the project
number, the controlling department number, and the
department manager’s last name, birth date, and
address.
– {i, k, s, u, v | (∃h)(∃j)( (hijk) ϵ PROJECT AND (∃q)
(∃r)(∃t)(∃w) (∃x) (∃y) (∃z) ( (qrstuvwxyz) ϵ EMPLOYEE AND
(∃l)(∃m)(∃n) (∃o) ( (lmno) ϵ DEPARTMENT AND k=m AND
n=t AND j=‘Stafford’)))}

Dr. Pradyumna Kumar Tripathy 148


Dr. Pradyumna Kumar Tripathy 149
Dr. Pradyumna Kumar Tripathy 150
Dr. Pradyumna Kumar Tripathy 151
Dr. Pradyumna Kumar Tripathy 153

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy