0% found this document useful (0 votes)
6 views

DBMS_07-08

The document provides an overview of query languages, specifically focusing on Relational Algebra and Relational Calculus, which are essential for manipulating data in relational databases. It discusses various operations such as selection, projection, and set operations, along with their properties and examples. Additionally, it highlights the importance of SQL as a relationally complete language that implements these theoretical models in practice.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

DBMS_07-08

The document provides an overview of query languages, specifically focusing on Relational Algebra and Relational Calculus, which are essential for manipulating data in relational databases. It discusses various operations such as selection, projection, and set operations, along with their properties and examples. Additionally, it highlights the importance of SQL as a relationally complete language that implements these theoretical models in practice.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

1/21/2025

Database Management Systems


Dr. Dev Narayan Yadav
Department of CSE, NIT Rourkela
Email: [email protected]
Mo. – 8349869748
Room – CS204

Department of CSE, National Institute of Technology Rourkela

Abstract Query Languages:


Relational Algebra and Relational Calculus

Department of CSE, National Institute of Technology Rourkela

1
1/21/2025

Introduction to Query Language


Query languages help in manipulate data in a relation.

Query Language Just an


Idea

Procedural Non-Procedural

Relational Relational
Algebra Calculus

SQL
Department of CSE, National Institute of Technology Rourkela

Introduction to Query Language


Query languages help in manipulate data in a relation.
 The Query models (Relational Algebra and Relational
Calculus) are abstract or theoretical.
 Relational Algebra and Relational Calculus (with only
safe queries) are equivalent.
 In practice, Relational model is implemented as
RDBMS, and the query model is implemented as SQL.
 A language is called relationally complete if it has
capacity to express every query expressible through
relational algebra. SQL is relationally complete.

Department of CSE, National Institute of Technology Rourkela

2
1/21/2025

Introduction to Query Language


 Degree / Arity of a Relation: is the number of attributes /
columns of the relation. It is an intrinsic property of a
relation. (Refer to structure)
 Cardinality: of a relation is the number of tuples / rows of
the relation. It is not an intrinsic property of a relation, and
represents a state of the relation. (refer to size)
ID Name Major
101 Vineet CSE Cardinality  6
102 Ayush CSE
Degree/Arity  3
103 Surya MTH
104 Ashish MTH
105 Anshu PHY
106 Sumit CHY
Department of CSE, National Institute of Technology Rourkela

Relational Algebra

Department of CSE, National Institute of Technology Rourkela

3
1/21/2025

Relational Algebra
 It is just like a simple operation; instead of number or
variables as an operand we will have relation as
operand).
8 + 9

Operand Operator Operand

A&B

Department of CSE, National Institute of Technology Rourkela

Operations
 Basic/Fundamental Operations:
• Unary Operations:
• Selection (σ): Selects a subset of tuples / rows from a relation
• Projection (π): Retains a subset of columns from a relation
• Rename (ρ): Renames a relation
• Binary operations:
• Cross Product / Cartesian Product (×): Allows to combine two
relations
• Difference / Set difference (−): Returns tuples present in one
relation but not in the other.
• Union (∪): Consolidates tuples in two relations

Department of CSE, National Institute of Technology Rourkela

4
1/21/2025

Operations
 Few More Operations:
• Intersection (∩): Returns tuples present in both relations
• Join (⋈): Allows to combine two or more relations
• There are several types of joins:
• semi join, left outer join, right outer join, full outer join
• Division (/): Returns those tuples of a relation which
subsets tuples of another table
• Assignment (←): Assigning the output to a relation

Department of CSE, National Institute of Technology Rourkela

Operations: Selection (σ)


 Selection (σ):
