Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities...

72
©Silberschatz, Korth and Sudarsha 1 Database System Concepts Entity-Relationship Modelling Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction of an E-R Diagram to Tables Chapter 7

description

©Silberschatz, Korth and Sudarshan3Database System Concepts Entity Sets customer and loan Entity sets:  The set of all customers of the Bank  The set of all loans at the bank Entities:  Customers of the Bank – Bob Jones, Sue Smith, Mark Hayes, etc.  Loans at the Bank – L17, L15, L23, etc. Attributes:  Bob Jones has an ID# ( ), a street address (475 Main Street), a city of residence (Orlando), and a last name (Jones).  Loan L17 has an amount ($4537), a date when the loan was taken out (12/15/2009), and a loan number (L17).

Transcript of Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities...

Page 1: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan1Database System Concepts

Entity-Relationship ModellingEntity-Relationship Modelling

Entities Attributes Relationships Mapping Cardinality Keys Reduction of an E-R Diagram to Tables

Chapter 7

Page 2: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan2Database System Concepts

Entity SetsEntity Sets

A database can be modeled as a collection of: entities, and relationships among those entities.

An entity is an object that is distinguishable from other objects. A specific person, company, automobile, etc.

Entities have attributes: People have names and addresses An entity and its’ attributes are represented by a tuple

(342-97-4873, Smith, Main, Orlando)

An entity set is a set of entities of the same type, i.e., that share the same properties and attributes. An extension is an instance of it. Set of all students, set of all companies, set of all automobiles

Page 3: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan3Database System Concepts

Entity Sets Entity Sets customercustomer and and loanloan

Entity sets: The set of all customers of the Bank The set of all loans at the bank

Entities: Customers of the Bank – Bob Jones, Sue Smith, Mark Hayes, etc. Loans at the Bank – L17, L15, L23, etc.

Attributes: Bob Jones has an ID# (321-12-3231), a street address (475 Main Street), a

city of residence (Orlando), and a last name (Jones). Loan L17 has an amount ($4537), a date when the loan was taken out

(12/15/2009), and a loan number (L17).

Page 4: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan4Database System Concepts

AttributesAttributes

The set of permitted values for an attribute is called the domain of that attribute.

Attributes can be one of several types: Simple (i.e., atomic) – height in inches, weight in ounces, last-name Composite – name, address Single-valued – date of birth, name Multi-valued – phone-numbers, dependents, hobbies Derived – Age is derived, or rather, computed from date-of-birth

Page 5: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan5Database System Concepts

Attributes, Cont.Attributes, Cont.

Keep in mind that modeling is NOT design! during modeling we are focused on what the relevant data is, and not whether

or how it will be stored in the database. age vs. date-of-birth

This approach is: consistent with most text-books somewhat inconsistent with industry

Page 6: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan6Database System Concepts

Relationship SetsRelationship Sets

A relationship is an association among two (or more) entities Hayes is a depositor for account A-102 The relationship is denoted by a tuple (Hayes, A-102)

More simply stated, a relationship set is a set of relationships, all of the same type.

Page 7: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan7Database System Concepts

Attributes, Cont.Attributes, Cont.

Relationships can be visualized graphically:

customer borrower

Jones loan

Smith L-23

Hayes L-15

Jackson L-14

Curry L-19

Williams L-17

Adams

Page 8: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan8Database System Concepts

Relationship Sets (Cont.)Relationship Sets (Cont.)

An attribute can also be property of a relationship set.

Page 9: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan9Database System Concepts

Relationship SetsRelationship Setswith Attributes, Cont.with Attributes, Cont.

Another example of a relationship set having attributes: Entities: Student and Course Relationship: Has-Taken

Where does the attribute grade go?

Page 10: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan10Database System Concepts

Degree of a Relationship SetDegree of a Relationship Set

The number of entity sets that participate in a relationship set is referred to as the degree of that relationship set.

Relationship sets that involve two entity sets are called binary. Most relationship sets are binary.

Example of a ternary relationship set: Employees of a bank have jobs at multiple branches, with different jobs at

different branches. This gives a ternary relationship set between employee, job and branch.

Page 11: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan11Database System Concepts

Mapping CardinalitiesMapping Cardinalities

