Anda di halaman 1dari 38

Pengantar Basis Data

Relational Algebra and Calculus


Part II
KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 1
Review
Unary
SELECT
PROJECT
RENAME
Binary / n-ary
UNION
INTERSECTION
DIFFERENCE
Each Characteristics (komutatif)

KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 2
Pre Test
Gambarkan operasi berikut dalam diagram
Venn (R dan S) yang saling beririsan:
a. -R
b. -S
c. R S
d. S R
e. R U S
f. R INTERSECT S

KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 3
Cartesian Product
Used to combine tuples from two relations in
a combinatorial fashion.
R(A
1
, A
2
, A
n
) x S(B
1
, B
2
, B
m
)
The resulting relation state has one tuple for
each combination of tuples one from R and
one from S.
The two operands do NOT have to be type
compatible.
KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 4
Cartesian Product
NRP FirstName LastName
5211100024 Dian Sastro
5213100074 Wardoyo Raline
5212100020 Shah Annisa
KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 5
NRPRef Age FaveColor Race
5211100024 34 Pink Asian
5213100077 14 Black Caucasian
5212100088 56 Fuchsia African
STUDENTS
DEMOGRAPHY
X
Cartesian Product
NRP FirstName LastName NRPRef Age FaveColor Race
5211100024 Dian Sastro 5211100024 34 Pink Asian
5211100024 Dian Sastro 5213100077 14 Black Caucasian
5211100024 Dian Sastro 5212100088 56 Fuchsia African
5213100074 Wardoyo Raline 5211100024 34 Pink Asian
5213100074 Wardoyo Raline 5213100077 14 Black Caucasian
5213100074 Wardoyo Raline 5212100088 56 Fuchsia African
5212100020 Shah Annisa 5211100024 34 Pink Asian
5212100020 Shah Annisa 5213100077 14 Black Caucasian
5212100020 Shah Annisa 5212100088 56 Fuchsia African
KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 6
RESULT OF STUDENTS X DEMOGRAPHY
Cartesian Product
Generally, cartesian (cross) product is not a
meaningful operation. But, it can be
meaningful if it is followed by other operation.
Result relation will have all combination of R
and S without any proven relationship.

KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 7
JOIN
The cartesian product which is followed by
SELECT and or other operations.
This operation is mandatory in any DBMS
because it allows users combine related tuples
from various relations.
KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 8
JOIN
KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 9
NRP FirstName LastName NRPRef Age FaveColor Race
5211100024 Dian Sastro 5211100024 34 Pink Asian
5211100024 Dian Sastro 5213100077 14 Black Caucasian
5211100024 Dian Sastro 5212100088 56 Fuchsia African
5213100074 Wardoyo Raline 5211100024 34 Pink Asian
5213100074 Wardoyo Raline 5213100077 14 Black Caucasian
5213100074 Wardoyo Raline 5212100088 56 Fuchsia African
5212100020 Shah Annisa 5211100024 34 Pink Asian
5212100020 Shah Annisa 5213100077 14 Black Caucasian
5212100020 Shah Annisa 5212100088 56 Fuchsia African
STUDENTS_DEMOGRAPHY
PROVEN <- (
NRP=NRPRef
(STUDENTS_DEMOGRAPHY))
FINAL <-
NRP, FirstName, LastName, Age, FaveColor, Race
(PROVEN)
JOIN
Dua perintah sekuensial diatas sesungguhnya
hanya bertujuan untuk mencari apa data
demography yang sesuai dengan data
students yang dimiliki.
Tiga perintah ini (Cartesian Product, Select,
Project) dapat digantikan oleh satu perintah
saja, yang bernama JOIN
STUDENTS DEMOGRAPHY
KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 10
NRP=NRPRef
Theta
JOIN
Theta can be in any expression
R.Ai < S.Bj AND (R.Ak = S.Bl OR R.Ap < S.Bq)


R.Ai = S.Bj AND R.Ak = S.Bl AND R.Ap = S.Bq)
KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 11
Math Operator Logical Operator
Dua field yang saling kompatible di kedua relations, biasanya adalah foreign key
All math operators used is =, thus, it is called as EQUIJOIN
JOIN
Join condition can also be specified as
STUDENTS.NRP = DEMOGRAPHY.NRPRef




