28
The Entity- Relationship Model CIS 4301 Lecture Notes 1/12/2006

The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

Embed Size (px)

Citation preview

Page 1: The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

The Entity-Relationship Model

CIS 4301

Lecture Notes

1/12/2006

Page 2: 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

Page 3: The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

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

Page 4: The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

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)

Page 5: The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

Lecture 2© CIS 4301 - Spring 2006 5

E/R Diagrams - Notation

Entity Set

Relationship Set

Attribute

Multivalued

Derived

...

CompositeAttribute

Role Name

Page 6: The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

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

Page 7: The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

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

Page 8: The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

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)

Page 9: The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

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

Page 10: The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

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

Page 11: The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

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

Page 12: The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

Lecture 2© CIS 4301 - Spring 2006 12

In Pictures…

many-many many-one one-one

Page 13: The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

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

Page 14: The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

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

Page 15: The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

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

Page 16: The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

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

Page 17: The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

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

Page 18: The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

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

Page 19: The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

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

Page 20: The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

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

Page 21: The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

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

Page 22: The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

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

Page 23: The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

Lecture 3© CIS 4301 - Fall 2003 23

Another Example

Accounts

account-numberbalance

SavingsAccounts

CheckingAccounts

overdraft-amountinterest-rate

contents aredisjoint

ISA

disjoint

Page 24: The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

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

Page 25: The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

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

??

Page 26: The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

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

Page 27: The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

Lecture 3© CIS 4301 - Fall 2003 27

Example

MoviesStarsStars-in

title year

lengthfilmType

name address

address

name

MN

street city zip

??

Page 28: The Entity-Relationship Model CIS 4301 Lecture Notes 1/12/2006

Lecture 3© CIS 4301 - Fall 2003 28

Example

AcctSets Customers

Accounts Addressesnumber

balance

Memberof

address

Livesat

Has

nameowner-address