The mapping cardinality of a relationship set expresses the number of entities to which one entity can be associated via the relationship set.

Most useful in describing binary relationship sets.

For a binary relationship there are four types: One to one – legal US marriages One to many – academic advisors (at most one major) Many to one – same as one-to-many Many to many – depositors

Page 12: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan12Database System Concepts

Mapping CardinalitiesMapping Cardinalities

One to one One to many

Note: Some elements in A and B may not be mapped to any elements in the other set (i.e., total vs. partial relations)

Page 13: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan13Database System Concepts

Mapping Cardinalities Mapping Cardinalities

Many to one Many to many

Note: Some elements in A and B may not be mapped to any elements in the other set

Page 14: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan14Database System Concepts

Mapping CardinalitiesMapping Cardinalitiesaffect Attribute Placementaffect Attribute Placement

access-date could be an attribute of account instead of a relationship attribute if each account can have only one customer, i.e., if the relationship is one-to-many.

Page 15: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan15Database System Concepts

E-R DiagramsE-R Diagrams

Rectangles - entity sets (new: double for weak entity set) (new version: divided in name/attributes) (new version: undivided for attribute of relation)

Diamonds - relationship sets (new: double if linked to weak entity sets) Ellipses - attributes:

Double ellipses - multi-valued Dashed ellipses – derived Underlined - primary key attributes (will discuss later)

Lines - connect attributes to entity sets, and entity sets to relationship sets. (new) Dashed Lines: link attributes of relationship to relationship set (new) Double Lines: Total participation of an entity in a relationship set

Page 16: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan16Database System Concepts

E-R Diagram with Composite, Multivalued,E-R Diagram with Composite, Multivalued,and Derived Attributesand Derived Attributes

Notes: In many applications the terms are much more ambiguous (e.g., function designators) An ER diagram is typically accompanied by a document that defines all the terms Much harder to do than it appears

Page 17: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan17Database System Concepts

Relationship Sets with AttributesRelationship Sets with Attributes

Page 18: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan18Database System Concepts

Cardinality ConstraintsCardinality Constraints

Cardinality constraints are indicated by drawing a directed line (), signifying “one,” or an undirected line (—), signifying “many,” between the relationship and the entity.

If borrower were a one-to-one relationship: A customer would be associated with at most one loan A loan would be associated with at most one customer

Page 19: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan19Database System Concepts

One-To-Many RelationshipOne-To-Many Relationship

If borrower were a one-to-many relationship then a loan would be associated with at most one customer, and a customer would be associated with zero or more loans.

Page 20: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan20Database System Concepts

Many-To-One RelationshipsMany-To-One Relationships

If borrower were a many-to-one relationship then a loan would be associated with zero or more customers, and a customer would be associated with at most one loan.

Page 21: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan21Database System Concepts

Many-To-Many RelationshipMany-To-Many Relationship

If borrower were a many-to-many relationship then a loan would be associated with zero or more customers, and a customer would be associated with zero of more loans.

Page 22: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan22Database System Concepts

Participation of an Entity SetParticipation of an Entity Setin a Relationship Setin a Relationship Set

If every entity in an entity set participates in a relationship set, then that entity set is said to have total participation in the relationship. Indicated by a double-line

If participation in a relationship is optional for some entities then that entity set is said to have partial participation in the relationship.

Page 23: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan23Database System Concepts

Another ExampleAnother Exampleof Total Participationof Total Participation

Consider a relationship between entity sets Student and Faculty called Advises.

Assumptions: Each student has exactly one major, and hence exactly one advisor Some faculty don’t advise students

In this case the Advises relationship set is: One-to-many from Faculty to Student Student participation is total

In most universities, the Advises relationship is more likely many-to-many, with the Student participation being total.

Page 24: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan24Database System Concepts

Alternative NotationAlternative Notationfor Cardinality Limitsfor Cardinality Limits

Specific cardinality limits can also express participation constraints 0..*: a customer can have 0 or more loans. 1..1: a loan belongs to 1 and only 1 customer

Page 25: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan25Database System Concepts

RolesRoles

Relationships can be labeled with additional descriptive information called roles. Roles are optional, and used to clarify a relationship. Particularly helpful when the entities in a relationship are not distinct

