ER Model Overview for Database Systems
ER Model Overview for Database Systems
Database Systems
L03: ER Model
CS2102: Database Systems -- Adi Yoga Sidi Prabawa 1 / 76
Tutorial
Tutorial Schedule
Off-Days
❱ TAs
More Consultation
Note
Table Basic
Rows
Key Challenge
Principles
CREATE TABLE <table_name> ( ALTER TABLE <table_name>
<attr_1> <type_1> [<column_constraint_1>], [ALTER / ADD / DROP]
[COLUMN / CONSTRAINT] <name>
<attr_2> <type_2> [<column_constraint_2>],
<changes>;
⁝
<attr_n> <type_n> [<column_constraint_n>],
[<table_constraint_1>], -- single line comment
[<table_constraint_2>], /* multi-line comment */
DROP TABLE
⁝
[<table_constraint_m>] -- no comma at the end DROP TABLE [IF EXISTS] <table_name>;
);
Principles
INSERT INTO <table_name> [(<attr>, ...)] VALUES (<values>, ...) [, (<values>, ...)];
UPDATE
DELETE
Primary Key PRIMARY KEY PRIMARY KEY (A1,A2,...) UNIQUE & NOT NULL
The tuple exists in R1
FOREIGN KEY (A1,A2,...,An) or the tuple (a1,a2,...,an)
Foreign Key REFERENCES R1(B)
REFERENCES R1(B1,B2,...,Bn) contains NULL value such that
ai ∈ Dom(Ai)
General CHECK (c) CHECK (c) Condition c does not evaluate to False
Principle of Acceptance
Principle of Rejection
Three-Valued Logic
Why Design?
House built, Boss. And the bricks laid evenly? It couldn't be more even, Boss
Basic Considerations
Concepts Analysis
Elements
Online Airline Reservation Systems
Concepts
Concepts Concepts
Elements
What Is It? Advantage
Concepts
The most common model for conceptual database design The diagram can be understood with
Developed by Peter Chen in 1976 less practice than that of reading code.
Visualized using ER Diagrams
Core Concepts
Entity Set
✱
While "booking" is a noun (e.g., "user makes a booking"), its root word may also be used as a verb (e.g., "user books a flight").
Careful judgment needs to be made whether it is an entity or a relationship.
CS2102: Database Systems -- Adi Yoga Sidi Prabawa 21 / 76
Entity Relationship
Preliminary
Elements
❱ Elements
Entities Attributes
Attributes
Kinds
Relationships
Relationship Users need to be able to make bookings
[...] We record the booking date. [...]
Concepts A relationship is an association among one or more entities.
(e.g., users make bookings)
Relationship Set
Additional Concepts
❱ Concepts Roles
Roles
Degrees
Relationship Role [...] from an origin to a destination
airport which may comprime multiple
A relationship role is a descriptor of an entity set's participation
connecting flights [...]
in a relationship
Most of the time, it is implicitly given by the name of the entity
sets
Explicit role label only in case of ambiguities
✱
We may also add roles on the line between Flights and travels to further describe the relationship but this is optional.
CS2102: Database Systems -- Adi Yoga Sidi Prabawa 24 / 76
Entity Relationship
Preliminary
Concepts
Elements
❱ Concepts Degrees
Roles
"In typical modeling, binary relationships are
Degree of Relationship Sets
Degrees the most common and relationships with n>3 are
The degree of a relationship sets is the number of entity sets very rare"
-- Peter Chen (2009)
involved in the relationship set
If there are n entities, we call it n-ary relationship
In principle, there is no limitation on the number of entity sets
involved
Kinds Basic
Cardinalities
Relationships
Participations
Dependency Relationships relates one or more entities. We only record True relationships. In other words, if the information is
Summary
available in the database, the relationship is true.
Not all users are in "Makes" (there is a user not related to booking).
Users not making bookings are not in Makes (e.g., with NULL value for "bid").
The last row in "Makes" is not allowed.
Kinds Kinds
Cardinalities
Cardinality Constraints
Participations
Dependency Describes an upper bound to the number of times an entity can participate in a relationship (either 1 or ∞)
Summary Upper limits of 1 are called key constraints
Participation Constraints
Describes an lower bound to the number of times an entity can participate in a relationship (either 0 or 1)
Lower limits of 0 are called partial participation constraints
Lower limits of 1 are called total participation constraints
Dependency Constraints
Describes a weak entity set that does NOT have its own key (its keys are called partial keys)
Its existence is also dependent on owning entity set
Entities that are not weak are called strong entity sets
Participations 1. Many-to-Many e.g., a flight can be performed by different aircrafts; an aircraft can perform different flights
Dependency 2. Many-to-One e.g., a user can make many bookings, but each booking is done by one user
Summary
3. One-to-One e.g., a user is associated with one set of credit card details, and vice versa
ER Diagram
One-to-One Example
Participations Each booking can include 0 or more flights✱ and each flight can be part of 0 or more bookings.
Dependency
Summary
ER Diagram Visualization
Explanation ☞
There can be a booking with
different flights (e.g., 83465)
There can be a flight with
different bookings (e.g., EY454)
There can be a booking without
flight (e.g., 89557)
There can be a flight without
booking (e.g., SQ7952)
✱
A booking with 0 flights might not be meaningful and we will improve on that.
CS2102: Database Systems -- Adi Yoga Sidi Prabawa 30 / 76
Relationship Constraints
Preliminary
Cardinalities
❱ Cardinalities
Many-to-Many Many-to-One
Many-to-One
One-to-One Example
Participations Each user can include 0 or more bookings and each booking can be made by at most 1 user✱.
Dependency
Summary
ER Diagram Visualization
Explanation ☞
There are no booking with
different users
There can be a user with
different bookings (e.g., 103)
There can be a booking without
user (e.g., 18545)
There can be a user without
booking (e.g., 102)
✱
Still not perfect yet because what is a booking that is made by no user? We will also improve on that.
CS2102: Database Systems -- Adi Yoga Sidi Prabawa 31 / 76
Relationship Constraints
Preliminary
Cardinalities
❱ Cardinalities
Many-to-Many One-to-One
Many-to-One
One-to-One
Example
Participations Each user can provide at most 1 credit card and each credit card is associated to at most 1 user✱.
Dependency
Summary
ER Diagram Visualization
Explanation
✱
May be too strict, because a family may share a single credit card.
CS2102: Database Systems -- Adi Yoga Sidi Prabawa 32 / 76
Relationship Constraints
Preliminary
Participations
Cardinalities
ER Diagram
Dependency Each user booking includes at least 1 flights and each flight can be art of 0 or more bookings.
Summary
ER Diagram Visualization
Explanation ☜
There can be a booking with
different flights (e.g., 83465)
There can be a flight with
different bookings (e.g., EY454)
There are no booking without a
flight
There can be a flight without
booking (e.g., SQ7952)
Dependency Each user can make 0 or more bookings and each booking can be made by exactly 1 user.
Summary
ER Diagram Visualization
Explanation ☜
There are no booking with
different users
There can be a user with
different bookings (e.g., 103)
There are no booking without
user
There can be a user without
booking (e.g., 102)
Example A weak entity set is an entity set that does NOT have its own key
Summary
Its key is called partial key
Its key cannot uniquely identify an entity
Its key can uniquely identify an entity with the help of the key from owning entity set
Properties Example
Existence of weak entity is dependent on the existence of its owner A book chapter is dependent on a
Requirements
× entity book and a book chapter cannot
Weak entity set must be connected to an owning entity set via uniquely identify a chapter
For an entity set to be a identifying relationship set without book name
weak entity set, it must
satisfy all of these:
1. Its keys are partial keys
2. Existence of its entity is
dependent on the ER Diagram
existence of owning
entity (i.e., existence
dependency)
Participations Example
❱ Dependency ISBN: 0-07-232206-3 ISBN: 0-07-246563-8
Weak Entity
Example
Summary
Note
"Chapter 3: The Relational Model" cannot uniquely identify the rest of the attributes (e.g., subchapters)?
Weak Entity + Identifying E is a weak entity set with identifying owner E' and
Relationship indentifying relationship set R
There are three main constraints that need to be satisfied on a general ER diagram.
1. Can the key attributes uniquely identify the rest of the attributes?
This applies to entity set
Additionally, key attributes should not uniquely identify additional attributes not part of the entity set unless
bound constraints requires it
Multi-Valued Attributes
ER Diagram Schema ER diagram does not capture the data
Relationship Set type. We assume that the data type is as
Name of entity set Name of table
Guidelines logical as possible.
Attribute of entity set Attribute/column of table name ≠ INT
Key attribute of entity set Primary key of table price ≠ INT
Derived attribute of entity set should not appear✱ (should be NUMERIC and not FLOAT or
FLOAT8 or DOUBLE too)
Other Constraints?
✱
Derived attributes are derived, it means that there is a computation that can be used to generate them from the row or from
other table. Typically used for attributes that frequently change (e.g., age).
CS2102: Database Systems -- Adi Yoga Sidi Prabawa 41 / 76
Relational Mapping
Preliminary
Entity Set
❱ Entity Set
Basic Mapping Key Attributes
Key Attributes
ER Diagram
Composite Attributes Strategy
Multi-Valued Attributes
Key attributes can be enforced via PRIMARY KEY
Relationship Set constraint
Guidelines ER diagram can only encode one set of key for
each entity set
Schema Table
);
The attribute age is not present in the translation (i.e.,
not in the table) as it can be computed from the
attribute dob instead.
Multi-Valued Attributes
Composite attributes are converted into a series
Relationship Set of single-valued attributes corresponding to the
Guidelines decomposed attributes
Tables should only hold atomic values
Schema Table
zip INT
The attribute address is not present in the
); translation as it can be reproduced from the
attribute street, block, and zip instead.
Multi-Valued Attributes
Convert multi-valued attributes into a sequence of
Relationship Set single-valued attributes
Guidelines Limitation: can only have a fixed number of
attributes
Schema Table
phone3 INT
Searching for a user with certain phone numbers
); is more complicated
Ensuring that phone1 is filled before phone2 and
phone3
Multi-Valued Attributes
Create additional table with foreign key constraint
Relationship Set Limitation: retrieval requires joining two tables
Guidelines which can be more expensive
Schema Table
✱
Unless the multiplicity of the attributes is known in advanced, you should use Strategy #2 instead.
CS2102: Database Systems -- Adi Yoga Sidi Prabawa 45 / 76
Relational Mapping
Preliminary
Relationship Set
Entity Set
Many-to-One n entity sets E1, E2, ..., En We assume each entity sets involved only have
One-to-One m attributes A1, A2, ..., Am one key denoted by keyE_i for entity set Ei
Key + Total Extension can be made for entity sets with
Weak Entity Set We assume unconstrained relationship multiple key attributes using table constraint
Guidelines
CREATE TABLE R (
keyE_1 TYPE REFERENCES E_1,
keyE_2 TYPE REFERENCES E_2,
: :
keyE_n TYPE REFERENCES E_n,
A_1 TYPE,
A_2 TYPE,
: :
A_m TYPE,
PRIMARY KEY (keyE_1, keyE_2, ..., keyE_n)
);
Many-to-One
One-to-One
Key + Total
Guidelines
Strategy
Users entity
One-to-One
bid uniquely identify
Key + Total
bdate
Weak Entity entity
Bookings Set
We cannot have both
Guidelines
(bid, uid1) and
(bid, uid2) in Makes Strategy #1: Separate Tables
Upper bound = 1
We may have both CREATE TABLE Users ( CREATE TABLE Makes (
(bid1, uid) and uid INT PRIMARY KEY, uid INT NOT NULL, -- not NULL to avoid redundant info
(bid2, uid) in Makes
Upper bound = ∞ uname VARCHAR(100), bid INT,
If a uid is in Users, it dob DATE
need not necessarily be ); FOREIGN KEY (uid) REFERENCES Users (uid),
in Makes
Lower bound = 0 CREATE TABLE Bookings ( FOREIGN KEY (bid) REFERENCES Bookings (bid),
If a bid is in Bookings, bid INT PRIMARY KEY, PRIMARY KEY (bid)
it need not necessarily
bdate DATE );
be in Makes
Lower bound = 0 );
bdate
One-to-One
Bookings entity
Key + Total
We cannot have both
(bid,
Weak Entity uid1)
Set and
(bid, uid2) in
Guidelines
MakesBookings
Upper bound = 1 Strategy #2: Combined Tables
We may have both
(bid1, uid) and CREATE TABLE Users ( CREATE TABLE MakesBookings (
(bid2, uid) in uid INT PRIMARY KEY, uid INT, -- can be NULL (i.e., no corresponding uid)
MakesBookings
Upper bound = ∞ uname VARCHAR(100), bid INT,
If a uid is in Users, it dob DATE bdate DATE, -- Bookings attribute from merging
need not necessarily be ); FOREIGN KEY (uid) REFERENCES Users (uid),
in MakesBookings
Lower bound = 0 -- In this approach, we remove -- no more REFERENCES Bookings (bid)
If a bid is in -- "Bookings" and combine with PRIMARY KEY (bid)
MakesBookings, it need
-- "makes" into a single table );
not necessarily has the
corresponding uid -- called MakesBookings
Lower bound = 0
✱
This is an example of "Circular Reference".
CS2102: Database Systems -- Adi Yoga Sidi Prabawa 50 / 76
Relational Mapping
Preliminary
Relationship Set
Entity Set Enforced ×
Constraints
❱ Relationship Set
uid uniquely identify
One-to-One
Basic and uname
dob Mapping ER Diagram
Users entity
Many-to-Many
ccnum uniquely identify
Many-to-One
exp
CreditCards entity
One-to-One
We cannot have both
Key + Total
(uid, ccnum1) and
(uid,
Weak Entityccnum2)
Set in
Users
Guidelines
Upper bound = 1
We cannot have both Strategy #2: Combined Tables
(ccnum, uid1) and
(ccnum, uid2) in -- Alternative #1 -- Alternative #2
CreditCards CREATE TABLE Users ( CREATE TABLE Users (
Upper bound = 1
Cannot have uid uid INT PRIMARY KEY, uid INT UNIQUE,
without ccnum in uname VARCHAR(100), uname VARCHAR(100),
Alternative #2 dob DATE, dob DATE,
Lower bound = 0
Cannot have ccnum ccnum INT UNIQUE, ccnum INT PRIMARY KEY, -- ccnum is chosen as PK
without uid in exp DATE exp DATE
Alternative #1
); );
Lower bound = 0
Partial solution
-- uid is chosen as PK
Less preferrable
✱
There is a "hidden" problem here, we can have user with no credit card in alternative #1 but there is an expiry date.
CS2102: Database Systems -- Adi Yoga Sidi Prabawa 51 / 76
Quiz #5
Relational Mapping
Can we split Makes and Bookings into separate table?
Preliminary
Relationship Set
Entity Set Enforced ×
Constraints
❱ Relationship Set
uid uniquely identify
Key + Total
Basic and uname
dob Mapping ER Diagram
Users entity
Many-to-Many
bid uniquely identify
Many-to-One
bdate
Bookings entity
One-to-One
We cannot have both
Key + Total
(bid, uid1) and
(bid,
Weak uid2)
Entity Set in
MakesBookings
Guidelines
Upper bound = 1
We cannot have both Strategy: Combined Tables
(bid1, uid) and
(bid2, uid) in CREATE TABLE Users ( CREATE TABLE MakesBookings (
MakesBookings uid INT PRIMARY KEY, bid INT PRIMARY KEY,
Upper bound = ∞
Cannot have uid uname VARCHAR(100), bdate DATE,
without bid in Users dob DATE uid INT NOT NULL, -- ensures there must be a user!
Lower bound = 0 ); FOREIGN KEY (uid) REFERENCES Users (uid)
Cannot have bid
without uid in -- Users cannot be combined as it );
MakesBookings -- needs to identify dob and uname -- if we combine Users into MakesBooking, we may have
Lower bound = 1 -- (i.e., it is the PK of Users) -- (bid, uid, uname1) and (bid, uid, uname2)
Due to NOT NULL
-- see example on MakesBookings
An ER diagram should capture as many The relational schema should capture as many
constraints as possible (from the requirements) constraints as possible (from the requirements)
An ER diagram must NOT impose any constraints Using column and/or table constraints (e.g., NOT
that are not required (by the requirements) NULL, UNIQUE, CHECK (...), etc)
Attributes should have the same name if and only The relational schema must NOT impose any
if they are semantically equivalent constraints that are not required (by the
Often called the Universal Schema assumption requirements)
(makes query easier) The relational schema should use logical data
types (e.g., names/price should not be INT)
Interpretation Basic
Constraints
Interpretation Interpretation
Constraints
Interpretation Constraints
Constraints
Relational Mapping Can a generalized entity belongs to multiple Must a generalized entity belong to at least 1
Aggregation
specialized entity set? specialized entity set?
Interpretation Notation
Constraints
Note
Example
Relational Mapping
Aggregation
✱
We typically omit "but not both" when using "either" and we typically omit "or any combination" when using "or"
(e.g., "each staff is an admin, research, or teaching").
CS2102: Database Systems -- Adi Yoga Sidi Prabawa 60 / 76
Quiz #6
Extended Notations
Can we also directly map the ISA hierarchies when overlap = FALSE or
covering = TRUE? If yes, how? If no, what kinds of problems will we face?
❱ ISA Hierarchies ISA Hierarchies
Basic
Aggregation
ER Diagram Schema
ER Diagram
Example
Limitations
ER Diagram
Example
Limitations
ER Diagram
Note
Relational Mapping
Existing Relations Foreign Key Constraints
Schema
ER Model
Quiz #3 Question
Quiz #4
Quiz #1
Quiz #5
Where does the attribute fnum comes from?
Quiz #6
Solution
The attribute comes from Flights because Flight_Instances is a weak entity set with Flights being the owning entity set.
Solution
The table Users stores all user information (whether they have made bookings or not)
The table Bookings stores all booking information (whether it is made by a user or not)
The table Makes stores only bookings that have been booked by a user
In other words, it stores users who have made bookings
So we simply look for users who are in the table Users but not in the table Makes
The users in Users include the users in Makes. What we want is the users
that are in Users but not in Makes.
❱ Quiz #3 Question
Quiz #4
Quiz #3
Quiz #5
Which approach is preferrable?
Quiz #6
Solution
This is more of a discussion but we will list some advantages of each approach.
Approach #1 Approach #2
We can find all pairs of bookings bid and users uid No need to look at Makes, and Bookings to find
that have made a booking. We simply look at the bdate of the bookings made by a certain user
Makes without the need to check that uid is NULL with a given uid as both are in MakesBookings.
as it cannot be NULL.
Quiz #3 Question
❱ Quiz #4
Quiz #4
Quiz #5
Is there another implementation with more tables?
Quiz #6
Solution
Quiz #3 Question
Quiz #4
Quiz #5
❱ Quiz #5
Can we split Makes and Bookings into separate table?
Quiz #6
Solution
In this case, NO. If we split the table, then we cannot enforce the total participation constraint on Bookings with
respect to Makes. This is because we can insert into Bookings without many any insertion into Makes.
Quiz #3 Question
Quiz #4
Quiz #6
Quiz #5
Can we also directly map the ISA hierarchies when overlap = FALSE or covering = TRUE? If yes, how? If no, what kinds of
❱ Quiz #6
problems will we face?
Solution
THere are "tricks" that we can use to enforce these but it involves adding redundant columns. We will not go into these
in details and we do not expect students to implement these tricks.
Without these tricks, the only other strategy we have is to not create a table for the generalized entity set. Instead, we
can make them into a VIEW (i.e., a computed table that adds no additional storage and is computed each time we want to query).
The problem with this approach is that the attributes of the generalized entity set have to be stored in the specialized
entity set. Firstly, it is a redundant storage. Secondly, we no longer can uniquely identify the attributes since they can
be different in different specialized entity sets.