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

Relational Algebra and Calculus

C104 S901 S901 Aditya 19 C106 S911 S911 Steve 18 C109 S921 S921 Paul 19 C110 S933 S944 Mike 20 • STUDENT ⟗COURSE Course_Id Student_Id Student_Name Student_Age C104 S901 Aditya 19 C106 S911 Steve 18 C109 S921 Paul 19 C110 S933 NULL NULL NULL S944 Mike 20 The document defines relational algebra and its operations. It describes select, project
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)
92 views

Relational Algebra and Calculus

C104 S901 S901 Aditya 19 C106 S911 S911 Steve 18 C109 S921 S921 Paul 19 C110 S933 S944 Mike 20 • STUDENT ⟗COURSE Course_Id Student_Id Student_Name Student_Age C104 S901 Aditya 19 C106 S911 Steve 18 C109 S921 Paul 19 C110 S933 NULL NULL NULL S944 Mike 20 The document defines relational algebra and its operations. It describes select, project
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/ 32

Relational Algebra and

Calculus
Francis Noel I. Alarcon
University of the East
Query Language: Definition

• In simple words, a Language which is used to store and retrieve


data from database is known as query language.
Relational Algebra: Definition

• It is a procedural query language that works on relational model.


• procedural query language means that it tells what data to be retrieved
and how to be retrieved.
Relational Algebra: Types of operations
1. Basic Operations
• Select (σ)
• Project (∏)
• Union (∪)
• Set Difference (-)
• Cartesian product (X)
• Rename (ρ)
2. Derived Operations
• Natural Join (⋈)
• Left, Right, Full outer join (⟕, ⟖, ⟗)
• Intersection (∩)
• Division (÷)
Relational Algebra: Select (σ)

• Select Operator is denoted by sigma (σ) and it is used to find the


tuples (or rows) in a relation (or table) which satisfy the given
condition.
• Syntax
• σ Condition/Predicate(Relation/Table name)
Relational Algebra: Example
• Table CUSTOMER
Customer_Id Customer_Name Customer_City
C10100 Steve Agra
C10111 Raghu Agra
C10115 Chaitanya Noida
C10117 Ajeet Delhi
C10118 Carl Delhi

• σ Customer_City="Agra" (CUSTOMER)
Customer_Id Customer_Name Customer_City
C10100 Steve Agra
C10111 Raghu Agra
Relational Algebra: Project (∏)

• Project operator is denoted by ∏ symbol and it is used to select


desired columns (or attributes) from a table (or relation).
• Syntax
• ∏ column_name1, column_name2, ...., column_nameN(table_name)
Relational Algebra: Example
• Table CUSTOMER
Customer_Id Customer_Name Customer_City
C10100 Steve Agra
C10111 Raghu Agra
C10115 Chaitanya Noida
C10117 Ajeet Delhi
C10118 Carl Delhi

• ∏ Customer_Name, Customer_City (CUSTOMER)


Customer_Name Customer_City
Steve Agra
Raghu Agra
Chaitanya Noida
Ajeet Delhi
Carl Delhi
Relational Algebra: Union (∪)

• Union operator is denoted by ∪ symbol and it is used to select all


the rows (tuples) from two tables (relations).
• The rows (tuples) that are present in both the tables will only
appear once in the union set.
• Syntax
• table_name1 ∪ table_name2
Relational Algebra: Example
• Table COURSE Table STUDENT
Course_Id Student_Name Student_Id Student_Id Student_Name Student_Age
C101 Aditya S901 S901 Aditya 19
C104 Aditya S901 S911 Steve 18
C106 Steve S911 S921 Paul 19
C109 Paul S921 S941 Carl 16

• ∏C115
Student_Name
Lucy (COURSE)
S931 ∪ ∏ Student_Name
S951 (STUDENT)
Rick 18

Student_Name
Aditya
Steve
Paul
Lucy
Carl
Rick
Relational Algebra: Set Difference (-)

• Set Difference is denoted by – symbol.


• Displays Tuples that are present in Table 1 BUT NOT present in
Table 2
• Syntax
• table_name1 - table_name2
Relational Algebra: Example
• Table COURSE Table STUDENT
Course_Id Student_Name Student_Id Student_Id Student_Name Student_Age
C101 Aditya S901 S901 Aditya 19
C104 Aditya S901 S911 Steve 18
C106 Steve S911 S921 Paul 19
C109 Paul S921 S941 Carl 16

• ∏C115
Student_Name
Lucy (STUDENT)
S931 - S951
∏ Student_Name (COURSE)
Rick 18

Student_Name
Carl
Rick
Relational Algebra: Cartesian product (X)

• Cartesian Product is denoted by X symbol.


• Displays Tuples that would combine each tuple of the first table
with each tuple of second table.
• R1 X R2
Relational Algebra: Example
• Table R Table S
Col_A Col_B Col_X Col_Y
AA 100 XX 50
AB 200 XY 25

• RXS
Col_A Col_B Col_X Col_Y
AA 100 XX 50
AA 100 XY 25
AB 200 XX 50
AB 200 XY 25
Relational Algebra: Rename (ρ)

• Rename (ρ) operation can be used to rename a relation or an attribute of a