• e.g., prerequisite course for a give course

The roles manager and worker specify how employee entities interact via the works-for relationship.

Page 26: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan26Database System Concepts

KeysKeys

A super key of an entity set is a set of one or more attributes that uniquely identify each entity in the entity set.

A candidate key of an entity set is a minimal super key customer-id is candidate key of customer account-number is candidate key of account

Although several candidate keys may exist, one of the candidate keys is selected to be the primary key. All others are referred to as secondary keys

Later we will also discuss foreign keys and search keys.

Page 27: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan27Database System Concepts

Other Examples of KeysOther Examples of Keys

Student = (SS#, Name, Date-of-Birth, Status)SS# - Super key and candidate key

SS#, Name, DOB - Super key, but not candidate key

SS#, Status - Super key, but not candidate key

Name - Neither

Payment = (Payment#, Loan#, Date-Made, ID#)ID# - Super key, and candidate key

Payment#, Loan# - Super key, and candidate key

ID#, Date-Made - Super key, but not candidate key

Date-Made - Neither

ID# is frequently referred to ask a pseudo-key.

Page 28: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan28Database System Concepts

Keys in an ER DiagramKeys in an ER Diagram

A primary key of an entity set is specified in an ER diagram by underlining the key attributes.

Page 29: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan29Database System Concepts

Keys for Relationship SetsKeys for Relationship Sets

The combination of primary keys of the participating entity sets forms a super key of a relationship set. (customer-id, account-number) is the super key of depositor Question: why primary and super in the above?

The mapping cardinality of a relationship set will determine, in part, what the candidate keys of the relationship set are.

Page 30: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan30Database System Concepts

Design OptionsDesign Options

Frequently there are many ways to model a given situation.

Use of an entity vs. an attribute: Is Telephone-Number an attribute or an entity? Choice mainly depends on the structure of the enterprise being modeled, and

on the semantics/meaning associated with the attribute in question.

Use of an entity vs. a relationship: Is Loan an entity or a relationship? Possible guideline - use a relationship to describe an action between entities

Binary versus n-ary relationships: e.g., Family, as opposed to mother, father, brother, sister

Placement of relationship attributes

Page 31: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

Lets Try an Example...Lets Try an Example...

7.1 Construct an ER diagram for a car insurance company whose customers own one or more cars each. Each car has associated with it zero to any number of recorded accidents.

Page 32: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan32Database System Concepts

Weak Entity SetsWeak Entity Sets

An entity set that has a primary key is referred to as a strong entity set.

An entity set that does not have a primary key is referred to as a weak entity set.

A weak entity set is typically associated with an identifying entity set (which is usually strong) via a total, one-to-many relationship. e.g., course section (sec_id,semester, year) caraccident (date, cost)

Page 33: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan33Database System Concepts

Weak Entity Sets, Cont.Weak Entity Sets, Cont.

The discriminator (or partial key) of a weak entity set is the set of attributes that distinguishes among all entities of the weak entity set associated with one identifying entity.

The primary key of a weak entity set has two parts: The primary key of the associated identifying entity set The weak entity set’s discriminator

Page 34: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan34Database System Concepts

Weak Entity Sets (Cont.)Weak Entity Sets (Cont.)

A weak entity set is represented by double rectangles.

The discriminator is underlined with a dashed line.

Primary key for payment is (loan-number, payment-number)

Page 35: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan35Database System Concepts

Weak Entity Sets (Cont.)Weak Entity Sets (Cont.)

The primary key of the strong entity set is not explicitly specified in the weak entity set, since it is implicit in the identifying relationship.

If loan-number were explicitly specified: Payment would be a strong entity set The relationship between payment and loan would not be as clear

Page 36: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan36Database System Concepts

More Weak Entity Set ExamplesMore Weak Entity Set Examples

In a university, a course is a strong entity and a course-offering can be modeled as a weak entity.

Course = (course-number, name, description)

Course-offering = (semester, section-number, instructor)

The discriminator of course-offering would be semester (including year) and section-number (if there is more than one section).

If course-offering were modeled as a strong entity then it would have course-number as an attribute. The relationship with course would be implicit in the course-number attribute.

Page 37: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan37Database System Concepts

Specialization andSpecialization andGeneralization ExampleGeneralization Example

Page 38: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan38Database System Concepts

SpecializationSpecialization

Top-down design process; we designate sub-groupings within an entity set that are distinctive from other entities in the set.

The lower-level entity sets may: Have attributes that do not apply to the higher-level entity set. Participate in relationships that do not apply to the higher-level entity set.

A lower-level entity set is said to inherit all the attributes and relationships from the higher-level entity set to which it is linked.

Depicted by a triangle component labeled ISA, e.g. customer “is a” person.

Page 39: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan39Database System Concepts

GeneralizationGeneralization

Bottom-up design process: combine a number of entity sets that share the same features into a higher-level entity set.

The terms specialization and generalization are used interchangeably, for the obvious reasons.

The ISA relationship also referred to as a superclass-subclass relationship.

Page 40: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan40Database System Concepts

SpecializationSpecializationand Generalization, Cont.and Generalization, Cont.

A specialization/generalization relationship can be: disjoint overlapping total Partial

The above could be noted in any number of ways on the diagram, e.g., by writing “disjoint” next to the relationship.

Multiple specializations of an entity set are possible: permanent-employee vs. temporary-employee In addition to officer vs. secretary vs. teller

Each particular employee would be a member of: one of permanent-employee or temporary-employee, and one of officer, secretary, or teller

Page 41: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan41Database System Concepts

E-R DiagramE-R Diagramfor a Banking Enterprisefor a Banking Enterprise

Page 42: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan42Database System Concepts

Alternative E-R NotationsAlternative E-R Notations

Page 43: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan43Database System Concepts

UMLUML

UML: Unified Modeling Language

UML has many components to graphically model different aspects of an entire software system

UML Class Diagrams correspond to E-R Diagram, but several differences. Focus is on software, e.g., UML includes methods Keys are not included

Page 44: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan44Database System Concepts

Reduction ofReduction ofan E-R Schema to Tablesan E-R Schema to Tables

An E-R diagram can be converted to a collection of tables in a relational database. Each entity set is converted to its’ own table. Each relationship can be (but does not have to be) converted to its’ own

table.

Each table has a number of columns, which generally corresponds to the attributes in the corresponding entity or relationship set.

Page 45: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan45Database System Concepts

Representing Entity Sets as TablesRepresenting Entity Sets as Tables

A strong entity set reduces to a table with the same attributes.

Page 46: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan46Database System Concepts

Composite andComposite andMulti-valued AttributesMulti-valued Attributes

Composite attributes are broken up: Example: attribute name with components first-name and last-name becomes two

attributes in the corresponding table: name.first-name and name.last-name.

A multi-valued attribute M of entity E is represented by a new table with the following attributes: The primary key of E An attribute corresponding to multi-valued attribute M

Example:Entity Set:

employee with attributes id#, name, phone#, dependentsTables:

employee (id#, name, phone#)dependent (id#, dname)

Page 47: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan47Database System Concepts

Representing Weak Entity SetsRepresenting Weak Entity Sets

A weak entity set becomes a table that includes a column for the primary key of the identifying strong entity set.

Page 48: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan48Database System Concepts

Representing RelationshipRepresenting RelationshipSets as TablesSets as Tables

A many-to-many relationship set is represented as a table with columns for the primary keys of the two participating entity sets, and any descriptive attributes of the relationship set.

Example: the borrower relationship set:

Page 49: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan49Database System Concepts

Representing RelationshipRepresenting RelationshipSets as Tables, cont.Sets as Tables, cont.

A many-to-one relationship set can be represented just like a many-to-many relationship.

Technically this is not necessary, and in some cases it does not result in a good design.

Example:Entity Sets:

account with attributes account-number, balancebranch with attributes branch-name, branch-city, assets

Relationship Set (total, many-to-one from account to branch):account-branch with attributes account-number, branch-name

Page 50: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan50Database System Concepts

Representing RelationshipRepresenting RelationshipSets as Tables, cont.Sets as Tables, cont.

The preceding could be converted to tables directly, or as follows:account (account-number, balance, branch-name)branch (branch-name, branch-city, assets)

Since the above relationship is total, a distinct table for the relationship set is probably not necessary. Eliminating an unnecessary table is frequently considered…cool

On the other hand, suppose: The relationship is partial, and most accounts don’t have branches The account table is sorted by balance Queries that look up the branch-name for a given account # are common.

Page 51: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan51Database System Concepts

Representing RelationshipRepresenting RelationshipSets as Tables, cont.Sets as Tables, cont.

For one-to-one relationship sets, the extra attribute can be added to either of the tables corresponding to the two entity sets.

Note that either of the above could introduce null values if the relationship is not total.

Page 52: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan52Database System Concepts

Representing SpecializationRepresenting Specializationas Tablesas Tables

Note: This discussion assumes a 2-level inheritance hierarchy. Exercise: Generalize it to an arbitrarily deep hierarchy.

Method 1: Form a table for the higher level entity set. Form a table for each lower level entity set, including the primary key of the higher

level entity set and local attributes.table

attributesperson name, street, citycustomer name, credit-ratingemployee name, salary

One Drawback: getting information about specific entities requires accessing two tables

Page 53: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan53Database System Concepts

Representing SpecializationRepresenting Specializationas Tables cont.as Tables cont.

Method 2: Form a table for each entity set with all local and inherited attributes

table table attributespersonname, street, citycustomername, street, city, credit-ratingemployee name, street, city, salary

This method can be redundant: Some duplication for persons who are both customers and employees. If specialization is total, the table for the generalized entity is redundant.

• May still be needed for foreign key constraints.• For convenience, the generalized entity can an be defined as a “view” relation containing

union of specialization tables.

Page 54: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan54Database System Concepts

Representing SpecializationRepresenting Specializationas Tablesas Tables

Method 3: (not presented in the book) Form one table for the higher level entity set This table has one column for every attribute in every subclass.

table attributesperson name, street, city, credit-rating, salary

Optionally, include a type attribute that indicates which subclass the stored entity belongs to.

One Drawback: Contains multiple nullable attributes.

Sometimes referred to as a “junk drawer”

Page 55: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan55Database System Concepts

Constraints on GeneralizationConstraints on Generalization

Condition-defined: student_type=graduate User-defined: manual placement

Disjointness constraint Overlapping generalizations

Completeness constraint (does each high level entity belong to some low level one?):

total generalization/specializationPartial generalization/specialization

Page 56: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

End of Chapter 7End of Chapter 7

Page 57: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan57Database System Concepts

E-RE-R Diagram with a Ternary Relationship Diagram with a Ternary Relationship

Page 58: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan58Database System Concepts

Cardinality ConstraintsCardinality Constraintson Ternary Relationship Setson Ternary Relationship Sets

We allow at most one arrow out of a ternary (or greater degree) relationship to indicate a cardinality constraint An arrow from works-on to job indicates each employee works on at

most one job at any branch. If there is more than one arrow, there are two ways of defining the

meaning. E.g a ternary relationship R between A, B and C with arrows to B and C

could mean 1. each A entity is associated with a unique entity from B and C or 2. each pair of entities from (A, B) is associated with a unique C entity,

and each pair (A, C) is associated with a unique B Each alternative has been used in different formalisms To avoid confusion we outlaw more than one arrow

Page 59: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan59Database System Concepts

Binary Vs. Non-Binary RelationshipsBinary Vs. Non-Binary Relationships

Some relationships that appear to be non-binary may be better represented using binary relationships E.g. A ternary relationship parents, relating a child to his/her father and

mother, is best replaced by two binary relationships, father and mother• Using two binary relationships allows partial information (e.g. only

mother being know) But there are some relationships that are naturally non-binary

• E.g. works-on

Page 60: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan60Database System Concepts

Converting Non-Binary Relationships to Binary Converting Non-Binary Relationships to Binary Form (Hidden Variable Encoding)Form (Hidden Variable Encoding)

In general, any non-binary relationship can be represented using binary relationships by creating an artificial entity set. Replace R between entity sets A, B and C by an entity set E, and three

relationship sets:

1. RA, relating E and A 2.RB, relating E and B

3. RC, relating E and C Create a special identifying attribute for E Add any attributes of R to E For each relationship (ai , bi , ci) in R, create

1. a new entity ei in the entity set E 2. add (ei , ai ) to RA

3. add (ei , bi ) to RB 4. add (ei , ci ) to RC

Page 61: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan61Database System Concepts

Converting Non-Binary Converting Non-Binary Relationships, Cont.Relationships, Cont.

Also need to translate constraints Translating all constraints may not be possible There may be instances in the translated schema that

cannot correspond to any instance of R• Exercise: add constraints to the relationships RA, RB and RC to

ensure that a newly created entity corresponds to exactly one entity in each of entity sets A, B and C

We can avoid creating an identifying attribute by making E a weak entity set (described shortly) identified by the three relationship sets

Page 62: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan62Database System Concepts

E-R Diagram for Exercise 7.20E-R Diagram for Exercise 7.20

a) List entity sets and primary keysb) model addition of Blue-Ray

discs and downloadable video. Same item may be available in different formats with different prices.

c) Use generalization to model the case where a shopping basket may contain any combination of books, blue-ray discs and downloadable video

