Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams.

Similar presentations


Presentation on theme: "CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams."— Presentation transcript:

1 CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams

2 CMU SCS Problem Develop an application for U.G. admin: –Student info –Who-takes-what class –Class rosters –Transcripts How do you proceed? –(Which role(s) are you playing?) Faloutsos - PavloCMU SCS 15-415/6152

3 CMU SCS Faloutsos - PavloCMU SCS 15-415/6153 Database Design Requirements Analysis Conceptual Design Logical Design Schema Refinement Physical Design Security Design

4 CMU SCS Faloutsos - PavloCMU SCS 15-415/6154 Database Design Requirements Analysis Conceptual Design Logical Design Schema Refinement Physical Design Security Design users needs high level (ER) Tables Normalization Indices etc Access controls

5 CMU SCS Problem Develop an application for U.G. admin: –Student info –Who-takes-what class –Class rosters –Transcripts If you are the *new* DBA, what would you rather inherit: Faloutsos - PavloCMU SCS 15-415/6155 Maintain

6 CMU SCS This or this ? drop table if exists student; create table student (ssn fixed, name char(20)); drop table if exists takes; create table takes (ssn fixed, cid char(10), grade fixed); Faloutsos - PavloCMU SCS 15-415/6156 Student Course Takes

7 CMU SCS True story Health insurance company Wants to catch (some of the abundant) fraud Schema: –patients, visit doctors, get medicine, –Doctors perform operations, … –Nurses monitor patients, … –etc etc Q: How many tables do you think it spans? Faloutsos - PavloCMU SCS 15-415/6157

8 CMU SCS True story Schema: –patients, visit doctors, get medicine, –Doctors perform operations, … –Nurses monitor patients, … –etc etc Q: How many tables do you think it spans? 10? 20? 30? Faloutsos - PavloCMU SCS 15-415/6158

9 CMU SCS True story Schema: –patients, visit doctors, get medicine, –Doctors perform operations, … –Nurses monitor patients, … –etc etc Q: How many tables do you think it spans? 10? 20? 30? A: 120 PAGES of schema Faloutsos - PavloCMU SCS 15-415/6159

10 CMU SCS Motivation & upcoming conclusion: E-R diagrams are excellent documentation tools Faloutsos - PavloCMU SCS 15-415/61510 Student Course Takes

11 CMU SCS Faloutsos - PavloCMU SCS 15-415/61511 Overview concepts –Entities –Relationships –Attributes –Specialization/Generalization –Aggregation –ER modeling questions

12 CMU SCS Faloutsos - PavloCMU SCS 15-415/61512 Tools Entities (entity sets) Relationships (rel. sets) and mapping constraints attributes NM P Student Course Takes

13 CMU SCS Faloutsos - PavloCMU SCS 15-415/61513 Example Students, taking courses, offered by instructors; a course may have multiple sections; one instructor per section nouns -> entity sets verbs -> relationship sets

14 CMU SCS Faloutsos - PavloCMU SCS 15-415/61514 Example Students, taking courses, offered by instructors; a course may have multiple sections; one instructor per section nouns -> entity sets verbs -> relationship sets

15 CMU SCS Faloutsos - PavloCMU SCS 15-415/61515 Example Students, taking courses, offered by instructors; a course may have multiple sections; one instructor per section nouns -> entity sets verbs -> relationship sets

16 CMU SCS Faloutsos - PavloCMU SCS 15-415/61516 STUDENT name ssn... INSTRUCTOR issn primary key = unique identifier -> underline

17 CMU SCS Faloutsos - PavloCMU SCS 15-415/61517 STUDENT name ssn... COURSE c-id c-name but: sections of course (with different instructors)? INSTRUCTOR issn

18 CMU SCS Faloutsos - PavloCMU SCS 15-415/61518 COURSE c-id INSTRUCTOR issn STUDENT ssn SECTION s-id but: s-id is not unique... (see later)

19 CMU SCS Faloutsos - PavloCMU SCS 15-415/61519 COURSE c-id INSTRUCTOR issn STUDENT ssn SECTION s-id Q: how to record that students take courses?

20 CMU SCS Faloutsos - PavloCMU SCS 15-415/61520 COURSE c-id INSTRUCTOR issn STUDENT ssn SECTION s-id takes N M

