Download presentation
Presentation is loading. Please wait.
Published byElmer Newton Modified over 6 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
Administrivia Course url: Course policies Foils in pps:
Course policies Foils in pps: Faloutsos - Pavlo CMU SCS /615
3
Course Topics ✔ Introduction to DBMSs Data Models Query Language (SQL)
Faloutsos/Pavlo CMU /615 Course Topics ✔ Introduction to DBMSs Data Models Query Language (SQL) Database Design Query Optimization & Indexing Transaction Management Advanced Topics Faloutsos/Pavlo CMU SCS /615
4
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
5
Database users ‘naive’ users casual users application programmers
[ DBA (Data base administrator)] Faloutsos/Pavlo CMU SCS /615
6
Casual users DBMS and meta-data = catalog data select * from student
Faloutsos/Pavlo CMU SCS /615
7
``Naive’’ users Pictorially: app. (eg., report generator) DBMS
and meta-data = catalog data Faloutsos/Pavlo CMU SCS /615
8
App. programmers Authors of applications (like the ‘report generator’)
DBMS data and meta-data = catalog app. (eg., report generator) Faloutsos/Pavlo CMU SCS /615
9
DB Administrator (DBA)
Duties? DBMS data and meta-data = catalog Faloutsos/Pavlo CMU SCS /615
10
DB Administrator (DBA)
Duties? DBMS and meta-data = catalog data Faloutsos/Pavlo CMU SCS /615
11
DB Administrator (DBA)
schema definition (‘logical’ level) physical schema (storage structure, access methods schema modifications granting authorizations integrity constraint specification Faloutsos/Pavlo CMU SCS /615
12
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
13
Database users ‘naive’ users casual users application programmers
[ DBA (Data base administrator)] Faloutsos/Pavlo CMU SCS /615
14
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
15
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
16
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
17
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
18
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
19
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
20
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
21
Motivation & upcoming conclusion:
E-R diagrams are excellent documentation tools Student Course Takes 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); Faloutsos - Pavlo CMU SCS /615
22
Overview concepts Basic Advanced/ rare Entities Relationships
Attributes Specialization/Generalization Aggregation ER modeling questions Basic Advanced/ rare Faloutsos - Pavlo CMU SCS /615
23
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
24
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
25
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
26
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
27
unique identifier ->
... name STUDENT ssn INSTRUCTOR issn primary key = unique identifier -> underline Faloutsos - Pavlo CMU SCS /615
28
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
29
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
30
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
31
ssn STUDENT N c-id takes M COURSE s-id SECTION INSTRUCTOR issn
Faloutsos - Pavlo CMU SCS /615
32
STUDENT N c-id takes M s-id SECTION COURSE N teaches 1 INSTRUCTOR
Faloutsos - Pavlo CMU SCS /615
33
STUDENT N c-id takes M N 1 s-id has SECTION COURSE N teaches 1
INSTRUCTOR Faloutsos - Pavlo CMU SCS /615
34
Cardinalities 1 to 1 (example?) 1 to N N to M 1 1 1 N N M
Faloutsos - Pavlo CMU SCS /615
35
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
36
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
37
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
38
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
39
‘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
40
‘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
41
‘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
42
‘Total/partial’ participation
Is it ‘legal’? partial, total 1:1 0:N owns PERSON CAR Faloutsos - Pavlo CMU SCS /615
43
‘Total/partial’ participation
Is it ‘legal’? partial, total NO! why not? 1:1 0:N owns PERSON CAR Faloutsos - Pavlo CMU SCS /615
44
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
45
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
46
Weak entities Other example(s) of weak entities? ??id N 1 ?? ??? ??
Faloutsos - Pavlo CMU SCS /615
47
Weak entities Other example(s) of weak entities? e-id N 1 name
Dependent has Employee Faloutsos - Pavlo CMU SCS /615
48
More details self-relationships - example? ?? ?? ?? ??
Faloutsos - Pavlo CMU SCS /615
49
More details self-relationships - example? manages 1 EMPLOYEE N
Faloutsos - Pavlo CMU SCS /615
50
More details self-relationships - example? Has-friend ?? FB user ??
Faloutsos - Pavlo CMU SCS /615
51
More details 3-way and k-way relationships? Faloutsos - Pavlo
CMU SCS /615
52
More details 3-way and k-way relationships? Rare, but possible: N M
EMPLOYEE TOOL uses P PROJECT Faloutsos - Pavlo CMU SCS /615
53
More details 3-way and k-way relationships? Rare, but possible: N M ??
Other cases? ?? Faloutsos - Pavlo CMU SCS /615
54
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
55
Overview concepts Entities Relationships Attributes
Specialization/Generalization Aggregation ER modeling questions Faloutsos - Pavlo CMU SCS /615
56
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
57
Overview concepts Basic Advanced/ rare Entities Relationships
Attributes Specialization/Generalization Aggregation ER modeling questions Basic Advanced/ rare Faloutsos - Pavlo CMU SCS /615
58
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
59
Observations Generalization: exact reverse of ‘specialization’
attribute inheritance could have many levels of an IS-A hierarchy Faloutsos - Pavlo CMU SCS /615
60
More details Overlap constraints Covering constraints A B C
Faloutsos - Pavlo CMU SCS /615
61
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
62
More details Overlap constraints - examples? A No overlap with overlap
B C A A C C B B Faloutsos - Pavlo CMU SCS /615
63
More details Covering constraints - examples? A Total coverage A C B B
Partial coverage A C B Faloutsos - Pavlo CMU SCS /615
64
Overview concepts Entities Relationships Attributes
Specialization/Generalization Aggregation ER modeling questions Faloutsos - Pavlo CMU SCS /615
65
Aggregation computer model (w/ CPU and HD) and Maker (eg., Dell, HP) M
? MAKER HD CPU Faloutsos - Pavlo CMU SCS /615
66
Aggregation treat a relationship as an entity
used to express a relationship among relationships CPU HD N M MAKER Faloutsos - Pavlo CMU SCS /615
67
Overview concepts Entities Relationships Attributes
Specialization/Generalization Aggregation ER modeling questions Faloutsos - Pavlo CMU SCS /615
68
Conceptual design Entity vs attribute Entity vs relationship
Binary or ternary relationships? Aggregation? Faloutsos - Pavlo CMU SCS /615
69
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
70
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
71
Entity vs. Relationship
STUDENT STUDENT 1 N N OR takes TAKES M N SECTION 1 SECTION Faloutsos - Pavlo CMU SCS /615
72
Binary vs Ternary Relationships
usually, binary relationships are ‘cleaner’: Faloutsos - Pavlo CMU SCS /615
73
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
74
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
75
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
76
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
77
Binary vs Ternary Rel. But sometimes ternary rel. can not be replaced by a set of binary rel’s: Faloutsos - Pavlo CMU SCS /615
78
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
79
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
80
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
81
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
82
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
83
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
84
Ternary vs. aggregation
How would you handle this case? CPU HD N M MAKER Faloutsos - Pavlo CMU SCS /615
85
Ternary vs. aggregation
How would you handle this case? COMP. MODEL MAKER HD CPU Faloutsos - Pavlo CMU SCS /615
86
Ternary vs. aggregation
How would you handle this case? COMP. MODEL ? ? ? ? MAKER HD CPU Faloutsos - Pavlo CMU SCS /615
87
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
88
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
89
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
90
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
91
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
92
Summary - cont’d IS-A aggregation Faloutsos - Pavlo CMU SCS /615
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.