Presentation is loading. Please wait.

Presentation is loading. Please wait.

INFS614, Fall 08 1 Relational Algebra Lecture 4. INFS614, Fall 08 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of.

Similar presentations


Presentation on theme: "INFS614, Fall 08 1 Relational Algebra Lecture 4. INFS614, Fall 08 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of."— Presentation transcript:

1 INFS614, Fall 08 1 Relational Algebra Lecture 4

2 INFS614, Fall 08 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of data from a database. v Relational model supports simple, powerful QLs: – Strong formal foundation based on logic. – Allows for much optimization. v Query Languages != programming languages! – QLs not expected to be “Turing complete”. – QLs not intended to be used for complex calculations. – QLs support easy, efficient access to large data sets.

3 INFS614, Fall 08 3 Formal Relational Query Languages Two mathematical Query Languages form the basis for “real” languages (e.g. SQL), and for implementation: ¶ Relational Algebra: More operational, very useful for representing execution plans. · Relational Calculus: Let users describe what they want, rather than how to compute it. (Non-operational, declarative.)  Understanding Algebra is key to understanding SQL,  and query processing !

4 INFS614, Fall 08 4 Algebra Preliminaries v A query is applied to relation instances, and the result of a query is also a relation instance. – Schemas of input relations for a query are fixed. – The schema for the result of a given query is also fixed! Determined by definition of query language constructs.

5 INFS614, Fall 08 5 Example Instances R1 S1 S2 v “Sailors” and “Reserves” relations for our examples.

6 INFS614, Fall 08 6 Algebra Operations v Look what we want to get from the following table:

7 INFS614, Fall 08 7 Projection v Deletes attributes that are not in projection list. v Schema of result contains exactly the fields in the projection list, with the same names that they had in the (only one) input relation. v Projection operator has to eliminate duplicates ! (Why??) – Note: real systems typically don’t do duplicate elimination unless the user explicitly asks for it. (Why not?)

8 INFS614, Fall 08 8 Selection v Selects rows that satisfy selection condition. v No duplicates in result! (Why?) v Schema of result identical to schema of (only one) input relation. S2

9 INFS614, Fall 08 9 Composition of Operations v Result relation can be the input for another relational algebra operation ! (Operator composition.) S2

10 INFS614, Fall 08 10 What do we want to get from two relations? R1 S1 What about: Who reserved boat 101? Or: Find the name of the sailor who reserved boat 101.

11 INFS614, Fall 08 11 Cross-Product v Each row of S1 is paired with each row of R1. v Result schema has one field per field of S1 and R1, with field names inherited.  Renaming operator (because naming conflict) :

12 INFS614, Fall 08 12 Why does this cross product help Query: Find the name of the sailor who reserved boat 101. * Note my use of “temporary” relation CP.

13 INFS614, Fall 08 13 Another example v Find the name of the sailor having the highest rating. What’s in “Result?” ? Does it answer our query?

14 INFS614, Fall 08 14 Union, Intersection, Set-Difference v All of these operations take two input relations, which must be union- compatible: – Same number of fields. – `Corresponding’ fields have the same type. v What is the schema of result?

15 INFS614, Fall 08 15 Back to our query v Find the name of the sailor having the highest rating. * Why not project on sname only for Tmp?

16 INFS614, Fall 08 16 Relational Algebra (Summary) v Basic operations: – Selection (  ) Selects a subset of rows from relation. – Projection (  ) Deletes unwanted columns from relation. – Cross-product (  ) Allows us to combine two relations. – Set-difference ( - ) Tuples in reln. 1, but not in reln. 2. – Union (  ) Tuples either in reln. 1 or in reln. 2 or in both. – Rename (  ) Changes names of the attributes v Since each operation returns a relation, operations can be composed ! (Algebra is “closed”.) v Use of temporary relations recommended.

17 INFS614, Fall 08 17 Natural Join v Natural-Join: v Result schema similar to cross-product, but only one copy of each field which appears in both relations. v Natural Join = Cross Product + Selection on common fields + drop duplicate fields.

18 INFS614, Fall 08 18 Query revisited using natural join Query: Find the name of the sailor who reserved boat 101.

19 INFS614, Fall 08 19 Consider yet another query v Find the sailor(s) (sid) who reserved all the red boats. R1B

20 INFS614, Fall 08 20 Start an attempt v who reserved what boat: v All the red boats:

21 INFS614, Fall 08 21 Division v Not supported as a primitive operator, but useful for expressing queries like: Find sailors who have reserved all red boats. v Let S1 have 2 fields, x and y; S2 have only field y: – S1/S2 = – i.e., S1/S2 contains all x tuples (sailors) such that for every y tuple (redboat) in S2, there is an xy tuple in S1 (i.e, x reserved y). v In general, x and y can be any lists of fields; y is the list of fields in S2, and x  y is the list of fields of S1.

22 INFS614, Fall 08 22 Examples of Division A/B A B1 B2 B3 A/B1A/B2A/B3

23 INFS614, Fall 08 23 Find names of sailors who’ve reserved boat #103  Solution 1 :  Solution 2 :  Solution 3 :

24 INFS614, Fall 08 24 Find names of sailors who’ve reserved a red boat  Information about boat color only available in Boats; so need an extra join :  A more efficient solution :  A query optimizer can find this given the first solution!

25 INFS614, Fall 08 25 Find sailors who’ve reserved a red or a green boat v Can identify all red or green boats, then find sailors who’ve reserved one of these boats:  Can also define Tempboats using union! (How?) v What happens if is replaced by in this query?

26 INFS614, Fall 08 26 Find sailors who’ve reserved a red and a green boat v Previous approach won’t work! Must identify sailors who’ve reserved red boats, sailors who’ve reserved green boats, then find the intersection (note that sid is a key for Sailors):

27 INFS614, Fall 08 27 Find the names of sailors who’ve reserved all boats v Uses division; schemas of the input relations must be carefully chosen:  To find sailors who’ve reserved all ‘Interlake’ boats:.....

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


Download ppt "INFS614, Fall 08 1 Relational Algebra Lecture 4. INFS614, Fall 08 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of."

Similar presentations


Ads by Google