21 CMU SCS Faloutsos - PavloCMU SCS 15-415/61521 COURSE c-id INSTRUCTOR STUDENT SECTION s-id takes N M N 1 teaches

22 CMU SCS Faloutsos - PavloCMU SCS 15-415/61522 COURSE c-id INSTRUCTOR STUDENT SECTION s-id takes N M N 1 has N 1 teaches

23 CMU SCS Faloutsos - PavloCMU SCS 15-415/61523 Cardinalities 1 to 1 (example?) 1 to N N to M 11 1N NM

24 CMU SCS Faloutsos - PavloCMU SCS 15-415/61524 Cardinalities 11 1N NM COUNTRY CAPITAL PERSON STUDENT SECTION has CARowns takes

25 CMU SCS Faloutsos - PavloCMU SCS 15-415/61525 Cardinalities PERSON STUDENT SECTION CARowns takes Books notation: COUNTRY has CAPITAL

26 CMU SCS Faloutsos - PavloCMU SCS 15-415/61526 Cardinalities 11 1N NM PERSON STUDENT SECTION CARowns takes Books notation vs 1 to N notation CAPITAL COUNTRY has

27 CMU SCS Faloutsos - PavloCMU SCS 15-415/61527 Cardinalities 11 1N NM PERSON STUDENT SECTION CARowns takes CAPITAL COUNTRY has

28 CMU SCS Faloutsos - PavloCMU SCS 15-415/61528 Total/partial participation 1:1 ?:1?:N ?:M PERSON STUDENT SECTION CARowns takes total, total ?? COUNTRY has CAPITAL

29 CMU SCS Faloutsos - PavloCMU SCS 15-415/61529 Total/partial participation 1:1 0:N ?:N?:M PERSON STUDENT SECTION CARowns takes total, total ?? partial, total COUNTRY has CAPITAL

30 CMU SCS Faloutsos - PavloCMU SCS 15-415/61530 Total/partial participation 1:1 0:N 1:N0:M PERSON STUDENT SECTION CARowns takes total, total partial, total COUNTRY has CAPITAL

31 CMU SCS Faloutsos - PavloCMU SCS 15-415/61531 Total/partial participation 1:10:N PERSON CARowns partial, total Is it legal?

32 CMU SCS Faloutsos - PavloCMU SCS 15-415/61532 Total/partial participation 1:10:N PERSON CARowns partial, total Is it legal? NO! why not?

33 CMU SCS Faloutsos - PavloCMU SCS 15-415/61533 Subtle concept: Weak entities section has no unique-id of its own!(?) COURSE c-id SECTION s-idhas N 1

34 CMU SCS Faloutsos - PavloCMU SCS 15-415/61534 Weak entities weak entities: if they need to borrow a unique id from a strong entity - thick box. c-id + s-id: unique id for SECTION partial key (eg., s-id) - dashed-underline identifying relationship (eg., has) COURSE c-id SECTION s-idhas N 1

35 CMU SCS Faloutsos - PavloCMU SCS 15-415/61535 Weak entities Other example(s) of weak entities? ??? ??id ??? ?? N 1

36 CMU SCS Faloutsos - PavloCMU SCS 15-415/61536 Weak entities Other example(s) of weak entities? Employee e-id Dependent namehas N 1

37 CMU SCS Faloutsos - PavloCMU SCS 15-415/61537 More details self-relationships - example? ??

38 CMU SCS Faloutsos - PavloCMU SCS 15-415/61538 More details self-relationships - example? EMPLOYEE manages 1 N

39 CMU SCS Faloutsos - PavloCMU SCS 15-415/61539 More details self-relationships - example? FB user Has-friend ??

40 CMU SCS Faloutsos - PavloCMU SCS 15-415/61540 More details 3-way and k-way relationships?

41 CMU SCS Faloutsos - PavloCMU SCS 15-415/61541 More details 3-way and k-way relationships? Rare, but possible: EMPLOYEE uses PROJECT TOOL N M P

42 CMU SCS Faloutsos - PavloCMU SCS 15-415/61542 More details 3-way and k-way relationships? Rare, but possible: user reviews app keyword N M P App-store/amazon reviews

