CMSC 424 – Database design Lecture 3: Entity-Relationship ...
Transcript of CMSC 424 – Database design Lecture 3: Entity-Relationship ...
![Page 1: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/1.jpg)
CMSC 424 – Database designLecture 3:
Entity-Relationship Model
Book: Chap. 1 and 6
Mihai Pop
![Page 2: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/2.jpg)
Database Design Steps
Three Levels of Modeling
info
Conceptual Data Model
Logical Data Model
Physical Data Model
Conceptual DB design
Logical DB design
Physical DB design
Entity-relationship Model Typically used for conceptual database design
Relational Model Typically used for logical database design
![Page 3: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/3.jpg)
Problems in our First Design• does not capture the fact that a library carries books of a specific
author• does not capture the fact that a library carries a specific book• does not capture the fact that an author has written a specific book• does not store which edition of the book the library has, how many
copies, etc.
WROTE-ON
INDEX
CARRY
SUBJECTSNAME
LIBRARYLNAME
AUTHOR
BDATENAMESS# TEL
BOOKISBN
TITLE
![Page 4: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/4.jpg)
2nd Attempt to the Library Design
• Much better
WROTE
INDEX SUBJECT SNAME
LIBRARYLNAME
BOOKISBN
TITLE
IN-STOCKQUANTITY
EDITION
AUTHOR
BDATENAMESS# TEL
![Page 5: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/5.jpg)
Next: Types of Attributes
• Simple vs Composite Single value per attribute ?
• Single-valued vs Multi-valued E.g. Phone numbers are multi-valued
• Derived If date-of-birth is present, age can be derived Can help in avoiding redundancy, enforcing constraints
etc…
![Page 6: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/6.jpg)
Types of Attributes
customer has
cust-street
cust-id
cust-name
cust-city
account
balance
numberaccess-date
![Page 7: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/7.jpg)
Types of Attributes
customer
cust-street
cust-id
cust-name
cust-city
has account
balance
numberaccess-date
date-of-birth
age
• multi-valued (double ellipse)(one can have many phone numbers)• derived (dashed ellipse)(age can be computed from birth date)
phone no.
![Page 8: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/8.jpg)
Types of Attributes
customer
cust-street
cust-id
cust-name
cust-city
has account
balance
numberaccess-date
date-of-birth
age
month day year
Composite Attribute
phone no.
![Page 9: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/9.jpg)
Next: Keys
• Key = set of attributes that uniquely identifies an entity or a relationship
![Page 10: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/10.jpg)
Entity Keys• {cust-id} is a natural primary key• Typically, SSN forms a good primary key• Try to use a candidate key that rarely
changes e.g. something involving address not a great
idea
customer
cust-street
cust-id
cust-name
cust-city
age
date-of-birth
phone no.
![Page 11: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/11.jpg)
Entity Keys
• Superkey any set of attributes that can distinguish entities
• Candidate key a minimal superkey
Can’t remove any attribute and preserve key-ness {cust-id, age} not a candidate key {cust-name, cust-city, cust-street} is
assuming cust-name is not unique
• Primary key Candidate key chosen as the key by DBA Underlined in the ER Diagram
![Page 12: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/12.jpg)
Entity Keys• {cust-id} is a natural primary key• Typically, SSN forms a good primary key• Try to use a candidate key that rarely
changes e.g. something involving address not a great
idea
customer
cust-street
cust-id
cust-name
cust-city
age
date-of-birth
phone no.
![Page 13: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/13.jpg)
Relationship Set Keys• What attributes are needed to represent a relationship completely
and uniquely ?– Union of primary keys of the entities involved, and relationship
attributes
– {cust-id, access-date, account number} describes a relationship completely
customer has
cust-id
account
numberaccess-date
![Page 14: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/14.jpg)
Relationship Set Keys• Is {cust-id, access-date, account number} a candidate key ?
– No. Attribute access-date can be removed from this set without losing key-ness– In fact, union of primary keys of associated entities is always a superkey
customer has
cust-id
account
numberaccess-date
![Page 15: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/15.jpg)
Relationship Set Keys
• Is {cust-id, account-number} a candidate key ?– Depends
customer has
cust-id
account
numberaccess-date
![Page 16: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/16.jpg)
Relationship Set Keys
• Is {cust-id, account-number} a candidate key ?– Depends
customer has
cust-id
account
numberaccess-date
If one-to-one relationship, either {cust-id} or {account-number} sufficient Since a given customer can only have one account, she can only participate in
one relationship Ditto account
![Page 17: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/17.jpg)
Relationship Set Keys
• Is {cust-id, account-number} a candidate key ?– Depends
customer has
cust-id
account
numberaccess-date
If one-to-many relationship (as shown), {account-number} is a candidate key A given customer can have many accounts, but at most one account holder per
account allowed
![Page 18: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/18.jpg)
Relationship Set Keys
• General rule for binary relationships one-to-one: primary key of either entity set one-to-many: primary key of the entity set on the many side many-to-many: union of primary keys of the associate entity
sets• n-ary relationships
More complicated rules
![Page 19: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/19.jpg)
…
• What have we been doing
• Why ?
• Understanding this is important Rest are details !! That’s what books/manuals are for.
![Page 20: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/20.jpg)
Next: Recursive Relationships
• Sometimes a relationship associates an entity set to itself
![Page 21: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/21.jpg)
Recursive Relationships
Must be declared with roles
employee works-for
emp-street
emp-id
emp-name
emp-city
manager
worker
![Page 22: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/22.jpg)
Next: Weak Entity Sets
• An entity set without enough attributes to have a primary key
• E.g. Transaction Entity– Attributes:
• transaction-number, transaction-date, transaction-amount, transaction-type
• transaction-number: may not be unique across accounts
![Page 23: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/23.jpg)
Weak Entity Sets
• A weak entity set must be associated with an identifying or owner entity set
• Account is the owner entity set for Transaction
![Page 24: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/24.jpg)
Weak Entity Sets
account
balance
number
Transactionhas
trans-type
trans-number
trans-date
trans-amt
Still need to be able to distinguish between different weak entities associated with the same strong entity
![Page 25: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/25.jpg)
Weak Entity Sets
account
balance
number
Transactionhas
trans-type
trans-number
trans-date
trans-amt
Discriminator: A set of attributes that can be used for that
![Page 26: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/26.jpg)
Weak Entity Sets
• Primary key:– Primary key of the associated strong entity +
discriminator attribute set– For Transaction:
• {account-number, transaction-number}
![Page 27: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/27.jpg)
Example Design
• We will model a university database– Main entities:
• Professor• Projects• Departments• Graduate students• etc…
![Page 28: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/28.jpg)
professorarea
name
SSN
rank
projectstart
sponsor
proj-number
budget
deptoffice
name
dept-no
homepage
gradage
name
SSN
degree
![Page 29: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/29.jpg)
professorarea
name
SSN
rank
projectstart
sponsor
proj-number
budget
deptoffice
name
dept-no
homepage
gradage
name
SSN
degree
![Page 30: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/30.jpg)
professorarea
name
SSN
rank
projectstart
sponsor
proj-number
budget
deptoffice
name
dept-no
homepage
gradage
name
SSN
degree
PI
Co-PI
RA
Major
ChairSupervises
Mentorad
vise
e
advi
sor
Appt
Time (%)
![Page 31: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/31.jpg)
professorarea
name
SSN
rank
projectstart
sponsor
proj-number
budget
deptoffice
name
dept-no
homepage
gradage
name
SSN
degree
PI
Co-PI
RA
Major
Chair ApptSupervises
Mentorad
vise
e
advi
sor
Time (%)
![Page 32: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/32.jpg)
professorarea
name
SSN
rank
projectstart
sponsor
proj-number
budget
deptoffice
name
dept-no
homepage
gradage
name
SSN
degree
PI
Co-PI
RA
Major
Chair ApptSupervises
Mentorad
vise
e
advi
sor
Time (%)
![Page 33: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/33.jpg)
Thoughts…
• Nothing about actual data– How is it stored ?
• No talk about the query languages– How do we access the data ?
• Semantic vs Syntactic Data Models– Remember: E/R Model is used for conceptual modeling– Many conceptual models have the same properties
• They are much more about representing the knowledge than about database storage/querying
![Page 34: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/34.jpg)
Thoughts…
• Basic design principles– Faithful
• Must make sense– Satisfies the application requirements– Models the requisite domain knowledge
• If not modeled, lost afterwards– Avoid redundancy
• Potential for inconsistencies– Go for simplicity
• Typically an iterative process that goes back and forth
![Page 35: CMSC 424 – Database design Lecture 3: Entity-Relationship ...](https://reader030.fdocuments.us/reader030/viewer/2022020701/61f8e415e21d3e03550965b5/html5/thumbnails/35.jpg)
Design Issues
• Entity sets vs attributes– Depends on the semantics of the application– Consider telephone (entity: customer, attribute: telephone)
or (entity: telephone, entity: customer, relationship: customer_phone)
• Entity sets vs Relationship sets– Consider loan (entity or relationship between customer and bank?)
• N-ary vs binary relationships Possible to avoid n-ary relationships, but there are some cases where
it is advantageous to use them It is not an exact science !!