The superfluous attribute better to be diminished in
order to minimize allocated memory -> using
NATURAL JOIN (denoted by * )
KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 12
NRP FirstName LastName NRPRef Age FaveColor Race
5211100024 Dian Sastro 5211100024 34 Pink Asian
Superfluous attribute
Soal Latihan
Ngecartesian
Setiap mahasiswa urun 2 tuples.
KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 13
X
Soal Latihan Ngejoin
Mhs 1: Tentukan thetanya apa
Mhs 2: Tuliskan hasil joinnya (Dnumber,
Dlocation, Pname, Pnumber, Plocation, Dnum)
KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 14
NATURAL JOIN
We will perform a natural join between
DEPARTMENT and DEPT_LOCATIONS
KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 15
NATURAL JOIN
Step 1: Always search for two or more
attributes that compatible to each others.
Dnumber in DEPARTMENT
Dnum in PROJECT
Step 2: Perform DEPARTMENT PROJECT
DEPT_PROJ <- DEPARTMENT.Dnumber =
PROJECT.Dum
KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 16
NATURAL JOIN
KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 17
Dname Dnu
mb
er
Mgr_
ssn
Mgr_start_
date
Pname Pnu
mbe
r
Plocation Dnu
m
Research 5 333445
555
1988-05-22 Product X 1 Bellaire 5
Research 5 333445
555
1988-05-22 Product Y 2 Sugarland 5
Research 5 333445
555
1988-05-22 Product Z 3 Houston 5
Administration 4 987654
321
1995-01-01 Computerization 10 Stafford 4
Administration 4 987654
321
1995-01-01 New Benefits 30 Stafford 4
Headquarters 1 888665
555
1981-06-19 Reorganization 20 Houston 1
DEPT_PROJ
NATURAL JOIN
Step 3: Check whether the compatible
attributes have same name or not, RENAME
them.
RESULT (Dname, Dnumber, Mgr_ssn,
Mgr_start_date, Pname, Pnumber, Plocation,
Dnumber) <- DEPT_PROJ (Dname, Dnumber,
Mgr_ssn, Mgr_start_date, Pname, Pnumber,
Plocation, Dnum)
KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 18
NATURAL JOIN
KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 19
Dname Dnu
mb
er
Mgr_
ssn
Mgr_start_
date
Pname Pnu
mbe
r
Plocation Dnu
mbe
r
Research 5 333445
555
1988-05-22 Product X 1 Bellaire 5
Research 5 333445
555
1988-05-22 Product Y 2 Sugarland 5
Research 5 333445
555
1988-05-22 Product Z 3 Houston 5
Administration 4 987654
321
1995-01-01 Computerization 10 Stafford 4
Administration 4 987654
321
1995-01-01 New Benefits 30 Stafford 4
Headquarters 1 888665
555
1981-06-19 Reorganization 20 Houston 1
RESULT
NATURAL JOIN
Step 4: Eliminate one of the same attributes
with PROJECT operator.
FINAL <-
Dname, Dnumber, Mgr_ssn, Mgr_start_date, Pname,
Pnumber, Plocation
(RESULT)

All those steps is denoted by DEPARTMENT *
PROJECT
KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 20
NATURAL JOIN
KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 21
Dname Dnu
mbe
r
Mgr_
ssn
Mgr_start_
date
Pname Pnu
mbe
r
Plocation
Research 5 333445555 1988-05-22 Product X 1 Bellaire
Research 5 333445555 1988-05-22 Product Y 2 Sugarland
Research 5 333445555 1988-05-22 Product Z 3 Houston
Administration 4 987654321 1995-01-01 Computerization 10 Stafford
Administration 4 987654321 1995-01-01 New Benefits 30 Stafford
Headquarters 1 888665555 1981-06-19 Reorganization 20 Houston
FINAL
OUTER JOIN
Allows tuples without a matching or related
tuple to be displayed.
RIGHT OUTER JOIN
Keeps every tuple in the second / right relation.
LEFT OUTER JOIN
Keeps every tuple in the first / left relation.
The tuples which have no relative, will be
filled with NULL.
KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 22
LEFT OUTER JOIN

KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 23
EMPLOYEE.Dno=DEPARTMENT.Dumber AND EMPLOYEE.Ssn=DEPARTMENT.Mgr_ssn
LEFT OUTER JOIN
Project only Fname, Minit, Lname and Dname
KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 24
RIGHT OUTER JOIN

KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 25
EMPLOYEE.Dno=DEPARTMENT.Dumber AND EMPLOYEE.Ssn=DEPARTMENT.Mgr_ssn
RIGHT OUTER JOIN
Project only Dname, Dnumber, Mgr_ssn,
Fname, Lname
KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 26
Dname Dnumber Mgr_ssn Fname Lname
Research 5 333445555 Franklin Wong
Administration 4 987654321 Jennifer Wallace
Headquarters 1 888665555 James Borg
Soal Latihan
Ngenatural Join

KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 27
*
Soal Latihan Ngeright
Outer Join

KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 28
Complete Set
The set of operations including SELECT,
PROJECT, UNION, DIFFERENCE, RENAME and
CARTESIAN PRODUCT is called as complete
set.

KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 29
DIVISION
The division is applied to two relations R and
S, where all attributes in S are subset of R.
R(Z) / S(X) = T(Y)
T is a relation with tuple that appear in R with
every tuple in S.
KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 30
SSN_PNOS /
SMITH_PNOS =
SSNS
KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 31
R / S = T
KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 32

KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 33
Aggregate Functions
Denoted by
SUM

SUM Salary
(EMPLOYEE) retrieves the sum of the Salary
from EMPLOYEE relation
AVERAGE
MAXIMUM
MINIMUM
COUNT

COUNT SSN, AVERAGE Salary
(EMPLOYEE) computes the count
(SSN) of employees and their average salary.
KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 34
Grouping Function
Group is come along with aggregate function
It is written on the left of notation

DNO

COUNT SSN, AVERAGE Salary
(EMPLOYEE)
KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 35
Illustration

KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 36
RECURSIVE
Retrieve all supervisees of James Borg (SSN =
888665555)

KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 37
RECURSIVE

KS141304 PBD Institut Teknologi Sepuluh Nopember (ITS) 38

Anda mungkin juga menyukai