1 / 39

CHAPTER 5

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

colin-vega
Download Presentation

CHAPTER 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 5 RELATIONAL MODEL(FORMAL QUERY LANGUAGE)

  2. 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)

  3. Contoh sebuah Relasi

  4. 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

  5. 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

  6. 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)

  7. 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

  8. Relasi-relasi yang tidak urut • Pengurutan tuple-tuple adalah irrelevant • Tuple-tuple bisa disimpan dalam sebuah arbitrary order • Contoh: relasi account dengan unordered tuples

  9. 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”

  10. 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

  11. Relasi customer

  12. Relasi depositor

  13. 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.

  14. 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.

  15. 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

  16. 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)

  17. 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 =

  18. 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 accountaccount_number, balance (account)

  19. Union • Relations r, s: A B A B    1 2 1   2 3 s r A B • r  s:     1 2 1 3

  20. 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)

  21. Set Difference • Relasi r, s: A B A B    1 2 1   2 3 s r A B • r – s:   1 1

  22. 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

  23. 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:

  24. 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.

  25. 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

  26. Operasi Tambahan Operasi tambahan yang didefinisikan tidak menambah kelebihan dari relational algebra, membuat sederhana query-query yang umum. • Set intersection • Natural join • Division • Assignment

  27. Set-Intersection • Notasi: r s • Didefinisikan: rs = { t | trandts } • Assume: • r, s have the same arity • attribute dari r dan s compatible • Note: rs = r – (r – s)

  28. Contoh Set-Intersection • Relation r, s: • r s A B A B    1 2 1   2 3 r s A B  2

  29. 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 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 • Notation: r s

  30. 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))

  31. 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     

  32. 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.

  33. 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

  34. 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  

  35. 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.

  36. 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

  37. Aggregate Operation – Example • Relation r: A B C         7 7 3 10 • gsum(c) (r) sum(c ) 27

  38. 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

  39. 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)

More Related