Class 9 Relational Algebra
Class 9 Relational Algebra
Prof.G.R.Karpagam
PSG-CSE
Relational query languages
• Relational query languages are languages for describing queries on a relational
database. Introduced by Edgar 'Ted' Codd of IBM Research in 1970. Concept of
mathematical relation as the underlying basis. The standard database model for
most transactional databases today.
• Three variants:
– Relational Algebra
– Relational Calculus
– SQL
• Turing Machine is a mathematical model of a hypothetical computing machine A Turing Complete system means a
system in which a program can be written that will find an answer although with no guarantees regarding runtime or
memory
What is Algebra?
• A language based on operators and a domain of values
• Operators map values taken from the domain into other
domain values
• Hence, an expression involving operators and arguments
produces a value in the domain
• Consider arithmetic operations +, - and * over integers.
– Algebra expressions: 2+3, (46–3)+3, (7*x)+(3*x)
• Relational algebra:
– Domain: the set of all relations
– Expression: referred to as a query
Relational Algebra
Relational Algebra Vs Relational Calculus
• Relational Algebra and Relational Calculus are the formal query languages for a relational model.
Both form the base for the SQL language which is used in most of the relational DBMSs.
• Procedural: In a procedural language, you define the whole process and provide the steps how to
do it. You just provide orders and define how the process will be served.
– Go to kitchen
– Get sugar, milk, and tea,
– Mix them, and heat over the fire till it boils
– Put that in a cup and bring it to me
• Declarative: In a declarative language, you just set the command or order, and let it be on the
system how to complete that order. You just need your result without digging into how it should be
done.
– Get me a cup of tea.
Basis for
Comparison Relational Algebra Relational Calculus
A B C D E
1 A 212 Y 2
2 C 45 N 84
3 B 8656 N 4
4 D 324 N 56
5 C 5656 Y 34
6 A 445 N 4
7 B 546 Y 55
Selection
SELECTION: selection-condition ( R )
Select from R all tuples that satisfy the selection-
condition
A B C D E
1 A 212 Y 2
2 C 45 N 84
3 B 8656 N 4
4 D 324 N 56
5 C 5656 Y 34
6 A 445 N 4
7 B 546 Y 55
Projection
PROJECT: attribute-list (R)
Examples:
ID (FACULY)
ID ( Name like ‘%gam’ FACULTY )
CrsCode, Textbook (COURSE)
PrereqCrsCode ( CrsCode=‘CSCI4380’ REQUIRES )
Unary Relational Operations: PROJECT
• PROJECT Operation
Slide 6- 23
Unary Relational Operations: PROJECT (contd.)
Slide 6- 25
Employee
Unary Relational Operations: SELECT and PROJECT
Slide 6- 27
Unary Relational Operations: RENAME
Slide 6- 28
Rename
• Rename all the attributes in the relation
DEP5_EMPS DNO=5(EMPLOYEE)
tables
INTERSECTION DIFFERENCE
UNION
Set Operators
• Set operators connect two relations by set
operations.
• However, for a set operation R1 op R2 to be valid, R1
and R2 should be union-compatible, that is
– R1, R2 have the same number of columns
– The names of the attributes are the same in both relations
– Attributes with the same name in both relations have the
same domain
Set Operators
• Given two relations R1, R2 that are union-
compatible, we have that
R1 R2 returns the set of tuples that are in R1 or R2.
[UNION]
∆ R1 R2 returns the set of tuples that are both in R1 and
R2. [INTERSECTION]
∆ R1 - R2 returns the set of tuples that are in R1, but not in
R2. [SET DIFFERENCE]
∆ Note that set difference is the only negative operator, i.e.
not in R2.
Example Database
STUDENT(Id, Name, Password, Address)
FACULTY(Id, Name, DeptId, Password, Address)
COURSE(CrsCode, DeptId, CrsName, CreditHours)
REQUIRES(CrsCode, PrereqCrsCode, EnforcedSince)
CLASS(CrsCode, SectionNo, Semester, Year,Textbook, ClassTime,
Enrollment, MaxEnrollment, ClassroomId, InstructorId)
CLASSROOM(ClassroomId, Seats)
TRANSCRIPT(StudId, CrsCode, SectionNo, Semester, Year, Grade)
STUDENT(Id, Name, Password, Address)
FACULTY(Id, Name, DeptId, Password, Address)
Set Operators
Name (FACULY) Name (STUDENT)
Address (FACULY) Address (STUDENT)
CrsCode (CLASS) - CrsCode (TRANSCRIPT)
Example:
Temp:= InstructorId (Year=2020 AND Semester=‘odd’ COURSE)
InstructorId (Year=2020 AND Semester=‘even’ COURSE)
‘All faculty who taught courses both in ‘odd’ and ‘even’ 2020.’
Id (FACULTY) - Temp[Id]
All faculty who did not teach courses both in ‘odd’ and ‘even’
2020.’’
Relational Algebra Operations from
Set Theory: UNION
• UNION Operation
– The result of this operation, denoted by R S, is a
relation that includes all tuples that are either in R
or in S or in both R and S. Duplicate tuples are
eliminated.
Slide 6- 38
Relational Algebra Operations from
Set Theory: UNION
• Example:
– To retrieve the social security numbers of all employees who either
work in department 5 or directly supervise an employee who
works in department 5
– We can use the union operation as follows:
DEP5_EMPS DNO=5 (EMPLOYEE)
RESULT1 SSN(DEP5_EMPS)
RESULT2(SSN) SUPERSSN(DEP5_EMPS)
RESULT RESULT1 RESULT2
– The union operation produces the tuples that are in either RESULT1
or RESULT2 or both.
– The two operands must be “type compatible”.
Slide 6- 39
Relational Algebra Operations from
Set Theory
• Type Compatibility
– The operand relations R1(A1, A2, ..., An) and R2(B1,
B2, ..., Bn) must have the same number of
attributes, and the domains of corresponding
attributes must be compatible; that is,
dom(Ai)=dom(Bi) for i=1, 2, ..., n.
– The resulting relation for
• R1R2, R1R2, or R1-R2
– has the same attribute names as the first operand
relation R1 (by convention).
Slide 6- 40
Relational Algebra Operations from
Set Theory: UNION (contd.)
• UNION Example
STUDENTINSTRUCTOR
Slide 6- 41
Relational Algebra Operations from Set Theory
UNION (contd.) – use Fig. 6.4
Slide 6- 42
Intersection
k x y A Intersect B
1 A 2
2 B 4
Table A k x y
3 C 6 1 A 2
4 D 8
5 E 10
k x y Table B
1 A 2
Relational Algebra Operations from Set Theory:
INTERSECTION
• The result of this operation, denoted by R S, is a
relation that includes all tuples that are in both R and S.
• The two operands must be “type compatible”
– Example: The result of the intersection operation (figure
below) includes only those who are both students and
instructors.
STUDENT INSTRUCTOR
Slide 6- 44
MINUS
The difference of two relational tables is a third that contains those
rows that occur in the first table but not in the second. The Difference
operation requires that the tables be union compatible.
The notation for difference is A MINUS B or A-B. As with arithmetic, the
order of subtraction matters. That is, A - B is not the same as B - A.
k x y k x y
1 A 2 Table A A MINUS B 2 B 4
2 B 4 3 C 6
3 C 6
k x y k x y
B MINUS A
1 A 2 4 D 8
Table B
4 D 8 5 E 10
5 E 10
Relational Algebra Operations from Set Theory:
DIFFERENCE (cont.)
• Set Difference (or MINUS) Operation
• The result of this operation, denoted by R - S, is a
relation that includes all tuples that are in R but
not in S. The two operands must be "type
compatible”.
– Example: The figure shows the names of students
who are not instructors, and the names of instructors
who are not students.
STUDENT-INSTRUCTOR INSTRUCTOR-STUDENT
Slide 6- 46
Relational Algebra Operations from Set Theory
Slide 6- 47
Cartesian Product
• Given two sets, A={a1,a2,a3}, B={b1,b2}, the Cartesian
Product (A B) returns the set of tuples
A B={(a1,b1), (a1,b2), (a2,b1), (a2,b2), (a3,b1), (a3,b2)}