relation.
• Syntax
• ρ(new_relation_name, old_relation_name)
Relational Algebra: Example
• Table CUSTOMER
Customer_Id Customer_Name Customer_City
C10100 Steve Agra
C10111 Raghu Agra
C10115 Chaitanya Noida
C10117 Ajeet Delhi
C10118 Carl Delhi

• ρ(CUST_NAMES, ∏(Customer_Name)(CUSTOMER))
CUST_NAMES
Steve
Raghu
Chaitanya
Ajeet
Carl
Relational Algebra: Natural Join (⋈)

• is the set of tuples of all combinations in R and S that are equal on


their common attribute names. It is denoted by ⋈.
• Syntax
• R⋈S
Relational Algebra: Example
• Table Course Table Student
Course_Id Student_Id Student_Id Student_Name Student_Age
C104 S901 S901 Aditya 19
C106 S911 S911 Steve 18
C109 S921 S921 Paul 19

• STUDENT ⋈ COURSE
Course_Id Student_Id Student_Name Student_Age
C104 S901 Aditya 19
C106 S911 Steve 18
C109 S921 Paul 19
Relational Algebra: Left outer join (⟕)

• Left outer join contains the set of tuples of all combinations in R


and S that are equal on their common attribute names.
• In the left outer join, tuples in R have no matching tuples in S.
• It is denoted by ⟕.
Relational Algebra: Example
• Table Course
Course_Id
Table Student
Student_Id
Student_Id Student_Name Student_Age
C104 S901 S901 Aditya 19
C106 S911 S911 Steve 18
C109 S921 S921 Paul 19
C110 S933 S944 Mike 20

• STUDENT ⟕COURSE
Course_Id Student_Id Student_Name Student_Age
C104 S901 Aditya 19
C106 S911 Steve 18
C109 S921 Paul 19
C110 S933 NULL NULL
Relational Algebra: Right outer join (⟖)

• Right outer join contains the set of tuples of all combinations in R and S that
are equal on their common attribute names.
• In right outer join, tuples in S have no matching tuples in R.
• It is denoted by ⟖.
Relational Algebra: Example
• Table Course
Course_Id
Table Student
Student_Id
Student_Id Student_Name Student_Age
C104 S901 S901 Aditya 19
C106 S911 S911 Steve 18
C109 S921 S921 Paul 19
C110 S933 S944 Mike 20

• STUDENT ⟖COURSE
Student_Id Course_Id Student_Name Student_Age
S901 C104 Aditya 19
S911 C106 Steve 18
S921 C109 Paul 19
S944 NULL Mike 20
Relational Algebra: Full outer join (⟗)

• Full outer join is like a left or right join except that it contains all rows from
both tables.
• In full outer join, tuples in R that have no matching tuples in S and tuples in S
that have no matching tuples in R in their common attribute name.
• It is denoted by ⟗.
Relational Algebra: Example
• Table Course
Course_Id
Table Student
Student_Id
Student_Id Student_Name Student_Age
C104 S901 S901 Aditya 19
C106 S911 S911 Steve 18
C109 S921 S921 Paul 19
C110 S933 S944 Mike 20

• STUDENT ⟖COURSE
Student_Id Course_Id Student_Name Student_Age
S901 C104 Aditya 19
S911 C106 Steve 18
S921 C109 Paul 19
S933 C110 NULL NULL
S944 NULL Mike 20
Relational Algebra: Intersection (∩)

• Intersection operator is denoted by ∩ symbol and it is used to


select common rows (tuples) from two tables (relations).
• Only those rows that are present in both the tables will appear in
the result set.
• Syntax
• table_name1 ∩ table_name2
Relational Algebra: Example
• Table Course Table Student
Course_Id Student_Name Student_Id Student_Id Student_Name Student_Age
C101 Aditya S901 S901 Aditya 19
C104 Aditya S901 S911 Steve 18
C106 Steve S911 S921 Paul 19
C109 Paul S921 S941 Carl 16

• ∏C115
Student_Name
Lucy (COURSE)
S931 ∩ ∏ Student_Name
S951 (STUDENT)
Rick 18

Student_Name
Aditya
Steve
Paul
Relational Algebra: Division (÷)

• 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.
Relational Calculus: Definition

• It is a non-procedural query language, which means it tells what data to be


retrieved but doesn’t tell how to retrieve it.
Relational Calculus: Types

1. Tuple Relational Calculus (TRC)


2. Domain Relational Calculus (DRC)
Relational Calculus: Tuple Relational Calculus (TRC)

• Tuple relational calculus is used for selecting those tuples that


satisfy the given condition.
• Notation
• {T | P (T)} or {T | Condition (T)}

• T is the resulting tuples


• P(T) is the condition used to fetch T.
Relational Algebra: Example
• Table Students
Student_Id Student_Name Age
S901 Aditya 19
S911 Steve 18
S921 Paul 20
S931 Lucy 16

• { t. Std_Name | Student(t) AND t.Age < 17 }


Std_Name
Lucy
Relational Calculus: Domain Relational Calculus (DRC)

• In domain relational calculus the records are filtered based on the domains.
• Notation
• { a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}

• a1, a2 are attributes


• P stands for formula built by inner attributes
• The symbols used for logical operators are: ∧ for AND, ∨ for OR and ┓ for NOT

You might also like