Understanding Relational Query Languages in Database Applications

Slide Note
Embed
Share

In this lecture, Mohammad Hammoud discusses the importance of relational query languages (QLs) in manipulating and retrieving data in databases. He covers the strong formal foundation of QLs, their distinction from programming languages, and their effectiveness for accessing large datasets. The session explores formal relational query languages like Relational Algebra and Relational Calculus, providing insights into their structure and utility in commercial languages such as SQL.


Uploaded on Sep 08, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Database Applications (15-415) Relational Algebra Lecture 5, Jan 26, 2020 Mohammad Hammoud

  2. Today Last Session: The relational model Today s Session: Relational algebra Relational query languages (in general) Relational operators Few examples Announcement: PS1 is due on Jan 28 by midnight

  3. Outline Query Languages Relational Operators Examples on Relational Algebra

  4. Relational Query Languages Query languages(QLs) allow manipulating and retrieving data from databases The relational model supports simple and powerful QLs: Strong formal foundation based on logic High amenability for effective optimizations Query Languages != programming languages! QLs are not expected to be Turing complete QLs are not intended to be used for complex calculations QLs support easy and efficient access to large datasets

  5. Formal Relational Query Languages There are two mathematical Query Languages which form the basis for commercial languages (e.g. SQL) Relational Algebra Queries are composed of operators Each query describes a step-by-step procedure for computing the desired answer Very useful for representing execution plans Relational Calculus Queries are subsets of first-order logic Queries describe desired answers without specifying how they will be computed A type of non-procedural (or declarative) formal query language

  6. Formal Relational Query Languages There are two mathematical Query Languages which form the basis for commercial languages (e.g. SQL) Relational Algebra Queries are composed of operators Each query describes a step-by-step procedure for computing the desired answer Very useful for representing execution plans This session s topic Relational Calculus Queries are subsets of first-order logic Queries describe desired answers without specifying how they will be computed A type of non-procedural (or declarative) formal query language Next session s topic (very briefly)

  7. Outline Query Languages Relational Operators Examples on Relational Algebra

  8. Relational Algebra Operators (with notations): Selection ( ) Projection ( ) Cross-product ( ) Set-difference ( ) Union ( ) Intersection ( ) Join ( ) Division ( ) Renaming ( ) 1. 2. 3. 4. 5. 6. 7. 8. 9. Each operation returns a relation, hence, operations can be composed! (i.e., Algebra is closed )

  9. Relational Algebra Operators (with notations): Selection ( ) Projection ( ) Cross-product ( ) Set-difference ( ) Union ( ) Intersection ( ) Join ( ) Division ( ) Renaming ( ) 1. 2. Basic 3. 4. 5. 6. Additional, yet extremely useful! 7. 8. 9. Each operation returns a relation, hence, operations can be composed! (i.e., Algebra is closed )

  10. The Projection Operatation ) (R list att Projection: Project out attributes that are NOT in att-list The schema of the output relation contains ONLY the fields in att-list, with the same names that they had in the input relation ( ) 2 S Example 1: , sname rating Output Relation: Input Relation: sname yuppy lubber guppy rusty rating 9 8 5 10 sid 28 31 44 58 sname yuppy lubber guppy rusty rating 9 8 5 10 age 35.0 55.5 35.0 35.0 S2

  11. The Projection Operation ageS ( ) 2 Example 2: Input Relation: Output Relation: sid 28 31 44 58 sname yuppy lubber guppy rusty rating 9 8 5 10 age 35.0 55.5 35.0 35.0 age 35.0 55.5 S2 The projection operator eliminates duplicates! Note: real DBMSs typically do not eliminate duplicates unless explicitly asked for

  12. The Selection Operation (R condition ) Selection: Selects rows that satisfy the selection condition The schema of the output relation is identical to the schema of the input relation ( ) 2 8S Example: rating Output Relation: sid sname 28 yuppy 58 rusty Input Relation: rating 9 10 age 35.0 35.0 sid 28 31 44 58 sname yuppy lubber guppy rusty rating 9 8 5 10 age 35.0 55.5 35.0 35.0 S2

  13. Operator Composition The output relation can be the input for another relational algebra operation! (Operatorcomposition) ( ( 2 )) S Example: 8 sname rating , rating Input Relation: Intermediate Relation: sid 28 58 sname yuppy rusty rating 9 10 age 35.0 35.0 sid 28 31 44 58 sname yuppy lubber guppy rusty rating 9 8 5 10 age 35.0 55.5 35.0 35.0 Final Output Relation: S2 sname yuppy rusty rating 9 10

  14. The Union Operation Union: The two input relations must be union-compatible R U S Same number of fields `Corresponding fields have the same type The output relation includes all tuples that occur in either R or S or both The schema of the output relation is identical to the schema of R 1 S S 2 Example: Output Relation: Input Relations: sid 22 31 58 44 28 sname dustin lubber rusty guppy yuppy rating 7 8 10 5 9 age 45.0 55.5 35.0 35.0 35.0 sid 22 31 58 sname dustin lubber rusty rating age 7 8 10 sid 28 31 44 58 sname yuppy lubber guppy rusty rating 9 8 5 10 age 35.0 55.5 35.0 35.0 45.0 55.5 35.0 S1 S2

  15. The Intersection Operation Intersection: The two input relations must be union-compatible The output relation includes all tuples that occur in both R and S The schema of the output relation is identical to the schema of R ? ? 1 S S 2 Example: Input Relations: Output Relation: sid 31 58 sname lubber rusty rating 8 10 age 55.5 35.0 sid 22 31 58 sname dustin lubber rusty rating age 7 8 10 sid 28 31 44 58 sname yuppy lubber guppy rusty rating 9 8 5 10 age 35.0 55.5 35.0 35.0 45.0 55.5 35.0 S1 S2

  16. The Set-Difference Operation Set-Difference: The two input relations must be union-compatible The output relation includes all tuples that occur in R but not in S The schema of the output relation is identical to the schema of R ? ? 1 S S 2 Example: Input Relations: Output Relation: sid 22 31 58 sname dustin lubber rusty rating age 7 8 10 sid 28 31 44 58 sname yuppy lubber guppy rusty rating 9 8 5 10 age 35.0 55.5 35.0 35.0 sid 22 sname dustin rating 7 age 45.0 45.0 55.5 35.0 S1 S2

  17. The Cross-Product and Renaming Operations Cross Product: Each row of R is paired with each row of S The schema of the output relation concatenates S1 s and R1 s schemas Conflict: R and S might have the same field name Solution: Rename fields using the Renaming Operator Renaming: ) ), ( ( E F R ??? Output Relation: 1XR S 1 Example: (sid) sname rating age 22 dustin 22 dustin 31 lubber 31 lubber 58 rusty 58 rusty (sid) bid 22 58 22 58 22 58 day 10/10/96 11/12/96 10/10/96 11/12/96 10/10/96 11/12/96 7 7 8 8 10 10 45.0 45.0 55.5 55.5 35.0 35.0 101 103 101 103 101 103 Input Relations: sid bid 22 58 sid sname rating age 22 dustin 31 lubber 58 rusty day 7 8 10 45.0 55.5 35.0 101 103 10/10/96 11/12/96 R1 S1 Conflict: Both S1 and R1 have a field called sid

  18. The Cross-Product and Renaming Operations Cross Product: Each row of R is paired with each row of S The schema of the output relation concatenates S1 s and R1 s schemas Conflict: R and S might have the same field name Solution: Rename fields using the Renaming Operator Renaming: ) ), ( ( E F R ??? Output Relation: 1XR S 1 Example: (sid) sname rating age 22 dustin 22 dustin 31 lubber 31 lubber 58 rusty 58 rusty (sid) bid 22 58 22 58 22 58 day 10/10/96 11/12/96 10/10/96 11/12/96 10/10/96 11/12/96 7 7 8 8 10 10 45.0 45.0 55.5 55.5 35.0 35.0 101 103 101 103 101 103 Input Relations: sid bid 22 58 sid sname rating age 22 dustin 31 lubber 58 rusty day 7 8 10 45.0 55.5 35.0 101 103 10/10/96 11/12/96 R1 S1 ( 1 ( 5 , 1 2 ), 1 ) 1 R C sid sid S

  19. The Join Operation = ( ) R S R S (Theta) Join : The schema of the output relation is the same as that of cross-product It usually includes fewer tuples than cross-product c c 1 1 S R Example: S sid 1 . R sid 1 . Input Relations: Output Relation: (sid) 22 31 sname dustin lubber rating 7 8 age 45.0 55.5 (sid) 58 58 bid 103 103 day 11/12/96 11/12/96 sid bid 22 58 day sid sname rating age 22 dustin 31 lubber 58 rusty 7 8 10 45.0 55.5 35.0 101 103 10/10/96 11/12/96 S1 R1 Will be redundant if the condition is S1.sid = R1.sid!

  20. The Join Operation ( R c c = ) R S S Equi-Join: A special case of theta join where the condition c contains only equalities The schema of the output relation is the same as that of cross-product, but only one copy of the fields for which equality is specified R S Natural Join: Equijoin on all common fields 1 1 S R Example: = . 1 S . 1 R sid sid Output Relation: Input Relations: rating age 7 45.0 8 55.5 10 35.0 S1 sid 22 58 sname dustin rusty rating 7 10 age 45.0 35.0 bid 101 103 day 10/10/96 11/12/96 sid 22 31 58 sname dustin lubber rusty sid 22 58 bid 101 103 day 10/10/96 11/12/96 ONLY one sid column! R1

  21. The Join Operation ( R c c = ) R S S Equi-Join: A special case of theta join where the condition c contains only equalities The schema of the output relation is the same as that of cross-product, but only one copy of the fields for which equality is specified R S Natural Join: Equijoin on all common fields Natural Join 1 1 S R Example: Output Relation: Input Relations: rating age 7 45.0 8 55.5 10 35.0 S1 sid 22 58 sname dustin rusty rating 7 10 age 45.0 35.0 bid 101 103 day 10/10/96 11/12/96 sid 22 31 58 sname dustin lubber rusty sid 22 58 bid 101 103 day 10/10/96 11/12/96 In this case, same as equi-join! R1

  22. The Division Operation S R Division: Not supported as a primitive operator, but useful for expressing queries like: Find sailors who have reserved all boats Let A have 2 fields, x and y; B have only field y: A/B contains all x tuples (sailors) such that for everyy tuple (boat) in B, there is an xy tuple in A Or: If the set of y values (boats) associated with an x value (sailor) in A contains all y values in B, then x value is in A/B x x y | , A y B Formally: A/B = In general, x and y can be any lists of fields; y is the list of fields in B, and x y is the list of fields in A

  23. Examples of Divisions sno s1 s2 s3 s4 A/B1 sno s1 s1 s1 s1 s2 s2 s3 s4 s4 pno p1 p2 p3 p4 p1 p2 p2 p2 p4 A pno p1 p2 p4 B3 pno p2 B1 pno p2 p4 B2 sno s1 A/B3 sno s1 s4 A/B2

  24. Expressing A/B Using Basic Operators Division can be derived from the fundamental operators Idea: For A/B, compute all x values that are not `disqualified by some y value in B x value is disqualified if by attaching y value from B, we obtain an xy tuple that is not in A (( ( ) ) ) xA B A Disqualified x values: x xA ( ) all disqualified tuples A/B:

  25. Relational Algebra: Summary Operators (with notations): Selection ( ): selects a subset of rows from a relation 1. Projection ( ): deletes unwanted columns from a relation 2. Cross-product ( ): allows combining two relations 3. Set-difference ( ): retains tuples which are in relation 1, but not in relation 2 4. Union ( ): retains tuples which are in either relation 1 or relation 2, or in both 5.

  26. Relational Algebra: Summary Operators (with notations): Intersection ( ): retains tuples which are in relation 1 and in relation 2 6. Join ( ): allows combining two relations according to a specific condition (e.g., theta, equi and natural joins) 7. Division ( ): generates the largest instance Q such that Q B A when computing A/B 8. Renaming ( ): returns an instance of a new relation with some fields being potentially renamed 9.

  27. Outline Query Languages Relational Operators Examples on Relational Algebra

  28. Additional Examples Q1: Find names of sailors who ve reserved boat #103 Sid Bid Day Sid Sname Rating Age 22 101 10/10/98 22 Dustin 7 45.0 22 102 10/10/98 29 Brutus 1 33.0 22 103 10/8/98 31 Lubber 8 55.5 Bid Bname Color 22 104 10/7/98 32 Andy 8 25.5 101 Interlake Blue 31 102 11/10/98 58 Rusty 10 35.0 102 Interlake Red 31 103 11/6/98 64 Horatio 7 35.0 103 Clipper Green 31 104 11/12/98 71 Zorba 10 16.0 104 Marine Red 64 101 9/5/98 74 Horatio 9 35.0 An Instance B1 of Boats 64 102 9/8/98 85 Art 3 25.5 74 103 9/8/98 95 Bob 3 63.5 An Instance R2 of Reserves An Instance S3 of Sailors

  29. Additional Examples Q1: Find names of sailors who ve reserved boat #103 Re ( ( 103 bid sname = ) ) serves Sailors OR: ( Re ( )) serves Sailors sname 103 = OR: bid Temp Temp ( ( , 1 , 2 (Temp Re Sailors ) serves bid= Temp 103 1 ) 2 ) sname Which one to choose?

  30. Additional Examples Q2: Find names of sailors who ve reserved a red boat Sid Bid Day Sid Sname Rating Age 22 101 10/10/98 22 Dustin 7 45.0 22 102 10/10/98 29 Brutus 1 33.0 22 103 10/8/98 31 Lubber 8 55.5 Bid Bname Color 22 104 10/7/98 32 Andy 8 25.5 101 Interlake Blue 31 102 11/10/98 58 Rusty 10 35.0 102 Interlake Red 31 103 11/6/98 64 Horatio 7 35.0 103 Clipper Green 31 104 11/12/98 71 Zorba 10 16.0 104 Marine Red 64 101 9/5/98 74 Horatio 9 35.0 An Instance B1 of Boats 64 102 9/8/98 85 Art 3 25.5 74 103 9/8/98 95 Bob 3 63.5 An Instance R2 of Reserves An Instance S3 of Sailors

  31. Additional Examples Q2: Find names of sailors who ve reserved a red boat sname color ' ' = (( ) Re ) redBoats serves Sailors OR: ( (( ) Re ) ) Boats s Sailors sname = ' ' sid bid color red A query optimizer can find the second one, given the first solution!

  32. Additional Examples Q3: Find sailors who ve reserved a red or a green boat Sid Bid Day Sid Sname Rating Age 22 101 10/10/98 22 Dustin 7 45.0 22 102 10/10/98 29 Brutus 1 33.0 22 103 10/8/98 31 Lubber 8 55.5 Bid Bname Color 22 104 10/7/98 32 Andy 8 25.5 101 Interlake Blue 31 102 11/10/98 58 Rusty 10 35.0 102 Interlake Red 31 103 11/6/98 64 Horatio 7 35.0 103 Clipper Green 31 104 11/12/98 71 Zorba 10 16.0 104 Marine Red 64 101 9/5/98 74 Horatio 9 35.0 An Instance B1 of Boats 64 102 9/8/98 85 Art 3 25.5 74 103 9/8/98 95 Bob 3 63.5 An Instance R2 of Reserves An Instance S3 of Sailors

  33. Additional Examples Q3: Find sailors who ve reserved a red or a green boat ( ,( )) Tempboats Boats = = ' ' ' ' color red color green snameTempboats ( Re ) serves Sailors Can we define Tempboats using union? What happens if is replaced by ?

  34. Additional Examples Q4: Find sailors who ve reserved a red and a green boat Sid Bid Day Sid Sname Rating Age 22 101 10/10/98 22 Dustin 7 45.0 22 102 10/10/98 29 Brutus 1 33.0 22 103 10/8/98 31 Lubber 8 55.5 Bid Bname Color 22 104 10/7/98 32 Andy 8 25.5 101 Interlake Blue 31 102 11/10/98 58 Rusty 10 35.0 102 Interlake Red 31 103 11/6/98 64 Horatio 7 35.0 103 Clipper Green 31 104 11/12/98 71 Zorba 10 16.0 104 Marine Red 64 101 9/5/98 74 Horatio 9 35.0 An Instance B1 of Boats 64 102 9/8/98 85 Art 3 25.5 74 103 9/8/98 95 Bob 3 63.5 An Instance R2 of Reserves An Instance S3 of Sailors

  35. Additional Examples Q4: Find sailors who ve reserved a red and a green boat ( , (( ) Re )) Tempred redBoats serves = ' ' sid color (( color greenBoats = ( , ) Re )) Tempgreen serves ' ' sid snameTempred (( ) ) Tempgreen Sailors

  36. Additional Examples Q5: Find the names of sailors who ve reserved all boats Sid Bid Day Sid Sname Rating Age 22 101 10/10/98 22 Dustin 7 45.0 22 102 10/10/98 29 Brutus 1 33.0 22 103 10/8/98 31 Lubber 8 55.5 Bid Bname Color 22 104 10/7/98 32 Andy 8 25.5 101 Interlake Blue 31 102 11/10/98 58 Rusty 10 35.0 102 Interlake Red 31 103 11/6/98 64 Horatio 7 35.0 103 Clipper Green 31 104 11/12/98 71 Zorba 10 16.0 104 Marine Red 64 101 9/5/98 74 Horatio 9 35.0 An Instance B1 of Boats 64 102 9/8/98 85 Art 3 25.5 74 103 9/8/98 95 Bob 3 63.5 An Instance R2 of Reserves An Instance S3 of Sailors

  37. Additional Examples Q5: Find the names of sailors who ve reserved all boats ( , ( Re ) / ( )) Tempsids serves bidBoats , sid bid snameTempsids ( ) Sailors How can we find sailors who ve reserved all Interlake boats?

  38. Summary The relational model has rigorously defined query languages that are simple and powerful Relational algebra is operational; useful as internal representation for query evaluation plans Several ways of expressing a given query; a query optimizer should choose the most efficient version

  39. Next Class Relational Calculus

Related


More Related Content