ER Data Models Ctd. CSET 3300. ER Diagram - Example Consider a database that records the grades of...

34
ER Data Models Ctd. ER Data Models Ctd. CSET 3300

Transcript of ER Data Models Ctd. CSET 3300. ER Diagram - Example Consider a database that records the grades of...

Page 1: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

ER Data Models Ctd.ER Data Models Ctd.ER Data Models Ctd.ER Data Models Ctd.

CSET 3300

Page 2: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

ER Diagram - ExampleER Diagram - Example Consider a database that records the grades of students in

different exams of different course offerings.a) assume that course offerings include time, room; and can be

identified uniquely by course_no, semester, year, section_no.

b) students, including student-id, name, and program;

c) exam, including eid (optional), name, place, time.

Furthermore, grades of students in different exams of course offerings must be appropriately modeled.

Construct an E-R diagram that models exams as entities, and uses a a ternary relationshipternary relationship, for the above database.

Page 3: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

student

sid

program

course-offerings

year

semestertime

room

section_no

takes

grade

exameid

name place

time

course_noname

ER Diagram with Ternary RelationshipER Diagram with Ternary Relationship

Page 4: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

SubclassesSubclasses• Subclass = special case = fewer entities = more

properties.• Example: Ales are a kind of beer.

– Not every beer is an ale, but some are.– Let us suppose that in addition to all the

properties (attributes and relationships) of beers, ales also have the attribute color.

Page 5: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

Subclasses in E/R DiagramsSubclasses in E/R Diagrams• Assume subclasses form a tree.

– I.e., no multiple inheritance.

• Is_a triangles indicate the subclass relationship.– Point to the superclass.– NOTE:Every isa relationship is one-one– NOTE:But we don’t draw 2 arrows

Beers

Ales

isa

name

manufacturer

color

Page 6: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

Another ExampleAnother Example

Movies

Murder-Mysteries

isa

title

weapon

year

cartoons

isa

Voices

MovieStars

length

Page 7: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

KeysKeys• A key is a set of attributes for one entity set

such that no two entities in this set agree on all the attributes of the key.– It is allowed for two entities to agree on some, but

not all, of the key attributes.

• We must designate a key for every entity set.

Page 8: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

Keys in E/R DiagramsKeys in E/R Diagrams

• Underline the key attribute(s).

Beersname

manufacturer

Page 9: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

Keys in is_a RelationshipKeys in is_a Relationship

Beers

Ales

Is_a

name

color

manufacturer

In an Is_a hierarchy, only the root entity set has a key, and it must serve as the key for all entities in the hierarchy.

Page 10: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

Example: a Multi-attribute KeyExample: a Multi-attribute Key

Courses

dept number hours room

Entity can have more than one key.

Note that hours and room could also serve as a key, but we must select only one key.

Page 11: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

Weak Entity SetsWeak Entity Sets• Occasionally, entities of an entity set need

“help” to identify them uniquely.• Entity set E is said to be weak if in order to

identify entities of E uniquely, we need to follow one or more many-one relationships from E and include the key of the related entities from the connected entity sets.

Page 12: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

ExampleExample

• name is almost a key for football players, but there might be two with the same name.

• number is certainly not a key, since players on two teams could have the same number.

• But number, together with the Team related to the player by Plays-on should be unique.

Page 13: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

In E/R DiagramsIn E/R Diagrams

Players TeamsPlays-on

name namenumber

• Double diamond for supporting many-one relationship.• Double rectangle for the weak entity set.

A weak entityweak entity can be identified uniquely only by considering the primary key of another (owner) entity Owner entity set and weak entity set must participate in

one-to-manyone-to-many relationships (one owner, many weak entities).

Weak entity set must have total participationtotal participation in this identifying relationship set.

Page 14: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

Example: Course OfferingExample: Course Offering• course offerings includes course numbercourse number,

year, semester, section number, instructorinstructor(s), timings, and classroom

time

roomcourse

section_no

semester

year

course- offeringsis offered

course_no

Page 15: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

Weak Entity-Set RulesWeak Entity-Set Rules• A weak entity set has one or more many-one

relationships to other (supporting) entity sets.– Not every many-one relationship from a weak

entity set need be supporting.

• The key for a weak entity set is its own underlined attributes and the keys for the supporting entity sets.– E.g., player-number and team-name is a key for

Players in the player-team example.

Page 16: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

Identifying a Weak Entity TypeIdentifying a Weak Entity Type• A Weak entity type doesn’t have a primary key.• If X is a weak entity type and Y is the entity type on

which X is dependent.• We form a primary key for X by combining the

primary key of Y which one or more attributes, called discriminator or partial key, from X.

