Relational Algebra

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

CS-2005 Database Systems

Lecture 13
Relational Algebra
• It is a procedural query language which uses collection of ‘OPERATORS’
to compose the queries.

• Programmer needs to mention to thing in order to write relational algebra


query
• What to do?
• How to do?

• Each relational algebra query describe a step by step procedure for


computing the desired answer.
Cont..
• Relational algebra is the mathematical formalization of what happens in
relational databases.

• Relational algebra is a set of operations that enable the user to specify


basic retrieval requests. Each operation produces results which is a
relation.

• Relational algebra expression is a sequence of relational algebra


operations whose result will also be a relation.
Relation 1 Relation 2

Operation on
1&2

Relation 4
Relation 3

Operation on
3&4

Relation 5
Why is it still useful?

• Prior to relational databases, there were various


kinds of databases (hierarchical, among others),
but there was little regularity among them.

• The formalization of relational databases led to a


big step forward in systematizing database
technology.

• it gives us a standard internal representation of a


query that allows for query optimization.
Types of Operations
• There are two groups of relational algebra operations:
• Operations developed specifically for relational database, such as SELECT,
PROJECT, and JOIN. [Operations for relational databases]
• Operations from mathematical set theory, such as UNION, SET DIFFERENCE,
INTERSECTION, CARTESIAN PRODUCT, and DIVISION. [Operations for
mathematical theory]
Fundamental operations of Relational Algebra

• SELECT (symbol: σ)
Unary Relational Operations (applied on single table)
• PROJECT (symbol: π)
• RENAME (symbol: ρ)
• UNION (υ)
• INTERSECTION ( ), Relational Algebra Operations From Set Theory
• DIFFERENCE (-)
• CARTESIAN PRODUCT ( x )
• JOIN
Binary Relational Operations
• DIVISION
1. Select Operation:
• The select operation selects tuples that satisfy a given predicate.
• It is denoted by sigma (σ).
• It provide rows as a results

• Where:
• σ is used for selection prediction
• r is used for relation
• p is used as a propositional logic formula which may use connectors like: AND OR
and NOT. These relational can use as relational operators like =, ≠, ≥, <, >, ≤.
<attribute name> <comparison operator> <constant value> or <attribute name>
Example
Student

Select all the student of Team A :

σ Team = 'A' (Student)

select * from Student WHERE Team=‘A’


Example
Select all the students whose fees is greater
Student then equal to 10000 and belongs to Team
other than A.

σ Fees >= 10000(σTeam != 'A' (Student))


Selects tuples from books where subject is 'database'.

Selects tuples from books where subject is 'database' and 'price' is 450.

Selects tuples from books where subject is 'database' and 'price' is 450 or those books published
after 2010.
Project
• It projects column(s) that satisfy a given predicate.
• Duplicate rows are automatically eliminated, as relation is a set.
• (π) symbol is used to choose attributes from a relation.
• This operator helps you to keep specific columns from a relation and
discards the other columns.

π A (R)
 symbol ‘π(pi)’ is used to denote the Project operator
 where ‘A’ is the attribute list, it is the desired set of attributes from the attributes of
relation(R),
 R is generally a relational algebra expression, which results in a relation.
Example Display Class and Dept from
Faculty

Faculty

In SQL, SELECT DISTINCT query is exactly as same as


PROJECT here.
select class, dept from Faculty
Example Project Position from Faculty

Faculty

all the duplicate tuples are removed from the relation


Example Project Class from Faculty –

Faculty

all the duplicate tuples are removed from the relation


Display the name of customer and status from the customer table
Example
Student

Select name of the student of Team A :

σ Team = 'A' (Student)


Example
Student

Select name of the student of Team A :

Πname σ Team = 'A' (Student)


RENAME (ρ)

• The RENAME operation is used to rename the output of a relation.


• The rename operation denoted by the ρ is used to rename the given
relation to another name given.
• Rename is a unary operation used for renaming attributes of a relation.
• Reasons to rename a relation can be many, like –
• We may want to save the result of a relational algebra expression as a relation so that
we can use it later.
• We may want to join a relation with itself, in that case, it becomes too confusing to
specify which one of the tables we are talking about, in that case, we rename one of
the tables and perform join operations on them.

ρnewname (tablename)
Student

The student table is renamed with newstudent with the help of the following command −

ρnewstudent (student)
The name, branch column of student table are renamed and newbranch respectively

ρnewname,newbranch(∏name,branch( student))
Example-1: Query to rename the relation Student as Male Student and the attributes of
Student – Roll, Name as (Sno, SName).

