Aljabar Relasi SBD
Aljabar Relasi Merupakan Procedural language Operator-operator dasar select: project: union: set difference: – Cartesian product: x rename: Operator menggunakan 1 atau 2 relasi sebagai masukan dan menghasilkan relasi baru
Operasi Select A=B ^ D > 5 (r) relasi r A B C D 1 5 12 23 7 3 10 A=B ^ D > 5 (r) A B C D 1 23 7 10
Select Notasi: p(r) p : predikat seleksi Definisikan sebagai: p(r) = {t | t r and p(t)} dimana p merupakan suatu formula dalam kalkulus proposional yang terdiri dari atribut/konstanta yg dihubungkan dengan : (and), (or), (not) <attribute> op <attribute> or <constant> op : =, , >, . <. Contoh selection: tmp_lahir=“Pekalongan”(mahasiswa)
Mohammad Arief Fatkhuriza Contoh Selection tmp_lahir=“Pekalongan” v jurusan=“Syariah” (mahasiswa) m_nim m_nama tmp_lahir jurusan 34.1.2.10.002 Syamsul Arifin Pekalongan Syariah 34.1.2.10.003 Mohammad Arief Fatkhuriza 34.1.2.10.004 Yuli Wahyuiningrum 34.1.2.10.005 Siti Zuhrotun Nisa 34.1.2.10.007 Sri Mulyani 34.1.2.10.010 Mohamad Noor Faiz 34.1.2.10.012 Nunung Kurniawati 34.1.2.10.013 Aminatul Azkiyah 34.1.2.10.014 Sugiyanto.H.S.
Project Relation r: A,C (r) A B C 10 20 30 40 1 2 A C A C 1 2 =
Project Notasi: A1, A2 : nama atribut r : suatu nama relasi. Hasil merupakan relasi dari sejumlah k kolom yg disebutkan dengan menghilangkan kolom yang tidak ada dalam daftar. Baris Kembar dihilangkan Contoh : m_nim, m_nama,tmp_lahir (mahasiswa)
Mohammad Arief Fatkhuriza Contoh Project m_nim, m_nama,tmp_lahir (mahasiswa) m_nim m_nama tmp_lahir 34.1.2.10.002 Syamsul Arifin Pekalongan 34.1.2.10.003 Mohammad Arief Fatkhuriza 34.1.2.10.004 Yuli Wahyuiningrum 34.1.2.10.005 Siti Zuhrotun Nisa 34.1.2.10.007 Sri Mulyani 34.1.2.10.010 Mohamad Noor Faiz 34.1.2.10.012 Nunung Kurniawati 34.1.2.10.013 Aminatul Azkiyah 34.1.2.10.014 Sugiyanto.H.S.
Union Relasi r, s: A B A B 1 2 2 3 s r A B 1 2 3 r s:
Union Notasi: r s Didefinisikan sbg: r s = {t | t r or t s} untuk r s menjadi valid. 1. r, s harus memiliki jumlah atribut yg sama (memiliki aritas yg sama ) 2. Domain/tipe attribute harus sesuai Contoh: mendapatkan seluruh kota tempat lahir dari mahasiswa ataupun dosen tmp_lahir (mahasiswa) tmp_lahir (dosen)
Contoh Union tmp_lahir (mahasiswa) tmp_lahir (dosen) tmp_lahir Tasikmalaya Tangerang Solo Pemalang Pekalongan tmp_lahir Bumiayu Kudus Banyumas Pekalongan Tegal Sumedang Pemalang Batang tmp_lahir Bumiayu Kudus Banyumas Pekalongan Tegal Sumedang Pemalang Batang Brebes Jakarta Solo Tasikmalaya Tangerang
Set Difference Relasi r, s: Merupakan operasi pengurangan data di tabel pertama oleh data di tabel ke dua Relasi r, s: A B A B 1 2 2 3 s r r – s: A B 1
Set Difference Notasi r – s Defined as: r – s = {t | t r and t s} r dan s harus memiliki kesamaan aritas domains attribute(tipe data) r dan s harus sama
Contoh set difference Mendapatkan kota lahir yang ada di tabel mahasiswa tapi tidak ada di tabel dosen tmp_lahir (mahasiswa) - tmp_lahir (dosen) Mahasiswa tmp_lahir Solo Pekalongan Batang Pemalang Tasikmalaya Tanggerang Jakarta Banyumas Dosen tmp_lahir Bumiayu Kudus Banyumas Pekalongan Tegal Sumedang Pemalang Batang Brebes Jakarta Mahasiswa tmp_lahir Solo Batang Tasikmalaya Tanggerang Jakarta
Cartesian-Product Menggabungkan data dari dua tabel/hasil query Relations r, s: A B C D E 1 2 10 20 a b r s r x s: A B C D E 1 2 10 20 a b
Cartesian-Product Notasi r x s Defined as: r x s = {t q | t r and q s} Attributes r(R) dan s(S) merupakan atribut disjoint. (That is, R S = ).
Contoh cartesian product (mahasiswa x Nilai) Nilai m_nim smt kdmk klpk nilai_huruf 34.1.1.11.002 6 SE2 2204 SY.2 A SE2 2307 34.1.1.11.003 B+ C+ Mahasiswa m_nim m_nama 34.1.1.11.002 Asrori 34.1.1.11.003 Dwi Prasongko Nilai x Mahasiswa m_nim smt kdmk klpk nilai_huruf m_nama 34.1.1.11.002 6 SE2 2204 SY.2 A Asrori SE2 2307 34.1.1.11.003 B+ C+ Dwi Prasongko
Cartesian product nilai.m_nim=mahasiswa.m_nim (nilai x mahasiswa ) m_nim,m_nama,kdmk,nilai_huruf ( nilai.m_nim=mahasiswa.m_nim (mahasiswa x nilai)) m_nim smt kdmk klpk nilai_huruf m_nama 34.1.1.11.002 6 SE2 2204 SY.2 A Asrori SE2 2307 34.1.1.11.003 B+ Dwi Prasongko C+ m_nim m_nama kdmk nilai_huruf 34.1.1.11.002 Asrori SE2 2204 A SE2 2307 34.1.1.11.003 Dwi Prasongko B+ C+
Komposisi Dapat terdiri dari beberapa operasi Contoh : A=C(r x s) 1 2 10 20 a b A B C D E 1 2 10 20 a b
Contoh komposisi m_nim,m_nama,kdmk,nmmk,nilai_huruf ( nilai.m_nim=mahasiswa.m_nim nilai.kdmk=matakuliah.kdmk (mahasiswa x nilai x matakuliah)) m_nim m_nama kdmk nilai_huruf 34.1.1.11.002 Asrori SE2 2204 Pemikiran Ekonomi Islam A SE2 2307 Matematika Ekonomi 34.1.1.11.003 Dwi Prasongko B+ C+
Rename Operation Melakukan penamaan pada suatu tabel/hasil proyeksi. Contoh: x (E) x adalah nama baru dari ekspresi E Untuk ekspresi E dengan n atribut dimana A1,A2 adalah atribut pada E
Rename m_nim,m_nama,kdmk,nmmk,nilai_huruf ( n.m_nim=mhs.m_nim n.kdmk=mk.kdmk (mhs ( mahasiswa) x n( nilai) x mk ( matakuliah))
Banking Example branch (branch_name, branch_city, assets) customer (customer_name, customer_street, customer_city) account (account_number, branch_name, balance) loan (loan_number, branch_name, amount) depositor (customer_name, account_number) borrower (customer_name, loan_number)
Example Queries amount > 1200 (loan) Find all loans of over $1200 amount > 1200 (loan) Find the loan number for each loan of an amount greater than $1200 loan_number (amount > 1200 (loan)) Find the names of all customers who have a loan, an account, or both, from the bank customer_name (borrower) customer_name (depositor)
Example Queries Find the names of all customers who have a loan at the Perryridge branch. customer_name (branch_name=“Perryridge” (borrower.loan_number = loan.loan_number(borrower x loan))) Find the names of all customers who have a loan at the Perryridge branch but do not have an account at any branch of the bank. customer_name (branch_name = “Perryridge” (borrower.loan_number = loan.loan_number(borrower x loan))) – customer_name(depositor)
Example Queries Find the names of all customers who have a loan at the Perryridge branch. Query 1 customer_name (branch_name = “Perryridge” ( borrower.loan_number = loan.loan_number (borrower x loan))) Query 2 customer_name(loan.loan_number = borrower.loan_number ( (branch_name = “Perryridge” (loan)) x borrower))
Formal Definition A basic expression in the relational algebra consists of either one of the following: A relation in the database A constant relation Let E1 and E2 be relational-algebra expressions; the following are all relational-algebra expressions: E1 E2 E1 – E2 E1 x E2 p (E1), P is a predicate on attributes in E1 s(E1), S is a list consisting of some of the attributes in E1 x (E1), x is the new name for the result of E1
Write the following queries in relational algebra Find the titles of courses in the Comp. Sci. department that have 3 credits. Find the IDs of all students who were taught by an instructor named Einstein;make sure there are no duplicates in the result.
Latihan 3. Tampilkan instruktur dari dept_name= ' Comp. Sci ‘ 4. Tampilkan instruktur dari dept_name= ' Comp. Sci ‘ dan gaji >7000 5. Tampilkan ID,name dari instruktur 6. Tampilkan name dari instruktur untuk dept_name= ' Comp. Sci ‘
7. Find the set of all courses taught in the Fall 2009 semester, the Spring 2010 semester, or both 8. find all the courses taught in the Fall 2009 semester but not in Spring 2010 semester
9. the names of all instructors in the Physics department together with the course id of all courses they taught
10. Find the highest salary in the university