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

DBMS Lecture 5

The document discusses concepts related to relational algebra and relational calculus covered in Unit 5 of a database management systems course. It describes six fundamental relational algebra operations: selection, projection, rename, cartesian product, union, and set difference. It also discusses additional operations like intersection and joins. Joins are further divided into inner joins like theta join and natural join. The tuple and domain relational calculi are also introduced.

Uploaded by

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

DBMS Lecture 5

The document discusses concepts related to relational algebra and relational calculus covered in Unit 5 of a database management systems course. It describes six fundamental relational algebra operations: selection, projection, rename, cartesian product, union, and set difference. It also discusses additional operations like intersection and joins. Joins are further divided into inner joins like theta join and natural join. The tuple and domain relational calculi are also introduced.

Uploaded by

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

Database Management System

[CSC260]
BSc CSIT Sem: IV
Er. Sital Prasad
Alyssa Mandal
Pittman
Mechi Campus
Bhadrapur, Jhapa, Nepal

https://dbms-ctal.blogspot.com
What are you goingto learn?

▪ After the course, you will be able to…

• Integrate a database into an application

• Effectively manage data for long-term use

• Create database constructs to provide speedups

• Explain how a query is processed end-to-end

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

1. Unary Relational Operations: SELECT and PROJECT


2. Relational Algebra Operations from Set Theory
3. Binary Relational Operations: JOIN and DIVISION
4. Additional Relational Operations
5. the Tuple Relational Calculus
6. the Domain Relational Calculus

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

Query Languages
A query language is a language in which a user requests information from a database.
These are typically higher-level than programming languages.

They may be one of:

o Procedural, where the user instructs the system to perform a sequence of


operations on the database. This will compute the desired information.

o Nonprocedural, where the user specifies the information desired without


giving a procedure for obtaining the information.

A complete query language also contains facilities to insert and delete tuples as well as
to modify parts of existing tuples.

Formal languages
 relational algebra (procedural)
 relational calculus (non-procedural)
o tuple relational calculus
o domain relational calculus
https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

Relational Algebra
Relational algebra refers to a procedural query language that takes relation
instances as input and returns relation instances as output.
It performs queries with the help of operators. A binary or unary operator can
be used.
• In mathematics, a unary operation is an operation with only one operand, i.e.
a single input. An example is the function f : A → A, where A is a set. The
function f is a unary operation on A.
• binary operations, which use two operands. A calculation that combines two
elements (called operands) to produce another element.

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

1. Unary Relational Operations: SELECT and PROJECT

1. SELECT and PROJECT operations;


2. Both Sequences of operations;
3. RENAME operation

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

2. Relational Algebra Operations from Set Theory

1. UNION
2. INTERSECTION
3. MINUS
4. CARTESIAN PRODUCT operations

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

3. Binary Relational Operations: JOIN and DIVISION

1. JOIN operation and its variations;


2. DIVISION operation

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

4. Additional Relational Operations

1. Generalized projection,
2. aggregate functions,
3. NULL

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

5. Tuple Relational Calculus


6. Domain Relational Calculus

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5
The relational algebra is a procedural query language.
Six fundamental operations:
1. select (unary) (σ) Sigma
2. project (unary) ∏ pi
3. rename (unary) rho (ρ)
4. cartesian product (binary) X
5. union (binary) ∪
6. set-difference (binary) -
Several other operations, defined in terms of the fundamental
operations:
1. set-intersection ∩
2. natural join ⋈
3. Outer join
i. Left outer join (⟕)
ii. Right outer join (⟖)
iii. Full outer join (⟗)
Operations produce a new relation as a result.
https://dbms-ctal.blogspot.com
Selection (σ)

<, >, ≤ , ≥, = , ≠
AND: ∧
OR: ∧
NOT: ¬
R1 : = σc(R2) sigma
σ : predicate
R : relation name of table
selects tuples
c : condition
Selection (σ)

For example : Consider the table of relation R(Roll No, Name,


Age, Marks). If we want to select the name and age of student,
then it can be done by:
<, >, ≤ , ≥, = , ≠
Query Used : σ Name and Age>21 (Student_Details) AND: ∧
OR: ∧
NOT: ¬
R1 : = σc(R2) sigma
σ : predicate
R : relation name of table
selects tuples
c : condition
Projection (Π)
list of those attributes that we wish to appear, Eliminate duplicate tuples, if any.

Notation: ∏ A1, A2, An (r)


A1, A2, A3 is used as an attribute name of relation r.
Rename (ρ)

ρ (a/b)R
rename the attribute ‘b’ of relation by ‘a’.
The Relational Algebra and Relational Calculus Unit 5

2. Relational Algebra Operations from Set Theory

1. UNION
2. INTERSECTION
3. MINUS
4. CARTESIAN PRODUCT operations

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

2. Relational Algebra Operations from Set Theory

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

2. Relational Algebra Operations from Set Theory

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

2. Relational Algebra Operations from Set Theory


The Set Difference Operations : In order to fetch the data which is not present in
any one of the relation, set difference operation is used. The set difference
operation is denoted by “Minus(-)”.
Note : X1 - X2 ≠ X2 - X1 {Not Commutative}

Syntax : X1 - X2 or X2 - X1 , where X1 & X2 are two different relations having some


attributes.

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

2. Relational Algebra Operations from Set Theory


