Chapter 6
Chapter 6
MANIPULATION USING
RELATIONAL ALGEBRA
DATA MANIPULATION USING
RELATIONAL ALGEBRA
Relational algebra is a suite of operations on relations. A sequence of
relational algebra operations forms a relational algebra expression (algebraic
expression) that also results in a relation.
Each operation takes a relation (or two relations) as input and produces a
relation as output. (In arithmetic, operations take numbers and produce
numbers. Relational algebra is arithmetic for relations.)
Note that the term ‘relation’ is known as ‘table’ in relational database
environment or ‘set’ in a pure relational algebra environment.
RELATIONAL ALGEBRA
Relational algebra is the basis of the implementation of most
current database systems.
RELATIONAL ALGEBRA
EXAMPLE STUDENT DATABASE
Student Takes Subject
Id Name Suburb SID SNO No Name Dept
1108 Robert Kew 1108 21 21 Systems CSCE
3936 Glen Bundoora 1108 23 23 Database CSCE
8507 Norman Bundoora 8507 23 29 VB CSCE
8452 Mary Balwyn 8507 29 18 Algebra Maths
Enroll Course
SID Course No Name Dept • Students have names, ids, and suburbs,
3936 101 113 BCS CSCE enrol in courses, and take subjects.
1108 113 101 MCS CSCE • Subjects have names and departments.
8507 101 • Courses are offered by departments.
DATA MANIPULATION USING
RELATIONAL ALGEBRA
Relational Algebra Operations
PROJECTION
SELECTION
UNION
INTERSECTION
DIFFERENCE
PRODUCT
JOIN
DIVISION
1. PROJECTION -
An operation that selects specific attributes from a relation. Project chooses some
attributes.
Example query: find the names of all students.
In relational algebra:
Attribute (Relation)
The symbol p represents projection or choose attributes.
In the general case, projection is p attributes (R) where R is a relation and the
attributes are some of the attributes of R.
C1
C1 C2
C2 C3
C3 C4
C4 C5
C5 C2
C2 C5
C5
R1
R1
R2 R2
R2
R2
R3 R3
R3
R3
R4
R4
Problem:
Output:
No Name Dept
21 Systems CSCE
23 Database CSCE
29 VB CSCE
2. SELECTION – EXAMPLE 2A
Student
Given: Id Name Suburb
1108 Robert Kew
3936 Glen Bundoora
8507 Norman Bundoora
8452 Mary Balwyn
Name
Glen
Norman
2. SELECTION – EXAMPLE 2A
Student
Given: Id Name Suburb
1108 Robert Kew
3936 Glen Bundoora
8507 Norman Bundoora
8452 Mary Balwyn
R1
R1 R2
R2
R1
R1
R3
R2
R2
3. UNION – EXAMPLE 1
SubjectCode
SubjectCode CSE21DB
SubjectCode U CSE30PRJ CSE21SDT
CSE21DB (UNION ) CSE31DB CSE30PRJ
CSE21SDT CSE31DB
CSE42ADB
CSE42SPM CSE42ADB
CSE42SPM
3. UNION – EXAMPLE 2
Takes
Given:
SID SNO
1108 21
1108 23
8507 23
8507 29
Problem: What are the SIDs of students taking subject 21 or subject 29?
With intermediate or temporary relations:
SID
Query: R1 p SID (s SNO=21 (Takes) Output:
1108
SID
1108
R3 R1 R2
8507
4. INTERSECTION -
The intersection of two relations ( R1 and R2 )is a third relation containing common
tuples.
If two relations have the same set of attributes, they can be compared to see which
rows they have in common.
R1 and R2 have to be union-compatible, that is, they have the same set of attributes
R3 = R1 R2
R3
R3
R1
R1 R2
R2 R1
R1
R2
R2
4. INTERSECTION – EXAMPLE 1
Problem: What are the SIDs of students taking subject 21 and subject 23?
With intermediate or temporary relations:
SID
Query: R1 p SID (s SNO=21 (Takes) Output:
1108
SID
R2 p SID (s SNO=23 (Takes) 1108
8507
SID
R3 R1 R2 1108
5. DIFFERENCE - -
The difference of two relations ( R1 and R2 ) is a third relation containing tuples
which occur in the first relation but not in the second.
Both R1 and R2 must be union-compatible.
R3 = R1 - R2
R3
R1
R1 - R2
R2 R1
R1
R2
R2
5. DIFFERENCE – EXAMPLE
R3 = R1 X R2
aa xx
aa
aa yy
xx bb xx
bb
cc X yy bb yy
R3
cc xx
dd cc yy
dd xx
dd yy
6. CARTESIAN PRODUCT – EXAMPLE
Student
Given: Takes ID Name
SID SNO 1108 Robert
1108 21 3936 Glen
8507 23 8507 Norman
8452 Mary
R3 = R1 ⋈ <join condition> R2
c1
c1 c2
c2 c3
c3 c4
c4
aa rr rr xx
⋈c2=c3
aa rr xx
bb rr ss yy bb rr xx
cc vv tt zz
7. JOIN – EXAMPLE
Student Enroll
Given: Id Name Suburb
SID Course
1108 Robert Kew
3936 101
3936 Glen Bundoora
1108 113
8507 Norman Bundoora
8507 101
8452 Mary Balwyn
R1 ⋈
1108 113 Robert Kew
R2 SID=ID Student
Name
R p Name p Name (R2)
R3 (s Course=“113” (Enrol) ⋈ SID=ID Student) Robert
7. JOIN: OUTER JOIN –
While a (natural) join operation only gives matching tuples that satisfy the join
condition, the outer join operation will produce both matched and unmatched tuples
from either one or both of the participating relations in the operation.
Types:
Right Outer Join:
R3 = R1 <join condition> R2
keep unmatched tuples from right hand side relation
c1
c1 c2
c2 c3
c3 c4
c4
aa rr rr xx aa rr xx
bb rr ss yy bb rr xx
c2=c3
cc vv tt zz cc vv null
null
Problem: List all student IDs along with their enrolled course codes, make sure to
include also those students who haven’t enrolled in any course.
Query: R p ID, Course ( (Student) ID=SID Enrol) Output:
R
Student Enroll
ID Name Suburb ID Course
SID Course
1108 Robert Kew 1108 113
3936 101
3936 Glen Bundoora 3936 101
1108 113
8507 Norman Bundoora 8507 101
8507 101
8452 Mary Balwyn 8452 null
8. DIVISION – ÷,/
The division operator divides a dividend relation R1 of degree m+n by a divisor
relation R2 of degree n, and produces a quotient relation of degree m.
Attributes of R2 is proper subset of Attributes of R1.
The relation returned by division operator will return those tuples from relation A
which are associated to every B’s tuple.
R3 = R1 / R2
aa xx
aa yy xx
aa
bb
zz
xx
/ zz aa
cc yy
8. DIVISION – EXAMPLE