database system - ch04

61
High-level Database Models High-level Database Models Prof. Yin-Fu Huang Prof. Yin-Fu Huang CSIE, NYUST CSIE, NYUST Chapter 4 Chapter 4

Upload: -

Post on 12-Jan-2017

119 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Database System - Ch04

High-level Database ModelsHigh-level Database Models

Prof. Yin-Fu HuangProf. Yin-Fu HuangCSIE, NYUST CSIE, NYUST

Chapter 4Chapter 4

Page 2: Database System - Ch04

Database Systems Yin-Fu Huang

4.14.1 The Entity-Relationship ModelThe Entity-Relationship Model

Design phrase1) What information will be stored2) How information elements will be related to one another3) What constraints may be assumed (See Fig. 4.1)

High-level models:1) E/R (Entity/Relationship) diagram2) UML (Unified Modeling Language)3) ODL (Object Description Language)

Starting with a high-level model and then converting the design to the relational model

Page 3: Database System - Ch04

Database Systems Yin-Fu Huang

4.14.1 The Entity-Relationship ModelThe Entity-Relationship Model

Page 4: Database System - Ch04

Database Systems Yin-Fu Huang

Three principal element types:1) Entity sets2) Attributes3) Relationships

4.1.1 Entity SetsEntity set ↔ classHowever, the E/R model is a static concept, involving the structure of data and not the operations on data.

4.1.2 Attributeswe assume that attributes are of primitive types, such as strings, integers, or reals.

4.14.1 The Entity-Relationship ModelThe Entity-Relationship Model

Page 5: Database System - Ch04

Database Systems Yin-Fu Huang

4.1.3 Relationships Relationships are connections among two or more entity sets.

4.1.4 Entity-Relationship Diagrams Entity set ↔ rectangle Attribute ↔ oval Relationship ↔ diamond

(See Fig. 4.2)

4.14.1 The Entity-Relationship ModelThe Entity-Relationship Model

Page 6: Database System - Ch04

Database Systems Yin-Fu Huang

4.14.1 The Entity-Relationship ModelThe Entity-Relationship Model

Page 7: Database System - Ch04

Database Systems Yin-Fu Huang

4.1.5 Instances of an E/R Diagram A relationship R that connects n entity sets E1, E2, …, En may be

imagined to have an “instance” that consists of a finite set of tuples (e1, e2, …, en), where each ei is chosen from the entities that are in the current instance of entity set Ei.

4.1.6 Multiplicity of Binary E/R Relationships Many-one One-one Many-many The arrows means “at most one”

(See Fig. 4.3)

4.14.1 The Entity-Relationship ModelThe Entity-Relationship Model

Page 8: Database System - Ch04

Database Systems Yin-Fu Huang

4.14.1 The Entity-Relationship ModelThe Entity-Relationship Model

Page 9: Database System - Ch04

Database Systems Yin-Fu Huang

4.14.1 The Entity-Relationship ModelThe Entity-Relationship Model

4.1.7 Multiway Relationships Example

(See Fig. 4.4)

4.1.8 Roles in Relationships It is possible that one entity set appears two or more times in a

single relationship. Each line to the entity set represents a different role that the

entity set plays in the relationships. Example

(See Fig. 4.5 and Fig. 4.6)

Page 10: Database System - Ch04

Database Systems Yin-Fu Huang

4.14.1 The Entity-Relationship ModelThe Entity-Relationship Model

Page 11: Database System - Ch04

Database Systems Yin-Fu Huang

4.14.1 The Entity-Relationship ModelThe Entity-Relationship Model

Page 12: Database System - Ch04

Database Systems Yin-Fu Huang

4.14.1 The Entity-Relationship ModelThe Entity-Relationship Model

Page 13: Database System - Ch04

Database Systems Yin-Fu Huang

4.14.1 The Entity-Relationship ModelThe Entity-Relationship Model

4.1.9 Attributes on Relationships Example

