Presentation is loading. Please wait.

Presentation is loading. Please wait.

1. Chapter 2: The relational Database Modeling Section 2.4: An algebraic Query Language Chapter 5: Algebraic and logical Query Languages Section 5.1:

Similar presentations


Presentation on theme: "1. Chapter 2: The relational Database Modeling Section 2.4: An algebraic Query Language Chapter 5: Algebraic and logical Query Languages Section 5.1:"— Presentation transcript:

1 1

2 Chapter 2: The relational Database Modeling Section 2.4: An algebraic Query Language Chapter 5: Algebraic and logical Query Languages Section 5.1: Relational operations on Bags 2

3 Mathematical system consisting of: – Operands --- variables or values from which new values can be constructed. – Operators --- symbols denoting procedures that construct new values from given values. 3

4 An algebra whose operands are relations or variables that represent relations. Operators are designed to do the most common things that we need to do with relations in a database. – The result is an algebra that can be used as a query language for relations. 4

5 Union, intersection, and difference. – Usual set operations, but both operands must have the same relation schema. Selection: picking certain rows. Projection: picking certain columns. Products and joins: compositions of relations. Renaming of relations and attributes. 5

6 Selection: picking certain rows. Projection: picking certain columns. Products and joins: compositions of relations. Renaming of relations and attributes. 6

7 R1 := σ C (R2) – C is a condition (as in “if” statements) that refers to attributes of R2. – R1 is all those tuples of R2 that satisfy C. Attributes in R1 and R2 are the same 7

8 SamDrink = σ Supermarket =“Sam” (Sells): Supermarket Drinkprice Sam Pepsi2.50 Sam Juice2.75 Relation Sells: Supermarket Drinkprice Sam Pepsi2.50 Sam Juice2.75 Birds Pepsi2.50 Birds Juice3.00 8

9 Selection: picking certain rows. Projection: picking certain columns. Products and joins: compositions of relations. Renaming of relations and attributes. 9

10 R1 := π L (R2) – L is a list of attributes from the schema of R2. – R1 is constructed by looking at each tuple of R2, extracting the attributes on list L, in the order specified, and creating from those components a tuple for R1. – Eliminate duplicate tuples, if any. Attributes of R1 are not necessarily the same as of R2 Schema of R1 is determined by L 10

11 Prices := π Drink,price (Sells): Drinkprice Pepsi2.50 Juice2.75 Juice3.00 Relation Sells: Supermarket Drinkprice Sam Pepsi2.50 Sam Juice2.75 Birds Pepsi2.50 Birds Juice3.00 11

12 Using the same π L operator, we allow the list L to contain arbitrary expressions involving attributes: 1.Arithmetic on attributes, e.g., A+B->C. 2.Duplicate occurrences of the same attribute 12

13 Relation Sells: LibraryNb of books Nb of journals Antoine1450 500 Diwan1200 650 Future700 400 Stat := π Library, Nb of books + Nb of journals -> Total (Sells): LibraryTotal Antoine1950 Diwan1850 Future1100 13

14 Relation R: AB 12 34 R1 := π A+B -> C, A, A (R) C A1A2 311 733 14

15 Selection: picking certain rows. Projection: picking certain columns. Products and joins: compositions of relations. Renaming of relations and attributes. 15

16 R3 := R1 Χ R2 – Pair each tuple(row) t1 of R1 with each tuple t2 of R2. – Concatenation t1t2 is a tuple of R3. – Schema of R3 is the attributes of R1 and then R2, in order. – Attributes with the same name are prefixed by the relation name. Example: for attribute A of the same name in R1 and R2 we use R1.A and R2.A 16

17 R3 := R1 Χ R2 R1(A, B) X R2(B, C) := R3(A, R1.B, R2.B, C) R1AB 12 34 R2BC 56 78 9 10 R3AR1.BR2.BC 1256 1278 129 10 3456 3478 349 10 17

18 R3 := R1 ⋈ C R2 – Take the product R1 Χ R2. – Then apply σ C to the result (projection). C can be any Boolean-valued condition. – Historic versions of this operator allowed only A  B, where  is =, <, etc.; hence the name “theta-join.” 18

