0% found this document useful (0 votes)
30 views58 pages

Unit-2 CH II

The document discusses relational algebra and calculus, which are formal query languages for relational databases. It describes relational algebra operators like selection, projection, renaming, set operations, and join. It also discusses theta joins and compares relational algebra to relational calculus.

Uploaded by

kaartheeka01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views58 pages

Unit-2 CH II

The document discusses relational algebra and calculus, which are formal query languages for relational databases. It describes relational algebra operators like selection, projection, renaming, set operations, and join. It also discusses theta joins and compares relational algebra to relational calculus.

Uploaded by

kaartheeka01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 58

RELATIONAL ALGEBRA

AND
CALCULUS

UNIT-II
Relational Query Languages
• Query languages: Allow manipulation and retrieval of
data from a database.
• Relational model supports simple, powerful query
languages:
– Strong formal foundation based on logic.
– Allows for much optimization.
• Query Languages != programming languages!
– QLs not expected to be “Turing complete”.
– QLs not intended to be used for complex calculations.
– QLs support easy, efficient access to large data sets.

2
Formal Relational Query Languages
Two mathematical Query Languages form the
basis for “real” languages (e.g. SQL), and for
implementation:
• Relational Algebra: More operational, very useful
for representing execution plans.
• Relational Calculus: Lets users describe what
they want, rather than how to compute it. (Non-
operational, declarative.)

 Understanding Algebra & Calculus is key to


understanding SQL, query processing!
3
Preliminaries
• A query is applied to relation instances, and the
result of a query is also a relation instance.
– Schemas of input relations for a query are fixed
– The schema for the result of a given query is also fixed!
Determined by definition of query language constructs.
• Positional vs. named-field notation:
– Positional notation easier for formal definitions, named-
field notation more readable.
– Both used in SQL

4
R1 sid bid day
Example Instances 22 101 10/10/96
• “Sailors” and “Reserves” 58 103 11/12/96
relations for our examples.
• We’ll use positional or named S1 sid sname rating age
field notation, assume that 22 dustin 7 45.0
names of fields in query results
are `inherited’ from names of 31 lubber 8 55.5
fields in query input relations.
58 rusty 10 35.0

S2 sid sname rating age


28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
B1 58 rusty 10 35.0
5
• The Relational Algebra is used to define the ways in which
relations (tables) can be operated to manipulate their
data.
• It is used as the basis of SQL for relational databases, and
illustrates the basic operations required of any DML.
• This Algebra is composed of Unary operations (involving a
single table) and Binary operations (involving multiple
tables).
• There are two formal query languages associated with
the relational model.
 Relational algebra
 Relational calculus
• Query languages are specialized languages for asking
questions, or queries, that involve the data in a database.
• Queries in Relational algebra are composed
using a collection of operators, and each
query describes a step-by-step procedure for
computing the desired answer; that is,
queries are specified in an operational
manner.
• Relational calculus, in which a query
describes the desired answer without
specifying how the answer is to be
computed; this nonprocedural style of
querying is called declarative.
RELATIONAL ALGEBRA
• Relational algebra is one of the two formal query
languages associated with the relational model.
• 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.
• Each relational query describes a step-by-step
procedure for computing the desired answer,
based on the order in which operators are
applied in the query.
• The procedural nature of the algebra allows us
to think of an algebra expression as a recipe,
or a plan, for evaluating a query, and
relational systems in fact use algebra
expressions to represent query evaluation
plans.
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)
SELECTION
Relational algebra includes operators to select
rows from a relation () and to project columns ().
• These operations allow us to manipulate data in
a single relation.
rating>8(S2)
• The selection operator species the tuples to
retain through a selection condition.
• In general, the selection condition is a Boolean
combination (i.e., an expression using the
logical connectives ˄ and ˅) of terms that
have the form
attribute op constant
or
attribute1 op attribute2
• where op is one of the comparison operators
<,<=,=, ≠, >=, or >.
PROJECTION
• The projection operator allows us to extract
columns from a relation; for example,
• we can find out all sailor names and ratings
by using .
• The expression sname,rating(S2)
• For example, we can compute the names and ratings of
highly rated sailors by combining two of the preceding
queries.
• sname,rating(σrating>8(S2))
Renaming
• Renaming operator is designated by ρ(rho).
•The expression ρ(R(F¯),E) takes an arbitrary
relational algebra expression E and returns an
instance of a (new) relation called R.
• R contains the same tuples as the result of E, and
has the same schema as E, but some fields are
renamed.
• The field names in relation R are the same as in E,
except for fields renamed in the renaming list F ¯,
which is a list of terms having the form oldname
→ newname or position → newname.
For example, the expression
(R(1 → sid1, 5 → sid2) S1xR1)
returns a relation that contains the tuples
shown which has the following schema:
R(sid1:integer, sname: string, rating: integer,
age: real, sid2: integer, bid: integer,day: dates).
• S1 x R1: Each row of S1 paired with each row of R1.
• Q: How many rows in the result?
• Result schema has one field per field of S1 and R1, with
field names `inherited’ if possible.
– May have a naming conflict: Both S1 and R1 have a field
with the same name.
– In this case, can use the renaming operator:
Set Operations
The following standard operations on sets are also available in
relational algebra:
union(Ս),
intersection (Ո),
set-difference (−),
cross-product (X).
Union: RՍS returns a relation instance containing all tuples
that occur in either relation instance R or relation instance S
(or both). R and S must be union-compatible, and the schema
of the result is defined to be identical to the schema of R.
• Two relation instances are said to be union-compatible if
the following conditions hold:
- they have the same number of the fields, and
- corresponding fields, taken in order from left to right, have
the same domains.
Intersection: RՈS returns a relation instance
containing all tuples that occur in both R and S.
The relations R and S must be union-compatible,
and the schema of the result is defined to be
identical to the schema of R.
Set-difference: R−S returns a relation instance
containing all tuples that occur in R but not in S.
The relations R and S must be union-compatible,
and the schema of the result is defined to be
identical to the schema of R.
Cross-product: RxS returns a relation instance
whose schema contains all the fields of R (in the
same order as they appear in R) followed by all the
fields of S(in the same order as they appear in S).
The result of RxS contains one tuple ‹r, s› (the
concatenation of tuples r and s) for each pair of
tuples rεR; sεS. The cross-product operation is
sometimes called Cartesian product.
• Joins
-The join operation is one of the most useful operations in
relational algebra and the most commonly used way to
combine information from two or more relations.