(See Fig. 4.7) We can instead invent a new entity set, whose entities have the

attributes ascribed to the relationship.(See Fig. 4.8)

4.1.10 Converting Multiway Relationships to Binary Any relationship connecting more than two entity sets can be

converted to a collection of binary, many-one relationships. A connecting entity set

(See Fig. 4.9)

Page 14: Database System - Ch04

Database Systems Yin-Fu Huang

4.14.1 The Entity-Relationship ModelThe Entity-Relationship Model

Page 15: Database System - Ch04

Database Systems Yin-Fu Huang

4.14.1 The Entity-Relationship ModelThe Entity-Relationship Model

Page 16: Database System - Ch04

Database Systems Yin-Fu Huang

4.14.1 The Entity-Relationship ModelThe Entity-Relationship Model

Page 17: Database System - Ch04

Database Systems Yin-Fu Huang

4.14.1 The Entity-Relationship ModelThe Entity-Relationship Model

4.1.11 Subclasses in the E/R Model “isa” relationships

“an A is a B” expresses an “isa” relationship from entity set A to entity set B.

(See Fig. 4.10) Example

e.g., Roger RabbitThe three components are connected into one entity by theisa relationships.

Page 18: Database System - Ch04

Database Systems Yin-Fu Huang

4.14.1 The Entity-Relationship ModelThe Entity-Relationship Model

Page 19: Database System - Ch04

Database Systems Yin-Fu Huang

4.2.1 Faithfulness First and foremost, the design should be faithful to the

specifications of the application.

4.2.2 Avoiding Redundancy We should be careful to say everything once only.

e.g., having an attribute studioName of entity set Movies

Reasons:1) Extra space is required to represent the data.2) An update-anomaly potential

4.24.2 Design PrinciplesDesign Principles

Page 20: Database System - Ch04

Database Systems Yin-Fu Huang

4.2.3 Simplicity Counts Avoiding introducing more elements into your design than is

absolutely necessary.(See Fig. 4.11)

4.2.4 Choosing the Right Relationships Adding to our design every possible relationship is not often a

good idea.1) Redundancy2) Update anomaly3) Delete anomaly

Example (See Fig. 4.7 and Fig. 4.12)

4.24.2 Design PrinciplesDesign Principles

Page 21: Database System - Ch04

Database Systems Yin-Fu Huang

4.24.2 Design PrinciplesDesign Principles

Page 22: Database System - Ch04

Database Systems Yin-Fu Huang

4.24.2 Design PrinciplesDesign Principles

Page 23: Database System - Ch04

Database Systems Yin-Fu Huang

4.24.2 Design PrinciplesDesign Principles

Page 24: Database System - Ch04

Database Systems Yin-Fu Huang

4.2.5 Picking the Right Kind of Element Using attributes or using entity sets? Example

Make the name and address of the studio be attributes ofmovies and eliminate the Studio entity set.

Problems:1) Redundancy: the address of the studio2) Update anomaly3) Delete anomaly

If we did not record addresses of studios, then there is no harm in making the studio name an attribute of movies.

4.24.2 Design PrinciplesDesign Principles

Page 25: Database System - Ch04

Database Systems Yin-Fu Huang

The conditions under which we prefer to use an attribute instead of an entity set:

1) All relationships in which E is involved must have arrows entering E.2) The only key for E is all its attributes.3) No relationship involves E more than once.

Replacing E:1) If there is a many-one relationship R from some

entity set F to E, then remove R and make the attributes of

E be attributes of F.

4.24.2 Design PrinciplesDesign Principles

Page 26: Database System - Ch04

Database Systems Yin-Fu Huang

2) If there is a multiway relationship R with an arrow to E,

make the attributes of E be attributes of R and delete the arc from R to E.

Tradeoff between using a multiway relationship and using a connecting entity set

ExampleMore studios(See Fig. 4.13)

4.24.2 Design PrinciplesDesign Principles