19 SuperMInfo:= Sells ⋈ Sells.SuperM= SuperMarket.Name SuperMarket SuperMDrink priceName Street SamPepsi 2.50Sam Mazraa SamJuice 2.75Sam Mazraa BirdsPepsi 2.50Birds Mar Elias BirdsJuice 3.00Birds Mar Elias Relation Sells: SuperM Drinkprice Sam Pepsi2.50 Sam Juice2.75 Birds Pepsi2.50 Birds Juice3.00 19 Relation SuperMarket: NameStreet Sam Mazraa BirdsMar Elias

20 A useful join variant (natural join) connects two relations by: – Equating attributes of the same name, and – Projecting out one copy of each pair of equated attributes. Denoted R3 := R1 ⋈ R2. 20 NOTE: this is what make natural Join work

21 SuperMInfo:= Sells ⋈ SuperMarket SuperMDrink price Street SamPepsi 2.50 Mazraa SamJuice 2.75 Mazraa BirdsPepsi 2.50 Mar Elias BirdsJuice 3.00 Mar Elias Relation Sells: SuperM Drinkprice Sam Pepsi2.50 Sam Juice2.75 Birds Pepsi2.50 Birds Juice3.00 21 Relation SuperMarket: SuperMStreet Sam Mazraa BirdsMar Elias

22 Selection: picking certain rows. σ Projection: picking certain columns. π Products and joins: compositions of relations. Χ Renaming of relations and attributes. ρ 22

23 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. Simplified notation: R1(A1,…,An) := R2. 23

24 24 SuperMarket(SuperMStreet ) Sam Mazraa BirdsMar Elias R(Name, Street) := SuperMarket R (NAMEStreet ) Sam Mazraa BirdsMar Elias

25 Combine operators with parentheses and precedence rules. Three notations, just as in arithmetic: 1.Sequences of assignment statements. 2.Expressions with several operators. 3.Expression trees. 25

26 Create temporary relation names. Renaming can be implied by giving relations a list of attributes. Example: R3 := R1 ⋈ C R2 can be written: R4 := R1 Χ R2 R3 := σ C (R4) 26

27 Example: the theta-join R3 := R1 ⋈ C R2 can be written: R3 := σ C (R1 Χ R2) Precedence of relational operators: 1.[ σ, π, ρ ] (highest). 2.[ Χ, ⋈ ]. 3.∩. 4.[ ∪, — ] 27

28 Leaves are operands --- either variables standing for relations or particular, constant relations. Interior nodes are operators, applied to their child or children. 28

29 Using the relations SuperMarket(Name, Street) and Sells(Name, Drink, Price), find the names of all the supermarkets that are either on Mazraa St. or sell Pepsi for less than $3. 29 Relation Sells: Name DrinkPrice Sam Pepsi2.50 Sam Juice2.75 Birds Pepsi2.50 Birds Juice3.00 Relation SuperMarket: NameStreet Sam Mazraa BirdsMar Elias Name

30 30 SuperMarket Sells σ Street = “Mazraa” σ price<3 AND Drink=“Pepsi” π N ame ρ R(Name ) π SuperM ∪ Find the NameOn Mazraa streetOf all supermarketsThat sellPepsi for less than $3 OR Find the NameOf all supermarkets

31 Using Sells(SuperM, Drink, Price), find the Supermarkets that sell two different drinks at the same price. Strategy: by renaming, define a copy of Sells, called S(SuperM, Drink1, Price). The natural join of Sells and S consists of quadruples (SuperM, Drink, Drink1, Price) such that the Supermarket sells both drinks at this price. 31

32 32 Sells ρ S(SuperM, Drink1, Price) ⋈ π SuperM σ Drink != Drink1 Relation Sells: SuperM DrinkPrice Sam Pepsi2.75 Sam Juice2.75 Birds Pepsi2.50 Birds Juice3.00

33 33 Sells ρ S(SuperM, Drink1, Price) ⋈ π SuperM σ Drink != Drink1 Relation Sells: SuperM DrinkPrice Sam Pepsi2.75 Sam Juice2.75 Birds Pepsi2.50 Birds Juice3.00

