Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Relational Algebra & SQL. 2 Why SQL & Relational Algebra? uSQL is a very-high-level language. wSay “what to do” rather than “how to do it.” wAvoid a.

Similar presentations


Presentation on theme: "1 Relational Algebra & SQL. 2 Why SQL & Relational Algebra? uSQL is a very-high-level language. wSay “what to do” rather than “how to do it.” wAvoid a."— Presentation transcript:

1 1 Relational Algebra & SQL

2 2 Why SQL & Relational Algebra? uSQL is a very-high-level language. wSay “what to do” rather than “how to do it.” wAvoid a lot of data-manipulation details needed in procedural languages like C++ or Java. uDatabase management system figures out “best” way to execute query. wCalled “query optimization.” uRelational Algebra used in the Query Engine

3 3 Select-From-Where Statements SELECT desired attributes FROM one or more tables WHERE condition about tuples of the tables

4 4 Our Running Example uAll our SQL queries will be based on the following database schema. wUnderline indicates key attributes. Beers(name, manf) Bars(name, addr, license) Drinkers(name, addr, phone) Likes(drinker, beer) Sells(bar, beer, price) Frequents(drinker, bar)

5 5 Example: Selection R1 := σ C (R2) Relation Sells: barbeerprice Joe’sBud2.50 Joe’sMiller2.75 Sue’sBud2.50 Sue’sMiller3.00 JoeMenu := σ bar=“Joe’s” (Sells): barbeerprice Joe’sBud2.50 Joe’sMiller2.75 SELECT * FROM Sells WHERE bar = ‘Joe’’s’ SELECT * FROM Sells WHERE bar = ‘Joe’’s’

6 6 Example: Projection R1 := π A (R2) Relation Sells: barbeerprice Joe’sBud2.50 Joe’sMiller2.75 Sue’sBud2.50 Sue’sMiller3.00 Prices := π beer,price (Sells): beerprice Bud2.50 Miller2.75 Miller3.00 SELECT beer, price FROM Sells SELECT beer, price FROM Sells

7 7 Example: R3 := R1 Χ R2 R1(A,B ) 12 34 R2(B,C ) 56 78 9 10 R3(A,R1.B,R2.B,C ) 1256 1278 129 10 3456 3478 349 10 SELECT * FROM R1, R2 SELECT * FROM R1, R2

8 8 Example: Theta Join R3 := R1 ⋈ C R2 Sells(bar,beer,price ) Bars(name, addr) Joe’sBud2.50 Joe’s Maple St. Joe’sMiller2.75 Sue’s River Rd. Sue’sBud2.50 Sue’sCoors3.00 BarInfo := Sells ⋈ Sells.bar = Bars.name Bars BarInfo(bar,beer,price,name,addr ) Joe’sBud2.50Joe’sMaple St. Joe’sMiller2.75Joe’sMaple St. Sue’sBud2.50Sue’sRiver Rd. Sue’sCoors3.00Sue’sRiver Rd. SELECT * FROM Sells, Bars WHERE Sells.bar= Bars.name SELECT * FROM Sells, Bars WHERE Sells.bar= Bars.name

9 9 Example: Natural Join R3 := R1 ⋈ R2 Sells(bar,beer,price )Bars(bar,addr ) Joe’sBud2.50Joe’sMaple St. Joe’sMiller2.75Sue’sRiver Rd. Sue’sBud2.50 Sue’sCoors3.00 BarInfo(bar,beer,price,addr ) Joe’sBud2.50Maple St. Joe’sMilller2.75Maple St. Sue’sBud2.50River Rd. Sue’sCoors3.00River Rd. BarInfo := Sells ⋈ Bars SELECT S.bar, beer, price, addr FROM Sells S, Bars B WHERE S.bar=B.bar SELECT S.bar, beer, price, addr FROM Sells S, Bars B WHERE S.bar=B.bar

10 10 Renaming  The ρ operator gives a new schema to a relation.  R1 := ρ R1(A1,…,An) (R2) makes R1 be a relation with attributes A1,…,An and the same tuples as R2. uSimplified notation: R1(A1,…,An) := R2.

