Nothing Special   »   [go: up one dir, main page]

Relational Algebra

Download as pdf or txt
Download as pdf or txt
You are on page 1of 45

Database Management Systems (BCSC-0003)

Topic: Relational Algebra in DBMS


Nikhil Govil
Assistant Professor, Dept. of CEA, GLA University, Mathura.
Relational Algebra

• Relational algebra is a widely used procedural query language.

• It takes instances of relations as input and yields instances of


relations as output.

• It uses various operations to perform this action.

• Relational algebra mainly provides theoretical foundation for


relational databases and SQL.
Basic SQL Relational Algebra Operations

Relational Algebra may be divided in various groups:

1. Relational Algebra Operations From Set Theory


a. UNION ()
b. INTERSECTION ()
c. DIFFERENCE (–)
d. CARTESIAN PRODUCT ()
Basic SQL Relational Algebra Operations

2. Unary Relational Operations


a. SELECT ()
b. PROJECT ()
c. RENAME ()
Basic SQL Relational Algebra Operations
3. Binary Relational Operations
a. JOIN (⋈)

Types of JOIN:
Various forms of join operation are:
A. Inner Joins:
(i). Theta join (ii). EQUI join (iii). Natural join

B. Outer join:
(i). Left Outer Join (ii). Right Outer Join
(iii). Full Outer Join

b. DIVISION ()
Relational Algebra Operations From Set
Theory

UNION ()

UNION is symbolized by  symbol. It includes all tuples that


are in tables A or in B. It also eliminates duplicate tuples. So,
set A UNION set B would be expressed as:

R =A B
Relational Algebra Operations From Set
Theory
UNION ()
Exp.
Relation A Relation B
CNAME CSTATUS CNAME CSTATUS
RAJAT GOOD KARAN POOR
RAHUL EXCELLENT RAJAT GOOD

CNAME CSTATUS
R=A B RAJAT GOOD
RAHUL EXCELLENT
KARAN POOR
Relational Algebra Operations From Set
Theory

INTERSECTION ()

INTERSECTION is symbolized by  symbol. Defines a


relation consisting of a set of all tuple that are in both A and B.
However, A and B must be union-compatible.

R =A B
Relational Algebra Operations From Set
Theory
INTERSECTION ()
Exp.
Relation A Relation B
CNAME CSTATUS CNAME CSTATUS
RAJAT GOOD KARAN POOR
RAHUL EXCELLENT RAJAT GOOD

R=A B CNAME CSTATUS


RAJAT GOOD
Relational Algebra Operations From Set
Theory

DIFFERENCE (–)

DIFFERENCE is symbolized by – symbol. The result of A–B,


is a relation which includes all tuples that are in A but not in B.

R=A–B
Relational Algebra Operations From Set
Theory
DIFFERENCE (–)
Exp.
Relation A Relation B
CNAME CSTATUS CNAME CSTATUS
RAJAT GOOD KARAN POOR
RAHUL EXCELLENT RAJAT GOOD

R=A– B CNAME CSTATUS


RAHUL EXCELLENT
Relational Algebra Operations From Set
Theory

CARTESIAN PRODUCT ()

CARTESIAN PRODUCT is symbolized by  symbol. It is an


operation used to merge columns from two relations. It is also
called Cross Product or Cross Join.

R =A B
Relational Algebra Operations From Set
Theory
CARTESIAN PRODUCT ()
Exp.
Relation A Relation B
NAME AGE JOB LOCATION
KAJAL 32 DEVELOPER CHENNAI
ANIL 40 ANALYST MUMBAI

NAME AGE JOB LOCATION

R=A B KAJAL 32 DEVELOPER CHENNAI


KAJAL 32 ANALYST MUMBAI
ANIL 40 DEVELOPER CHENNAI
ANIL 40 ANALYST MUMBAI
Unary Relational Operations

a. SELECT ()
b. PROJECT ()
c. RENAME ()
SELECT Operator ()
• SELECT operation is used for selecting a subset of the
tuples according to a given selection condition.

• Select Operator is denoted by sigma (σ).

• It is used as an expression to choose tuples which meet


the selection condition.

• Select operator selects tuples that satisfy a given


predicate.
SELECT Operator ()
Syntax:
σ <Condition> (Relation/Table name)

Properties:

1. It is like a WHERE clause in SQL.


2. Selection operator is commutative i.e.,

