Unit-II
Unit-II
CALCULUS
UNIT-II
• 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 and Projection
• 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 ρ(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 (C(1 → sid1,
5 → sid2) S1xR1) returns a relation that
contains the tuples shown which has the
following schema:
C(sid1:integer, sname: string, rating:
integer, age: real, sid2: integer, bid:
integer,day: dates).
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.
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
Subjects
Class Subject
10 Math
10 English
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
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
102 Electronics
Right
A D
100 Alex
102 Meena
104 Mira
Left Right
A B D
100 Database Alex
101 Mechanics ---
102 Electronics Meena
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 Right
A B D
100 Database Alex
102 Electronics Meena
104 --- Mira
Full Outer Join: ( R S)
• All the tuples from both participating relations are included
in the resulting relation.
• If there are no matching tuples for both relations, their
respective unmatched attributes are made NULL.
• Left Right
A B D
100 Database Alex
101 Mechanics ---
102 Electronics Meena
104 --- Mira
Division
Table2: ALL_SPORTS
SPORTS
Badminton
Cricket
• To apply division operator as
STUDENT_SPORTS/ ALL_SPORTS
Table: Student
First_Name Last_Name Age
Ajeet Singh 30
Chaitanya Singh 31
Rajeev Bhatia 27
Carl Pratap 28