Title: Entity Relationship Diagram
1Entity Relationship Diagram
- This presentation is prepared by Yufei
Tao.http//www.cs.cityu.edu.hk/taoyf - Some examples are designed based on the slides
provided by the textbook website.
2Overview
- Database design is essentially to decide
- What data to store?
- How to store it?
- We must perform the design in a systematic way to
achieve the maximum efficiency. - For this purpose, we need a tool that allows us
to visualize our current design in a concise but
accurate manner. - The ER diagram is such a tool.
3An example ER diagram
4Entity and entity set
- An entity is an object.
- A person, a dog, a CD, a
-
- An entity has attributes.
- A person has a name and an address
- A dog has an owner
- A CD has a length-of-play
- An entity set is a set of entities that share the
same attributes. - A person entity set.
- A dog entity set.
- A CD entity set.
5Entity set examples
customer-id customer- customer- customer-
loan- amount
name street city
number
6Attribute types
- Simple vs. composite.
- Single-valued vs. multi-valued
- Name vs. phone-numbers
- Derived vs. non-derived
- Date of birth Non-derived
- Age Derived
7Representation of an entity
multi-valued attribute
derived attribute
8Relationship and relationship set
- A relationship describes the connection between
entities. - A person owns a dog.
- A person bought a CD.
- A man marries a woman.
- A relationship set captures relationships of the
same type. - See next.
9Relationship set (cont.)
- A relationship set is defined between two entity
sets A, B. - Each link is a relationship, and the set of all
links constitutes a relationship set.
10A relationship set with attributes
- From the diagram, we know that
- There are two types of data customer and
account. - Each customer/account has several attributes.
- Each deposit relationship has an attribute, too.
11Constraints on relationship sets
- Each relationship set is associated with two
constraints. - Cardinality constraint
- Participation constraint
- These constraints offer a powerful way to model
real-world connections.
12Cardinality constraint
- One to one
- One to many
- Many to one
- Many to many
- We will see examples in the next few slides.
13Cardinality constraint
One to one (e.g., husband-wife)
One to many (e.g., father-child)
14One-to-one representation
- A customer can borrow at most one loan.
- A loan can be borrowed by at most one customer.
one
15One-to-many representation
- A customer can borrow many loans.
- A loan can be borrowed by at most one customer.
many
one
16Cardinality constraint
Many to one (e.g., child-father)
Many to many (e.g., friendship)
17Many-to-one representation
- A customer can borrow at most one loan.
- A loan can be borrowed by many customers.
many
one
18Many-to-many representation
- A customer can borrow many loans.
- A loan can be borrowed by many customers.
19Constraints of relationship sets
- Each relationship set is associated with two
constraints. - Cardinality constraint
- Participation constraint
20Participation constraint
- Total participation Every entity in the entity
set must participate in at least one
relationship. - Partial participation Some entities may not
participate.
partial
total
- Some customers may not borrow any loan.
- Every loan must be borrowed by at least one
customer.
21Participation constraint (cont.)
- What can we imply from the participation
constraints in this relationship?
partial
total
- Every customer must borrow at least a loan.
- Some loan may not be borrowed by any customer.
- It does not make sense in practice.
22More on relationships constraints
- Cardinality constraints.
- 1-1, 1-m, m-1, m-m
- Participation constraints.
- Partial, total
- The two types of constraints are independent.
- Each cardinality constraint can be used with any
participation constraint. - See some examples next.
23More on constraints (cont.)
- What can you imply in the following relationships?
24More on constraints (cont.)
- What can you imply in the following relationships?
25More on constraints (cont.)
- What constraints should be applied if
- Every customer should borrow exactly a loan.
- Every loan should be borrowed by exactly one
customer.
26More on constraints (cont.)
- Every customer should borrow exactly a loan.
- Every loan should be borrowed by exactly one
customer. - Answer
27Self relationships
- The entity sets participating in a relationship
can be the same.
28Keys
- Primary keys of entities are underlined in an ER
diagram.
29Keys for relationship sets
- Attributes that can uniquely identify a
relationship in the relationship set. - The union of the primary keys of the
participating entity sets contains the primary
key of a relationship set. - Customer-id, loan-number can uniquely decide
each borrower relationship. - But it is not a primary key it is not minimum.
30Keys for 1-to-1 relationship sets
- The primary key can be customer-id or
loan-number.
31Keys for 1-m and m-m relationship sets
Primary key is loan_number
Primary key is customer-id, loan_number
32Ternary relationship set
Two example works-on relationships (John, CityU
branch, manager) (John, Diamond Hill branch,
secretary) They indicate that John takes
different roles in the two branches.
33An example ER diagram (revisited)
Now we should understand the following design
better.