σ <condition_1> ( (σ <condition_2> (R)) = σ <condition_2> ( (σ <condition_1> (R))


SELECT Operator ()
Examples: STUDENT ROLL_NO NAME AGE COURSE
1 ABHISHEK 17 BTech
2 AMIT 16 BCA
3 AJEET 17 BTech
4 AKHIL 18 BTech
5 PRASHANT 17 BCA

Query 1: Find out the students of course ‘BTech’.


Syntax: σ course = ‘BTech’ (student)

Query2: Find the student(s) whose age is greater than 17.


Syntax: σ age > 17 (student)
PROJECT or PROJECTION Operator ()

• PROJECT Operator is denoted by pi ().

• Project operation selects certain attributes discarding


other attributes.

• It is also known as vertical partitioning since it partitions


the relation or table vertically.

• Duplicate rows are automatically eliminated, as relation is


a set.
PROJECT Operator ()
Syntax:
 <Attribute list> (Relation/Table name)

Properties:
1. The degree of output relation (number of columns present) is
equal to the number of attributes mentioned in the attribute list.
2. Projection operator does not obey commutative property i.e.
π <list2> (π <list1> (R)) ≠ π <list1> (π <list2> (R))
PROJECT Operator ()
Examples: STUDENT ROLL_NO NAME AGE COURSE
1 ABHISHEK 17 BTech
2 AMIT 16 BCA
3 AJEET 17 BTech
4 AKHIL 18 BTech
5 PRASHANT 17 BCA

Query 1: Find out the name and course from STUDENT relation.
Syntax:  name, course (student)

AGE
Query 2: Find the age from STUDENT relation.
17
Syntax:  age (student) 16
18
RENAME Operator ()

• The results of relational algebra are also relations but


without any name.

• The RENAME operation allows us to rename the relation.

• It is denoted by rho (ρ).


RENAME Operator ()
Syntax:
ρ (Relation2, Relation1)

Examples:
Query 1: Rename STUDENT relation to STUDENT1 relation.
Syntax: ρ (STUDENT1, STUDENT)

Query 2: Create a relation STUDENT_NAMES with RNO and


NAME from STUDENT.
Syntax: ρ (STUDENT_NAMES,  (RNO, NAME) (STUDENT))
Binary Relational Operations
Binary Relational Operations
JOIN (⋈) DIVISION ()

A. Inner Join:
(i). Theta join (ii). EQUI join (iii). Natural join

B. Outer Join:
(i). Left Outer Join (ii). Right Outer Join
(iii). Full Outer Join
Join(⋈)
• Join is a binary operation which allows us to combine join
product and selection in one single statement.

• The goal of creating a join condition is that it helps us to


combine the data from two or more DBMS tables.

• Various forms of join operation are: Inner Join & Outer Join.
Inner Join
• INNER JOIN is used to return rows from both tables which
satisfy the given condition.
• It is the most widely used join operation and can be
considered as a default join-type.
• Inner Join further divided into three subtypes:
(i). Theta join (ii). EQUI join (iii). Natural join
Inner Join: Theta Join
• THETA JOIN allows us to merge two tables based on the
condition represented by theta.
• Theta joins work for all comparison operators.
• It is denoted by symbol θ.
• The general case of JOIN operation is called a Theta join.

Syntax: A ⋈θ B
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 = Φ.
Inner Join: Theta Join
Example: STUDENT SUBJECT
SID NAME STD CLASS SUBJECT
1001 AJAY 11 11 MATH

1002 JATIN 12 11 HINDI


12 COMPUTER
12 SCIENCE
STUDENT ⋈Student.Std = Subject.Class SUBJECT

Output: SID NAME STD CLASS SUBJECT


1001 AJAY 11 11 MATH
1001 AJAY 11 11 HINDI
1002 JATIN 12 12 COMPUTER
1002 JATIN 12 12 SCIENCE
Inner Join: Equi Join
• EQUI JOIN is done when a Theta join uses only the
equivalence condition.

• When Theta join uses only equality comparison operator, it is


said to be equi join.

• The previous example corresponds to equi join.


Inner Join: Natural Join
• NATURAL JOIN does not utilize any of the comparison
operators.
• In this type of join, the attributes should have the same name
and domain.
• In Natural Join, there should be at least one common attribute
between two relations.
• It performs selection forming equality on those attributes
which appear in both relations and eliminates the duplicate
attributes.
Inner Join: Natural Join
Example: R1 R2
NUM SQUARE NUM CUBE
2 4 2 8
3 9 3 27

R1 ⋈ R2

Output: NUM SQUARE CUBE


2 4 8
3 9 27
Outer Join
• An OUTER JOIN doesn't require each record in the two join
tables to have a matching record.
• In this type of join, the table retains each record even if no
other matching record exists.
• Outer Join further divided into three subtypes:
(i). Left Outer Join
(ii). Right Outer Join
(iii). Full Outer Join
Outer Join: Left Outer Join (⟕)
• LEFT OUTER JOIN returns all the rows from the table on
the left even if no matching rows have been found in the table
on the right.
• When no matching record found in the table on the right,
NULL is returned.
• It is denoted by symbol (⟕).

Syntax: A⟕B
Outer Join: Left Outer Join (⟕)
Example: R1 R2
NUM SQUARE NUM CUBE
2 4 2 8
3 9 3 27
4 16 5 125

R1 ⟕ R2

Output: NUM SQUARE CUBE


2 4 8
3 9 27
4 16 -
Outer Join: Right Outer Join (⟖)
• RIGHT OUTER JOIN returns all the columns from the table
on the right even if no matching rows have been found in the
table on the left.
• Where no matches have been found in the table on the left,
NULL is returned. RIGHT OUTER JOIN is the opposite of
LEFT OUTER JOIN.
• It is denoted by symbol (⟖).

Syntax: A⟖B
Outer Join: Right Outer Join (⟖)
Example: R1 R2
NUM SQUARE NUM CUBE
2 4 2 8
3 9 3 27
4 16 5 125

R1 ⟖ R2

Output: NUM CUBE SQUARE


2 8 4
3 27 9
5 125 -
Outer Join: Full Outer Join (⟗)
• In a FULL OUTER JOIN , all tuples from both relations are
included in the result, irrespective of the matching condition.
• It is denoted by symbol (⟗).

Syntax: A⟗B
Outer Join: Full Outer Join (⟗)
Example: R1 R2
NUM SQUARE NUM CUBE
2 4 2 8
3 9 3 27
4 16 5 125

R1 ⟗ R2

Output: NUM SQUARE CUBE


2 4 8
3 9 27
4 16 -
5 - 125
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.

Syntax: AB
DIVISION ()
Relation B has 3 tables as:
Example: Pno Pno Pno
Relation A has 1 P1 P2 P1

table as: Sno Pno P4 P2


S1 P1 P3
S1 P2 P4
S1 P3
P5
S1 P4
P6
S1 P5
S1 P6
S2 P1
Then, A DIVIDE BY B gives the
S2 P2
following resultant tables for all the three
S3 P2
cases as follows:
S4 P2 Sno Sno Sno
S4 P4 S1 S1 S1
S4 P5 S2 S4
Exercise
Example 1: Consider the following schema:
SUPPLIER (Sid, S_name, S_addr)
PARTS (Pid, P_name, color)
CATALOG (sid, pid, cost)

Now, answer the following queries in Relational Algebra:


(a) Find the name of all the suppliers who supply Yellow parts.
(b) Find the name of suppliers who supply all parts.
(c) Find the name of suppliers who supply both Blue & Black parts.
Exercise
Solution:
(a) Find the name of all the suppliers who supply Yellow parts.
 name (SUPPLIER) ⋈ CATALOG ⋈ (σ color = ‘Yellow’ (PARTS))

(b) Find the name of suppliers who supply all parts.


S_name, color (SUPPLIER) ⋈ (CATALOG ⋈ PARTS) ÷  color (PARTS)

(c) Find the name of suppliers who supply both Blue & Black parts.
S_name, color (SUPPLIER) ⋈ (CATALOG ⋈ PARTS) ÷
 color (σ color = ‘Blue’ or color = ‘Black’ (PARTS))
Exercise
Example 2: Consider a database that has the relation schema CR
(StudentName, CourseName). An instance of the schema CR is
as given below.
The following queries are made on the database.

T1← πCourseName(σStudentName=‘SA’(CR))
T2← CR ÷ T1
The number of rows in T2 are ______.

(GATE 2017)
Exercise
Solution:
T1 will give: CourseName
CA
CB
CC

T2CR÷T1 = All the tuples in CR which are matched with every tuple in T1:
StudentName
SA
SC
SD
SF
Reference Books

1. Elmasri and Navathe (2010), “Fudamentals of Database


Systems”, 5th Edition, Addision Wesley.
2. Date C J,” An Introduction to Database Systems”, 8th
Edition, Addision Wesley.
3. Korth, Silbertz and Sudarshan (1998), “Database
Concepts”, 4th Edition, TMH.
4. M. Tamer Oezsu, Patrick Valduriez (2011). “Principles of
Distributed Database Systems”, 2nd Edition, Prentice
Hall.
Thank You !!!

You might also like