• Selects a subset of tuple (rows) based on a given
condition. (also known as horizontal partitioning)
• Syntax: σ<condition>(<Relation>)
• σMajor=‘CSE’ (Students): Select all students majoring in
CSE.
ID Name Major
101 Vineet CSE
ID Name Major
102 Ayush CSE
101 Vineet CSE
103 Surya MTH
102 Ayush CSE
104 Ashish MTH
105 Anshu PHY
106 Sumit CHY

Department of CSE, National Institute of Technology Rourkela

5
1/21/2025

Selection (σ): Example 2


 Cascading conditions
 σ Major=‘CSE’ ^ Age>19 (Students)

ID Name Age Major


101 Vineet 19 CSE
102 Ayush 20 CSE ID Name Age Major
103 Surya 18 MTH 102 Ayush 20 CSE
104 Ashish 21 MTH
105 Anshu 22 PHY
106 Sumit 18 CHY

Department of CSE, National Institute of Technology Rourkela

Selection (σ): Example 3


 Nested Conditions
 σ Major=‘CSE’(σ Age> 19(Students))

ID Name Age Major


101 Vineet 19 CSE ID Name Age Major
102 Ayush 20 CSE 102 Ayush 20 CSE
103 Surya 18 MTH 104 Ashish 21 MTH
104 Ashish 21 MTH 105 Anshu 22 PHY
105 Anshu 22 PHY
106 Sumit 18 CHY
ID Name Age Major
102 Ayush 20 CSE

Department of CSE, National Institute of Technology Rourkela

6
1/21/2025

Selection (σ): Example 3


 Nested Conditions
 σ Major=‘CSE’(σ Age> 19(Students))

ID Name Age Major


101 Vineet 19 CSE ID Name Age Major
102 Ayush 20 CSE 102 Ayush 20 CSE
103 Surya 18 MTH 102 Ayush 20 CSE
104 Ashish 21 MTH
105 Anshu 22 PHY
106 Sumit 18 CHY ID Name Age Major
102 Ayush 20 CSE

 Order of nesting does not affect the output  Select


(σ) operator is commutative.
Department of CSE, National Institute of Technology Rourkela

Selection (σ) Operator Summary


Selection operator works on a single tuple at a time
The CONDITION involved in selection operation
MUST be a Boolean expression (or a logical
combination of more than one Boolean expressions).
Selection operator may reduce the cardinality of a
relation when it acts on, i.e.,
|σ<CONDITION>(<RELATION>)| ≤ |<RELATION>|
 Selection operator does not alter the arity of a relation
 Selection operator can act on a relation or any relational
algebra expression resulting on a table.
 Selection operator is commutative.

Department of CSE, National Institute of Technology Rourkela

7
1/21/2025

Projection (π): Example 1


 Projection (π): selects a subset of attributes from a relation.
Also called as vertical partitioning.
 Syntax: π<Attributes>(<Relation>)
 πName, Major(Students)

ID Name Age Major Name Major


101 Vineet 19 CSE Vineet CSE
102 Ayush 20 CSE Ayush CSE
103 Surya 18 MTH Surya MTH
104 Ashish 21 MTH Ashish MTH
105 Anshu 22 PHY Anshu PHY
106 Sumit 18 CHY Sumit CHY

Department of CSE, National Institute of Technology Rourkela

Projection (π): Example 2


 Automatically removes duplicates.
 πID (Enrolments)

SN ID CourseID ID
1 101 CS101 101
2 102 CS102 102
3 103 MT101 103
4 102 EE101 104
5 104 CH102 105
6 105 CH101

Department of CSE, National Institute of Technology Rourkela

8
1/21/2025

Projection (π) Operator Summary


 Is Projection (π) operation commutative?
 We know, for a projection operation to work, the
attributes it retrieves must be a subset of the attributes of
the input relation.
• For π<ATTRIBUTES1>(π<ATTRIBUTES2>(<RELATION>)) to be valid, the
following condition must be true:
• <ATTRIBUTES2> ⊆ <ATTRIBUTES1>
Also, for projection to be commutative,
• π<ATTRIBUTES2>(π<ATTRIBUTES1>(<RELATION>))=π<ATTRIBUTES1>(π<ATTRIBUTES2
>(<RELATION>))
• However,
• π<ATTRIBUTES2>(π<ATTRIBUTES1>(<RELATION>))=π<ATTRIBUTES2>(<RELATION
>)
 Hence projection is not commutative.

