IT-243-L13-14-2. pdf

NasirAli233814 4 views 22 slides Jun 24, 2024
Slide 1
Slide 1 of 22
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22

About This Presentation

DBMS


Slide Content

Database Systems
Lec# 13 &14

Topics
RelationalAlgebra:
•Join

Join
•Joininvolvingcrossproduct,selection,and(sometimes)projection.
•Aspecialformofcrossproductoftwotables
•TypesofJoin
1.ThetaJoin
2.EquiJoin
3.NaturalJoin
4.SemiJoin
5.InnerJoin
6.OuterJoin

Theta Join
Apply the condition through selecton one relation and then only
those rows are used in the cross product with the second relation
R1R2

ranksalarydeptfacNamefacId
F5678
F4567
F3456
F2345
32000
27000
23000
21000
ProfessorMNGSamad
Asso ProfENGAyesha
Asso ProfCSETahir
lecturerCSEUsman
COURSE
F2345Introduction to
Accounting
C3425
F4567Money & Capital
Market
C5678
Financial
Management
C4567
F2345Database SystemsC3456
fIdcrTitlecrCode
FACULTY
Theta Join Example

rank = ‘AssoProf’(FACULTY)) X COURSE
F2345

© Virtual University of Pakistan
Asso Prof27000ENGAyeshaF4567
Asso Prof27000ENGAyeshaF4567
Asso Prof27000ENGAyeshaF4567
Asso Prof27000ENGAyeshaF4567
Asso Prof23000CSETahirF3456
Asso Prof23000CSETahirF3456
Asso Prof23000CSETahirF3456
Asso Prof23000CSETahirF3456
RanksalarydeptfacNamefacId
F2345Introduction to
Accounting
C3425
F2345Database SystemsC3456
Financial
Management
C4567
F4567Money & Capital
Market
C5678
F2345Introduction to
Accounting
C3425
F4567Money & Capital
Market
C5678
Financial
Management
C4567
F2345Database SystemsC3456
fIdcrTitlecrCode

rank = ‘AssoProf’(FACULTY)) X COURSE

EquiJoin
•Rows are joined on the basis of values of a common attribute between the
two relations
•Rows having the same value in the common attribute are joined.
•Common attributes appears twice in the output
•Common attribute with the same name is qualified with the relation name
in the output

ranksalarydeptfacNamefacId
F5678
F4567
F3456
F2345
32000
27000
23000
21000
ProfessorMNGSamad
Asso ProfENGAyesha
Asso ProfCSETahir
lecturerCSEUsman
COURSE
F2345Introduction to
Accounting
C3425
F4567
Money & Capital
Market
C5678
F2345Database SystemsC3456
fIdcrTitlecrCode
FACULTY
Equijoin Example
FACULTY FACULTY.facId= COURSE.fIdCOURSE

lecturer21000CSEUsmanF2345
Asso Prof27000ENGAyeshaF4567
lecturer21000CSEUsmanF2345
RanksalarydeptfacNamefacId
F2345Introduction to
Accounting
C3425
F4567Money & Capital
Market
C5678
F2345Database SystemsC3456
fIdcrTitlecrCode
Equijoin Example
FACULTY FACULTY.facId= COURSE.fIdCOURSE

Natural Join
•Also called simply the join, most general form of join
•Same as equijoin with common column appearing once

Natural Join Example
FACULTY
FACULTY.facId, COURSE.facIdCOURSE
ranksalarydeptfacNamefacId
F5678
F4567
F3456
F2345
32000
27000
23000
21000
ProfessorMNGSamad
Asso ProfENGAyesha
Asso ProfCSETahir
lecturerCSEUsman
COURSE
F2345Introduction to
Accounting
C3425
F4567Money & Capital
Market
C5678
Financial
Management
C4567
F2345Database SystemsC3456
facIdcrTitlecrCode
FACULTY

lecturer21000CSEUsmanF2345
Asso Prof27000ENGAyeshaF4567
lecturer21000CSEUsmanF2345
RanksalarydeptfacNamefacId
Introduction to
Accounting
C3425
Money & Capital
Market
C5678
Database SystemsC3456
crTitlecrCode
Natural Join Example
FACULTY
FACULTY.facId, COURSE.facIdCOURSE

Semijoin
•First take the natural join of two tables
•Then take the projection on the attributes of first table
FACULTY COURSE

Semijoin Example
FACULTY COURSE
ranksalarydeptfacNamefacId
F4567
F2345
27000
21000
Asso ProfENGAyesha
lecturerCSEUsman

Inner Join
•An inner join includes only those tuples with matching attributes
and the rest are discarded in the resulting relation.
•Theta Join, Equijoin, and Natural Join are called inner joins

Outer Join
•All tuples in the left relation and right relation are
output.
•Denoted by ⟗
•types of outer join depends one the data kept in the
relations
•Left Outer Join
•Right Outer Join

ranksalarydeptfacNamefacId
F5678
F4567
F3456
F2345
32000
27000
23000
21000
ProfessorMNGSamad
Asso ProfENGAyesha
Asso ProfCSETahir
lecturerCSEUsman
COURSE
F2345Introduction to
Accounting
C3425
F4567Money & Capital
Market
C5678
Financial
Management
C4567
F2345Database SystemsC3456
fIdcrTitlecrCode
FACULTY
F2345

Outer Join Example
F5678 32000ProfessorMNGSamad
Financial ManagementC4567
Money & Capital Market
.
Money & Capital
Market
ranksalarydeptfacNamefacId
F3456
F4567
F2345
F2345
23000
27000
21000
21000
Asso Prof
CSETahir
Asso ProfENGAyesha
lecturerCSEUsman
lecturerCSEUsman
F4567C5678
F2345
Database SystemsC3456
facIdcrTitlecrCode
Intro. To Accounting
C3425
F2345
FACULTY OUTER JOIN COURSE

•Keep all of the tuples from the “left” relation
•Join with the right relation
•Pad the non-matching tuples with nulls
•Denoted by
Left Outer Join

Left Outer Join Example
FACULTY LEFT JOIN COURSE
ranksalarydeptfacNamefacId
F4567
F3456
F2345
F2345
27000
23000
21000
21000
Asso ProfENGAyesha
Asso ProfCSETahir
lecturerCSEUsman
lecturerCSEUsman
Money & Capital Market
. To Accounting
C5678
Intro. To AccountingC3425
F2345Database SystemsC3456
facIdcrTitlecrCode
F5678 32000ProfessorMNGSamad
F4567

Right Outer Join
•Same as the left, but keep tuples from the “right” relation
•Denoted by

Right Outer Join Example
F2345
FACULTY RIGHT OUTER JOIN COURSE
Money & Capital Market
.
Money & Capital
Market
ranksalarydeptfacNamefacId
F2345
F4567
F2345
21000
27000
21000
lecturerCSEUsman
Asso ProfENGAyesha
lecturerCSEUsman
F2345
F4567C5678
Database SystemsC3456
facIdcrTitlecrCode
Intro. To AccountingC3425
Financial ManagementC4567