43 CMU SCS Faloutsos - PavloCMU SCS 15-415/61543 Overview concepts –Entities –Relationships –Attributes –Specialization/Generalization –Aggregation –ER modeling questions

44 CMU SCS Faloutsos - PavloCMU SCS 15-415/61544 More details - attributes key (or primary key): unique identifier underlined, in the ER diagram [not in textbook - FYI: –multivalued or set-valued attributes (eg., dependents for EMPLOYEE) –derived attributes (eg., 15% tip) ]

45 CMU SCS Faloutsos - PavloCMU SCS 15-415/61545 Overview concepts –Entities –Relationships –Attributes –Specialization/Generalization –Aggregation –ER modeling questions Basic Advanced/ rare

46 CMU SCS Faloutsos - PavloCMU SCS 15-415/61546 Specialization eg., students: part time (#credit- hours) and full time (major) STUDENT name ssn PT-STUDENT #credits FT-STUDENT major IS-A

47 CMU SCS Faloutsos - PavloCMU SCS 15-415/61547 Observations Generalization: exact reverse of specialization attribute inheritance could have many levels of an IS-A hierarchy

48 CMU SCS Faloutsos - PavloCMU SCS 15-415/61548 More details Overlap constraints Covering constraints ABC

49 CMU SCS Faloutsos - PavloCMU SCS 15-415/61549 More details Overlap constraints –can an entity belong to both B and C? Covering constraints –can an A entity belong to neither B nor C? ABC

50 CMU SCS Faloutsos - PavloCMU SCS 15-415/61550 More details Overlap constraints - examples? ABC A B C No overlap A B C with overlap

51 CMU SCS Faloutsos - PavloCMU SCS 15-415/61551 More details Covering constraints - examples? ABC A B C Total coverage A B C Partial coverage

52 CMU SCS Faloutsos - PavloCMU SCS 15-415/61552 Overview concepts –Entities –Relationships –Attributes –Specialization/Generalization –Aggregation –ER modeling questions

53 CMU SCS Faloutsos - PavloCMU SCS 15-415/61553 Aggregation CPU HD computer model (w/ CPU and HD) and Maker (eg., Dell, HP) N M MAKER ?

54 CMU SCS Faloutsos - PavloCMU SCS 15-415/61554 Aggregation treat a relationship as an entity used to express a relationship among relationships CPU HD N M MAKER

55 CMU SCS Faloutsos - PavloCMU SCS 15-415/61555 Overview concepts –Entities –Relationships –Attributes –Specialization/Generalization –Aggregation –ER modeling questions

56 CMU SCS Faloutsos - PavloCMU SCS 15-415/61556 Conceptual design Entity vs attribute Entity vs relationship Binary or ternary relationships? Aggregation?

57 CMU SCS Faloutsos - PavloCMU SCS 15-415/61557 Entity vs. attribute Entity EMPLOYEE (w/ emp#, name, job_code,...) Q: How about spouse - entity or attribute? Q: How about dependents?

58 CMU SCS Faloutsos - PavloCMU SCS 15-415/61558 Entity vs. attribute Entity EMPLOYEE (w/ emp#, name, job_code,...) Q: How about spouse - entity or attribute? A: probably, attribute is enough Q: How about dependents? A: Entity - we may have many dependents

59 CMU SCS Faloutsos - PavloCMU SCS 15-415/61559 Entity vs. Relationship STUDENT SECTION takes N M STUDENT SECTION TAKES 1 N 1 N OR

60 CMU SCS Faloutsos - PavloCMU SCS 15-415/61560 Binary vs Ternary Relationships usually, binary relationships are cleaner:

61 CMU SCS Faloutsos - PavloCMU SCS 15-415/61561 Binary vs. Ternary Relationships If each policy is owned by just 1 employee: Policies policyid cost age pname Dependents Covers name Employees ssn lot

62 CMU SCS Faloutsos - PavloCMU SCS 15-415/61562 Binary vs. Ternary Relationships If each policy is owned by just 1 employee: Bad design Policies policyid cost age pname Dependents Covers name Employees ssn lot

63 CMU SCS Faloutsos - PavloCMU SCS 15-415/61563 Binary vs. Ternary Relationships If each policy is owned by just 1 employee: Bad design Policies policyid cost age pname Dependents Covers name Employees ssn lot Key constraint on Policies would mean policy can only cover 1 dependent!

64 CMU SCS Faloutsos - PavloCMU SCS 15-415/61564 Binary vs. Ternary Relationships If each policy is owned by just 1 employee: Bad design Beneficiary age pname Dependents policyid cost Policies Purchaser name Employees ssn lot Better design What are the additional constraints in the 2nd diagram? Policies policyid cost age pname Dependents Covers name Employees ssn lot Key constraint on Policies would mean policy can only cover 1 dependent!

65 CMU SCS Faloutsos - PavloCMU SCS 15-415/61565 Binary vs Ternary Rel. But sometimes ternary rel. can not be replaced by a set of binary rels:

66 CMU SCS Faloutsos - PavloCMU SCS 15-415/61566 Binary vs. Ternary Relationships (Contd.) Suppliers qty Departments Contract Parts Suppliers Departments deals-with Parts can-supply VS. why is it bad?

67 CMU SCS Faloutsos - PavloCMU SCS 15-415/61567 Binary vs. Ternary Relationships (Contd.) –S can-supply P, D needs P, and D deals-with S does not imply that D has agreed to buy P from S. –How do we record qty? Suppliers qty Departments Contract Parts Suppliers Departments deals-with Parts can-supply VS.

68 CMU SCS Faloutsos - PavloCMU SCS 15-415/61568 Binary vs. Ternary Relationships (Contd.) Suppliers qty Departments Contract Parts Suppliers qty Departments Contract Parts Not in textbook: in practice, often:

69 CMU SCS Faloutsos - PavloCMU SCS 15-415/61569 Binary vs. Ternary Relationships (Contd.) Suppliers qty Departments Contract Parts c-id Not in textbook: in practice, often:

70 CMU SCS Faloutsos - PavloCMU SCS 15-415/61570 Binary vs. Ternary Relationships (Contd.) Suppliers qty Departments Contract Parts c-id Not in textbook: in practice, often: N M 1 N 1N

71 CMU SCS Faloutsos - PavloCMU SCS 15-415/61571 Ternary vs. aggregation use aggregation, if we want to attach a relationship to a relationship (see book for example) (in practice, again we create a unique-id and resort to binary relationships)

72 CMU SCS Ternary vs. aggregation How would you handle this case? Faloutsos - PavloCMU SCS 15-415/61572 CPU HD N M MAKER

73 CMU SCS Ternary vs. aggregation How would you handle this case? Faloutsos - PavloCMU SCS 15-415/61573 CPU HD MAKER COMP. MODEL

74 CMU SCS Ternary vs. aggregation How would you handle this case? Faloutsos - PavloCMU SCS 15-415/61574 CPU HD ? ? MAKER COMP. MODEL ? ?

75 CMU SCS Ternary vs. aggregation How would you handle this case? Faloutsos - PavloCMU SCS 15-415/61575 CPU HD N M MAKER COMP. MODEL N M HAS_CPU

76 CMU SCS Faloutsos - PavloCMU SCS 15-415/61576 Summary E-R Diagrams: a powerful, user-friendly tool for data modeling: –Entities (strong, weak) –Attributes (primary keys, discriminators, derived, multivalued) –Relationships (1:1, 1:N, N:M; multi-way) –Generalization/Specialization; Aggregation

77 CMU SCS Faloutsos - PavloCMU SCS 15-415/61577 Summary E-R Diagrams: a powerful, user-friendly tool for data modeling: –Entities (strong, weak) –Attributes (primary keys, discriminators, derived, multivalued) –Relationships (1:1, 1:N, N:M; multi-way) –Generalization/Specialization; Aggregation POPULAR

78 CMU SCS Faloutsos - PavloCMU SCS 15-415/61578 Summary - contd (strong) entity set weak entity set relationship set identifying rel. set for weak entity attribute primary key A partial key A

79 CMU SCS Faloutsos - PavloCMU SCS 15-415/61579 Summary - contd cardinalities N M with limits l:h cardinalities partial/total (not in textbook - FYI)

80 CMU SCS Faloutsos - PavloCMU SCS 15-415/61580 Summary - contd IS-A aggregation


Download ppt "CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams."

Similar presentations


Ads by Google