Page 27: Database System - Ch04

Database Systems Yin-Fu Huang

4.24.2 Design PrinciplesDesign Principles

Page 28: Database System - Ch04

Database Systems Yin-Fu Huang

4.3.1 Keys in the E/R Model Every entity set must have a key. There can be more than one possible key for an entity set. When an entity set is involved in an isa-hierarchy, we require

that the root entity set have all the attributes needed for a key, and that the key for each entity is found from its component in the root entity set.

4.3.2 Representing Keys in the E/R Model Example

(See Fig. 4.17)

4.34.3 Constraints in the E/R ModelConstraints in the E/R Model

Page 29: Database System - Ch04

Database Systems Yin-Fu Huang

4.34.3 Constraints in the E/R ModelConstraints in the E/R Model

Page 30: Database System - Ch04

Database Systems Yin-Fu Huang

4.3.3 Referential Integrity A value appearing in one context must also appear in another.

e.g., relationship Owns from Movies to Studios Example

(See Fig. 4.18)

4.3.4 Degree Constraints A bounding number to the edges that connect a relationship to

an entity set Example

(See Fig. 4.19)

4.34.3 Constraints in the E/R ModelConstraints in the E/R Model

Page 31: Database System - Ch04

Database Systems Yin-Fu Huang

4.34.3 Constraints in the E/R ModelConstraints in the E/R Model

Page 32: Database System - Ch04

Database Systems Yin-Fu Huang

4.34.3 Constraints in the E/R ModelConstraints in the E/R Model

Page 33: Database System - Ch04

Database Systems Yin-Fu Huang

It is possible for an entity set’s key to be composed of attributes, some or all of which belong to another entity set..

4.4.1 Causes of Weak Entity Sets Two principal reasons:

1) A hierarchy based on classifications unrelated to the “isa hierarchy”2) The connecting entity sets

Examplekey: (Studios)name+(Crews)number

(See Fig. 4.20)

4.44.4 Weak Entity SetsWeak Entity Sets

Page 34: Database System - Ch04

Database Systems Yin-Fu Huang

4.44.4 Weak Entity SetsWeak Entity Sets

Page 35: Database System - Ch04

Database Systems Yin-Fu Huang

key: (Genus)name+(Species)name(See Fig. 4.21)

key: (Stars)name+(Studios)name+(Movies)(title+year)(See Fig. 4.22)

4.4.2 Requirements for Weak Entity Sets If E is a weak entity set then its key consists of:

1) zero or more of its own attributes, and2) key attributes from entity sets that are reached by certain many-one relationships from E to other

entity sets, called supporting relationships for E.

4.44.4 Weak Entity SetsWeak Entity Sets

Page 36: Database System - Ch04

Database Systems Yin-Fu Huang

4.44.4 Weak Entity SetsWeak Entity Sets

Page 37: Database System - Ch04

Database Systems Yin-Fu Huang

4.44.4 Weak Entity SetsWeak Entity Sets

Page 38: Database System - Ch04

Database Systems Yin-Fu Huang

In order for R to be a supporting relationship for E, the following conditions must be obeyed:

1) R must be a binary, many-one relationship from E to F.

2) R must have referential integrity from E to F.3) The attributes that F supplies for the key of E must

be key attributes of F.4) If F is itself weak, then some or all of the key

attributes of F supplied to E will be key attributes of one or more entity sets G to which F is connected by a supporting relationship.

4.44.4 Weak Entity SetsWeak Entity Sets

Page 39: Database System - Ch04

Database Systems Yin-Fu Huang

5) If there are several different supporting relationships from E to F, then each relationship is used to supply

a copy of the key attributes of F to help form the key

of E.

4.4.3 Weak Entity Set Notation If an entity set is weak, it will be shown as a rectangle with a

double border. Its supporting many-one relationships will be shown as

diamonds with a double border. If an entity set supplies any attributes for its own key, then

those attributes will be underlined.