• In an E/R Diagram, a partial key is usually dash-underlined.

• e.g., primary key for DEPENDENT: {Employee No., DName}.

Page 17: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

EmpolyeeEMP_DEP

GenderDName

Emp No.

Name

Dependent

Emp No.

Example: Employee-DependentExample: Employee-DependentExample: Employee-DependentExample: Employee-Dependent

Address

Name

Page 18: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

Notations – A SummaryNotations – A Summary

Name

Name

isa

Page 19: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

Design TechniquesDesign Techniques

1. Avoid redundancy.2. Don’t use an entity set when an attribute will

do.3. Limit the use of weak entity sets.

Page 20: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

Avoiding RedundancyAvoiding Redundancy• Redundancy occurs when we say the same

thing in two different ways.• Redundancy wastes space and (more

importantly) encourages inconsistency.– The two instances of the same fact may become

inconsistent if we change one and forget to change the other, related version.

Page 21: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

Example: GoodExample: Good

Beers ManfsManfBy

name

This design gives the address of each manufacturer exactly once.

name addr

Page 22: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

Example: BadExample: Bad

Beers ManfsManfBy

name

This design states the manufacturer of a beer twice: as an attribute and as a related entity.

name

manf

addr

Page 23: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

Example: BadExample: Bad

Beers

name

This design repeats the manufacturer’s address once for each beer; loses the address if there are temporarily no beers for a manufacturer.

manf manfAddr

Page 24: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

Entity Sets Versus AttributesEntity Sets Versus Attributes• An entity set should satisfy at least one of

the following conditions:– It is more than the name of something; it has at

least one nonkey attribute.or

– It is the “many” in a many-one or many-many relationship.

Page 25: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

Example: GoodExample: Good

Beers ManfsManfBy

name

•Manfs deserves to be an entity set because of the nonkey attribute addr.•Beers deserves to be an entity set because it is the “many” of the many-one relationship ManfBy.

name addr

Page 26: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

Example: GoodExample: Good

Beers

name

There is no need to make the manufacturer an entity set, because we record nothing about manufacturers besides their name.

manf

Page 27: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

Example: BadExample: Bad

Beers ManfsManfBy

name

Since the manufacturer is nothing but a name, and is not at the “many” end of any relationship, it should not be an entity set.

name

Page 28: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

Don’t Overuse Weak Entity SetsDon’t Overuse Weak Entity Sets• Beginning database designers often doubt that

anything could be a key by itself.– They make all entity sets weak, supported by all

other entity sets to which they are linked.

• In reality, we usually create unique ID’s for entity sets.– Examples include social-security numbers,

automobile VIN’s etc.

Page 29: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

When Do We Need Weak Entity Sets?When Do We Need Weak Entity Sets?• The usual reason is that there is no global

authority capable of creating unique ID’s.• Example: it is unlikely that there could be an

agreement to assign unique player numbers across all football teams in the world.

Page 30: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

Example: University DBExample: University DB

The university keeps track of each student's name, student number, social security number, current address and phone number, birthdate, sex, class (freshman, graduate), major department, minor department (if any), degree program (B.A., B.S., ... Ph.D.).  Some user applications need to refer to the city, state, and zip code of the student's current address and to the student's last name.  Both social security number and student number are unique for each student. All students will have at least a major department.

Page 31: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

Example: University DB (cont.)Example: University DB (cont.) Each department is described by a name, department code,

office number, and college.  Both the name and code have unique values for each department.

Each course has a course name, description, course number, number of credits, and offering department.  The course number is unique for each course.

Page 32: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

Example: University DB (cont.)Example: University DB (cont.)

Each section has an instructor, semester, year, course, and section number.  The section number distinguishes sections of the same course that are taught during the same semester/year; its value is an integer (1, 2, 3, ... up to the number of sections taught during each semester).

Page 33: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

Example: University DB (cont.)Example: University DB (cont.)

A grade report must be generated for each student that lists the section, letter grade, and numeric grade (0,1,2,3, or 4) for each student and calculates his or her average GPA.

Page 34: ER Data Models Ctd. CSET 3300. ER Diagram - Example  Consider a database that records the grades of students in different exams of different course offerings.

ER Diagram for University DBER Diagram for University DB

Student

Class

StudentID SSN

Sex

Zip

Degree

City

Birth date

State

Name

Address

Department

DName DCode OfficeNumber

OfficePhone

College

Course

CName

CourseDesc

CNumber

Credits

Section

Instructor Year

SemesterSectionNumber

GPA

Numeric Grade

Letter Grade

Grade_Report

Belong_To

Offer

Minor In

Major In