ρ MaleStudent(Sno, SName) πRoll,Name(σCondition(Student))


Example
Display the first name an last name of
employee which belongs to department
no 30
Final Output
Cross Product
• Cartesian Product in DBMS is an operation used to merge columns from
two relations.
• Generally, a cartesian product is never a meaningful operation when it
performs alone.
• However, it becomes meaningful when it is followed by other operations.
• It is also called Cross Product or Cross Join.
• It is denoted by X.
EMPLOYEE DEPARTMENT

#Cols = M+N
#Rows = M*N

select * from Employee, Department


Cont..
• In SQL, CARTESIAN PRODUCT(CROSS PRODUCT) can be applied
using CROSS JOIN.

• In general, we don’t use cartesian Product unnecessarily, which means


without proper meaning we don’t use Cartesian Product.

• Generally, we use Cartesian Product followed by a Selection operation and


comparison on the operators as shown below :
STUDENT X SUB_REGD
RA: σage>15 (student X sub_regd)

SQL: SELECT * FROM student, sub_regd WHERE age>15;


Set Difference (-)
• The result of A – B, is a relation which includes all tuples that are in A but
not in B.

 The attribute name of A has to match with the attribute name in B.


 The two-operand relations A and B should be either compatible or Union compatible.
 It should be defined relation consisting of the tuples that are in relation A, but not in B.
Find the regno of students who plays indoor sports
but not outdoor sports.

RA: (Π regno (stu_indoor)) - (Π regno (stu_outdoor))


SQL: (SELECT regno FROM stu_indoor) MINUS (SELECT
regno FROM stu_outdoor);
BORROW RELATION DEPOSITOR RELATION

select * from BORROW except select * from DEPOSITOR


Example

Provides the name of authors who have written books but not articles.
Union Operation
• The union operation contains all the tuples that are either in R
or S or both in R & S.
• It eliminates the duplicate tuples. It is denoted by ∪.
• A union operation must hold the following condition:
• R and S must have the attribute of the same number.
• Duplicate tuples are eliminated automatically.
Example
• Projects the names of the authors who have either written a
book or an article or both.
BORROW RELATION DEPOSITOR
RELATION

select * from R union select * from S


Intersection
• Suppose there are two tuples R and S. The set intersection
operation contains all tuples that are in both R & S.
• It is denoted by intersection ∩.
BORROW RELATION DEPOSITOR RELATION

select * from R intersect select * from S


Division
• The division operator is used when we have to evaluate queries
which contain the keyword ALL, at All, Every.
• Which person has account in all the banks of a particular city?
• Which students have taken all the courses required to graduate?
• R1 ÷ R2 = tuples of R1 associated with all tuples of R2.

A(X,Y)/ B(Y)
Finds records in R1 that is that is related to all related to all records in records in R2
– Related is measured based on attributes with same name.
Division Operator (÷)
• Division operator can be applied to 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 have which are associated to every B’s tuple.
Example of Division A/B

A/B1

S1->P2
S1->P4
S4->P2
S4->P4
StudentEnrolled
Sid Cid Course
S1 C1 Cid

S2 C1 C1

S1 C2 C2

S3 C2

Query: Retrieve Sid of student who enrolled in every course

StudentEnrolled (Sid, Cid) / Course(Cid) = S2


Query: What are those employees who likes all hobbies

Student

Hobby
Expressing A/B using basic operators
• Division can be expressed using cross product, set-difference and
projection
- =
Join Operations
• Join operation is essentially a cartesian product followed by a
selection criterion.

• Join operation denoted by ⋈.

• JOIN operation also allows joining variously related tuples from


different relations.
Display the managers of each department
Result
Result
Types of JOIN
• Inner Joins:
Theta join
EQUI join
Natural join
• Outer join:
Left Outer Join
Right Outer Join
Full Outer Join
Theta join
• If we join R1 and R2 other than the equal to condition then it is
called theta join/ non-equi join.
• The general case of JOIN operation is called a Theta join.
• It is denoted by symbol θ
RXS
EQUI join
• When a theta join uses only equivalence condition, it becomes
a equi join.

• For example:
NATURAL JOIN (⋈)
• Natural join can only be performed if there is a common
attribute (column) between the relations.
• The name and type of the attribute must be same.

R1 ⋈ R2
∏EMP_NAME, SALARY (EMPLOYEE ⋈ SALARY)
Left outer join
• Left outer join contains the set of tuples of all combinations in A and B that
are equal on their common attribute names.
• In the left outer join, tuples in A have no matching tuples in B.
• It is denoted by ⟕.
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 ⟖.
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 ⟗.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy