1 / 45

Relational Algebra

Relational Algebra. 357337 – Web Programming and Web Database. Relational Algebra. Relational Algebra เป็นภาษาแบบ procedural query language พื้นฐานคำสั่งของ Relational Algebra คือ select, project, rename → unary operations

mariko-ryan
Download Presentation

Relational Algebra

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. Relational Algebra 357337 – Web Programming and Web Database

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

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

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

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

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

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

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

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

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

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

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

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

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

  15. Union Operation depositor borrower

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

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

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

  19. Set Difference Operation depositor borrower

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

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

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

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

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

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

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

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

  28. ตัวอย่าง depositor borrower หารหัสลูกค้าที่กู้และฝากเงินกับธนาคาร

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

  30. The Rename Operation X borrower borrower

  31. The Rename Operation X borrower

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

  33. Example :The Rename Operation X borrower

  34. Example :The Rename Operation borrower X

  35. Example :The Rename Operation

  36. Example :The Rename Operation - Borrower -

  37. Example :The Rename Operation Borrower -

  38. Additional Operations

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

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

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

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

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

  44. ผลการทำ Relational Algebra operation select (s) project (p) cartesian () x y a b c a a b b c c x y x y x y union () difference (-) rename (r)

  45. join ( ) division () a a a b c d d x y x y z y x y intersection () ผลการทำ Relational Algebra Operation

More Related