Page 63: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan63Database System Concepts

E-R Diagram for Exercise 7.6E-R Diagram for Exercise 7.6

a) Show a simple instance of E, A, B, C, Ra, Rb, Rc that cannot correspond to any instance of A, B, C, and R.

b) Modify the E-R diagram (b) to introduce constraints that will guarantee that any instance of E, A, B, C, Ra, Rb, Rc that satisfies the constraints will correspond to an instance of A, B, C, and R

c) Modify the translation above to handle total participation constraints on the ternary relationship

d) Show how to treat E as a weak entity (without a primary key)

Page 64: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan64Database System Concepts

E-R Diagram for Exercise 2.18E-R Diagram for Exercise 2.18

The figure shows a lattice structure of generalization and specialization.For entity sets A, B, C, explain how attributes are inherited from higher-levelentity sets X and Y. Discuss how to handle a case where an attribute X has the same name as some attribute of Y.

Page 65: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan65Database System Concepts

E-R Diagram for Exercise 7.24E-R Diagram for Exercise 7.24

Discuss the merits of representations (b) and (c) to represent (a)

Page 66: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan66Database System Concepts

Existence DependenciesExistence Dependencies

If the existence of entity x depends on the existence of entity y, then x is said to be existence dependent on y. y is a dominant entity (in example below, loan) x is a subordinate entity (in example below, payment)