Following are several variants of Join operation


• Conditional join
• Equijoin
• Natural join
Condition Joins
• The most general version of the join operation accepts
a join condition c and a pair of relation instances as
arguments, and returns a relation instance.
• The join condition is identical to a selection condition
in form.
• The operation is defined as follows:
0

R c S = σc(RxS)
• Thus is defined to be a cross-product followed by a
0

selection.
• Note that the condition c can (and typically does) refer
to attributes of both R and S.
Condition/Theta (θ) Join

• Theta join combines tuples from different relations


provided they satisfy the theta condition.
• The join condition is denoted by the symbol θ.
• Notation for θ join is
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 = Φ.
• Theta join can use all kinds of comparison operators.
Subject
Student Class Subject
SID Name Std 10 Math
101 Alex 5 10 English
102 Maria 9 11 Music
11 Sports
• STUDENT ⋈Student.Std < Subject.Class SUBJECT
Student_detail
SID Name Std Class Subject
101 Alex 5 10 Math
English
101 Alex 5 10

101 Alex 5 11 Music


101 Alex 5 11 Sports
102 Maria 9 10 Math

102 Maria 9 10 English

102 Maria 9 11 Music


102 Maria 9 11 Sports
Equijoin
•When Theta join uses only equality comparison
operator, it is said to be equijoin.
•when the join condition consists solely of equalities
(connected by ^) of the form R.name1 = S.name2, that
is, equalities between two fields in R and S.
•For join conditions that contain only such equalities,
the join operation is refined by doing an additional
projection in which S.name2 is dropped.
•The join operation with this refinement is called
equijoin.
Student Subject
Class Subject
SID Name Std 10 Math
101 Alex 10 10 English
11 Music
102 Maria 11 11 Sports
STUDENT ⋈Student.Std = Subject.Class SUBJECT

Student_detail
SID Name Std Class Subject
101 Alex 10 10 Math
101 Alex 10 10 English
102 Maria 11 11 Music
102 Maria 11 11 Sports
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.
• Natural join acts on those matching attributes
where the values of attributes in both the
relations are same.
Courses
CID Course Dept
CS01 Database CS
ME01 Mechanics ME
EE01 Electronics EE

HoD
Dept Head
CS Alex
ME Meena
EE Mira
Courses ⋈ HoD
Dept CID Course Head
CS CS01 Database Alex
ME ME01 Mechanics Meena
EE EE01 Electronics Mira
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 Outer Join(R S)
• All the tuples from the Left relation, R, are
included in the resulting relation.
• If there are tuples in R without any matching
tuple in the Right relation S, then the S-
attributes of the resulting relation are made
NULL.
• It gives the matching rows and the rows which
are in left table but not in right table.
Left
A B
100 Database
101 Mechanics Left Right
102 Electronics
A B D
Right 100 Database Alex
A D 101 Mechanics ---
100 Alex 102 Electronics Meena
102 Meena
104 Mira
Right Outer Join: ( R S)
• All the tuples from the Right relation, S, are
included in the resulting relation.
• If there are tuples in S without any matching
tuple in R, then the R-attributes of resulting
relation are made NULL.
• It gives the matching rows and the rows which
are in right table but not in left table.
Left
A B
100 Database Left Right
101 Mechanics A B D
102 Electronics 100 Database Alex
102 Electronics Meena
Right 104 --- Mira
A D
100 Alex
102 Meena
104 Mira
Full Outer Join: ( R S)
• All the tuples from both participating Left
relations are included in the resulting
relation. A B
• If there are no matching tuples for both 100 Database
relations, their respective unmatched
attributes are made NULL. 101 Mechanics
102 Electronics
• Left Right

