Download presentation
Presentation is loading. Please wait.
Published byKaia Fitt Modified over 3 years ago
1
CMU SCS Carnegie Mellon Univ. School of Computer Science 15-415/615 - DB Applications C. Faloutsos & A. Pavlo Lecture #4: Relational Algebra
2
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#2 Overview history concepts Formal query languages –relational algebra –rel. tuple calculus –rel. domain calculus
3
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#3 History before: records, pointers, sets etc introduced by E.F. Codd in 1970 revolutionary! first systems: 1977-8 (System R; Ingres) Turing award in 1981
4
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#4 Concepts - reminder Database: a set of relations (= tables) rows: tuples columns: attributes (or keys) superkey, candidate key, primary key
5
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#5 Example Database:
6
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#6 Example: cont’d Database: rel. schema (attr+domains) tuple k-th attribute (Dk domain)
7
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#7 Example: cont’d rel. schema (attr+domains) instance
8
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#8 Example: cont’d rel. schema (attr+domains) instance Di: the domain of the i-th attribute (eg., char(10)
9
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#9 Overview history concepts Formal query languages –relational algebra –rel. tuple calculus –rel. domain calculus
10
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#10 Formal query languages How do we collect information? Eg., find ssn’s of people in 415 (recall: everything is a set!) One solution: Rel. algebra, ie., set operators Q1: Which ones?? Q2: what is a minimal set of operators?
11
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#11. set union U set difference ‘-’ Relational operators
12
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#12 Example: Q: find all students (part or full time) A: PT-STUDENT union FT-STUDENT
13
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#13 Observations: two tables are ‘union compatible’ if they have the same attributes (‘domains’) Q: how about intersection U
14
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#14 Observations: A: redundant: STUDENT intersection STAFF = STUDENT STAFF
15
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#15 Observations: A: redundant: STUDENT intersection STAFF = STUDENT STAFF
16
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#16 Observations: A: redundant: STUDENT intersection STAFF = STUDENT - (STUDENT - STAFF) STUDENT STAFF
17
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#17 Observations: A: redundant: STUDENT intersection STAFF = STUDENT - (STUDENT - STAFF) Double negation: We’ll see it again, later…
18
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#18. set union set difference ‘-’ Relational operators U
19
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#19 Other operators? eg, find all students on ‘Main street’ A: ‘selection’
20
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#20 Other operators? Notice: selection (and rest of operators) expect tables, and produce tables (-> can be cascaded!!) For selection, in general:
21
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#21 Selection - examples Find all ‘Smiths’ on ‘Forbes Ave’ ‘condition’ can be any boolean combination of ‘=‘, ‘>’, ‘>=‘,...
22
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#22 selection. set union set difference R - S Relational operators R U S
23
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#23 selection picks rows - how about columns? A: ‘projection’ - eg.: finds all the ‘ssn’ - removing duplicates Relational operators
24
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#24 Cascading: ‘find ssn of students on ‘forbes ave’ Relational operators
25
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#25 selection projection. set union set difference R - S Relational operators R U S
26
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#26 Are we done yet? Q: Give a query we can not answer yet! Relational operators
27
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#27 A: any query across two or more tables, eg., ‘find names of students in 15-415’ Q: what extra operator do we need?? Relational operators
28
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#28 A: any query across two or more tables, eg., ‘find names of students in 15-415’ Q: what extra operator do we need?? A: surprisingly, cartesian product is enough! Relational operators
29
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#29 Cartesian product eg., dog-breeding: MALE x FEMALE gives all possible couples x =
30
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#30 so what? Eg., how do we find names of students taking 415?
31
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#31 Cartesian product A:
32
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#32 Cartesian product
33
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#33
34
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#34 selection projection cartesian product MALE x FEMALE set union set difference R - S FUNDAMENTAL Relational operators R U S
35
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#35 Relational ops Surprisingly, they are enough, to help us answer almost any query we want!! derived/convenience operators: –set intersection – join (theta join, equi-join, natural join) –‘rename’ operator –division
36
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#36 Joins Equijoin:
37
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#37 Cartesian product A:
38
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#38 Joins Equijoin: theta-joins: generalization of equi-join - any condition
39
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#39 Joins very popular: natural join: R S like equi-join, but it drops duplicate columns: STUDENT (ssn, name, address) TAKES (ssn, cid, grade)
40
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#40 Joins nat. join has 5 attributes equi-join: 6
41
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#41 Natural Joins - nit-picking if no attributes in common between R, S: nat. join -> cartesian product
42
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#42 Overview - rel. algebra fundamental operators derived operators –joins etc –rename –division examples
43
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#43 Rename op. Q: why? A: shorthand; self-joins; … for example, find the grand-parents of ‘Tom’, given PC (parent-id, child-id)
44
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#44 Rename op. PC (parent-id, child-id)
45
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#45 Rename op. first, WRONG attempt: (why? how many columns?) Second WRONG attempt:
46
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#46 Rename op. we clearly need two different names for the same table - hence, the ‘rename’ op.
47
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#47 Overview - rel. algebra fundamental operators derived operators –joins etc –rename –division examples
48
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#48 Division Rarely used, but powerful. Example: find suspicious suppliers, ie., suppliers that supplied all the parts in A_BOMB
49
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#49 Division
50
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#50 Division Observations: ~reverse of cartesian product It can be derived from the 5 fundamental operators (!!) How?
51
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#51 Division Answer: Observation: find ‘good’ suppliers, and subtract! (double negation)
52
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#52 Division Answer: Observation: find ‘good’ suppliers, and subtract! (double negation)
53
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#53 Division Answer: All suppliers All bad parts
54
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#54 Division Answer: all possible suspicious shipments
55
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#55 Division Answer: all possible suspicious shipments that didn’t happen
56
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#56 Division Answer: all suppliers who missed at least one suspicious shipment, i.e.: ‘good’ suppliers
57
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#57 Overview - rel. algebra fundamental operators derived operators –joins etc –rename –division examples
58
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#58 Sample schema find names of students that take 15-415
59
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#59 Examples find names of students that take 15-415
60
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#60 Examples find names of students that take 15-415
61
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#61 Sample schema find course names of ‘smith’
62
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#62 Examples find course names of ‘smith’
63
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#63 Examples find ssn of ‘overworked’ students, ie., that take 412, 413, 415
64
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#64 Examples find ssn of ‘overworked’ students, ie., that take 412, 413, 415: almost correct answer:
65
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#65 Examples find ssn of ‘overworked’ students, ie., that take 412, 413, 415 - Correct answer: c-name=413 c-name=415
66
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#66 Examples find ssn of students that work at least as hard as ssn=123, ie., they take all the courses of ssn=123, and maybe more
67
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#67 Sample schema
68
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#68 Examples find ssn of students that work at least as hard as ssn=123 (ie., they take all the courses of ssn=123, and maybe more
69
CMU SCS Faloutsos - PavloCMU SCS 15-415/615#69 Conclusions Relational model: only tables (‘relations’) relational algebra: powerful, minimal: 5 operators can handle almost any query!
Similar presentations
© 2018 SlidePlayer.com Inc.
All rights reserved.
Ppt on interchange of sentences Ppt on astronomy and astrophysics colleges How to use ppt on ipad Ppt on eye of tiger Ppt on english grammar tenses Animated ppt on solar system Ppt online open houses Ppt on fourth and fifth state of matter Ppt on blue eyes technology free download Ppt on satellite orbiting