loan-payment paymentloan

If a loan entity is deleted, then all its associated payment entities must be deleted also.

Page 67: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan67Database System Concepts

AggregationAggregation

Consider the ternary relationship works-on, which we saw earlier

Suppose we want to record managers for tasks performed by an employee at a branch

Page 68: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan68Database System Concepts

Aggregation (Cont.)Aggregation (Cont.)

Relationship sets works-on and manages represent overlapping information Every manages relationship corresponds to a works-on relationship However, some works-on relationships may not correspond to any

manages relationships • So we can’t discard the works-on relationship

Eliminate this redundancy via aggregation Treat relationship as an abstract entity Allows relationships between relationships Abstraction of relationship into new entity

Page 69: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan69Database System Concepts

Aggregation (Cont.)Aggregation (Cont.)

Without introducing redundancy, the following diagram represents: An employee works on a particular job at a particular branch An employee, branch, job combination may (or may not) have an

associated manager

Page 70: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan70Database System Concepts

E-R Diagram With AggregationE-R Diagram With Aggregation

Page 71: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan71Database System Concepts

Relations Corresponding to AggregationRelations Corresponding to Aggregation

To represent aggregation, create a table containing The primary key of the aggregated relationship, The primary key of the associated entity set Any descriptive attributes

Solution II: create a single table and use null values for manager if there is none

Page 72: Silberschatz, Korth and Sudarshan1Database System Concepts Entity-Relationship Modelling Entities Attributes Relationships Mapping Cardinality Keys Reduction.

©Silberschatz, Korth and Sudarshan72Database System Concepts

Relations Corresponding to Aggregation Relations Corresponding to Aggregation (Cont.)(Cont.)

Example: to represent aggregation manages between relationship works-on and entity set manager, create a table: manages(employee-id, branch-name, job, manager-name) Note: manager-name may be null. If this is acceptable, then table works-on is not required.