0% found this document useful (0 votes)
25 views15 pages

6ms1102 Ch6 Relational Algebra

Uploaded by

birungiderick887
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)
25 views15 pages

6ms1102 Ch6 Relational Algebra

Uploaded by

birungiderick887
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/ 15

BIT2103 - Database Management Systems

CHAPTER 6
RELATIONAL ALGEBRA
By: Mr.Mumbere Samuel – FICT.

© ISBAT UNIVERSITY – 2020. 12-Dec-22


Bachelor of Information Technology
Relation Algebra vs Relational calculus

Relational algebra is a conceptual procedural query language used on relational model.

Relational calculus is a conceptual non-procedural query language used on relational model

SQL is a practical implementation of relational algebra


and relational calculus

© ISBAT UNIVERSITY – 2020. 12-Dec-22


Relational Algebra, Calculus, RDBMS & SQL:

◻ Relational algebra and calculus are the theoretical concepts used on relational
model.
◻ RDBMS is a practical implementation of relational model.
◻ SQL is a practical implementation of relational algebra and calculus.

© ISBAT UNIVERSITY – 2020. 12-Dec-22


Relational Algebra

◻ Every database management system must define a query language to allow users to
access the data stored in the database. Relational Algebra is a procedural query
language used to query the database tables to access data in different ways
◻ In relational algebra, input is a relation(table from which data has to be accessed) and
output is also a relation(a temporary table holding the data asked for by the user).

Figure: 4.1

© ISBAT UNIVERSITY – 2020. 12-Dec-22


RA Operations

◻ The primary operations that we can perform using


relational algebra are:
1. Select
2. Project

3. Union

4. Set Different

5. Cartesian product

6. Rename

© ISBAT UNIVERSITY – 2020. 12-Dec-22


Symbols

Binary Operators
Unary Operators
Union ∪
Selection σ
Intersection ∩
Projection π
Difference —
Cartesian product ×

Natural Join ⋈
Logic Symbols Inner Join Using ⋈ a,b

Logical AND ∧ Inner Join On ⋈ a=b

Logical OR
Left Outer Join =⋈

Right Outer Join ⋈=
Logical NOT ¬ Full Outer Join =⋈=
© ISBAT UNIVERSITY – 2020. 12-Dec-22
Select Operation (σ)

◻ This is used to fetch rows(tuples) from table(relation) which


satisfies a given condition.

Syntax: σp(r)

Where, σ represents the Select Predicate, r is the name of


relation(table name in which you want to look for data), and p is
the prepositional logic, where we specify the conditions that must
be satisfied by the data. In prepositional logic, one can use unary
and binary operators like =, <, > etc, to specify the conditions.
© ISBAT UNIVERSITY – 2020. 12-Dec-22
Select Operation (σ)

◻ Let's take an example of the Student table Refer Figure: 4.1


we specified above in the Introduction of relational algebra, and
fetch data for students with age more than 17.
σage > 17 (Student)
This will fetch the tuples(rows) from table Student, for
which age will be greater than 17

You can also use, and, or etc operators, to specify two conditions,
for example,
σage > 17 and gender = 'Male' (Student)
© ISBAT UNIVERSITY – 2020. 12-Dec-22
Project Operation (∏)

Project operation is used to project only a certain set of


Project operator in relational algebra is similar to the Select statement in SQL

attributes of a relation
◻ Ex. If you want to see only the names all of the students in
the Student table, then you can use Project Operation
◻ It will only project or show the columns or attributes asked for,
and will also remove duplicate data from the columns
Syntax: ∏A1, A2...(r)
where A1, A2 etc are attribute names(column names).
For example,
∏Name, Age(Student)
Above statement will show us only the Name and Age columns for all the rows of data in Student table

© ISBAT UNIVERSITY – 2020. 12-Dec-22


Union Operation (∪)

◻ This operation is used to fetch data from two relations(tables) or


temporary relation(result of another operation).
◻ The rows (tuples) that are present in both the tables will only appear
once in the union set.
◻ Lets say we have two relations R1 and R2 both have same columns and
we want to select all the tuples(rows) from these relations then we can
apply the union operator on these relations

table_name1 ∪ table_name2

© ISBAT UNIVERSITY – 2020. 12-Dec-22


Intersection Operator (∩)

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


select common rows (tuples) from two tables (relations)
◻ Lets say we have two relations R1 and R2 both have same
columns and we want to select all those tuples(rows) that are
present in both the relations, then in that case we can apply
intersection operation on these two relations R1 ∩ R2.

© ISBAT UNIVERSITY – 2020. 12-Dec-22


Intersection Operator (∩)

Table 1: COURSE Table 2: STUDENT


Course_Id Student_Name Student_Id Student_Id Student_Name
--------- ------------ ---------- Student_Age
C101 Aditya S901 ------------ ---------- -----------
C104 Aditya S901 S901 Aditya 19
C106 Steve S911 S911 Steve 18
C109 Paul S921 S921 Paul 19
C115 Lucy S931 S931 Lucy 17
S941 Carl 16
S951 Rick 18
Query: ∏ Student_Name (COURSE) ∩ ∏ Student_Name (STUDENT)

Student_Name
------------
Output:
Aditya
Steve
Paul
Lucy
© ISBAT UNIVERSITY – 2020. 12-Dec-22
Set Difference (-)

◻ Set Difference is denoted by – symbol. Lets say we have two


relations R1 and R2 and we want to select all those
tuples(rows) that are present in Relation R1 but not present in
Relation R2, this can be done using Set difference R1 – R2

table_name1 - table_name2

Query: ∏ Student_Name (STUDENT) - ∏ Student_Name


(COURSE)

© ISBAT UNIVERSITY – 2020. 12-Dec-22


Cartesian product (X)

◻ Cartesian Product is denoted by X symbol. Lets say we have


two relations R1 and R2 then the cartesian product of these
two relations (R1 X R2) would combine each tuple of first
relation R1 with the each tuple of second relation R2.

R1 X R2

© ISBAT UNIVERSITY – 2020. 12-Dec-22


Rename (ρ)

◻ Rename (ρ) operation can be used to rename a relation or an


attribute of a relation

ρ(new_relation_name, old_relation_name)

Query: ρ(CUST_NAMES, ∏(Customer_Name)(CUSTOMER))

© ISBAT UNIVERSITY – 2020. 12-Dec-22

You might also like