cartesian product (binary) X

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

3. Binary Relational Operations: JOIN and DIVISION

1. JOIN operation and its variations;


2. DIVISION operation

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

3. Binary Relational Operations: JOIN and DIVISION

Database Joins : Introduction

• Database joins are the alternate to the Cartesian product


operation of the relational algebra concept.
• But, the logic behind the data set joins is same as that of
a Cartesian product but, joins in database includes the option for
condition which needs to be satisfied in order to get the desired
results/output.
• The database joins has the ability of combining two or more data
tables/tuples into a single table/table only if the following
conditions are satisfied.
• There must be a common attribute in both(tables which are
participating) tables.
• Join condition must be satisfied.

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

3. Binary Relational Operations: JOIN and DIVISION


• Database joins can be broadly classified into two categories which are
further categorized into sub categories. All of them are explained below.

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

3. Binary Relational Operations: JOIN and DIVISION

Database Joins : The Inner Joins

• The idea behind inner join is very simple. When inner


join is applied to tuples or tables, only those tuples of
the table are kept which have common attribute in all
the tables.
• Other tuples which are not common are dropped from
the resulting tuple/table.
• To implement this concept two possible inner joins are
available i.e. Theta Join & Natural Join.

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

3. Binary Relational Operations: JOIN and DIVISION


1. Theta Join
If a condition is satisfied by the participating tables from different
relations, then the tuples are combined together using Theta Join.
Theta join is denoted through “Theta(Θ)”.

Syntax : R1(X1, X2,X3…Xn) ⋈(Condition “θ”) R2(Y1, Y2,Y3…Yn)


where, R1 and R2 are relations having (X1, X2,X3…Xn) and (Y1, Y2,Y3…Yn) as attributes
respectively.
For example : Consider the tables Student_Details and Student_Result.
Now, if we want to implement theta join on these relations, the result will
look like:

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

https://dbms-ctal.blogspot.com
Natural join (⋈)
2. Natural Join
Natural join does not supports any condition such as
theta join and works only if, one attribute or more than
one attributes are common between the
joining/participating relations.
Syntax : R1(X1, X2,X3…Xn) ⋈ R2(Y1, Y2,Y3…Yn)
where, R1 and R2 are relations having (X1, X2,X3…Xn) and (Y1, Y2,Y3…Yn) as attributes
respectively.
For example : Consider the tables Student_Details and Student_Result. Now, if we
want to implement natural join on these relations, the result will look like:
Natural join (⋈)
Database Joins : The Outer Joins

• Outer join overcomes the inability of inner joins of


dropping the tuples which are uncommon among
participating relations. If we want to display those
tuples which are not common, the concept of outer
join is used.
• Also, if all the tuples needs to be displayed from all
the participating relations, outer joins can be used.
They are of three types :
1. Left Outer Join,
2. Right Outer Join &
3. Full Outer Join.
Left outer join (⟕)
Right outer join (⟖)
Full outer join (⟗)
The Relational Algebra and Relational Calculus Unit 5

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

4. Additional Relational Operations

1. Generalized projection,
2. aggregate functions,
3. NULL

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

Generalized Projection
• It extends the projection operation by allowing
arithmetic functions to be used in the projection list.
• It allows us to write arithmetic operations containing
attribute names and constrants in Projection list.
Denoted by
П F1, F2, . . ., Fn(E)
Where,
E – relational-algebra expression
F1, F2, . . ., Fn – arithmetic expressions involving
constants and attributes in the schema of E

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

Generalized Projection

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

Aggregate Functions
In some case, we would like to apply aggregate function
not only to the single set of tuples but to several groups,
where each group is set of tuples. It can be done by
using an operation called Grouping.

Denoted by aggregation operator “calligraphic G”- Ç


G1, G2, . . ., Gn ÇF1 A1, F2 A2, . . ., Fm Am ( E )
Where,
E – relational-algebra expression
G1, G2, …, Gn – list of attributes on which to group
Fi – an aggregate function
Ai – an attribute name
https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

Aggregate Functions
It take collection of values and return a single value as a
result.

List of aggregate functions


SUM – returns the sum of values
AVG – returns the average of values
COUNT – returns no. of elements in the collection
MIN – returns the minimum value in the collection
MAX - returns the maximum value in the collection

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

NULL Values

• Null represents value for an attribute that Is


currently unknown or not applicable for tuple.
• Represents the absence of a value. Not same As zero
/ spaces, which are values.

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

Relational Calculus

In contrast to Relational Algebra, Relational Calculus is a


non-procedural query language, that is, it tells what to do
but never explains how to do it. (true or false)

Relational calculus exists in two forms −

1. Tuple Relational Calculus (TRC)


2. Domain Relational Calculus (DRC)

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

Tuple Relational Calculus (TRC)


• It is used for selecting those tuples that satisfy the given condition.
• Filtering tuples based on the given condition.
{t | P(t) } or {t | condition(t)}
t = result tuples
P(t) it condition used to fetch it.

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

Domain Relational Calculus (DRC)


In DRC, the filtering variable uses the domain of
attributes instead of entire tuple values.
Notation −
{ a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}
Where a1, a2 are attributes and P stands for formulae
built by inner attributes.

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

https://dbms-ctal.blogspot.com
The Relational Algebra and Relational Calculus Unit 5

https://dbms-ctal.blogspot.com

You might also like