11 11 Example: Renaming Bars(name, addr ) Joe’sMaple St. Sue’sRiver Rd. R(bar, addr ) Joe’sMaple St. Sue’sRiver Rd. R(bar, addr) := Bars CREATE TABLE R AS SELECT name as bar, addr FROM Bars CREATE TABLE R AS SELECT name as bar, addr FROM Bars SELECT name as bar, addr FROM Bars SELECT name as bar, addr FROM Bars OR

12 12 Union, Intersection, and Difference uIn Relational Algebra: wR3 = R1 ∪ R2 (tuples in R1 or R2) wR3 = R1 ∩ R2 (tuples in both R1 and R2)  R3 = R1 — R2 (tuples in R1 and not in R2) uIn SQL: w( ) UNION ( ) SELECT surname FROM instructors UNION SELECT surname FROM students w( ) INTERSECT ( ) SELECT surname FROM instructors INTERSECT SELECT surname FROM students w( ) EXCEPT ( ) SELECT surname FROM instructors EXCEPT SELECT surname FROM students

13 13 Building Complex Expressions uCombine operators with parentheses and precedence rules.  Example: the theta-join R3 := R1 ⋈ C R2 can be written: R3 := σ C (R1 Χ R2) uPrecedence of relational operators: 1.[ σ, π, ρ ] (highest). 2.[ Χ, ⋈ ]. 3.∩. 4.[ ∪, — ]

14 14 Expression Trees uLeaves are operands --- either variables standing for relations or particular, constant relations. uInterior nodes are operators, applied to their child or children.

15 15 Query Tree: Find the names of all the bars that are either on Maple St. or sell Bud for less than $3 BarsSells σ addr = “Maple St.” σ price<3 AND beer=“Bud” π name ρ R(name) π bar ∪ Bars(name, addr)Sells(bar, beer, price)

16 16 Example: Self-Join uUsing Sells(bar, beer, price), find the bars that sell two different beers at the same price. uStrategy: by renaming, define a copy of Sells, called S(bar, beer1, price). The natural join of Sells and S consists of quadruples (bar, beer, beer1, price) such that the bar sells both beers at this price.

17 17 The Tree Sells ρ S(bar, beer1, price) ⋈ π bar σ beer != beer1

18 18 Schemas for Results uUnion, intersection, and difference: the schemas of the two operands must be the same, so use that schema for the result. uSelection: schema of the result is the same as the schema of the operand. uProjection: list of attributes tells us the schema.

19 19 Schemas for Results --- (2) uProduct: schema is the attributes of both relations. wUse R.A, etc., to distinguish two attributes named A. uTheta-join: same as product. uNatural join: union of the attributes of the two relations. uRenaming: the operator tells the schema.

20 20 Relational Algebra on Bags uA bag (or multiset ) is like a set, but an element may appear more than once. uExample: {1,2,1,3} is a bag. uExample: {1,2,3} is also a bag that happens to be a set. uSQL is actually a bag language. uSome operations, like projection, are more efficient on bags than sets.

21 21 Example: Selection R(A,B ) 12 56 12 AB1212AB1212 SELECT * FROM R WHERE A+B < 5 SELECT * FROM R WHERE A+B < 5 σ A+B < 5 (R) =AB 12 SELECT DISTINCT * FROM R WHERE A+B < 5 SELECT DISTINCT * FROM R WHERE A+B < 5

22 22 Example: Projection R(A,B ) 12 56 12 A151A151 SELECT DISTINCT A FROM R SELECT DISTINCT A FROM R π A (R) =A 1 5 SELECT A FROM R SELECT A FROM R

23 23 Example: Theta-Join R( A, B ) S(B, C ) 1 2 3 4 5 6 7 8 1 2 AR.BS.BC 1234 1278 5678 1234 1278 AR.BS.BC 1234 1278 5678 SELECT A, R.B, S.B, C FROM R, S WHERE R.B < S.B SELECT A, R.B, S.B, C FROM R, S WHERE R.B < S.B R ⋈ R.B<S.B S SELECT DISTINCT A, R.B, S.B, C FROM R, S WHERE R.B < S.B SELECT DISTINCT A, R.B, S.B, C FROM R, S WHERE R.B < S.B

