Download presentation
Presentation is loading. Please wait.
Published byOpal Simpson Modified over 9 years ago
1
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications C. Faloutsos Relational model
2
Carnegie Mellon 15-415 - C. Faloutsos2 Overview history concepts Formal query languages –relational algebra –rel. tuple calculus –rel. domain calculus
3
Carnegie Mellon 15-415 - C. Faloutsos3 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
Carnegie Mellon 15-415 - C. Faloutsos4 Concepts Database: a set of relations (= tables) rows: tuples columns: attributes (or keys) superkey, candidate key, primary key
5
Carnegie Mellon 15-415 - C. Faloutsos5 Example Database:
6
Carnegie Mellon 15-415 - C. Faloutsos6 Example: cont’d Database: rel. schema (attr+domains) tuple k-th attribute (Dk domain)
7
Carnegie Mellon 15-415 - C. Faloutsos7 Example: cont’d rel. schema (attr+domains) instance
8
Carnegie Mellon 15-415 - C. Faloutsos8 Example: cont’d rel. schema (attr+domains) instance Di: the domain of the I-th attribute (eg., char(10) Formally: an instance is a subset of (D1 x D2 x …x Dn)
9
Carnegie Mellon 15-415 - C. Faloutsos9 Example: cont’d superkey (eg., ‘ssn, name’): determines record cand. key (eg., ‘ssn’, or ‘st#’): minimal superkey primary key: one of the cand. keys
10
Carnegie Mellon 15-415 - C. Faloutsos10 Overview history concepts Formal query languages –relational algebra –rel. tuple calculus –rel. domain calculus
11
Carnegie Mellon 15-415 - C. Faloutsos11 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?
12
Carnegie Mellon 15-415 - C. Faloutsos12. set union U set difference ‘-’ Relational operators
13
Carnegie Mellon 15-415 - C. Faloutsos13 Example: Q: find all students (part or full time) A: PT-STUDENT union FT-STUDENT
14
Carnegie Mellon 15-415 - C. Faloutsos14 Observations: two tables are ‘union compatible’ if they have the same attributes (‘domains’) Q: how about intersection U
15
Carnegie Mellon 15-415 - C. Faloutsos15 Observations: A: redundant: STUDENT intersection STAFF = STUDENT - (STUDENT - STAFF) STUDENT STAFF
16
Carnegie Mellon 15-415 - C. Faloutsos16. set union set difference ‘-’ Relational operators U
17
Carnegie Mellon 15-415 - C. Faloutsos17 Other operators? eg, find all students on ‘Main street’ A: ‘selection’
18
Carnegie Mellon 15-415 - C. Faloutsos18 Other operators? Notice: selection (and rest of operators) expect tables, and produce tables (-> can be cascaded!!) For selection, in general:
19
Carnegie Mellon 15-415 - C. Faloutsos19 Selection - examples Find all ‘Smiths’ on ‘Forbes Ave’ ‘condition’ can be any boolean combination of ‘=‘, ‘>’, ‘>=‘,...
20
Carnegie Mellon 15-415 - C. Faloutsos20 selection. set union set difference R - S Relational operators R U S
21
Carnegie Mellon 15-415 - C. Faloutsos21 selection picks rows - how about columns? A: ‘projection’ - eg.: finds all the ‘ssn’ - removing duplicates Relational operators
22
Carnegie Mellon 15-415 - C. Faloutsos22 Cascading: ‘find ssn of students on ‘forbes ave’ Relational operators
23
Carnegie Mellon 15-415 - C. Faloutsos23 selection projection. set union set difference R - S Relational operators R U S
24
Carnegie Mellon 15-415 - C. Faloutsos24 Are we done yet? Q: Give a query we can not answer yet! Relational operators
25
Carnegie Mellon 15-415 - C. Faloutsos25 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
26
Carnegie Mellon 15-415 - C. Faloutsos26 Cartesian product eg., dog-breeding: MALE x FEMALE gives all possible couples x =
27
Carnegie Mellon 15-415 - C. Faloutsos27 so what? Eg., how do we find names of students taking 415?
28
Carnegie Mellon 15-415 - C. Faloutsos28 Cartesian product A:
29
Carnegie Mellon 15-415 - C. Faloutsos29 Cartesian product
30
Carnegie Mellon 15-415 - C. Faloutsos30
31
Carnegie Mellon 15-415 - C. Faloutsos31 selection projection cartesian product MALE x FEMALE set union set difference R - S FUNDAMENTAL Relational operators R U S
32
Carnegie Mellon 15-415 - C. Faloutsos32 Relational ops Surprisingly, they are enough, to help us answer almost any query we want!! derived operators, for convenience –set intersection –join (theta join, equi-join, natural join) –‘rename’ operator –division
33
Carnegie Mellon 15-415 - C. Faloutsos33 Joins Equijoin:
34
Carnegie Mellon 15-415 - C. Faloutsos34 Cartesian product A:
35
Carnegie Mellon 15-415 - C. Faloutsos35 Joins Equijoin: theta-joins: generalization of equi-join - any condition
36
Carnegie Mellon 15-415 - C. Faloutsos36 Joins very popular: natural join: R S like equi-join, but it drops duplicate columns: STUDENT(ssn, name, address) TAKES(ssn, cid, grade)
37
Carnegie Mellon 15-415 - C. Faloutsos37 Joins nat. join has 5 attributes equi-join: 6
38
Carnegie Mellon 15-415 - C. Faloutsos38 Natural Joins - nit-picking if no attributes in common between R, S: nat. join -> cartesian product:
39
Carnegie Mellon 15-415 - C. Faloutsos39 Overview - rel. algebra fundamental operators derived operators –joins etc –rename –division examples
40
Carnegie Mellon 15-415 - C. Faloutsos40 rename op. Q: why? A: shorthand; self-joins; … for example, find the grand-parents of ‘Tom’, given PC(parent-id, child-id)
41
Carnegie Mellon 15-415 - C. Faloutsos41 rename op. PC(parent-id, child-id)
42
Carnegie Mellon 15-415 - C. Faloutsos42 rename op. first, WRONG attempt: (why? how many columns?) Second WRONG attempt:
43
Carnegie Mellon 15-415 - C. Faloutsos43 rename op. we clearly need two different names for the same table - hence, the ‘rename’ op.
44
Carnegie Mellon 15-415 - C. Faloutsos44 Overview - rel. algebra fundamental operators derived operators –joins etc –rename –division examples
45
Carnegie Mellon 15-415 - C. Faloutsos45 Division Rarely used, but powerful. Example: find suspicious suppliers, ie., suppliers that supplied all the parts in A_BOMB
46
Carnegie Mellon 15-415 - C. Faloutsos46 Division
47
Carnegie Mellon 15-415 - C. Faloutsos47 Division Observations: ~reverse of cartesian product It can be derived from the 5 fundamental operators (!!) How?
48
Carnegie Mellon 15-415 - C. Faloutsos48 Division Answer:
49
Carnegie Mellon 15-415 - C. Faloutsos49 Overview - rel. algebra fundamental operators derived operators –joins etc –rename –division examples
50
Carnegie Mellon 15-415 - C. Faloutsos50 Sample schema find names of students that take 15-415
51
Carnegie Mellon 15-415 - C. Faloutsos51 Examples find names of students that take 15-415
52
Carnegie Mellon 15-415 - C. Faloutsos52 Sample schema find course names of ‘smith’
53
Carnegie Mellon 15-415 - C. Faloutsos53 Examples find course names of ‘smith’
54
Carnegie Mellon 15-415 - C. Faloutsos54 Examples find ssn of ‘overworked’ students, ie., that take 412, 413, 415
55
Carnegie Mellon 15-415 - C. Faloutsos55 Examples find ssn of ‘overworked’ students, ie., that take 412, 413, 415: almost correct answer: U U U
56
Carnegie Mellon 15-415 - C. Faloutsos56 Examples find ssn of ‘overworked’ students, ie., that take 412, 413, 415 - Correct answer: U U U c-name=413 c-name=415
57
Carnegie Mellon 15-415 - C. Faloutsos57 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
58
Carnegie Mellon 15-415 - C. Faloutsos58 Sample schema
59
Carnegie Mellon 15-415 - C. Faloutsos59 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
60
Carnegie Mellon 15-415 - C. Faloutsos60 Conclusions Relational model: only tables (‘relations’) relational algebra: powerful, minimal: 5 operators can handle almost any query! most non-trivial op.: join
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.