DB02
DB02
1 2
Relational
Ideas E/R Relations
DBMS
3 4
5
GPA address credits 6
1
Entities and entity sets Relationships and Relationship Sets
Relationships are association among entities
Relationship set is a set of relationships of the same type
year If E1, . . ., En are entity sets, a relationship R on these sets is
title name address defined as: R ⊆ E1 × . . . × En
• In general, relationships are n-ary, where n ≥ 2
Movies Stars • Many database relationships are binary
9 10
2
Multiplicity of Binary Relationships Multiplicity of Binary Relationships
The relationship Chair between the entity sets Department and
Professor is one-one, indicating which professor is the chair of
which department. Note that this represents also the situation where
a department is assigned no chair.
name address name address
lastName firstName DID name Presidents Runs Studios
ID Professors Chair Departments
address
researchArea address
13 14
length filmType
15 16
3
Moving an attribute to an entity set Moving an attribute to an entity set
We could add an attribute(s) to a relationship
Alternatively, we could do the following: lastName firstName courseNumber name
invent a new entity set, whose entities have the attributes
ascribed to the relationship ID Students EnrolledIn Courses
“connect/include” this entity set in the relationship
omit the attribute from the relationship itself GPA address credits
letterGrade
19 20
amount 21 22
Sequel-of Movies
courseNumber Name
4
A more complex example Converting n-ary relationship to binary
Suppose, each star is associated with exactly one studio Any n-ary relationship R can be converted into a
Supp. studio s1 of star a1 may further contract with another studio collection of M-1 binary relationships without loosing
s2 to allow a1 to play in movie m2 made by studio s2 the information represented by R
name address To do this:
Studios Introduce a new entity set E, called connecting entity set,
Studio
whose entities might be thought of as tuples in R (the original
Producing
studio of star n-ary relationship)
Introduce M-1 relationships from E to each one of the entity sets
title year name address involved in R
Movies Contracts Stars If an entity set E plays more than one role, then E is the target
of one relationship for each role
length filmType 25 26
Studios Studios
Producing Studio Studio Producing Studio
of star
studio Producing of star studio of star
studio Contracts
Contracts
Movie-of Movie-of
Star-of Star-of
title year title year
Movie Contracts name address Movies name address
Stars Stars 28
length filmType 27
length filmType
major Murder-
option Cartoons Mystery
GradStudents UgradStudents
29 30
5
Inheritance in E/R Constraints
There is a subtle difference between the concept of inheritance in There are some important aspects of the real world that
an OO approach and in E/R
cannot be represented using the ODL or E/R model
In OO, an object must be a member of exactly one class
introduced so far
In E/R
We consider an entity as having “components” belonging to several The additional information about these aspects often takes
entity sets that are “part of” a single isa-hierarchy the form of constraints on the data
The “components” are connected into a single entity by the isa
relationships Sometimes modeling this additional information goes
The entity has whatever attributes any of its components has, and beyond the structural and type constraints imposed by
participates in whatever relationships its components participate in classes, entity sets, attributes, and relationships
In an E/R diagram, we represent an entity set (e.g.,
CartoonMurderMystery) only if it has attributes and/or relationships
of its own
31 32
Example Example
What should we consider as a key for Movie ? What should be the key for Star = {name, address}?
title? name?
there could be different movies with the same name We may think that the name cannot serve to distinguish two
{title, year}? people, but for stars, the names distinguish them, since
traditionally they choose “stage names” as names
there still could be two movies with the same title made in the
same year, but that’s very unlikely as we understand!
title year
name address
Movies {title, year} is a key for Movie
Stars {name} is the key for Star
length filmType
35 36
6
Example Selecting Primary Key
What should be considered as the key for Studio? B
A C Suppose candidate keys for E are :
name? 1. {A, B}
H E D 2. {D, N}
It is “reasonable” to assume that there are no studios
3. {G, M, N}
with the same names N
G M
name address
37 38
39 40
7
Referential Integrity Constraints Domain constraints
Domain constraints restrict the values of an attribute to
title year name address name address be drawn from a set
Movies Owns Studios Runs Presidents E/R does not support imposing domain constraints
ODL allows using types to limit/control possible values of the
length filmType attributes
ODL does not support restricting the “range” of values allowed
The studio for a movie must always be present in Studio for an attribute
If someone is a president, then the studio that he/she runs must
exist in Studio
However, the model allows studios without presidents (temporarily)
43 44
47 48
8
Sources of Weak Entity Sets Sources of Weak Entity Sets
Entity sets fall into a hierarchy Connecting entity sets (CES):
The entities in set Crew are sub-units of entities in set Studio
• The “numbers” used for crew entities are not unique until we CES’s often have no attributes of their own
take into account the name of Studio with which she/he is Their key is formed from the key attributes of the entity
associated (subordinate)
It is possible to have a chain of “weak” entity sets/relationships sets which they connect
A connecting entity set is always weak
number number name address
49 50
Example
name address Design Principles
Studios
Design should
Producing Studio Reflect the “reality” we are trying to model – faithful/realistic
studio of star Avoid redundancy -- minimal
Contracts
• Redundant information takes space
• Could cause inconsistency
Movie-of salary Be as simple as possible
Star-of
title year Be careful when choosing between using attributes and
Movies
using classes or entity sets. Remember that:
name address
An attribute is simpler to implement than a class/entity set or a
Stars relationship
length filmType If something has more information associated with it than just its
51 name, it should probably be modeled as an entity set or a class52
A quick test!
Which of the following statements is NOT correct?
A. A data model is a collection of concepts for describing data and
their relationships.
B. A data model is a language for describing the data semantics
and the constraints.
C. A data model is a language for expressing queries and
transactions over a database.
D. A data model is an abstract representation of the structure of
the data.
53