24 24 Union, Intersection, Difference uIn SQL UNION, INTERSECTION, EXCEPT return distinct tuples (no duplicates) uUse UNION ALL, INTERSECTION ALL, EXCEPT ALL to return duplicates (if any)  Bag Union: {1,2,1} ∪ {1,1,2,3,1} = {1,1,1,1,1,2,2,3}  Bag Intersection: {1,2,1,1} ∩ {1,2,1,3} = {1,1,2} uBag Difference: {1,2,1,1} – {1,2,3} = {1,1}

25 25 SQL Semantics Select-From-Where Statements Multirelation Queries Subqueries

26 26 Example uUsing Beers(name, manf), what beers are made by Anheuser-Busch? SELECT name FROM Beers WHERE manf = ’Anheuser-Busch’;

27 27 Result of Query name Bud Bud Lite Michelob... The answer is a relation with a single attribute, name, and tuples with the name of each beer by Anheuser-Busch, such as Bud.

28 28 Meaning of Single-Relation Query uBegin with the relation in the FROM clause. uApply the selection indicated by the WHERE clause. uApply the extended projection indicated by the SELECT clause.

29 29 Operational Semantics Check if Anheuser-Busch name manf BudAnheuser-Busch Include t.name in the result, if so Tuple-variable t loops over all tuples

30 30 Operational Semantics --- General uThink of a tuple variable visiting each tuple of the relation mentioned in FROM. uCheck if the “current” tuple satisfies the WHERE clause. uIf so, compute the attributes or expressions of the SELECT clause using the components of this tuple.

31 31 * In SELECT clauses uWhen there is one relation in the FROM clause, * in the SELECT clause stands for “all attributes of this relation.” uExample: Using Beers(name, manf): SELECT * FROM Beers WHERE manf = ’Anheuser-Busch’;

32 32 Result of Query: namemanf BudAnheuser-Busch Bud LiteAnheuser-Busch MichelobAnheuser-Busch... Now, the result has each of the attributes of Beers.

33 33 Renaming Attributes uIf you want the result to have different attribute names, use “AS ” to rename an attribute. uExample: Using Beers(name, manf): SELECT name AS beer, manf FROM Beers WHERE manf = ’Anheuser-Busch’

34 34 Result of Query: beermanf BudAnheuser-Busch Bud LiteAnheuser-Busch MichelobAnheuser-Busch...

35 35 Expressions in SELECT Clauses uAny expression that makes sense can appear as an element of a SELECT clause. uExample: Using Sells(bar, beer, price): SELECT bar, beer, price*114 AS priceInYen FROM Sells;

36 36 Result of Query barbeerpriceInYen Joe’sBud285 Sue’sMiller342 … … …

37 37 Example: Constants as Expressions uUsing Likes(drinker, beer): SELECT drinker, ’likes Bud’ AS whoLikesBud FROM Likes WHERE beer = ’Bud’;

38 38 Result of Query drinkerwhoLikesBud Sallylikes Bud Fredlikes Bud …

39 39 Example: Information Integration uWe often build “data warehouses” from the data at many “sources.” uSuppose each bar has its own relation Menu(beer, price). uTo contribute to Sells(bar, beer, price) we need to query each bar and insert the name of the bar.

40 40 Information Integration --- (2)  For instance, at Joe’s Bar we can issue the query: SELECT ’Joe’’s Bar’as bar, beer, price FROM Menu; bar beer price Joe’s BarBud 2.50 Joe’s BarBud Light 2.75 …

41 41 Complex Conditions in WHERE Clause uBoolean operators AND, OR, NOT. uComparisons =, <>,, =. wAnd many other operators that produce boolean-valued results.

42 42 Example: Complex Condition uUsing Sells(bar, beer, price), find the price Joe’s Bar charges for Bud: SELECT price FROM Sells WHERE bar = ’Joe’’s Bar’ AND beer = ’Bud’;

43 43 Patterns uA condition can compare a string to a pattern by: w LIKE or NOT LIKE uPattern is a quoted string with % = “any string”; _ = “any character.”

44 44 Example: LIKE uUsing Drinkers(name, addr, phone) find the drinkers with exchange 555: SELECT name FROM Drinkers WHERE phone LIKE ’%555-_ _ _ _’;

