SISTEM BASIS DATA 2
Download
Report
Transcript SISTEM BASIS DATA 2
PENGANTAR BASIS DATA (3 SKS)
CHAPTER 5
RELATIONAL MODEL
(FORMAL QUERY LANGUAGE)
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.1
PENGANTAR BASIS DATA (3 SKS)
Relational Model
Structure of Relational Databases (Struktur
Basis Data)
Fundamental (Dasar) Relational-AlgebraOperations
Additional (Tambahan) Relational-AlgebraOperations
Extended (Lanjutan) Relational-AlgebraOperations
Null Values (Nilai Kosong)
Modification of the Database (Modifikasi basis
data)
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.2
PENGANTAR BASIS DATA (3 SKS)
Contoh sebuah Relasi
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.3
PENGANTAR BASIS DATA (3 SKS)
Struktur Dasar
Secara formal, himpunan D1, D2, …. Dn sebuah relasi r adalah
sebuah bagian dari himpunan
D1 x D2 x … x Dn
Sehingga, sebuah relasi adalah sebuah himpunan dari ntuples(row) (a1, a2, …, an) dimana setiap ai Di
Contoh: Jika
customer_name = {Agus,Budi,Cecep}
customer_street = {Jl. Cildug, Jl. Joglo, Jl. Ciputat}
customer_city
Maka r = {
= {Jaksel, Jakbar, Jaksel}
(Agus, Jl.Ciledug, Jaksel),
(Budi, Jl. Joglo, Jakbar),
(Cecep, Jl. Ciputat, Jaksel)}
adalah sebuah relasi yang berasal dari
customer_name x customer_street x customer_city
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.4
PENGANTAR BASIS DATA (3 SKS)
Tipe Atribut
Setiap atribut dari sebuah relasi mempunyai sebuah
nama
Setiap himpunan yang berisi nilai untuk setiap atribut
disebut domain dari atribut
Secara normal, nilai atribut yang diperlukan haruslah
atomic; artinya, indivisible
Catt: nilai multivalued attribute tidak atomic
Catt: nilai composite attribute tidak atomic
Setiap nilai null khusus adalah anggota dari setiap
domain
Setiap nilai null menyebabkan komplikasi dalam
pendefinisian pada banyak operations
Kita akan mengabaikan penyebab dari nilai null
dalam presentasi ini dan mempertimbangkan
penyebabnya di lain kali
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.5
PENGANTAR BASIS DATA (3 SKS)
Skema Relasi
A1, A2, …, An adalah atribut-atribut
R = (A1, A2, …, An ) adalah sebuah relation
schema (skema relasi)
Contoh:
Customer_schema = (customer_name,
customer_street, customer_city)
r(R) adalah sebuah relation (relasi) pada
relation schema R
Contoh:
customer (Customer_schema)
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.6
PENGANTAR BASIS DATA (3 SKS)
Instance Relasi
Nilai yang ada sekarang (relation instance) dari
sebuah relasi dinyatakan dalam sebuah tabel
Sebuah elemen t dari r adalah sebuah tuple,
dinyatakan sebagai sebuah row (baris) dalam sebuah
attributes
tabel
(or columns)
customer_name customer_street
Jones
Smith
Curry
Lindsay
Main
North
North
Park
customer_city
Harrison
Rye
Rye
Pittsfield
tuples
(or rows)
customer
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.7
PENGANTAR BASIS DATA (3 SKS)
Relasi-relasi yang tidak urut
Pengurutan tuple-tuple adalah irrelevant
Tuple-tuple bisa disimpan dalam sebuah arbitrary order
Contoh: relasi account dengan unordered tuples
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.8
PENGANTAR BASIS DATA (3 SKS)
Properti suatu relasi
Tidak ada duplikat tuple
Dalam suatu relasi tidak akan ada 2 tuple atau
lebih yang nilai-nilainya tepat sama
Tuple tidak mempunyai urutan
Tuple dapat diletakkan pada sembarang baris dari
atas ke bawah
Atribut tidak mempunyai urutan
Setiap atribut dapat diletakkan pada sembarang
posisi dari kiri ke kanan
Semua nilai atribut bersifat “atomic”
Nilai atribut harus tunggal
Tidak diperbolehkan adanya “repeating group”
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.9
PENGANTAR BASIS DATA (3 SKS)
Database (Basis Data)
Sebuah basis data terdiri dari multiple relations (banyak relasi)
Informasi tentang sebuah perusahaan dipecah menjadi beberapa
bagian, setiap relasi menyimpan satu bagian informasi
account : stores information about accounts
depositor : stores information about which customer
owns which account
customer : stores information about customers
Menyimpan semua informasi sebagai sebuah relasi tunggal sama seperti
bank(account_number, balance, customer_name, ..)
menghasilkan
pengulangan informasi (cth., dua customer mempunyai sebuah
account)
kebutuhan untuk nilai null (cth., menyatakan sebuah customer tanpa
sebuah account)
Teori Normalisasi akan menerangkan bagaimana mendisain skema relasi
yang baik
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.10
PENGANTAR BASIS DATA (3 SKS)
Relasi customer
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.11
PENGANTAR BASIS DATA (3 SKS)
Relasi depositor
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.12
PENGANTAR BASIS DATA (3 SKS)
Query Languages
Bahasa yang informasinya diminta (request) user dari
database.
Kategori dari bahasa
Procedural
Non-procedural, atau declarative
“Pure” languages:
Relational algebra
Tuple relational calculus
Domain relational calculus
Pure languages form menjadi basis utama dari query
languages yang digunakan orang.
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.13
PENGANTAR BASIS DATA (3 SKS)
Relational Algebra
Procedural language
6 basic operators
select:
project:
union:
set difference: –
Cartesian product: x
rename:
Operator mengambil 1 atau 2 relasi sebagai
input dan menghasilkan sebuah relasi.
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.14
PENGANTAR BASIS DATA (3 SKS)
Select
Relasi r
A
B
C
D
1
7
5
7
12
3
23 10
A=B ^ D > 5 (r)
FTI – UNIVERSITAS BUDI LUHUR
A
B
C
D
1
7
23 10
GANJIL 2008 KP213-1.15
PENGANTAR BASIS DATA (3 SKS)
Select
Notasi: p(r)
p disebut selection predicate
Didefinisikan sebagai:
p(r) = {t | t r and p(t)}
Dimana p adalah sebuah formula dalam propositional
calculus yang terdiri dari terms yang dihubungkan oleh :
(and), (or), (not)
Setiap term berisi:
<attribute> op <attribute> or <constant>
dimana op berisi: =, , >, . <.
Contoh selection:
branch_name=“Meanus”(account)
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.16
PENGANTAR BASIS DATA (3 SKS)
Project
Relasi r:
A,C (r)
A
B
C
10
1
20
1
30
1
40
2
A
C
A
C
1
1
1
1
1
2
2
FTI – UNIVERSITAS BUDI LUHUR
=
GANJIL 2008 KP213-1.17
PENGANTAR BASIS DATA (3 SKS)
Project
Notasi:
A1,A2 ,,Ak
(r )
dimana A1, A2 adalah nama atributnya dan r adalah
sebuah nama relasi.
Setiap hasil didefinisikan sebagai relasi dari kolom k
sehingga kolom yang dihapus tidak ditampilkan.
Hasilnya: baris yang isi relasi himpunannya sama
dihapus
Contoh: Untuk mengeliminasi atribut branch_name
dari account
account_number, balance (account)
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.18
PENGANTAR BASIS DATA (3 SKS)
Union
Relations r, s:
A
B
A
B
1
2
2
3
1
s
r
r s:
A
B
1
2
1
3
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.19
PENGANTAR BASIS DATA (3 SKS)
Union
Notasi: r s
Didefinisikan dengan:
r s = {t | t r or t s}
Untuk r s harus valid.
1. r, s harus mempunyai arity yang
sama(attributes yang sama)
2. domain dari attribute harus
compatible (contoh: ke dua kolom pada r
dan s mempunyai type yang sama)
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.20
PENGANTAR BASIS DATA (3 SKS)
Set Difference
Relasi r, s:
A
B
A
B
1
2
2
3
1
s
r
r – s:
FTI – UNIVERSITAS BUDI LUHUR
A
B
1
1
GANJIL 2008 KP213-1.21
PENGANTAR BASIS DATA (3 SKS)
Set Difference
Notasi r – s
Didefinisikan sebagai:
r – s = {t | t r and t s}
Set differences harus diambil dari relasi
compatible.
r dan s harus mempunyai arity yang sama
attribute domains dari r dan s harus
kompatibel
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.22
PENGANTAR BASIS DATA (3 SKS)
Cartesian-Product
Relasi r, s:
A
B
C
D
E
A
B
C
D
E
1
2
10
10
20
10
a
a
b
b
1
1
1
1
2
2
2
2
10
10
20
10
10
10
20
10
a
a
b
b
a
a
b
b
r
s
r x s:
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.23
PENGANTAR BASIS DATA (3 SKS)
Cartesian-Product
Notasi r x s
Didefinisikan sebagai:
r x s = {t q | t r and q s}
Diasumsikan bahwa atribut-atribut dari
r(R) dan s(S) adalah disjoint. (Yaitu, R
S = ).
Jika atribut-atribut dari r(R) dan s(S)
tidak disjoint, maka nama atribut harus
diganti.
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.24
PENGANTAR BASIS DATA (3 SKS)
Composition
Dapat membuat expressions
menggunakan multiple operations
Contoh: A=C(r x s)
r x s
A B
A B C D E A=C(r x s)
1
1
1
1
2
2
2
2
10
10
20
10
10
10
20
10
FTI – UNIVERSITAS BUDI LUHUR
a
a
b
b
a
a
b
b
1
2
2
C
D
E
10
10
20
a
a
b
GANJIL 2008 KP213-1.25
PENGANTAR BASIS DATA (3 SKS)
Operasi Tambahan
Operasi tambahan yang didefinisikan
tidak menambah kelebihan dari
relational algebra, membuat sederhana
query-query yang umum.
Set intersection
Natural join
Division
Assignment
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.26
PENGANTAR BASIS DATA (3 SKS)
Set-Intersection
Notasi: r s
Didefinisikan:
r s = { t | t r and t s }
Assume:
r, s have the same arity
attribute dari r dan s compatible
Note: r s = r – (r – s)
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.27
PENGANTAR BASIS DATA (3 SKS)
Contoh Set-Intersection
Relation r, s:
A
B
1
2
1
r
rs
B
2
3
s
A
B
2
FTI – UNIVERSITAS BUDI LUHUR
A
GANJIL 2008 KP213-1.28
PENGANTAR BASIS DATA (3 SKS)
Natural-Join
Notation: r
s
Let r and s be relations on schemas R and S
respectively.
Then, r
s is a relation on schema R S
obtained as follows:
Consider each pair of tuples tr from r and ts from s.
If tr and ts have the same value on each of the
attributes in R S, add a tuple t to the result, where
t has the same value as tr on r
t has the same value as ts on s
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.29
PENGANTAR BASIS DATA (3 SKS)
Natural-Join
Example:
R = (A, B, C, D)
S = (E, B, D)
Result schema = (A, B, C, D, E)
r
s didefinisikan:
r.A, r.B, r.C, r.D, s.E (r.B = s.B r.D = s.D (r x s))
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.30
PENGANTAR BASIS DATA (3 SKS)
Contoh Natural Join
Relasi r, s:
A
B
C
D
B
D
E
1
2
4
1
2
a
a
b
a
b
1
3
1
2
3
a
a
a
b
b
r
r
s
s
FTI – UNIVERSITAS BUDI LUHUR
A
B
C
D
E
1
1
1
1
2
a
a
a
a
b
GANJIL 2008 KP213-1.31
PENGANTAR BASIS DATA (3 SKS)
Division
Notation: r s
memisahkan r dan s pada relasi dalam schema R dan S
secara berturut-turut diaman
R = (A1, …, Am , B1, …, Bn )
S = (B1, …, Bn)
hasil r s adalah selasi dalam schema
R – S = (A1, …, Am)
rs={t | t
R-S
(r) u s ( tu r ) }
Tu merupakan rentetan tuple t dan u untuk
menghasilkan tuple tunggal.
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.32
PENGANTAR BASIS DATA (3 SKS)
Division
Relations r, s:
r s:
A
A
B
1
2
3
1
1
1
3
4
6
1
2
B
1
2
s
r
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.33
PENGANTAR BASIS DATA (3 SKS)
Division
Relations r, s:
A
B
C
D
E
D
E
a
a
a
a
a
a
a
a
a
a
b
a
b
a
b
b
1
1
1
1
3
1
1
1
a
b
1
1
r s:
FTI – UNIVERSITAS BUDI LUHUR
s
r
A
B
C
a
a
GANJIL 2008 KP213-1.34
PENGANTAR BASIS DATA (3 SKS)
Division
Property
Let q = r s
Then q is the largest relation satisfying q x s r
Definition in terms of the basic algebra operation
Let r(R) and s(S) be relations, and let S R
r s = R-S (r ) – R-S ( ( R-S (r ) x s ) – R-S,S(r ))
To see why
R-S,S (r) simply reorders attributes of r
R-S (R-S (r ) x s ) – R-S,S(r) ) gives those tuples t in
R-S (r ) such that for some tuple u s, tu r.
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.35
PENGANTAR BASIS DATA (3 SKS)
Aggregate Functions and Operations
Aggregation function takes a collection of values and
returns a single value as a result.
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
Aggregate operation in relational algebra
G1,G2 ,,Gn
F ( A ),F ( A ,,F ( A ) (E )
1
1
2
2
n
n
E is any relational-algebra expression
G1, G2 …, Gn is a list of attributes on which to group
(can be empty)
Each Fi is an aggregate function
Each A is an attribute name
GANJIL 2008 KP213-1.36
FTI – UNIVERSITASi BUDI LUHUR
PENGANTAR BASIS DATA (3 SKS)
Aggregate Operation – Example
Relation r:
g sum(c) (r)
A
B
C
7
7
3
10
sum(c )
27
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.37
PENGANTAR BASIS DATA (3 SKS)
Aggregate Operation – Example
Relation account grouped by branchname:
branch_name account_number
Perryridge
Perryridge
Brighton
Brighton
Redwood
balance
A-102
A-201
A-217
A-215
A-222
400
900
750
750
700
branch_name g sum(balance) (account)
branch_name
Perryridge
Brighton
Redwood
FTI – UNIVERSITAS BUDI LUHUR
sum(balance)
1300
1500
700
GANJIL 2008 KP213-1.38
PENGANTAR BASIS DATA (3 SKS)
Aggregate Functions (Cont.)
Result of aggregation does not have a
name
Can use rename operation to give it a name
For convenience, we permit renaming as
part of aggregate operation
branch_name
g
sum(balance) as sum_balance (account)
FTI – UNIVERSITAS BUDI LUHUR
GANJIL 2008 KP213-1.39