0% found this document useful (0 votes)
5 views57 pages

Unit-II

The document discusses two formal query languages associated with the relational model: relational algebra and relational calculus. Relational algebra is a procedural language that uses operators to manipulate relations, while relational calculus is a non-procedural language that focuses on what data to retrieve without specifying how to compute it. The document also covers various operations in relational algebra, including selection, projection, joins, and set operations, as well as the basics of tuple and domain relational calculus.

Uploaded by

lohithapendli147
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)
5 views57 pages

Unit-II

The document discusses two formal query languages associated with the relational model: relational algebra and relational calculus. Relational algebra is a procedural language that uses operators to manipulate relations, while relational calculus is a non-procedural language that focuses on what data to retrieve without specifying how to compute it. The document also covers various operations in relational algebra, including selection, projection, joins, and set operations, as well as the basics of tuple and domain relational calculus.

Uploaded by

lohithapendli147
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/ 57

RELATIONAL ALGEBRA AND

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.

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.
Student
SID Name Std
101 Alex 5
102 Maria 9

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

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
SID Name Std
101 Alex 10
102 Maria 11
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 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

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
ROLL_NO SPORTS
1 Badminton
2 Cricket
2 Badminton
4 Badminton

Table2: ALL_SPORTS
SPORTS
Badminton
Cricket
• 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
be:
ROLL_NO
2
example
(Q1) Find the names of sailors who have reserved
boat 103.
(Q2) Find the names of sailors who have reserved
a red boat.
(Q3) Find the colors of boats reserved by Lubber.
(Q4) Find the names of sailors who have reserved
at least one boat.
(Q5) Find the names of sailors who have reserved
a red boat and green boat.
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

-Find the loan number for each loan of an amount


greater or equal to 10000.

{t| ∃ s ∈ loan(t[loan number] = s[loan number]


∧ s[amount]>=10000)}
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