1 / 51

353352 – Database

353352 – Database. Relational Model Choopan Rattanapoka CIT, KMUTNB. Relational Model. เป็นตัวแบบรากฐานของ ระบบฐานข้อมูลแบบ Relational โครงสร้างของ Relational Model ประกอบไปด้วย ตาราง ( tables) ที่มีชื่อเฉพาะตัวไม่ซ้ำ ความสัมพันธ์ของค่าในตาราง ( relationship). Database Schema.

Download Presentation

353352 – Database

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. 353352 – Database Relational Model ChoopanRattanapoka CIT, KMUTNB

  2. Relational Model • เป็นตัวแบบรากฐานของ ระบบฐานข้อมูลแบบ Relational • โครงสร้างของ Relational Model ประกอบไปด้วย • ตาราง (tables)ที่มีชื่อเฉพาะตัวไม่ซ้ำ • ความสัมพันธ์ของค่าในตาราง (relationship)

  3. Database Schema • โครงสร้างข้อมูลของตาราง • ตารางที่ใช้โครงสร้างข้อมูลแบบนี้ • Table_name(Schema_name)

  4. Example : Database Schema จากตาราง customer ต่อไปนี้ จงเขียน database schema Customer_schema = ( Customer_id, Customer_name, Customer_city) Customer(Customer_schema) หรือ Customer(Customer_id, Customer_name, Customer_city)

  5. Relational Algebra • Relational Algebra เป็นภาษาแบบ procedural query language • พื้นฐานคำสั่งของ Relational Algebra คือ • select, project, rename → unary operations • union, set difference และ cartesian product → binary operations • นอกจากคำสั่งพื้นฐานแล้วยังมีคำสั่งอื่นๆ เช่น • set intersection, natural join, division, และ assignment

  6. Select Operations • เป็นคำสั่งที่ใช้เลือก ข้อมูลในแถวที่ตรงกับเงื่อนไขที่กำหนด • เขียนแทนด้วยสัญลักษณ์ • วิธีการใช้งาน • Condition สามารถใช้เครื่องหมาย • การเปรียบเทียบ :=, <, >, ≥, ≤, ≠ • Logic operation : ¬, ,

  7. Example : Select • กำหนดตาราง สมุดบัญชี (Account) มี schema คือ • Account(Branch-name, Branch,city, Balance) • Branch-name คือ ชื่อสาขา • Branch-city คือ จังหวัดที่สาขานั้นตั้งอยู่ • Balance คือ ยอดเงินในบัญชี

  8. Example : Select • จงเลือกข้อมูลของสาขาที่อยู่ในกรุงเทพ

  9. Example : Select • จงเลือกข้อมูล ที่เงินในบัญชีมากกว่า 12,000

  10. Example : Select • จงเลือกข้อมูล ที่เงินในบัญชีมากกว่า 12,000 และบัญชีอยู่ที่ นนทบุรี

  11. Exercise • กำหนดตารางนักเรียนที่มีโครงสร้างดังนี้ • Student(Fname, Lname, Faculty, GPA) • Fname = ชื่อ • Lname = นามสกุล • Faculty = คณะ • GPA = คะแนน GPA • จงเขียน Relational Algebra เพื่อหา • ข้อมูลของนักเรียนที่อยู่ที่คณะ “วทอ” • ข้อมูลของนักเรียนที่มี GPA > 2.00 • ข้อมูลของนักเรียนที่มีนามสกุล “ชินวัด” และ GPA < 2.00 • ข้อมูลของนักเรียนที่มีนามสกุล “ชินวัด” หรือนามสกุล “วัดชิน”

  12. Project Operation • Project operation เป็นตัวที่ใช้แสดงข้อมูลของ attribute ที่ต้องการ • ใช้สัญลักษณ์ • วิธีใช้งาน

  13. Example : Project • กำหนดตาราง สมุดบัญชี (Account) มี schema คือ • Account(Branch-name, Branch,city, Balance) • Branch-name คือ ชื่อสาขา • Branch-city คือ จังหวัดที่สาขานั้นตั้งอยู่ • Balance คือ ยอดเงินในบัญชี

  14. Example : Project • จงแสดงข้อมูลเฉพาะสาขา และจำนวนเงิน

  15. Composition of Relational Operations • เราสามารถใช้ Select และ Project ต่อเนื่องกันได้ • เช่นจากตารางรถ (car) ต่อไปนี้ • ให้แสดงเฉพาะชื่อ ของเจ้าของรถ Honda สีดำ

  16. Union Operation • ใช้เมื่อมีความต้องการเชื่อมข้อมูลกับตารางมากกว่า 1 ตาราง • เช่น ตารางผู้กู้เงินจากธนาคาร (borrower) และ ตารางผู้ฝากเงินกับธนาคาร (depositor) borrower depositor • จงหา รหัสของลูกค้าของธนาคารที่กู้เงินกับธนาคารหรือมีเงินฝากกับธนาคาร

  17. Union Operation depositor • รหัสลูกค้าที่ฝากเงินกับธนาคาร

  18. Union Operation borrower • รหัสลูกค้าที่กู้เงินกับธนาคาร

  19. Union Operation depositor borrower

  20. Set Difference Operation • ใช้เพื่อหาข้อมูลที่อยู่ในความสัมพันธ์หนึ่งแต่ไม่ในอีกความสัมพันธ์หนึ่ง • เช่น ตารางผู้กู้เงินจากธนาคาร (borrower) และ ตารางผู้ฝากเงินกับธนาคาร (depositor) borrower depositor • จงหา รหัสของลูกค้าของธนาคารที่กู้เงินกับธนาคาร แต่ไม่มีเงินฝากกับธนาคาร

  21. Set Difference Operation depositor • รหัสลูกค้าที่ฝากเงินกับธนาคาร

  22. Set Difference Operation borrower • รหัสลูกค้าที่กู้เงินกับธนาคาร

  23. Set Difference Operation depositor borrower

  24. Exercise 1 จากตาราง Buyer (อยากซื้อรถ) Seller (อยากขายรถ) • จงเขียน relational algebra และผลลัพธ์ เพื่อหา • รหัสลูกค้าที่อยากซื้อรถ Honda สีดำ • รหัสลูกค้า,ยี่ห้อและราคารถ ที่ขายรถในราคาต่ำกว่า 400000 บาท • รหัสลูกค้าทั้งหมด ทั้งขาย และซื้อ • รหัสลูกค้าที่ซื้อรถ แต่ไม่ขายรถ

  25. Exercise 2 จากตาราง Buyer (อยากซื้อรถ) Seller (อยากขายรถ) จาก relational algebra ต่อไปนี้จงหาผลลัพธ์

  26. The Cartesian-Product Operation • Cartesian-Product ใช้สัญลักษณ์คือ X (cross) • เป็นการรวมข้อมูลของ 2 relations เข้าด้วยกัน • การใช้งาน เช่น table1 x table2 ตัวอย่าง : มี 2 ตาราง ตารางกู้เงิน (borrower) และ ตารางฝากเงิน (depositor) borrower depositor

  27. The Cartesian-Product Operation depositor borrower borrower X depositor

  28. The Cartesian-Product Operation depositor borrower borrower X depositor

  29. The Cartesian-Product Operation depositor borrower borrower X depositor

  30. The Cartesian-Product Operation depositor borrower borrower X depositor

  31. The Cartesian-Product Operation depositor borrower borrower X depositor

  32. The Cartesian-Product Operation depositor borrower borrower X depositor

  33. The Cartesian-Product Operation depositor borrower X borrower X depositor

  34. Exercise 3 depositor borrower หารหัสลูกค้าที่กู้และฝากเงินกับธนาคาร

  35. The Rename Operation • ใช้สำหรับเปลี่ยนชื่อของตาราง • ใช้สัญลักษณ์ • การใช้งาน • xชื่อของตารางใหม่ • Eตารางหรือ expression

  36. The Rename Operation X borrower borrower

  37. The Rename Operation X borrower

  38. Example: The Rename Operation จากตารางผู้กู้เงิน (borrower) จงเขียน relational algebra เพื่อหาผู้ที่กู้เงินมากที่สุด

  39. Example :The Rename Operation X borrower

  40. Example :The Rename Operation borrower X

  41. Example :The Rename Operation

  42. Example :The Rename Operation - Borrower -

  43. Example :The Rename Operation Borrower -

  44. Additional Operations

  45. The Set-Intersection Operation • การทำงานเหมือน intersect ของ set • ใช้สัญลักษณ์ • ตัวอย่าง หารหัสลูกค้าที่กู้และฝากเงินกับธนาคาร borrower depositor

  46. The Natural-Join Operation • เนื่องจากการใช้ cartesian product เชื่อมต่อ 2 ตารางเข้าด้วยกันทุกคน ทำให้รูปสมการยาว จึงเกิด Natural-Join • Natural-join จะบังคับ operation = ให้กับฟิลด์ที่เหมือนกันของ 2 ตาราง • ใช้สัญลักษณ์

  47. The Natural-Join Operation ตัวอย่าง : มี 2 ตาราง ตารางชื่อลูกค้า (CustName) และ ตารางเมืองของลูกค้า (CustCity) จงสร้างตารางสัมพันธ์ที่แสดง รหัสลูกค้า, ชื่อลูกค้า และ เมืองที่ลูกค้าอยู่ CustName CustCity วิธีที่ใช้ Cartesian product : วิธีที่ใช้ Natural-Join :

  48. The Natural-Join Operation Natural-Join Operation เป็นแบบ associative คือไม่มีผลก่อนหลัง A B C = (A B ) C = A ( B C )

  49. The Division Operation • ใช้สัญลักษณ์ • ใช้สำหรับเมื่อเราต้องการหาแบบ “for all”

  50. Exercise 1 • ในระบบฐานข้อมูลของผู้ผลิตรถยนต์ซึ่งมี • Client(clientID, name, addr) • ลูกค้า ประกอบด้วย รหัสลูกค้า, ชื่อลูกค้า, ที่อยู่ลูกค้า • Service(serviceID, name, addr) • ศูนย์บริการลูกค้า : รหัสศูนย์, ชื่อศูนย์, ที่อยู่ศูนย์ • Car(model, color, weight) • รถยนต์ : รุ่นรถ, สีรถ, น้ำหนัก • Order(clientID, serviceID, model, quantity) • สั่งซื้อ : รหัสลูกค้า, รหัสศูนย์, รุ่นรถ, จำนวน • จงหา • รหัสศูนย์บริการลูกค้าที่ลูกค้าที่มีรหัส “c002” สั่งซื้อ • รหัสศูนย์บริการที่มีลูกค้ารหัส “c002” สั่งซื้อรถรุ่น zx-001 • รหัสศูนย์บริการที่มีลูกค้ารหัส “c002” สั่งซื้อรถสีดำ

More Related