4.44.4 Weak Entity SetsWeak Entity Sets

Page 40: Database System - Ch04

Database Systems Yin-Fu Huang

It is possible for there to be many-one relationships from a weak entity set that are not supporting relationships, and therefore do not get a double diamond.

e.g., relationship: Studio-of(Each movie has a unique owning studio, determined

by the many-one relationship from Movies to Studios.)

4.44.4 Weak Entity SetsWeak Entity Sets

Page 41: Database System - Ch04

Database Systems Yin-Fu Huang

Converting an E/R design to a relational database schema:1) Turn each entity set into a relation with the same set

of attributes, and2) Replace a relationship by a relation whose attributes are the keys for the connected entity sets.

Several special situations:1) Weak entity sets cannot be translated

straightforwardly to relations.2) “Isa” relationships and subclasses require careful treatment.

4.54.5 From E/R Diagrams to Relational DesignsFrom E/R Diagrams to Relational Designs

Page 42: Database System - Ch04

Database Systems Yin-Fu Huang

3) Sometimes, we do well to combine two relations, especially the relation for an entity set E and the relation that comes from a many-one relationship

from E to some other entity set.

4.5.1 From Entity Sets to Relations Example

(See Fig. 4.23)

4.5.2 From E/R Relationships to Relations Relationships in the E/R model are also represented by

relations.

4.54.5 From E/R Diagrams to Relational DesignsFrom E/R Diagrams to Relational Designs

Page 43: Database System - Ch04

Database Systems Yin-Fu Huang

4.54.5 From E/R Diagrams to Relational DesignsFrom E/R Diagrams to Relational Designs

Page 44: Database System - Ch04

Database Systems Yin-Fu Huang

1) For each entity set involved in relationship R, we take

its key attribute or attributes as part of the schema of the relation for R.2) If the relationship has attributes, then these are also attributes of relation R.

Example(See Fig. 4.24 and Fig. 4.25)

Key of Contracts: starName+title+year+studioOfStar +producingStudio

4.54.5 From E/R Diagrams to Relational DesignsFrom E/R Diagrams to Relational Designs

Page 45: Database System - Ch04

Database Systems Yin-Fu Huang

4.54.5 From E/R Diagrams to Relational DesignsFrom E/R Diagrams to Relational Designs

Page 46: Database System - Ch04

Database Systems Yin-Fu Huang

4.54.5 From E/R Diagrams to Relational DesignsFrom E/R Diagrams to Relational Designs

Page 47: Database System - Ch04

Database Systems Yin-Fu Huang

4.5.3 Combining Relations One common situation occurs when there is an entity set E

with a many-one relationship R from E to F. The relations E and R can be combined into one relation with

a schema consisting of:1) All attributes of E2) The key attributes of F3) Any attributes belonging to relationship R

Example(See Fig. 4.26)

4.54.5 From E/R Diagrams to Relational DesignsFrom E/R Diagrams to Relational Designs

Page 48: Database System - Ch04

Database Systems Yin-Fu Huang

4.54.5 From E/R Diagrams to Relational DesignsFrom E/R Diagrams to Relational Designs

Page 49: Database System - Ch04

Database Systems Yin-Fu Huang

But not for a many-many relationship Example

(See Fig. 4.27)

4.5.4 Handling Weak Entity Sets Three things for weak entity sets:

1) The relation for the weak entity set W itself must include not only the attributes of W but also the key attributes of the supporting entity sets.

4.54.5 From E/R Diagrams to Relational DesignsFrom E/R Diagrams to Relational Designs

Page 50: Database System - Ch04

Database Systems Yin-Fu Huang

4.54.5 From E/R Diagrams to Relational DesignsFrom E/R Diagrams to Relational Designs

Page 51: Database System - Ch04

Database Systems Yin-Fu Huang

2) The relation for any relationship in which the weak entity set W appears must use as a key for W all of

its key attributes, including those of other entity sets