A B D Right
100 Database Alex A D
101 Mechanics ---
100 Alex
102 Electronics Meena
104 --- Mira 102 Meena
104 Mira
Division

Division operator A/B can be applied if and only if:


• Attributes of B is proper subset of Attributes of A.
• The relation returned by division operator will
have attributes = (All attributes of A – All
Attributes of B)
• The relation returned by division operator will
return those tuples from relation A which are
associated to every B’s tuple.
For e.g.
• Consider two relation instances A and B in which
A has (exactly) two fields x and y and B has just
one field y, with the same domain as in A.
• We define the division operation AlB as the set of
all x values (in the form of unary tuples) such that
for every y value in (a tuple of) B, there is a tuple
(x,y) in A.
Table1 :STUDENT_SPORTS Table2: ALL_SPORTS

ROLL_NO SPORTS SPORTS

1 Badminton Badminton
2 Cricket Cricket
2 Badminton
4 Badminton
• To apply division operator as
STUDENT_SPORTS/ ALL_SPORTS

The operation is valid as


• attributes in ALL_SPORTS is a proper subset of attributes in
STUDENT_SPORTS.
• The attributes in resulting relation will have attributes
{ROLL_NO,SPORTS}-{SPORTS}=ROLL_NO
• The tuples in resulting relation will have those ROLL_NO which
are associated with all B’s tuple {Badminton, Cricket}.
• ROLL_NO 1 and 4 are associated to Badminton only. ROLL_NO
2 is associated to all tuples of B. So the resulting relation will
ROLL_NO
be:
2
example
(Q1) Find the names of sailors who have reserved boat 103.
A) Select s.sname from sailor s, reserves r where s.sid=r.sid and r.bid=103;
(Q2) Find the names of sailors who have reserved a red boat.
B) Select s.sname from sailor s, reserves r, boats b where
r.bid=b.bid and s.sid=r.sid and b.color = “red”;

(Q3) Find the colors of boats reserved by Lubber.


C) Select b.color from sailor s, reserves r, boats b where s.sname=lubber and
s.sid=r.sid and r.bid=b.bid;

(Q4) Find the names of sailors who have reserved at least one boat.
A) Select s.sname from sailor s, reserves r, boats b where r.bid=b.bid and
s.sid=r.sid ;