45 45 NULL Values uTuples in SQL relations can have NULL as a value for one or more components. uMeaning depends on context. Two common cases: wMissing value : e.g., we know Joe’s Bar has some address, but we don’t know what it is. wInapplicable : e.g., the value of attribute spouse for an unmarried person.

46 46 Comparing NULL’s to Values uThe logic of conditions in SQL is really 3- valued logic: TRUE, FALSE, UNKNOWN. uComparing any value (including NULL itself) with NULL yields UNKNOWN. uA tuple is in a query answer iff the WHERE clause is TRUE (not FALSE or UNKNOWN).

47 47 Three-Valued Logic uTo understand how AND, OR, and NOT work in 3-valued logic, think of TRUE = 1, FALSE = 0, and UNKNOWN = ½. uAND = MIN; OR = MAX, NOT(x) = 1-x. uExample: TRUE AND (FALSE OR NOT(UNKNOWN)) = MIN(1, MAX(0, (1 - ½ ))) = MIN(1, MAX(0, ½ )) = MIN(1, ½ ) = ½.

48 Three-valued Logic uAND = MIN; OR = MAX, NOT(x) = 1-x OR uTRUE and UNKNOWN = UNKNOWN uFALSE and UNKNOWN = FALSE uTRUE or UNKNOWN = TRUE uFALSE or UNKNOWN = UNKNOWN unot UNKNOWN = UNKNOWN uExample: TRUE AND (FALSE OR NOT(UNKNOWN)) = TRUE AND (FALSE OR UNKNOWN) = TRUE AND UNKNOWN = UNKNOWN 48

49 49 Surprising Example uFrom the following Sells relation: barbeerprice Joe’s BarBudNULL SELECT bar FROM Sells WHERE price = 2.00; UNKNOWN

50 50 Reason: 2-Valued Laws != 3-Valued Laws uSome common laws, like commutativity of AND, hold in 3-valued logic. uBut not others, e.g., the law of the excluded middle : p OR NOT p = TRUE. wWhen p = UNKNOWN, the left side is MAX( ½, (1 – ½ )) = ½ != 1. wi.e., UNKNOWN or not UNKNOWN = UNKNOWN

51 51 Multirelation Queries uInteresting queries often combine data from more than one relation. uWe can address several relations in one query by listing them all in the FROM clause. uDistinguish attributes of the same name by “. ”.

52 52 Example: Joining Two Relations uUsing relations Likes(drinker, beer) and Frequents(drinker, bar), find the beers liked by at least one person who frequents Joe’s Bar. SELECT beer FROM Likes, Frequents WHERE bar = ’Joe’’s Bar’ AND Frequents.drinker = Likes.drinker;

53 53 Formal Semantics uAlmost the same as for single-relation queries: 1.Start with the product of all the relations in the FROM clause. 2.Apply the selection condition from the WHERE clause. 3.Project onto the list of attributes and expressions in the SELECT clause.

54 54 Operational Semantics uImagine one tuple-variable for each relation in the FROM clause. wThese tuple-variables visit each combination of tuples, one from each relation. uIf the tuple-variables are pointing to tuples that satisfy the WHERE clause, send these tuples to the SELECT clause.

55 55 Example drinker bardrinker beer tv1tv2 Sally Bud Sally Joe’s Likes Frequents to output check these are equal check for Joe

56 56 Explicit Tuple-Variables uSometimes, a query needs to use two copies of the same relation. uDistinguish copies by following the relation name by the name of a tuple- variable, in the FROM clause. uIt’s always an option to rename relations this way, even when not essential.

57 57 Example: Self-Join uFrom Beers(name, manf), find all pairs of beers by the same manufacturer. wDo not produce pairs like (Bud, Bud). wProduce pairs in alphabetic order, e.g. (Bud, Miller), not (Miller, Bud). SELECT b1.name, b2.name FROM Beers b1, Beers b2 WHERE b1.manf = b2.manf AND b1.name < b2.name;


Download ppt "1 Relational Algebra & SQL. 2 Why SQL & Relational Algebra? uSQL is a very-high-level language. wSay “what to do” rather than “how to do it.” wAvoid a."

Similar presentations


Ads by Google