390 likes | 527 Views
CHAPTER 5. RELATIONAL MODEL (FORMAL QUERY LANGUAGE). Relational Model. Structure of Relational Databases (Struktur Basis Data) Fundamental (Dasar) Relational-Algebra-Operations Additional (Tambahan) Relational-Algebra-Operations Extended (Lanjutan) Relational-Algebra-Operations
E N D
CHAPTER 5 RELATIONAL MODEL(FORMAL QUERY LANGUAGE)
Relational Model • Structure of Relational Databases (Struktur Basis Data) • Fundamental (Dasar) Relational-Algebra-Operations • Additional (Tambahan) Relational-Algebra-Operations • Extended (Lanjutan) Relational-Algebra-Operations • Null Values (Nilai Kosong) • Modification of the Database (Modifikasi basis data)
Struktur Dasar • Secara formal, himpunan D1, D2, …. Dn sebuah relasi r adalah sebuah bagian dari himpunan D1 x D2 x … x DnSehingga, sebuah relasi adalah sebuah himpunan dari n-tuples(row) (a1, a2, …, an) dimana setiap ai Di • Contoh: Jikacustomer_name = {Agus,Budi,Cecep}customer_street = {Jl. Cildug, Jl. Joglo, Jl. Ciputat}customer_city = {Jaksel, Jakbar, Jaksel}Maka r = { (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
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
Skema Relasi • A1, A2, …, Anadalah 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)
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 tabel attributes (or columns) customer_name customer_street customer_city Jones Smith Curry Lindsay Main North North Park Harrison Rye Rye Pittsfield tuples (or rows) customer
Relasi-relasi yang tidak urut • Pengurutan tuple-tuple adalah irrelevant • Tuple-tuple bisa disimpan dalam sebuah arbitrary order • Contoh: relasi account dengan unordered tuples
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”
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 accountsdepositor : 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
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.
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.
Select A B C D • Relasi r 1 5 12 23 7 7 3 10 • A=B ^ D > 5(r) A B C D 1 23 7 10
Select • Notasi: p(r) • p disebut selection predicate • Didefinisikan sebagai:p(r) = {t | t rand 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)
Project • Relasi r: A B C 10 20 30 40 1 1 1 2 A,C (r) A C A C 1 1 1 2 1 1 2 =
Project • Notasi: 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 accountaccount_number, balance (account)
Union • Relations r, s: A B A B 1 2 1 2 3 s r A B • r s: 1 2 1 3
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)
Set Difference • Relasi r, s: A B A B 1 2 1 2 3 s r A B • r – s: 1 1
Set Difference • Notasi r – s • Didefinisikan sebagai: r – s = {t | t rand 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
Cartesian-Product A B C D E A B C D E • Relasi r, s: 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 xs:
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.
Composition • Dapat membuat expressions menggunakan multiple operations • Contoh: A=C(r x s) • r x s A B C D E A B C D E • A=C(r x s) 10 10 20 a a b 1 2 2 1 1 1 1 2 2 2 2 10 10 20 10 10 10 20 10 a a b b a a b b
Operasi Tambahan Operasi tambahan yang didefinisikan tidak menambah kelebihan dari relational algebra, membuat sederhana query-query yang umum. • Set intersection • Natural join • Division • Assignment
Set-Intersection • Notasi: r s • Didefinisikan: rs = { t | trandts } • Assume: • r, s have the same arity • attribute dari r dan s compatible • Note: rs = r – (r – s)
Contoh Set-Intersection • Relation r, s: • r s A B A B 1 2 1 2 3 r s A B 2
Natural-Join • 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 RS, 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 • Notation: r s
Natural-Join • Example: R = (A, B, C, D) S = (E, B, D) • Result schema = (A, B, C, D, E) • rs didefinisikan:r.A, r.B, r.C, r.D, s.E (r.B = s.B r.D = s.D (r x s))
r s 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 s A B C D E 1 1 1 1 2 a a a a b
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) r s = { t | t R-S (r) u s ( tu r ) } Tu merupakan rentetan tuple t dan u untuk menghasilkan tuple tunggal.
Division A B • Relations r, s: B 1 2 3 1 1 1 3 4 6 1 2 1 2 s • r s: A r
Division A B C D E D E • Relations r, s: 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 s r • r s: A B C a a
Division • Property • Let q = r s • Then q is the largest relation satisfying q x s r • Definition in terms of the basic algebra operationLet 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.
Aggregate Functions and Operations • Aggregation function takes a collection of values and returns a single value as a result. avg: average valuemin: minimum valuemax: maximum valuesum: sum of valuescount: number of values • Aggregate operation in relational algebra E is any relational-algebra expression • G1, G2 …, Gn is a list of attributes on which to group (can be empty) • Each Fiis an aggregate function • Each Aiis an attribute name
Aggregate Operation – Example • Relation r: A B C 7 7 3 10 • gsum(c) (r) sum(c ) 27
Aggregate Operation – Example • Relation account grouped by branch-name: branch_name account_number balance Perryridge Perryridge Brighton Brighton Redwood A-102 A-201 A-217 A-215 A-222 400 900 750 750 700 branch_nameg sum(balance) (account) branch_name sum(balance) Perryridge Brighton Redwood 1300 1500 700
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_nameg sum(balance) as sum_balance (account)