Download presentation
Presentation is loading. Please wait.
Published bySucianty Irawan Modified over 5 years ago
1
Faloutsos - Pavlo C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams
CMU SCS /615 Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos - A. Pavlo Lecture#2: E-R diagrams
2
Problem Develop an application for U.G. admin: How do you proceed?
Faloutsos - Pavlo CMU SCS /615 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 - Pavlo CMU SCS /615
3
Database Design Requirements Analysis Conceptual Design Logical Design
Faloutsos - Pavlo CMU SCS /615 Database Design Requirements Analysis Conceptual Design Logical Design Schema Refinement Physical Design Security Design Faloutsos - Pavlo CMU SCS /615
4
Database Design Requirements Analysis Conceptual Design Logical Design
Faloutsos - Pavlo CMU SCS /615 Database Design ✔ Requirements Analysis Conceptual Design Logical Design Schema Refinement Physical Design Security Design user’s needs high level (ER) Tables Normalization Indices etc Access controls Faloutsos - Pavlo CMU SCS /615
5
Problem’ Maintain Develop an application for U.G. admin:
Faloutsos - Pavlo CMU SCS /615 Problem’ Maintain 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 - Pavlo CMU SCS /615
6
This or this ? Student Takes Course drop table if exists student;
create table student (ssn fixed, name char(20)); drop table if exists takes; create table takes cid char(10), grade fixed); Student Course Takes Faloutsos - Pavlo CMU SCS /615
7
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 - Pavlo CMU SCS /615
8
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 - Pavlo CMU SCS /615
9
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 - Pavlo CMU SCS /615
10
Motivation & upcoming conclusion:
E-R diagrams are excellent documentation tools Student Course Takes Faloutsos - Pavlo CMU SCS /615
11
Overview concepts Entities Relationships Attributes
Specialization/Generalization Aggregation ER modeling questions Faloutsos - Pavlo CMU SCS /615
12
Tools Entities (‘entity sets’) Relationships (‘rel. sets’)
Student Course Takes Tools Entities (‘entity sets’) Relationships (‘rel. sets’) and mapping constraints N M P attributes Faloutsos - Pavlo CMU SCS /615
13
Example Students, taking courses, offered by instructors; a course may have multiple sections; one instructor per section nouns -> entity sets verbs -> relationship sets Faloutsos - Pavlo CMU SCS /615
14
Example Students, taking courses, offered by instructors; a course may have multiple sections; one instructor per section nouns -> entity sets verbs -> relationship sets Faloutsos - Pavlo CMU SCS /615
15
Example Students, taking courses, offered by instructors; a course may have multiple sections; one instructor per section nouns -> entity sets verbs -> relationship sets Faloutsos - Pavlo CMU SCS /615
16
unique identifier ->
... name STUDENT ssn INSTRUCTOR issn primary key = unique identifier -> underline Faloutsos - Pavlo CMU SCS /615
17
but: sections of course (with different instructors)?
... name STUDENT c-id ssn c-name COURSE INSTRUCTOR issn but: sections of course (with different instructors)? Faloutsos - Pavlo CMU SCS /615
18
but: s-id is not unique... (see later) INSTRUCTOR issn
STUDENT c-id COURSE SECTION s-id but: s-id is not unique... (see later) INSTRUCTOR issn Faloutsos - Pavlo CMU SCS /615
19
Q: how to record that students take courses? INSTRUCTOR issn
c-id COURSE SECTION s-id Q: how to record that students take courses? INSTRUCTOR issn Faloutsos - Pavlo CMU SCS /615
20
ssn STUDENT N c-id takes M COURSE s-id SECTION INSTRUCTOR issn
Faloutsos - Pavlo CMU SCS /615
21
STUDENT N c-id takes M s-id SECTION COURSE N teaches 1 INSTRUCTOR
Faloutsos - Pavlo CMU SCS /615
22
STUDENT N c-id takes M N 1 s-id has SECTION COURSE N teaches 1
INSTRUCTOR Faloutsos - Pavlo CMU SCS /615
23
Cardinalities 1 to 1 (example?) 1 to N N to M 1 1 1 N N M
Faloutsos - Pavlo CMU SCS /615
24
Cardinalities 1 1 has CAPITAL 1 N owns PERSON CAR N M takes SECTION
COUNTRY CAPITAL 1 N owns PERSON CAR N M takes SECTION STUDENT Faloutsos - Pavlo CMU SCS /615
25
Cardinalities Book’s notation: has CAPITAL owns PERSON CAR takes
COUNTRY CAPITAL Book’s notation: owns PERSON CAR takes SECTION STUDENT Faloutsos - Pavlo CMU SCS /615
26
Cardinalities Book’s notation vs 1 to N notation 1 1 has CAPITAL 1 N
COUNTRY CAPITAL Book’s notation vs 1 to N notation 1 N owns PERSON CAR N M takes SECTION STUDENT Faloutsos - Pavlo CMU SCS /615
27
Cardinalities 1 1 has CAPITAL 1 N owns PERSON CAR N M takes SECTION
COUNTRY CAPITAL 1 N owns PERSON CAR N M takes SECTION STUDENT Faloutsos - Pavlo CMU SCS /615
28
‘Total/partial’ participation
1:1 1:1 total, total has COUNTRY CAPITAL ?:1 ?:N owns PERSON CAR ?? ?:N ?:M takes SECTION ?? STUDENT Faloutsos - Pavlo CMU SCS /615
29
‘Total/partial’ participation
1:1 1:1 total, total has COUNTRY CAPITAL 1:1 0:N owns PERSON CAR partial, total ?:N ?:M takes SECTION ?? STUDENT Faloutsos - Pavlo CMU SCS /615
30
‘Total/partial’ participation
1:1 1:1 total, total has COUNTRY CAPITAL 1:1 0:N owns PERSON CAR partial, total 1:N 0:M takes SECTION partial, total STUDENT Faloutsos - Pavlo CMU SCS /615
31
‘Total/partial’ participation
Is it ‘legal’? partial, total 1:1 0:N owns PERSON CAR Faloutsos - Pavlo CMU SCS /615
32
‘Total/partial’ participation
Is it ‘legal’? partial, total NO! why not? 1:1 0:N owns PERSON CAR Faloutsos - Pavlo CMU SCS /615
33
Subtle concept: Weak entities
‘section’ has no unique-id of its own!(?) c-id N 1 s-id has SECTION COURSE Faloutsos - Pavlo CMU SCS /615
34
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’) c-id N 1 s-id SECTION has COURSE Faloutsos - Pavlo CMU SCS /615
35
Weak entities Other example(s) of weak entities? ??id N 1 ?? ??? ??
Faloutsos - Pavlo CMU SCS /615
36
Weak entities Other example(s) of weak entities? e-id N 1 name
Dependent has Employee Faloutsos - Pavlo CMU SCS /615
37
More details self-relationships - example? ?? ?? ?? ??
Faloutsos - Pavlo CMU SCS /615
38
More details self-relationships - example? manages 1 EMPLOYEE N
Faloutsos - Pavlo CMU SCS /615
39
More details self-relationships - example? Has-friend ?? FB user ??
Faloutsos - Pavlo CMU SCS /615
40
More details 3-way and k-way relationships? Faloutsos - Pavlo
CMU SCS /615
41
More details 3-way and k-way relationships? Rare, but possible: N M
EMPLOYEE TOOL uses P PROJECT Faloutsos - Pavlo CMU SCS /615
42
More details 3-way and k-way relationships? Rare, but possible: N M ??
Other cases? ?? Faloutsos - Pavlo CMU SCS /615
43
More details 3-way and k-way relationships? Rare, but possible: N M
reviews user keyword P App-store/amazon reviews app Faloutsos - Pavlo CMU SCS /615
44
Overview concepts Entities Relationships Attributes
Specialization/Generalization Aggregation ER modeling questions Faloutsos - Pavlo CMU SCS /615
45
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) ] Faloutsos - Pavlo CMU SCS /615
46
Overview concepts Basic Advanced/ rare Entities Relationships
Attributes Specialization/Generalization Aggregation ER modeling questions Basic Advanced/ rare Faloutsos - Pavlo CMU SCS /615
47
Specialization eg., students: part time (#credit-hours) and full time (major) name STUDENT ssn IS-A FT-STUDENT PT-STUDENT major #credits Faloutsos - Pavlo CMU SCS /615
48
Observations Generalization: exact reverse of ‘specialization’
attribute inheritance could have many levels of an IS-A hierarchy Faloutsos - Pavlo CMU SCS /615
49
More details Overlap constraints Covering constraints A B C
Faloutsos - Pavlo CMU SCS /615
50
More details Overlap constraints Covering constraints
can an entity belong to both ‘B’ and ‘C’? Covering constraints can an ‘A’ entity belong to neither ‘B’ nor ‘C’? B C Faloutsos - Pavlo CMU SCS /615
51
More details Overlap constraints - examples? A No overlap with overlap
B C A A C C B B Faloutsos - Pavlo CMU SCS /615
52
More details Covering constraints - examples? A Total coverage A C B B
Partial coverage A C B Faloutsos - Pavlo CMU SCS /615
53
Overview concepts Entities Relationships Attributes
Specialization/Generalization Aggregation ER modeling questions Faloutsos - Pavlo CMU SCS /615
54
Aggregation computer model (w/ CPU and HD) and Maker (eg., Dell, HP) M
? MAKER HD CPU Faloutsos - Pavlo CMU SCS /615
55
Aggregation treat a relationship as an entity
used to express a relationship among relationships CPU HD N M MAKER Faloutsos - Pavlo CMU SCS /615
56
Overview concepts Entities Relationships Attributes
Specialization/Generalization Aggregation ER modeling questions Faloutsos - Pavlo CMU SCS /615
57
Conceptual design Entity vs attribute Entity vs relationship
Binary or ternary relationships? Aggregation? Faloutsos - Pavlo CMU SCS /615
58
Entity vs. attribute Entity EMPLOYEE (w/ emp#, name, job_code, ...)
Q: How about ‘spouse’ - entity or attribute? Q: How about ‘dependents’? Faloutsos - Pavlo CMU SCS /615
59
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 Faloutsos - Pavlo CMU SCS /615
60
Entity vs. Relationship
STUDENT STUDENT 1 N N OR takes TAKES M N SECTION 1 SECTION Faloutsos - Pavlo CMU SCS /615
61
Binary vs Ternary Relationships
usually, binary relationships are ‘cleaner’: Faloutsos - Pavlo CMU SCS /615
62
Binary vs. Ternary Relationships
Faloutsos - Pavlo CMU SCS /615 age pname Dependents Covers name Employees ssn lot If each policy is owned by just 1 employee: Policies policyid cost Faloutsos - Pavlo CMU SCS /615 7
63
Binary vs. Ternary Relationships
Faloutsos - Pavlo CMU SCS /615 Policies policyid cost age pname Dependents Covers name Employees ssn lot If each policy is owned by just 1 employee: Bad design Faloutsos - Pavlo CMU SCS /615 7
64
Binary vs. Ternary Relationships
Faloutsos - Pavlo CMU SCS /615 Policies policyid cost age pname Dependents Covers name Employees ssn lot If each policy is owned by just 1 employee: Bad design Key constraint on Policies would mean policy can only cover 1 dependent! Faloutsos - Pavlo CMU SCS /615 7
65
Binary vs. Ternary Relationships
Faloutsos - Pavlo CMU SCS /615 Policies policyid cost age pname Dependents Covers name Employees ssn lot If each policy is owned by just 1 employee: Bad design Key constraint on Policies would mean policy can only cover 1 dependent! Beneficiary age pname Dependents policyid cost Policies Purchaser name Employees ssn lot Better design What are the additional constraints in the 2nd diagram? Faloutsos - Pavlo CMU SCS /615 7
66
Binary vs Ternary Rel. But sometimes ternary rel. can not be replaced by a set of binary rel’s: Faloutsos - Pavlo CMU SCS /615
67
Binary vs. Ternary Relationships (Contd.)
Faloutsos - Pavlo CMU SCS /615 Binary vs. Ternary Relationships (Contd.) qty Parts Contract Departments VS. Suppliers Parts Departments can-supply Suppliers deals-with why is it bad? Faloutsos - Pavlo CMU SCS /615 9
68
Binary vs. Ternary Relationships (Contd.)
Faloutsos - Pavlo CMU SCS /615 Binary vs. Ternary Relationships (Contd.) qty Parts Contract Departments VS. Suppliers Parts Departments can-supply Suppliers deals-with 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? Faloutsos - Pavlo CMU SCS /615 9
69
Binary vs. Ternary Relationships (Contd.)
Faloutsos - Pavlo CMU SCS /615 Binary vs. Ternary Relationships (Contd.) qty Not in textbook: in practice, often: Parts Contract Departments Suppliers qty Parts Contract Departments Suppliers Faloutsos - Pavlo CMU SCS /615 9
70
Binary vs. Ternary Relationships (Contd.)
Faloutsos - Pavlo CMU SCS /615 Binary vs. Ternary Relationships (Contd.) Not in textbook: in practice, often: c-id qty Parts Contract Departments Suppliers Faloutsos - Pavlo CMU SCS /615 9
71
Binary vs. Ternary Relationships (Contd.)
Faloutsos - Pavlo CMU SCS /615 Binary vs. Ternary Relationships (Contd.) Not in textbook: in practice, often: c-id qty M N Parts Contract Departments N N 1 1 Suppliers Faloutsos - Pavlo CMU SCS /615 9
72
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) Faloutsos - Pavlo CMU SCS /615
73
Ternary vs. aggregation
How would you handle this case? CPU HD N M MAKER Faloutsos - Pavlo CMU SCS /615
74
Ternary vs. aggregation
How would you handle this case? COMP. MODEL MAKER HD CPU Faloutsos - Pavlo CMU SCS /615
75
Ternary vs. aggregation
How would you handle this case? COMP. MODEL ? ? ? ? MAKER HD CPU Faloutsos - Pavlo CMU SCS /615
76
Ternary vs. aggregation
How would you handle this case? COMP. MODEL M M HAS_CPU N N MAKER HD CPU Faloutsos - Pavlo CMU SCS /615
77
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 Faloutsos - Pavlo CMU SCS /615
78
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 Faloutsos - Pavlo CMU SCS /615
79
Summary - cont’d attribute (strong) entity set weak entity set
primary key A relationship set partial key A identifying rel. set for weak entity Faloutsos - Pavlo CMU SCS /615
80
Summary - cont’d M N cardinalities cardinalities l’:h’ l:h
with limits l:h l’:h’ partial/total (not in textbook - FYI) Faloutsos - Pavlo CMU SCS /615
81
Summary - cont’d IS-A aggregation Faloutsos - Pavlo CMU SCS /615
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.