5. DBI_Chapter4_2024_Nov
5. DBI_Chapter4_2024_Nov
chapTer 4
high-LeveL
DaTabaSe
MODeL
ObJecTiveS
Relational
Getting
High-Level Database Relational
User
Requirement Design Schema DBMS
Design
Employee
erD - reLaTiONShip
11
Relationship: Association among two or more entities. Relationships can have
their own attributes (descriptive attributes).
verb phrases
§ 1-1
§ 1- M/ M -1
§ M-M
Degree Constraints
§ Recursive relationship
§ Unary, Binary, Ternary relationship (quan hệ 1 ngôi, hai ngôi,...)
A referential integrity constraints (ràng buộc toàn vẹn tham chiếu)
§ A value appearing in one context must also appear in another
Type of Attributes
§ Key attribute (khoá) EmpID
Address
Strong Entity
A strong entity is an entity type whose existence doesn't
depend on any other entity.
1
Weak Entity Sets
Weak entity type doesn’t have a key attribute. Weak
entity type can’t be identified on its own. It depends
upon some other strong entity for its distinct identity
1
Requirements for Weak Entity Sets
R is a relationship from E to F
R is called supporting relationship if
§ R must be a binary, many-one (M - 1)
relationship from E to F
§ R must have referential integrity from E to F
§ The attributes that F supplies for the key of E
must be key attributes of F
Subclasses in E/R Model
Consider Cartoons and Murder Mysteries are the special
kinds of movies, with some special properties
Voices Movies
Entities Attribute
University UniversityID, Name
Faculty ...
Department
Lecturer
...
HINT
23
Identify relationships between entities:
• The relationship between School and Faculty is a second-order relationship,
type 1-M.
• The relationship between Faculty and Department is a second-order
relationship, type 1-M.
• …
• In particular, the relationship between Student and Subject is a second-
order relationship, type M-M (with separate attributes such as “TestTime”,
“Score”).
From ER Diagram to Relational Model
24
Overview:
§ 1 entity = 1 relation
§ attributes of entity ~ attributes of relation
§ key of entity ~ key of relation
§ Convert 1-1 relationship
§ Convert 1- M relationship
§ Put key attribute of one-side to n-side
§ Convert M-M relationship
§ Generate 1 relation, Primary key of this relation combined from two
relations. Attributes of new relation ~ attributes of relationship (if
have)
From ER Diagram to Relational Model
Convert 1-1 relationship
P-Key2
A relationship Another Set
E-Set 2
P-Key3
E-Set 3
SSN Name
Street City
Representing Multivalue Attribute
Major GPA
Stud_SID Children
1234 Johnson
1234 Mary
SID Name Major GPA
5678 Bart
1234 John CS 2.8
5678 Lisa
5678 Homer EE 3.6
5678 Maggie
Representing Class Hierarchy
Two general approaches depending on disjointness and
completeness
§ For non-disjoint and/or non-complete class hierarchy:
§ create a table for each super class entity set according to normal entity set
translation method.
§ Create a table for each subclass entity set with a column for each of the
attributes of that entity set plus one for each attributes of the primary key of
the super class entity set
§ This primary key from super class entity set is also used as the primary key
for this new table
Example SSN Name
Person
SID Status
Gender
ISA
Student
Major GPA
SSN Name Gender
1234 Homer Male
5678 Marge Female
ISA
SID Faculty
Student Disjoint and
Complete
mapping
Major GPA Dept
Dept
SID
Name
member
Primary Key of
Advisor Dept
SID Code
Code
1234 04
Primary key of
5678 08 Dept
From E/R Relationship to Relations
Studio Producing
of star studio
Studios
name Movies
address Movies(title,year,length,genre,studioName)
Studios(name,address)
Crews(number,crewChief,studioName)
If W is a weak entity set, construct for W a relation whose schema consists of:
• All attributes of W
• All own attributes of supporting relationships for W
• For each supporting relationship for W, say a many-one relationship from W to entity
set E, all the key attributes of E
Rename attributes, if necessary, to avoid name conflicts
Do not construct a relation for any supporting relationship for W
Converting Subclass Structures to Relations
How we convert this structure to relations?
Voices Movies
Murder
Cartoons
Mysteries
Converting Subclass Structures to Relations
The principal conversion strategies
Follow 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 belong to E
Treat entities as object-oriented
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
Use null values
Create only one relation with all attributes of all 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
E/R Style Conversion
to Stars length title year genre
Voices Movies
Murder
Cartoons
Mysteries
Movies(title,year,length,genre)
MurderMysteries(title,year,weapon)
Cartoons(title,year) remove
Voices(title,year,starName)
An Object-Oriented Approach
Voices Movies
Murder
Cartoons
Mysteries
Movies(title,year,length,genre)
MoviesC(title,year,length,genre)
MoviesMM(title,year,length,genre,weapon)
MoviesCMM(title,year,length,genre,weapon)
Using Null Values
to Stars length title year genre
Voices Movies
Murder
Cartoons
Mysteries
Movie(title,year,length,genre,weapon)
Unified Modeling Language –self
studying
Introduction
UML is designed to model software in an
object-oriented style, but has been adapted
as a database modeling language
UML offers much the same capabilities as the
E/R model, with the exception of multi-way
relationships, only binary relationships in UML.
UML vs. E/R Model
UML E/R Model
Movies
title PK
State
year PK Class’ name
length
genre
init()
Behavior
modify()
Associations
Consider an associations between Movies, Stars, and Studios in UML
Studios
0..1
name
PK Owns Movies
address 0..* title PK
year PK
Stars 0..* length
name 0..* Stars-in
genre
PK
address
Associations
Comparison with E/R Multiplicities
E/R UML
0..* 0..*
0..* 0..1
0..* 1..1
Self-Associations
An association can have both ends at the same
class; such an association is called a self-
association
Example
Movies
title PK
year PK
length
genre
Movies
title PK Studios Presidents
1..* name 0..1
year PK
0..1 PK 1..1
length
address
genre
Figure 4.41: An aggregation from Movies to Studios and
a composition from Presidents to Studios
UML-to-Relations Basics
Classes to Relations
For each class, create a relation
name is the name of the class
attributes are the attributes of the class
Associations to Relations
For each association, create a relation
name is the name of that association
attributes are the key attributes of the two connected classes
UML-to-Relations Basics
Studios
name 0..1
PK
Owns Movies
address
0..* title PK
Stars year PK
name 0..* length
PK 0..* Stars-in
genre
address
Movies(title,year,length,genre) Stars-In(movieTitle,movieYear,starName)
Stars(name,address) Owns(movieTitle,movieYear,studioName)
Studios(name,address)
From UML Subclasses to Relations
We can use any of the three strategies outlined for
E/R to convert a class and its subclasses to relations
E/R-style: each subclass’ relation stores only its own
attributes, plus key
OO-style: relations store attributes of subclass and all
super-classes
Nulls: One relation, with NULL’s as needed
From Aggregations and Composition
to Relation
No relation for the aggregation or composition
Add to the relation for the class at the non-diamond end the key
attribute(s) of the class at the diamond end
In the case of an aggregation, it is possible that these attributes can be null
From Aggregations and Composition to Relation
MovieExecs(cert#,name,address,netWorth) MovieExecs
Presidents(cert#,studioName) cert# PK
Movies(title,year,length,genre,studioName) name
Studios(name,address) address
networth
Movies
title PK Studios President
1..* 0..1
year PK name
0..1 PK 1..1
length
address
genre
The UML Analog of Weak Entity Sets
We use the composition, which goes from the weak class to the supporting
class, for a weak entity set
Example:
Studios(name,address)
Crews(number,crewChief,studioName)
Crews Studios
0..* 1..1
number PK PK name PK
crewChief address
EXERCISE 1
EXERCISE 2 - Convert ERD to RD
Steps to Convert ERD to Relational Schema
1. Identify Entities
- Convert each strong entity into a table.
- Attributes of the entity become columns in the table.
- Choose the primary key (PK) from the entity's key attributes.
2. Handle Weak Entities
- Create a table for each weak entity.
- Include attributes of the weak entity and add a foreign key (FK)
referencing the strong entity it depends on.
- The combination of the FK and the weak entity's discriminator (if any)
becomes the PK.
3. Map Relationships
• 1:1 Relationship: Add FK to one of the participating entity tables
(preferably on the side with total participation).
• 1:N Relationship: Add FK to the "many" side referencing the "one" side.
• M:N Relationship: Create a new table. Include FKs referencing the
primary keys of the participating entities and any attributes of the
relationship. Combine the FKs to form the PK.
4. Handle Multivalued Attributes
• Create a new table.
• Include the multivalued attribute and the PK of the entity it belongs to
as FKs.
• Combine the PK of the entity and the multivalued attribute to form the
PK of the new table.