34 34 Sells ρ S(SuperM, Drink1, Price) ⋈ π SuperM σ Drink != Drink1 Relation Sells: SuperM DrinkPrice Sam Pepsi2.75 Sam Juice2.75 Birds Pepsi2.50 Birds Juice3.00 Relation S: SuperM Drink1Price Sam Pepsi2.75 Sam Juice2.75 Birds Pepsi2.50 Birds Juice3.00

35 35 Sells ρ S(SuperM, Drink1, Price) ⋈ π SuperM σ Drink != Drink1 SuperMDrinkDrink1Price Sam PepsiPepsi2.75 SamPepsiJuice2.75 SamJuicePepsi2.75 Sam Juice Juice2.75 BirdsPepsiPepsi2.50 BirdsJuiceJuice3.00

36 36 Sells ρ S(SuperM, Drink1, Price) ⋈ π SuperM σ Drink != Drink1 SuperMDrinkDrink1Price Sam PepsiPepsi2.75 SamPepsiJuice2.75 SamJuicePepsi2.75 Sam Juice Juice2.75 BirdsPepsiPepsi2.50 BirdsJuiceJuice3.00 SuperMDrinkDrink1Price SamPepsiJuice2.75 SamJuicePepsi2.75

37 37 Sells ρ S(SuperM, Drink1, Price) ⋈ π SuperM σ Drink != Drink1 SuperMDrinkDrink1Price Sam PepsiPepsi2.75 SamPepsiJuice2.75 SamJuicePepsi2.75 Sam Juice Juice2.75 BirdsPepsiPepsi2.50 BirdsJuiceJuice3.00 SuperMDrinkDrink1Price SamPepsiJuice2.75 SamJuicePepsi2.75 SuperM Sam

38 Union, intersection, and difference: the schemas of the two operands must be the same, so use that schema for the result. Selection: schema of the result is the same as the schema of the operand. Projection: list of attributes tells us the schema. Product: schema is the attributes of both relations. – Use R.A, etc., to distinguish two attributes named A. Theta-join: same as product. Natural join: union of the attributes of the two relations. Renaming: the operator tells the schema 38

39 A bag (or multiset ) is like a set, but an element may appear more than once. Example: {1,2,1,3} is a bag. Example: {1,2,3} is also a bag that happens to be a set. 39

40 SQL, the most important query language for relational databases, is actually a bag language. Some operations, like projection, are more efficient on bags than sets. 40

41 Selection applies to each tuple, so its effect on bags is like its effect on sets. Projection also applies to each tuple, but as a bag operator, we do not eliminate duplicates. Products and joins are done on each pair of tuples, so duplicates in bags have no effect on how we operate. 41

42 42 R (A,B ) 12 56 12 σ A+B < 5 (R) A,B 12 R (A,B ) 12 56 12 π A (R) A 1 5 1

43 43 R(A,B ) S(B,C ) 1234 5678 12 R Χ S =AR.BS.BC 1234 1278 5634 5678 1234 1278

44 44 R(A,B ) S(B,C ) 1234 5678 12 R ⋈ R.B<S.B S =AR.BS.BC 1234 1278 5678 1234 1278

45 An element appears in the union of two bags the sum of the number of times it appears in each bag. Example: {1,2,1} ∪ {1,1,2,3,1} = {1,1,1,1,1,2,2,3} An element appears in the intersection of two bags the minimum of the number of times it appears in either. Example: {1,2,1,1} ∩ {1,2,1,3} = {1,1,2}. 45

46 An element appears in the difference A – B of bags as many times as it appears in A, minus the number of times it appears in B. – But never less than 0 times. Example: {1,2,1,1} – {1,2,3} = {1,1}. 46

47 Some, but not all algebraic laws that hold for sets also hold for bags. Example: the commutative law for union (R ∪ S = S ∪ R ) does hold for bags. – Since addition is commutative, adding the number of times x appears in R and S doesn’t depend on the order of R and S. Example: the set is idempotent meaning that (S ∪ S = S ) This does not hold for bags. – However, for bags, if x appears n times in S, then it appears 2n times in S ∪ S. Thus S ∪ S != S in general. – e.g., {1} ∪ {1} = {1,1} != {1}. 47


Download ppt "1. Chapter 2: The relational Database Modeling Section 2.4: An algebraic Query Language Chapter 5: Algebraic and logical Query Languages Section 5.1:"

Similar presentations


Ads by Google