(Q5) Find the names of sailors who have reserved a red boat and green boat.
D) A) Select s.sname from sailor s, reserves r, boats b where b.color = “red”
AND “green” and r.bid=b.bid and s.sid=r.sid;
Find sid’s of the sailors whose age>35
Select s.sid from sailor s where age>35
Find the name of the sailors and dates reserved by the sailors
Select s.sname, r.day from sailor s , reserves r where s.sid=r.sid;
Find the name and color of the boat of that sailor
select s.sname, b.color from sailor s, boat b, reserves r where s.sid=r.sid and r.bid=b.bid;
Find name and boat names of the sailors.
Select s.sname , b.bname from sailor s , boat b, reserves r where s.sid=r.sid and r.bid=b.bid;
Find the name of the sailor who have reserved a boat
select s.sname from sailor s , reserves r where s.sid=r.sid;
Find the name of the sailor who has reserved a boat 101
select s.sname from sailor s , reserves r where s.sid=r.sid and r.bid=101;
Find name of the sailor who has reserved a green boat
Select s.sname from sailor s , reserves r, boats b where b.color=“green” and s.sid=r.sid and
r.bid=b.bid;
Find the color of the boat reserved by Horatio.
select b.color from sailor s, reserves r, boats b where s.sid=r.sid and r.bid=b.bid and
s.sname=“horatio”;
Find the name of the sailor who has reserved a red or blue boat.
Select s.sname from sailor s, reserves r, boats b where s.sid=r.sid and r.bid=b.bid and
b.color=“red” or “blue”;
Find sid’s of sailor whose age>35 and who has not reserved a green boat.
Select s.sid from sailor s, reserves r, boat b where s.age>35 and s.sid=r.sid and r.bid=b.bid and
b.color!=“green”;
11)Find the name of the sailor’s who has reserved atleast two boats.
12)Find the name of all sailors who has reserved all boats
13)Find the names of sailors who has reserved all Interlake boat
14) Find the name of all sailors who has reserved all dipper boat
Find the name of all the sailors whose rating>8 and age>30
Select s.sname from sailor s where s.rating>8 INTERSECT Select s.sname from sailor s where
s.age >30
Find the name of the sailor who has reserved a red or a green boat
Select s.sname from sailor s, reserves r, boats b where s.sid=r.sid and r.bid=b.bid and
b.color=“red” UNION Select s.sname from sailor s, reserves r, boats b where s.sid=r.sid and
r.bid=b.bid and b.color=“green”;
Find the name of the sailors who has reserved both red and green boat
Select s.sname from sailor s, resreves r, boats b where s.sid=r.sid and r.bid=b.bid and
b.color=“red” INTERSECT Select s.sname from sailor s, resreves r, boats b where s.sid=r.sid and
r.bid=b.bid and b.color=“green” ;
Find the name of sailors who has reserved a red boat but not green boat
Select s.sname from sailor s, resreves r, boats b where s.sid=r.sid and r.bid=b.bid and
b.color=“red” MINUS Select s.sname from sailor s, resreves r, boats b where s.sid=r.sid and
r.bid=b.bid and b.color=“green” ;
Relational Calculus
• Relational Algebra is a procedural query language to
fetch data and which also explains how it is done
• Relational Calculus in non-procedural query language
and has no description about how the query will work
or the data will be fetched.
• It only focusses on what to do, and not on how to do it.
• Relational Calculus exists in two forms:
1. Tuple Relational Calculus (TRC)
2. Domain Relational Calculus (DRC)
Tuple Relational Calculus (TRC)
• The tuple relational calculus is specified to select
the tuples in a relation.
• TRC is used for selecting those tuples that satisfy
the given condition.
• In TRC, filtering variable uses the tuples of a
relation.
• The result of the relation can have one or more
tuples.
• Notation:
{t | P(t)} or {t | Condition (t)}
• where t = resulting tuples,
P(t) = known as Predicate and these are the
conditions that are used to fetch t
• Thus, it generates set of all tuples , such that
Predicate P(t) is true for t.
• P(t) may have various conditions logically combined
with OR (∨), AND (∧), NOT(¬).
• We can also specify column name using a . dot
operator, with the tuple variable to only get a certain
attribute(column) in result.
 It also uses quantifiers:

• ∃ t ∈ r (Q(t)) : ”there exists” a tuple in t in


relation r such that predicate Q(t) is true.

• ∀ t ∈ r (Q(t)) : predicate Q(t) is true “for all”


tuples in relation r.
Example

-Find all sailors with a rating above 7.


{S I S ϵ Sailors Ʌ S.rating > 7}

Table: Student
First_Name Last_Name Age
Ajeet Singh 30
Chaitanya Singh 31
Rajeev Bhatia 27
Carl Pratap 28

-To fetch names of students, from Student, with age greater


than 17
-Query to display the last name of those students
where age is greater than 30
{ t.Last_Name | Student(t) AND t.age > 30 }

-Query to display all the details of students where


Last name is ‘Singh’
{ t | Student(t) AND t.Last_Name = 'Singh' }
Table: Loan
Loan number Branch name Amount
L33 ABC 10000
L35 DEF 15000
L49 GHI 9000
L98 DEF 65000
Q) Find the branch-name, loan number and
amount for loans over 9000
{t|t ∈ loan ∧ t[amount] >9000}
Domain Relational Calculus (DRC)
• In domain relational calculus, filtering is done based on
the domain of the attributes and not based on the tuple
values.
• In Domain Relational Calculus, a query is expressed as,
{ < x1, x2, x3, ..., xn > | P (x1, x2, x3, ..., xn ) }
• where, < x1, x2, x3, …, xn > represents resulting domains
of attributes and P (x1, x2, x3, …, xn ) represents the
condition or formula equivalent to the Predicate calculus.
• Predicate Calculus Formula:
 Set of all comparison operators
 Set of connectives like and, or, not
 Set of quantifiers
Example
First_Name Last_Name Age
Ajeet Singh 30
Chaitanya Singh 31
Rajeev Bhatia 27
Carl Pratap 28
Example
• Query to find the first name and age of students
where student age is greater than 27
{< First_Name, Age > | ∈ Student ∧ Age > 27}

{< First_Name, Age > | ∈ Student ∧ Age > 27 ∧


First_Name like ‘c%’}
Table-2: Loan
Loan number Branch name Amount
L01 Main 200
L03 Main 150
L10 Sub 90
L08 Main 60

Find the loan number, branch, amount of loans of


greater than or equal to 100 amount.

{<l, b, a> | <l, b, a> ∈ loan ∧ (a >= 100)}

You might also like