Presentation is loading. Please wait.

Presentation is loading. Please wait.

Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Relational Model I (based on notes by Silberchatz,Korth, and Sudarshan.

Similar presentations


Presentation on theme: "Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Relational Model I (based on notes by Silberchatz,Korth, and Sudarshan."— Presentation transcript:

1 Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Relational Model I (based on notes by Silberchatz,Korth, and Sudarshan and notes by C. Faloutsos at CMU)

2 Overview history concepts Formal query languages relational algebra rel. tuple calculus rel. domain calculus

3 History before: records, pointers, sets etc introduced by E.F. Codd (1923-2003) in 1970 revolutionary!!! first systems: 1977-8 (System R; Ingres) Turing award in 1981

4 Concepts Database: a set of relations (= tables) rows: tuples columns: attributes (or keys) superkey, candidate key, primary key

5 Example Database:

6 Example: cont’d Database: rel. schema (attr+domains) tuple k-th attribute (Dk domain)

7 Example: cont’d rel. schema (attr+domains) instance

8 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 Example: cont’d superkey (eg., ‘ssn, name’): determines record cand. key (eg., ‘ssn’, or ‘st#’): minimal superkey (no subset of it is a superkey) primary key: one of the cand. keys

10 Another example Example: if Customer-name = {Jones, Smith, Curry, Lindsay} Customer-street = {Main, North, Park} Customer-city = {Harrison, Rye, Pittsfield} Then r = { (Jones, Main, Harrison), (Smith, North, Rye), (Curry, North, Rye), (Lindsay, Park, Pittsfield)} is a relation over Customer-name x Customer-street x Customer-city

11 Relations, tuples Relation Schema: A 1, A 2, …, A n are attributes R = (A 1, A 2, …, A n ) is a relation schema E.g. Customer-schema = (customer-name, customer-street, customer-city) r(R) is a relation on the relation schema R E.g.customer (Customer-schema) Relations are unordered Order of tuples is irrelevant (tuples may be stored in an arbitrary order)

12 Database A database consists of multiple relations Information about an enterprise is broken up into parts, with each relation storing one part of the information E.g.: account : stores information about accounts depositor : stores information about which customer owns which account customer : stores information about customers Storing all information as a single relation such as bank(account-number, balance, customer-name,..) results in repetition of information (e.g. two customers own an account) the need for null values (e.g. represent a customer without an account) Normalization theory (discuss later) deals with how to design relational schemas

13 Overview history concepts Formal query languages relational algebra rel. tuple calculus rel. domain calculus

14 Formal query languages How do we collect information? Eg., find ssn’s of people in cis331 (recall: everything is a set!) One solution: Relational algebra, i.e., set operators (procedural language) Q1: Which operators?? Q2: What is a minimal set of operators?

15 . set union U set difference ‘-’ Relational operators

16 Example: Q: find all students (part or full time) A: PT-STUDENT union FT-STUDENT

17 Observations: two tables are ‘union compatible’ if they have the same attributes (i.e., same arity: number of attributes and same ‘domains’) Q: how about intersection ? U

18 Observations: A: redundant: STUDENT intersection STAFF = STUDENT - (STUDENT - STAFF) STUDENT STAFF

19 . set union set difference ‘-’ Relational operators U

20 Other operators? E.g., find all students on ‘Main street’ A: ‘selection’

21 Other operators? Notice: selection (and rest of operators) expect tables, and produce tables --> can be cascaded!! For selection, in general:

22 Selection - examples Find all ‘Smiths’ on ‘Forbes Ave’ ‘condition’ can be any boolean combination of ‘=‘, ‘>’, ‘>=‘,...

23 selection. set union set difference R - S Relational operators R U S

24 selection picks rows - how about columns? A: ‘projection’ - eg.: finds all the ‘ssn’ - removing duplicates Relational operators

25 Cascading: ‘find ssn of students on ‘forbes ave’ Relational operators

26 selection projection. set union set difference R - S Relational operators R U S

27 Are we done yet? Q: Give a query we can not answer yet! Relational operators

28 A: any query across two or more tables, eg., ‘find names of students in cis351’ Q: what extra operator do we need?? A: surprisingly, the cartesian product is enough! Relational operators TAKES SSNc-idgrade 123cis331A 234cis331B

29 Cartesian product E.g., dog-breeding: MALE x FEMALE gives all possible couples x =

30 so what? Eg., how do we find names of students taking cis351? TAKES SSNc-idgrade 123cis331A 234cis331B

31 Cartesian product A: SsnNameAddressssncidgrade 123smithmain str123cis331A 234jonesforbes ave123cis331A 123smithmain str234cis331B 234jonesforbes ave234cis331B

32 Cartesian product SsnNameAddressssncidgrade 123smithmain str123cis331A 234jonesforbes ave123cis331A 123smithmain str234cis331B 234jonesforbes ave234cis331B

33 SsnNameAddressssncidgrade 123smithmain str123cis331A 234jonesforbes ave123cis331A 123smithmain str234cis331B 234jonesforbes ave234cis331B

34 selection projection cartesian product MALE x FEMALE set union set difference R - S FUNDAMENTAL Relational operators R U S

35 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

36 Joins Equijoin:

37 Cartesian product A: SsnNameAddressssncidgrade 123smithmain str123cis331A 234jonesforbes ave123cis331A 123smithmain str234cis331B 234jonesforbes ave234cis331B

38 Joins Equijoin: theta-joins: generalization of equi-join - any condition

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 Joins nat. join has 5 attributes SsnNameAddressssncidgrade 123smithmain str123cis331A 234jonesforbes ave123cis331A 123smithmain str234cis331B 234jonesforbes ave234cis331B equi-join: 6

41 Natural Joins - nit-picking if no attributes in common between R, S: nat. join -> cartesian product:

42 Overview - rel. algebra fundamental operators derived operators joins etc rename division examples

43 rename op. Q: why? A: Shorthand (BEFORE can be a relational algebra expression) self-joins; … for example, find the grand-parents of ‘Tom’, given PC(parent-id, child-id)

44 rename op. PC(parent-id, child-id)

45 rename op. first, WRONG attempt: (why? how many columns?) Second WRONG attempt:

46 rename op. we clearly need two different names for the same table - hence, the ‘rename’ op.

47 Overview - rel. algebra fundamental operators derived operators joins etc rename division examples

48 Division Rarely used, but powerful. Suited for queries that include the phrase “for all” Example: find suspicious suppliers, i.e., suppliers that supplied all the parts in A_BOMB

49 Division

50 Observations: ~reverse of cartesian product It can be derived from the 5 fundamental operators (!!) How?

51 Division Answer: gives those tuples t in such that for some tuple u in S, tu not in R.

52 Overview - rel. algebra fundamental operators derived operators joins etc rename division examples

53 Sample schema CLASS c-idc-nameunits cis331d.b.2 cis321o.s.2 TAKES SSNc-idgrade 123cis331A 234cis331B find names of students that take cis351

54 Examples find names of students that take cis351

55 Sample schema find course names of ‘smith’ CLASS c-idc-nameunits cis331d.b.2 cis321o.s.2 TAKES SSNc-idgrade 123cis331A 234cis331B

56 Examples find course names of ‘smith’

57 Examples find ssn of ‘overworked’ students, ie., that take cis331, cis342, cis350

58 Examples find ssn of ‘overworked’ students, ie., that take cis331, cis342, cis350: almost correct answer: )( )( )( 350 342 331 TAKES namec c c        

59 Examples find ssn of ‘overworked’ students, ie., that take cis331, cis342, cis350 - Correct answer: )]([ ([ ([ 350 342 331 TAKES namecssn namecssn namecssn        

60 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)

61 Sample schema CLASS c-idc-nameunits cis331d.b.2 cis321o.s.2 TAKES SSNc-idgrade 123cis331A 234cis331B

62 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

63 Conclusions Relational model: only tables (‘relations’) relational algebra: powerful, minimal: 5 operators can handle almost any query! most non-trivial op.: join

64 The bank database


Download ppt "Temple University – CIS Dept. CIS331– Principles of Database Systems V. Megalooikonomou Relational Model I (based on notes by Silberchatz,Korth, and Sudarshan."

Similar presentations


Ads by Google