0% found this document useful (0 votes)
18 views29 pages

Chapter 6

Uploaded by

battal2023513
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views29 pages

Chapter 6

Uploaded by

battal2023513
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

CHAPTER 6 : DATA

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

Relational Algebra :  <attribute list> R


1. PROJECTION – EXAMPLE 1
Subject
 Given: No Name Dept
21 Systems CSCE
23 Database CSCE
29 VB CSCE
28 Algebra CSCE
18 Algebra Maths
 Problems:

Query: p Name (Subject) Query: p Name,Dept (Subject)


Output: Output: Name Dept
Name
Systems CSCE
Systems
Database CSCE
Database
VB CSCE
VB
Algebra CSCE
Algebra
Algebra Maths
2. SELECTION - 
 Where the project operator takes a vertical subset (columns) of a relation (R), the
selection takes a horizontal subset (rows).
 The selection operation means: choose from R each tuple where the condition
holds.
 <selection condition> Relation

R1
R1
R2 R2
R2
R2
R3 R3
R3
R3
R4
R4

Relational Algebra :  <selection condition> R


2. SELECTION – EXAMPLE 1
Subject
 Given:
No Name Dept
21 Systems CSCE
23 Database CSCE
29 VB CSCE
18 Algebra Maths

 Problem:

Query: s Dept=“CSCE” (Subject)

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

 Problem: What are the names of students who live in Bundoora?

Query: p Name (s Suburb=”Bundoora” (Student))


Output:

Name
Glen
Norman
2. SELECTION – EXAMPLE 2A
Student
 Given: Id Name Suburb
1108 Robert Kew
3936 Glen Bundoora
8507 Norman Bundoora
8452 Mary Balwyn

 Problem: Which students live in Bundoora?


With intermediate or temporary relations:

R1 s Suburb=“Bundoora” (Student) 3936


Id Name
Glen
Suburb
Bundoora
8507 Norman Bundoora
R2 p Name (R1) Name
Glen
Norman
3. UNION - 
 The union of two relations is formed by combining the tuples from one relation
(R1) with those of a second relation (R2) to produce a third relation (R3).
 If two relations have the same set of attributes (that is, if they are about the same
kinds of information), they can be added together.
 R1 and R2 have to be union-compatible, that is, they have the same set of attributes
 R3 = R1 U R2

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

p SID (s SNO=29 (Takes)


SID
R2 8507

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

SubjectCode SubjectCode SubjectCode


CSE21DB  CSE30PRJ CSE21DB
CSE21SDT (INTERSECT ) CSE31DB CSE30PRJ
CSE30PRJ CSE21DB
CSE42ADB
4. INTERSECTION– EXAMPLE 2
Takes
 Given:
SID SNO
1108 21
1108 23
8507 23
8507 29

 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

SubjectCode SubjectCode SubjectCode


CSE21DB - CSE30PRJ CSE21DB
CSE21SDT (DIFFERNCE ) CSE31DB CSE21SDT
CSE30PRJ CSE21ADC
CSE42ADB
6. CARTESIAN PRODUCT - X
 The product of two relations (R1 and R2) is the concatenation of every tuple of one
relation with every tuple of the other relation.

 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

 Problem: Takes X Student?

SID SNO ID Name


1108 21 1108 Robert
ID Name 1108 21 3936 Glen
SID SNO 1108 Robert 1108 21 8507 Norman
1108 21 3936 Glen 1108 21 8452 Mary
X
8507 23 8507 Norman 8507 23 1108 Robert
8452 Mary 8507 23 3936 Glen
8507 23 8507 Norman
8507 23 8452 Mary
7. JOIN – ⋈
 The join operation is the combination of the product selection and projection
operations.
 Join operation combines related tuples from different relations, if and only if a
given join condition is satisfied
 Natural Join

 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

 Problem: Which students are taking course 113?


Query: R1 s Course=“113” (Enroll) Output:
SID
1108
Course
113

SID Course Name Suburb

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

 Left Outer Join


 R3 = R1 <join condition> R2
 keep unmatched tuples from left hand side relation

 Full Outer Join


 R3 = R1 <join condition> R2
 keep unmatched tuples from both sides
7. JOIN: OUTER JOIN – EXAMPLE 1

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

• LEFT OUTER JOIN:


• keeping unmatched
tuples from the left
hand side relation
7. JOIN: OUTER JOIN – EXAMPLE 2
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

 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

Subject Code Location Subject Code


CSE31DB BG114 / CSE31DB Location
CSE41CNN BG114 CSE41CNN BG114
CSE41ADB BG212
CSE41DIS PS1201
RELATIONAL DATABASE
Next
Next
Lecture
Lecture ....
..
LANGUAGE - SQL

Reading : Chapter 8, Elmasri & Navathe

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