Upload
allan-mcgee
View
222
Download
2
Tags:
Embed Size (px)
Citation preview
The Entity-Relationship Model
CIS 4301
Lecture Notes
1/12/2006
Lecture 2© CIS 4301 - Spring 2006 2
Database Modeling Analysis of what information the database must hold
Relationships among components of that information
Database schema is specified in one of several languages or notations suitable for expressing designs
Next, design is committed to a form which can be input to a DBMS DB takes on physical existence
Design notation in this course is Entity-Relationship (E/R) model
Ideas
ODL, UML, etc.
E/R
Object-Oriented DBMS
Relational DBMS
Relations
Lecture 2© CIS 4301 - Spring 2006 3
Entity-Relationship Model
Entity: Real-world object, concept, or event
Entity type/set: Collection of entities of the same type; sets don’t have to be distinct
Attribute: Values describing some property of an entity simple vs. composite single-valued vs. multi-valued stored vs. derived
Relationship: Association between two or more entity sets
Relationship type/set: Collection of relationships
Lecture 2© CIS 4301 - Spring 2006 4
Examples - “Movie Database”
Entity: Movie, Star
Attributes: Name: “Harrison Ford” (simple, single-valued, stored)
Address: “123 Main Str., LA, CA” (complex) Birthdate: “1-1-50” (simple) Age: 50 (could be derived from Birthdate and current date)
Entity Types: Stars (Name,Address,Birthdate,Age) Movies(Title,Year,Length,FilmType)
Lecture 2© CIS 4301 - Spring 2006 5
E/R Diagrams - Notation
Entity Set
Relationship Set
Attribute
Multivalued
Derived
...
CompositeAttribute
Role Name
Lecture 2© CIS 4301 - Spring 2006 6
Sample Movie Database
Movies
Studios
Stars
Owns
Stars-in
title year
lengthfilmType
name address
address
name
street city zip
Lecture 2© CIS 4301 - Spring 2006 7
Relationships Define associations among entities Grouped together into relationships sets (types)
Example: Relationship type: Stars-in between Movies and Stars
Can be visualized as a table
Movies Stars
A. SchwarzeneggerTotal Recall
Sharon StoneTotal Recall
Sharon StoneBasic Instinct
Relationships
Relationship TypeStars-in
Lecture 2© CIS 4301 - Spring 2006 8
Relationships Cont’d Degree of a relationship type is the number of participating entity types binary, ternary, n-ary
Another example: Entities: Stars, Movies, Studios Ternary Relationship Set: Contracts
Each entity type that participates in a relationship type plays a role in the relationship Marked by (optional) name Entity type may play multiple roles in a relationship type (see example later)
Lecture 2© CIS 4301 - Spring 2006 9
Relationships Cont’d
Relationships can have attributes Example: Relationship type Contracts between Stars and Studios for Movies
Might want to record salary associated with each contract
Not necessary to place attributes on relationships; instead, can invent new entity type whose entities have the attributes ascribed to the relationship
Lecture 2© CIS 4301 - Spring 2006 10
Multiplicity of Binary Relationships
Limit the possible combinations of entities that participate in the relationship type Derived from real-world scenario which is being modeled
a.k.a cardinality ratio Specifies the number of relationships an entity can participate in 1:1, 1:N, N:1, N:M
Example: Stars-in between Stars and Movies is N:M from Stars to Movies
Lecture 2© CIS 4301 - Spring 2006 11
Multiplicity In a many-many relationship, an entity of either set can be connected to many entities of the other set
In a many-one relationship, each entity of the first set is connected to at most one entity of the second set An entity of the second set can be connected to zero, one, or many entities of the first set
In a one-one relationship, each entity of either set is connected to at most one entity of the other set
Lecture 2© CIS 4301 - Spring 2006 12
In Pictures…
many-many many-one one-one
Lecture 2© CIS 4301 - Spring 2006 13
Multiplicity in the E/R Model
Use arrow pointing to “one” side of relationship
many-many
many-one
one-one
N N
N 1
1 1
letters optional
Lecture 2© CIS 4301 - Spring 2006 14
Sample Movie Database
Movies
Studios
Stars
Owns
Stars-in
title year
lengthfilmType
name address
address
name
MN
1
N
street city zip
Lecture 2© CIS 4301 - Spring 2006 15
Multi-way Relationships
Movies
Studios
Stars
Contracts
1
N
N
Movies Stars
SchwarzeneggerTotal Recall
Sharon StoneTotal Recall
Sharon StoneBasic Instinct
Studios
MGM
MGM
Universal
H. FordStar Wars Fox
“1” means that if we select one entity from each of the other entity sets in the relationship, those entities are related to a unique entity in Studios “Contracts” Relationship
Ternary
Lecture 2© CIS 4301 - Spring 2006 16
Multi-way Relationships
Movies
Studios
Stars
Contracts
1
N
N
“1” means that if we select one entity from each of the other entity sets in the relationship, those entities are related to a unique entity in Studios
1
Producing StudioStudioof Star
4-way
Lecture 3© CIS 4301 - Fall 2003 17
Subclass/Superclass
Often, class contains certain objects that have special properties not associated with all members Useful to organize class into subclasses Each having its own special attributes and/or relationships
In addition to those of the superclass Example
Superclass: Movies Subclasses: Cartoons, Mystery, Science-Fiction
Lecture 3© CIS 4301 - Fall 2003 18
Subclass in E/R Diagrams
Assume subclasses form a tree (i.e., no multiple inheritance)
ISA triangles indicate subclass relationship Point to the superclass
Lecture 3© CIS 4301 - Fall 2003 19
Example
Movies
title year lengthfilmType
CartoonsMurder
Mysteries
weapon
Voices
N
M Indicates direction of superclass/subclass
relationship
ISA
Lecture 3© CIS 4301 - Fall 2003 20
Inheritance
Entity in subclass represents same real-world entity as in the superclass Should possess values for its specific attributes
As well as values for the attributes it inherits from its superclass
Entity inherits all attributes defined on superclass and the relationships in which superclass participates
Lecture 3© CIS 4301 - Fall 2003 21
E/R vs. Object-Oriented Subclasses
In object-oriented world, objects are in one class only Subclasses inherit properties from superclasses
In contrast, E/R entities have components in all subclasses to which they belong
Lecture 3© CIS 4301 - Fall 2003 22
Example
Movies
title year lengthfilmType
CartoonsMurder
Mysteries
weapon
Voices
How do we represent a cartoon-murder-mystery?
N
M
contents mayoverlap
ISA
The Pink Panther
Lecture 3© CIS 4301 - Fall 2003 23
Another Example
Accounts
account-numberbalance
SavingsAccounts
CheckingAccounts
overdraft-amountinterest-rate
contents aredisjoint
ISA
disjoint
Lecture 3© CIS 4301 - Fall 2003 24
Design Principles
Faithfulness Avoid redundancy
Relationship Owns between Movies and Studios; adding attribute studioName to Movies would be redundant
Simplicity counts
Movies StudiosOwns
Name
StudioName
redundant
Lecture 3© CIS 4301 - Fall 2003 25
Example
Movies
Studios
Stars
Owns
Stars-in
title year
lengthfilmType
name address
address
name
MN
1
N
street city zip
Worksfor
??
Lecture 3© CIS 4301 - Fall 2003 26
Design Principles
Picking the right kind of element Many of the choices are between using attributes and using entities
E.g., Should we have eliminated Studios and made name and address attributes of Movies instead?
If something has more info than just a name, it probably needs to be an entity type
Lecture 3© CIS 4301 - Fall 2003 27
Example
MoviesStarsStars-in
title year
lengthfilmType
name address
address
name
MN
street city zip
??
Lecture 3© CIS 4301 - Fall 2003 28
Example
AcctSets Customers
Accounts Addressesnumber
balance
Memberof
address
Livesat
Has
nameowner-address