Department of CSE, National Institute of Technology Rourkela

Projection (π) Operator Summary


The <ATTRIBUTES> involved in projection operation
MUST be a subset (may be equal) of all attributes in
<RELATION>
Projection operator may reduce the arity / degree of a
relation when it acts on.
 Projection operator may alter the cardinality of a
relation as it removes duplicate tuples
 If <ATTRIBUTES> is a superkey, then
|π<ATTRIBUTES>(<RELATION>)| = |<RELATION>|
 Projection operation defined in abstract relational
algebra language eliminates duplicates, but projection
operator in SQL retains duplicates.
Department of CSE, National Institute of Technology Rourkela

9
1/21/2025

Cascading Selection and Projection


 πName, Major(σAge>19(Students)

ID Name Age Major


101 Vineet 19 CSE Name Major
102 Ayush 20 CSE Ayush CSE
103 Surya 18 MTH Ashish MTH
104 Ashish 21 MTH Anshu PHY
105 Anshu 22 PHY
106 Sumit 18 CHY

 Please note that πName, Major(σAge>19(Students)) != σAge>19 (πName,


Major(Students)),
 i.e., the roles of selection and projection cannot be commutative.
Always selection acts first, and then projection.
Department of CSE, National Institute of Technology Rourkela

Rename (ρ)
 Renaming relation name and attributes:
• ρ<OUTPUT_RELATION>(<OUTPUT_ATTRIBUTES>)(<INPUT_RELATION>)
 Renaming relation name only (and not attributes):
• ρ<OUTPUT_RELATION>(<INPUT_RELATION>)
 Renaming attributes only (and not relation name):
• ρ(<OUTPUT_ATTRIBUTES>)(<INPUT_RELATION>)
 The input for rename operator can be a relation or a relational
algebra expression
 Rename operation is a special type of projection

Department of CSE, National Institute of Technology Rourkela

10
1/21/2025

Set Operations
 The set operations Union (∪), Intersection (∩), Difference
(−) are binary Operations.
 Union (∪): Consolidates tuples in two relations
 Intersection (∩): Extracts only common tuples from two
relations
 Difference (−): Extracts tuples present exclusively in first
relation (but not present in second relation).

Department of CSE, National Institute of Technology Rourkela

Set Operations
 Union Compatibility: The two input relations should have
(i) same degree and (ii) same domain for every attribute
 Union Compatibility is decided based on intrinsic properties
of two relations, not based on it’s state.
 R ∪ S automatically stores the result in first operand, i.e.,
R.
 Union eliminates duplicates from the output and returns the
common tuples only once in output, and intersection cannot
have duplicates.
 Intersection is a derived operation, i.e.
 R ∩ S = R − (R − S) = S − (S − R)

Department of CSE, National Institute of Technology Rourkela

11
1/21/2025

Set Operations
 Union and intersection operations are commutative, i.e.
• R ∪ S = S ∪ R and R ∩ S = S ∩ R
 Union and intersection operations are associative, i.e.
• (R ∪ S) ∪ T = R ∪ (S ∪ T) and (R ∩ S) ∩ T = R ∩ (S ∩ T)
 Difference is not commutative, i.e.,
• R − S != S − R

Department of CSE, National Institute of Technology Rourkela

Cardinality Analysis of Set Operations


 Assume Relation A having cardinality m, B having
cardinality n.

Department of CSE, National Institute of Technology Rourkela

12
1/21/2025

Union ( ): Example
 Students1 ∪ Students2

ID Name Age Major ID Name Age Major


101 Vineet 19 CSE ∪ 102 Ayush 20 CSE
102 Ayush 20 CSE 103 Surya 18 MTH

ID Name Age Major


101 Vineet 19 CSE
102 Ayush 20 CSE
103 Surya 18 MTH

Department of CSE, National Institute of Technology Rourkela

Union ( ): Example 2
 πID (Students1) ∪ πID (Students2)

ID Name Age Major ID Name Age Major


101 Vineet 19 CSE 102 Ayush 20 CSE
102 Ayush 20 CSE ∪ 103 Surya 18 MTH

ID
101
102
103

Department of CSE, National Institute of Technology Rourkela

13
1/21/2025

Intersection (∩): Example


 Returns the tuples that are common in two relations.
• Students1 ∩ Students2

ID Name Age Major ID Name Age Major


101 Vineet 19 CSE 102 Ayush 20 CSE
102 Ayush 20 CSE ∩ 103 Surya 18 MTH

ID Name Age Major


102 Ayush 20 CSE

Department of CSE, National Institute of Technology Rourkela

Difference (-): Example


 Return tuple available in one relation but not in other.
• Students1 – Students2

ID Name Age Major ID Name Age Major


101 Vineet 19 CSE 102 Ayush 20 CSE
102 Ayush 20 CSE - 103 Surya 18 MTH

ID Name Age Major


101 Vineet 19 CSE

Department of CSE, National Institute of Technology Rourkela

14
1/21/2025

Difference (-): Example 2


 πID (Students1) - πID (Students2)

ID Name Age Major ID Name Age Major


101 Vineet 19 CSE 101 Vineet 19 CSE
102 Ayush 20 CSE - 102 Ayush 20 CSE
103 Surya 18 MTH

No Output

Department of CSE, National Institute of Technology Rourkela

Cartesian Product (x)


 Cross product / Cartesian product (×) is a binary Operation.
 The input relations need not be union compatible for
participating in cross product.
• Arity of R × S = (Arity of R + Arity of S)
• Cardinality of R × S = (Cardinality of R × Cardinality of S)
ID Name Major SN CourseID
101 Vineet CSE
x 1 CS101
102 Ayush CSE 2 CS102

ID Name Major SR CourseID


101 Vineet CSE 1 CS101
101 Vineet CSE 2 CS102
102 Ayush CSE 1 CS101
102 Ayush CSE 2 CS102
Department of CSE, National Institute of Technology Rourkela

15
1/21/2025

Cartesian Product (x)


 Cross product is commutative, i.e, R × S = S × R
 Cross product is associative, i.e., (R × S) × T = R × (S × T)
 In most practical cases, such unconditional linking of every
tuple of a relation with every tuple of another relation is
meaningless.

Department of CSE, National Institute of Technology Rourkela

Join ( )
• Combines related tuple from two tables based on related column.
• Syntax: P ← R⋈<join condition>S
• Join product is commutative, i.e, R⋈<join condition>S = S⋈<join condition>R
• Arity of R ⋈ S = (Arity of R + Arity of S)
• Minimum Cardinality of R ⋈ S = 0
• Maximum Cardinality of R⋈S=(Cardinality of R × Cardinality of S)
• <join condition> is denoted as Ri θ Sj where Ri and Sj are two
attributes from the relations respectively, and θ is the binary
Boolean operation between them.
• θ can be checking if they are equal, or unequal, or one is greater
than other etc.
• As the operation between attributes from different relations is
classically represented by θ, it is also called theta join.

Department of CSE, National Institute of Technology Rourkela

16
1/21/2025

Join ( )
 Types:
• Natural Join: Matches rows with the same value in common
columns.
• Theta Join: Combines rows based on a condition.
• Outer Join: Includes unmatched rows (left, right, or full outer
joins).

Department of CSE, National Institute of Technology Rourkela

Natural Join ( or *)
 A special type of join operation, where <join condition> is not
explicitly written.
 An equality check between the attribute pairs having same name is
done, and accordingly the result of natural join is computed.
 R ⋈ S or R * S
A B C A
A1 B1 C1 A1
A2 B2 C2
* A2
A3 B3 C2

A B C
A1 B1 C1
A2 B2 C2

Department of CSE, National Institute of Technology Rourkela

17
1/21/2025

Natural Join ( or *) : Example 2


 Students ⋈ Enrolments
StudentID Name Major EnrollID StudentID CourseID
1 Vineet CSE 101 1 CS101
2 Ayushman CSE * 102 2 CS102

StudentID Name Major EnrollID CourseID


1 Vineet CSE 101 CS101
2 Ayushman CSE 102 CS102

 Common attributes will not repeat in the resultant relation.

Department of CSE, National Institute of Technology Rourkela

Natural Join ( or *) : Example 3


 Students ⋈ Enrolments
StudentID Name Major EnrollID StudentID CourseID
1 Vineet CSE 101 1 CS101
2 Ayushman CSE * 102 2 CS102
3 Ashish MTH

StudentID Name Major EnrollID CourseID


1 Vineet CSE 101 CS101
2 Ayushman CSE 102 CS102

 Common attributes will not repeat in the resultant relation.

Department of CSE, National Institute of Technology Rourkela

18
1/21/2025

Theta Join ( θ)
 Combines rows based on a condition (θ).
 Students⋈(Students.StudentID = Enrollments.StudentID) Enrollments

StudentID Name Major EnrollID StudentID CourseID


1 Vineet CSE 1 1 CS101
2 Ayushman CSE 2 3 CS102
3 Surya MTH
⋈θ

StudentID Name Major EnrollID CourseID


1 Vineet CSE 1 CS101
3 Surya MTH 2 CS102

Department of CSE, National Institute of Technology Rourkela

Theta Join ( θ): Example 2


 Students⋈(Students.StudentID >1) Enrollments

StudentID Name Major EnrollID StudentID CourseID


1 Vineet CSE 1 1 CS101
2 Ayushman CSE 2 3 CS102
3 Surya MTH ⋈θ

Department of CSE, National Institute of Technology Rourkela

19
1/21/2025

Theta Join ( θ): Example 2


 Students⋈(Students.StudentID >1) Enrollments
• Apply the Condition StudentID > 1 on Enrollments.

EnrollID StudentID CourseID


1 1 CS101
2 3 CS102

EnrollID StudentID CourseID


2 3 CS102

Department of CSE, National Institute of Technology Rourkela

Theta Join ( θ): Example 2


 Students⋈(Students.StudentID >1) Enrollments
• Apply the Condition StudentID > 1 on Enrollements.
• Perform the Join: Combine filtered Enrollments relation with
Students where Students.StudentID = Enrollments.StudentID.

EnrollID StudentID CourseID StudentID Name Major


2 3 CS102 1 Vineet CSE
⋈θ 2 Ayushman CSE
3 Surya MTH

StudentID Name Major EnrollID CourseID


3 Surya MTH 2 CS102

Department of CSE, National Institute of Technology Rourkela

20
1/21/2025

Inner Join
 Combines rows from two tables where there is a match based
on a specified condition. It is a specific case of theta join
where the condition is equality.
 Students⋈(Students.StudentID = Enrollments.StudentID) Enrollments

StudentID Name Major


EnrollID StudentID CourseID
1 Vineet CSE
1 1 CS101
2 Ayushman CSE ⋈
2 3 CS102
3 Surya MTH

StudentID Name Major EnrollID CourseID


1 Vineet CSE 1 CS101
3 Surya MTH 2 CS102

Department of CSE, National Institute of Technology Rourkela

Outer Join
 Combines rows from two tables, including unmatched rows,
by filling in NULL values where matches are not found.
 Types of outer joins:
• Left Outer Join
• Right Outer Join
• Full Outer Join

Department of CSE, National Institute of Technology Rourkela

21
1/21/2025

Left Outer Join ( )


 Returns all rows from the left table (Students), and matching
rows from the right table (Enrollments). Non-matching rows
in the right table are filled with NULL.
 Students ⟕ (Students.StudentID = Enrollments.StudentID) Enrollments
StudentID Name Major StudentID CourseID
1 Vineet CSE 1 CS101
2 Ayushman CSE ⟕ 4 CS103
3 Surya MTH

StudentID Name Major StudentID CourseI


D
1 Vineet CSE 1 CS101
2 Ayushman CSE NULL NULL
3 Surya MTH NULL NULL
Department of CSE, National Institute of Technology Rourkela

Right Outer Join ( )


 Returns all rows from the right table (Enrollments), and
matching rows from the left table (Students). Non-matching
rows in the left table are filled with NULL.
 Students ⟖ (Students.StudentID = Enrollments.StudentID) Enrollments
StudentID Name Major StudentID CourseID
1 Vineet CSE 1 CS101
2 Ayushman CSE ⟖ 4 CS103
3 Surya MTH

StudentID Name Major StudentID CourseI


D
1 Vineet CSE 1 CS101
NULL NULL NULL 4 103

Department of CSE, National Institute of Technology Rourkela

22
1/21/2025

Full Outer Join ( )


 Combine results of previous two joins.
 Students ⟗ (Students.StudentID = Enrollments.StudentID) Enrollments

StudentID Name Major StudentID CourseID


1 Vineet CSE 1 CS101
2 Ayushman CSE ⟗ 4 CS103
3 Surya MTH

StudentID Name Major StudentID CourseI


D
1 Vineet CSE 1 CS101
2 Ayushman CSE NULL NULL
3 Surya MTH NULL NULL
NULL NULL NULL 4 103

Department of CSE, National Institute of Technology Rourkela

Semi Join ( )
 A semi-join between two relations returns tuples from the
one relation where one or more matches (according to join
condition) are found in tuples of the other relation.
 Though a tuple in one relation may have match with more
than one tuples of the other relation, the tuple will appear
only once in output.
 left semi-join (R ⋉ S) retains those tuples of R which have a
match with S.
 right semi-join (R ⋊ S) retains those tuples of S which have a
match with R
 Semi-join is not commutative.

Department of CSE, National Institute of Technology Rourkela

23
1/21/2025

Left Semi Join ( )


 Returns rows from the left table that have matching rows in
the right table, without including columns from the right
table.
 Students ⋉ (Students.StudentID = Enrollments.StudentID) Enrolments
StudentID Name Major StudentID CourseID
1 Vineet CSE 1 CS101
2 Ayushman CSE 3 CS102

3 Surya MTH 4 CS103

StudentID Name Major


1 Vineet CSE
3 Surya MTH

Department of CSE, National Institute of Technology Rourkela

Right Semi Join ( )


 Returns rows from the right table that have matching rows in
the left table, without including columns from the right table.
 Students ⋊ (Students.StudentID = Enrollments.StudentID) Enrollments

StudentID Name Major StudentID CourseID


1 Vineet CSE 1 CS101
2 Ayushman CSE 3 CS102

3 Surya MTH 4 CS103

StudentID Course ID
1 CS101
3 CS102

Department of CSE, National Institute of Technology Rourkela

24
1/21/2025

Anti Join
 A anti-join or anti semi-join between two relations returns
those tuples from the one relation for which there exists no
corresponding matches (according to join condition) in tuples
of the other relation.
 left anti-join or called ONLY anti-join (R ▷ S) retains those
tuples of R which do not have a match with S
• R▷S = R - (R ⋉ S)
 right anti-join (R ◁ S) retains those tuples of S which do not
have a match with R.
• R◁ S = S - (R ⋊ S)
 anti-join is not commutative.

Department of CSE, National Institute of Technology Rourkela

Left Anti Join ( )


 Returns rows from the left table that do not have matching
rows in the right table.
 Students ▷ Enrolments = Students – (Students ⋉
(Students.StudentID = Enrollments.StudentID) Enrolments)

StudentID Name Major StudentID CourseID


1 Vineet CSE 1 CS101
2 Ayushman CSE 3 CS102
3 Surya MTH ▷ 4 CS103

StudentID Name Major


2 Ayushman CSE

Department of CSE, National Institute of Technology Rourkela

25
1/21/2025

Right Anti Join ( )


 Returns rows from the left table that do not have matching
rows in the right table.
 Students ◁ Enrolments = Enrollments – (Students ⋉
(Students.StudentID = Enrollments.StudentID) Enrolments)

StudentID Name Major StudentID CourseID


1 Vineet CSE 1 CS101
2 Ayushman CSE 3 CS102
3 Surya MTH ◁ 4 CS103

StudentID CourseID
4 CS103

Department of CSE, National Institute of Technology Rourkela

Cardinality Analysis of Join Operations

Department of CSE, National Institute of Technology Rourkela

26
1/21/2025

Division (/)
 P ← R / S will retain those attributes which are present in R
but not in S only for those tuples where ALL values of
common attributes in R matches any tuple of S
 Syntax: P  R/S
 Note: P*S may not return R, Non-Commutative.
 Find students taken all required courses.
Name CourseID
CourseID
Vineet CS101
CS101
Vineet CS102 / CS102
Surya CS103
Ashish CS102
Name
Vineet
Department of CSE, National Institute of Technology Rourkela

Division (/)
 P ← R / S will retain those attributes which are present in R
but not in S only for those tuples where ALL values of
common attributes in R matches any tuple of S
 Syntax: P  R/S
 Find students taken all required courses.

Name CourseID
CourseID
Vineet CS101
CS101
Vineet CS102 / CS102
Surya CS103
Ashish CS102
Ashish CS101 Name
Vineet
Ashish

Department of CSE, National Institute of Technology Rourkela

27
1/21/2025

Division (/)
 T←R/S
• T1 ← π(R-S)(R)
• T2 ← π(R-S) ((S × T1) - R)
• T ← T1 - T2

Department of CSE, National Institute of Technology Rourkela

Relational Calculus

Department of CSE, National Institute of Technology Rourkela

28
1/21/2025

Relational Calculus
Relational calculus is a non-procedural query language
used in relational databases to describe the desired
results of a query without specifying how to compute
them.
 It is based on mathematical logic and works with
variables, predicates, and logical connectives to define
queries.
Relational Calculus

Tuple Relational Domain Relational


Calculus Calculus
Department of CSE, National Institute of Technology Rourkela

Relational Calculus
 TRC and DRC are equivalent in power.
 TRC and DRC are also equivalent in power with
relational algebra.

Department of CSE, National Institute of Technology Rourkela

29
1/21/2025

Tuple Relational Calculus


 Works with rows as variables
 Syntax: {t | P{t}}
• t is a tuple
• P(t) is a predicted or condition that t must satisfy.
 Operations: P(t) may have several conditions logically
combined like AND (∧), OR (∨), NOT (¬), and quantifiers
like EXISTS (∃) and FOR ALL (∀).
 Atomic Operation: if we use single operation.
 Composite: Multiple operations.

Department of CSE, National Institute of Technology Rourkela

Tuple Relational Calculus


 Name of the Students.
• {Students.Name | Students (s)}

ID Name Age Major Name


101 Vineet 19 CSE Vineet
102 Ayush 20 CSE Ayush
103 Surya 18 MTH Surya
104 Ashish 21 MTH Ashish

Department of CSE, National Institute of Technology Rourkela

30
1/21/2025

Tuple Relational Calculus


 Name of the Students enrolled in CSE.
• {Students.Name | Students (s)^s.Major = ‘CSE’}

ID Name Age Major


101 Vineet 19 CSE Name
102 Ayush 20 CSE Vineet
103 Surya 18 MTH Ayush
104 Ashish 21 MTH

Department of CSE, National Institute of Technology Rourkela

Tuple Relational Calculus


 Name of the Students enrolled in CSE and Age>20.
• {Students.Name | Students (s)^s.Major = ‘CSE’^s.Age>19}

ID Name Age Major


101 Vineet 19 CSE Name
102 Ayush 20 CSE Vineet
103 Surya 18 MTH Ayush
104 Ashish 21 MTH

Department of CSE, National Institute of Technology Rourkela

31
1/21/2025

Tuple Relational Calculus


 Name of the Students enrolled in CSE and Age>20.
• {Students.Name | Students (s)^ (∃ e) (Enrolments(e) ^ s.ID =
e.StudentID ^ CourseID = ‘CS101’}

ID Name Age Major StudentID CourseID


101 Vineet 19 CSE 101 CS101
102 Ayush 20 CSE 103 CS102
104 CS103
103 Surya 18 MTH
104 Ashish 21 MTH

Name
Vineet

Department of CSE, National Institute of Technology Rourkela

Domain Relational Calculus


 Works with domain variables (individual
attributes/fields).
 Syntax: {x1, x2, … xn | P(x1, x2, …. Xn)}
• x1, ,x2 … are domain variables
• P(x1, ,x2) is a predicate or condition that the domain variable
must satisfy.
• i.e. Name of the students enrolled in CSE.
• {<x> | ∃y (x,y) ∈ Students ^ y =‘CSE’}

ID Name Age Major


101 Vineet 19 CSE ID Name Age Major
102 Ayush 20 CSE 101 Vineet 19 CSE
103 Surya 18 MTH 102 Ayush 20 CSE
104 Ashish 21 MTH
Department of CSE, National Institute of Technology Rourkela

32
1/21/2025

GATE Questions

Department of CSE, National Institute of Technology Rourkela

Gate Questions

• (a) Correct: As the selection operator is commutative, the order of


applying it will not impact the result.
• (b) Incorrect: Projection & Selection combined is non commutative.
• (c) correct: Selection distributed over union both are commutative.
• (d) Incorrect: In RHS if selection operator requires attribute A2, then the
transformation will become invalid.

Department of CSE, National Institute of Technology Rourkela

33
1/21/2025

Gate Questions

• (a) Always True: as B is foreign key, it will be present in R1 and R2


both.
• (b) Not always true: C may be present in R2, but not necessarily in R1.
• (c) Not always true: again C may contain additional values not
referenced by R1.
• (d) Always False: not possible as B is foreign key, and the relation
satisfy referential integrity constraints.

Department of CSE, National Institute of Technology Rourkela

Gate Questions

• R – R(R-S):
• R-S: computes the set of tuples in R but not in S
• R-(R-S), removes from R all tuples that are not in S, leaving only the
tuples common to both R and S. (reason for minimality: because it uses
only (-) operator.
• Other non minimal solutions:
– R∩S=(R∪S)−((R−S)∪(S−R))
– (R−(R−S))−(S−R)

Department of CSE, National Institute of Technology Rourkela

34
1/21/2025

Gate Questions

• Max: mn (every tuple of r matches with every tuple in S)


• Min: 0 (no match).

Department of CSE, National Institute of Technology Rourkela

Gate Questions

• First Part:
– Perform Cartesian product PxR
– Select P.Y = R.Y AND R.V = V
– Project attribute X from result  X1, X2
• Second Term:
– Perform QxR.
– Select Q.Y = R.Y AND Q.T>2
– Project attribute X from result  X2
• At last compute difference: X retrieved in first term – X retrieved in second
term. Result  X1

Department of CSE, National Institute of Technology Rourkela

35

You might also like