Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational Algebra – Basis for Relational Query Languages Based on presentation by Juliana Freire.

Similar presentations


Presentation on theme: "Relational Algebra – Basis for Relational Query Languages Based on presentation by Juliana Freire."— Presentation transcript:

1 Relational Algebra – Basis for Relational Query Languages Based on presentation by Juliana Freire

2 Formal relational query languages

3 Is this the Algebra you know? Algebra -> operators and atomic operands Expressions -> applying operators to atomic operands and/or other expressions Algebra of arithmetic: operands are variables and constants, and operators are the usual arithmetic operators E.g., (x+y)*2 or ((x+7)/(y-3)) + x Relational algebra: operands are variables that stand for relations and relations (sets of tuples), and operations include union, intersection, selection, projection, Cartesian product, etc – E.g., (π c-ownerChecking-account) ∩ (π s-ownerSavings-account)

4 What is a query? A query is applied to relation instances, and the result of a query is also a relation instance. (view, query) – Schemas of input and output fixed, but instances not. Operators refer to relation attributes by position or name: – E.g., Account(number, owner, balance, type) – Positional notation easier for formal definitions, named-field notation more readable. – Both used in SQL

5 Relational Algebra Operations The usual set operations: union, intersection, difference Operations that remove parts of relations: selection, projection Operations that combine tuples from two relations: Cartesian product, join Since each operation returns a relation, operations can be composed!

6 Removing Parts of Relations Selection – rows Projection - columns

7 Selection: Example

8 Another selection

9 Example of Projection

10 Projection removes duplicates

11 Set Operations Union Intersection Difference

12 What happens when sets unite?

13 Union Operation – Example Relations r, s: r  s: AB  121121 AB  2323 r s AB  12131213

14 Union Example

15 Union Compatibility

16 Intersection

17 Set Difference Operation – Example Relations r, s: r – s: AB  121121 AB  2323 r s AB  1111

18 Difference

19 Another way to show intersection?

20 Summary so far: E 1 U E 2 : union E 1 - E 2 : difference E 1 x E 2 : cartesian product  c (E 1 ) : select rows, c = condition (book has p for predicate) II s (E 1 ) : project columns : s =selected columns  x(c1,c2) (E 1 ) : rename, x is new name of E 1, c1 is new name of column

21 Combining Tuples of Two Relations Cross product (Cartesian product) Joins

22 Cartesian-Product Operation – Example Relations r, s: r x s: AB  1212 AB  1111222211112222 CD  10 20 10 20 10 E aabbaabbaabbaabb CD  20 10 E aabbaabb r s

23 Cross Product Example

24 Cross Product Renaming operator:  Rename whole relation: Teacher X  secondteacher (Teacher)  Teacher.t-num, Teacher.t-name, secondteacher.t-num, secondteacher.t-name OR rename attribute before combining: Teacher X  secondteacher(t-num2, t-name2) (Teacher)  t-num, t-name, t-num2, t-name2 OR rename after combining  c(t-num1, t-name1, t-num2, t-name2) (Teacher X Teacher)  t-num1, t-name1, t-num2, t-name2 How to resolve????

25 Join: Example

26

27 Condition Join

28 Equi and Natural Join

29 Divide operator

30 Divide Operation

31 Divide Definition

32 When to divide?

33 Division Example

34 Dividing without division sign

35 Working out an example

36 Assignment operation

37 Why would we use Relational Algebra?????

38 Equivalencies help

39 ER vs RA Both ER and the Relational Model can be used to model the structure of a database. Why is it the case that there are only Relational Databases and no ER databases?

40 RA vs Full Programming Language Relational Algebra is not Turing complete. There are operations that cannot be expressed in relational algebra. What is the advantage of using this language to query a database?

41 Summary of Operators updated Summary so far: E 1 U E 2 : union E 1 - E 2 : difference E 1 x E 2 : cartesian product  c (E 1 ) : select rows, c = condition (book has p for predicate) II s (E 1 ) : project columns : s =selected columns  x(c1,c2) (E 1 ) : rename, x is new name of E 1, c1 is new name of column E 1  E 2 : division E 1 E 2 : join, c = match condition

42

43 Practice Find names of stars who’ve appeared in a 1994 movie Information about movie year available in Movies; so need an extra join: σ year=1994 (πname(Stars ⋈ AppearIn ⋈ Movies)) An even more efficient solution: π name (Stars ⋈ π name (AppearIn ⋈ (π movieId σ year=1994 (Movies))) A query optimizer can find this, given the first solution! A more efficient solution: π name (Stars ⋈ AppearIn ⋈ (σ year=1994 ( Movies))

44 Extended Relational Algebra Operations Generalized projection Outer join Aggregate functions

45 Generalized projection – calculate fields

46 Aggregate Operation – Example Relatio n r: AB   C 7 3 10 g sum(c ) (r) sum(c ) 27

47 Aggregate Functions on more than one tuple Samples: –Sum –Count-distinct –Max –Min –Count –Avg Use “as” to rename branchname g sum(balance) as totalbalance ( account )

48 Aggregate Operation – Example Relation account grouped by branch-name: branch_name g sum( balance ) ( account ) branch_nameaccount_numberbalance Perryridge Brighton Redwood A-102 A-201 A-217 A-215 A-222 400 900 750 700 branch_namesum(balance) Perryridge Brighton Redwood 1300 1500 700

49 Outer Join Keep the outer side even if no join Fill in missing fields with nulls

50 Outer Join – Example Relation loan Relation borrower customer_nameloan_number Jones Smith Hayes L-170 L-230 L-155 3000 4000 1700 loan_numberamount L-170 L-230 L-260 branch_name Downtown Redwood Perryridge

51 Outer Join – Example Inner Join loan Borrower loan_numberamount L-170 L-230 3000 4000 customer_name Jones Smith branch_name Downtown Redwood Jones Smith null loan_numberamount L-170 L-230 L-260 3000 4000 1700 customer_namebranch_name Downtown Redwood Perryridge Left Outer Join loan Borrower

52 Outer Join – Example loan_numberamount L-170 L-230 L-155 3000 4000 null customer_name Jones Smith Hayes branch_name Downtown Redwood null loan_numberamount L-170 L-230 L-260 L-155 3000 4000 1700 null customer_name Jones Smith null Hayes branch_name Downtown Redwood Perryridge null Full Outer Join loan borrower Right Outer Join loan borrower

53 Summary of Operators - Full E 1 U E 2 : union E 1 - E 2 : difference E 1 x E 2 : cartesian product  c (E 1 ) : select rows, c = condition (book has p for predicate) II s (E 1 ) : project columns : s =selected columns separated by commas, can have calculations included  x(c1,c2) (E 1 ) : rename, x is new name of E 1, c1 is new name of column E 1  E 2 : division E 1 E 2 : join, c = match condition E 1 E 2 : outer join, c = match condition, keep the side with the arrows  : assignment – give a new name to an expression to make it easy to read as : rename a calculated column attribute1 g function (attribute2) (E 1 ) : perform function on attribute2 whenever attribute1 changes

54


Download ppt "Relational Algebra – Basis for Relational Query Languages Based on presentation by Juliana Freire."

Similar presentations


Ads by Google