that contribute to W's key.3) A supporting relationship R need not be converted

to a relation at all.

Example(See Fig. 4.28)No relation for Unit-of

4.54.5 From E/R Diagrams to Relational DesignsFrom E/R Diagrams to Relational Designs

Page 52: Database System - Ch04

Database Systems Yin-Fu Huang

4.54.5 From E/R Diagrams to Relational DesignsFrom E/R Diagrams to Relational Designs

Page 53: Database System - Ch04

Database Systems Yin-Fu Huang

The principal conversion strategies are:1) Follow the E/R viewpoint. For each entity set E in

the hierarchy, create a relation that includes the key attributes from the root and any attributes belonging

to E.2) Treat entities as objects belonging to a single class.

For each possible subtree that includes the root, create

one relation, whose schema includes all the attributes of all

the entity sets in the subtree.

4.64.6 Converting Subclass Structures to RelationsConverting Subclass Structures to Relations

Page 54: Database System - Ch04

Database Systems Yin-Fu Huang

3) Use null values. Create one relation with all the attributes of all the entity sets in the hierarchy. Each entity is represented by one tuple, and that tuple has

a null value for whatever attributes the entity does not have.

4.6.1 E/R-Style Conversion We do not create a relation for “isa”. Example

(See Fig. 4.31)

4.64.6 Converting Subclass Structures to RelationsConverting Subclass Structures to Relations

Page 55: Database System - Ch04

Database Systems Yin-Fu Huang

4.64.6 Converting Subclass Structures to RelationsConverting Subclass Structures to Relations

Page 56: Database System - Ch04

Database Systems Yin-Fu Huang

Movies(title, year, length, genre)MurderMysteries(title, year, weapon)Cartoons(title, year)Voices(title, year, starName)

The movies that are both cartoons and murder mysterieshave tuples in all first three relations.

There may be silent cartoons in our database.

4.6.2 An Object-Oriented Approach Example

4.64.6 Converting Subclass Structures to RelationsConverting Subclass Structures to Relations

Page 57: Database System - Ch04

Database Systems Yin-Fu Huang

Movies(title, year, length, genre)MoviesC(title, year, length, genre)MoviesMM(title, year, length, genre, weapon)MoviesCMM(title, year, length, genre, weapon)Voices(title, year, starName)

4.6.3 Using Null Values to Combine Relations Example

Movies(title, year, length, genre, weapon)

4.64.6 Converting Subclass Structures to RelationsConverting Subclass Structures to Relations

Page 58: Database System - Ch04

Database Systems Yin-Fu Huang

4.6.4 Comparison of Approaches A list of the principal issues:

1) We would prefer to find all the attributes we needed to

answer a query in one relation.

Nulls approach √

∗ A query like “what films of 2008 were longer than 150 minutes?” Straight-E/R approach √ Object-Oriented approach ×

4.64.6 Converting Subclass Structures to RelationsConverting Subclass Structures to Relations

Page 59: Database System - Ch04

Database Systems Yin-Fu Huang

∗ A query like “what weapons were used in cartoons of over 150 minutes in length?” Straight-E/R approach × Object-Oriented approach √

2) We would like not to use too many relations.Nulls approach: only one relationStraight-E/R approach: only one relation per

entity set in the hierarchy

Object-Oriented approach: 2n relations if n children

4.64.6 Converting Subclass Structures to RelationsConverting Subclass Structures to Relations

Page 60: Database System - Ch04

Database Systems Yin-Fu Huang

3) We would like to minimize space and avoid repeating

information.Object-Oriented approach: only one tuple per

entityNulls approach: only one tuple per entity, but

these tuples are “long”.

Straight-E/R approach: several tuples for each entity, but only the key attributes are repeated.

4.64.6 Converting Subclass Structures to RelationsConverting Subclass Structures to Relations

Page 61: Database System - Ch04

Database Systems Yin-Fu Huang

The End.