SQL&DALecture 2
SQL&DALecture 2
Diagrams (ERD)
Chapter 2
Entity-Relationship Diagram (ERD)
An entity-relationship diagram (ERD) is a graphical representation of the data model
representing the proposed database
Entity-Relationship Diagram (ERD)
ER diagram answers the questions:
What entities (person, place, or thing) are being represented?
What attributes (characteristics of data) are stored about each entity?
What are the relationships between the entities?
Basically, what data do we want to capture and what are the business rules
surrounding that data
Entity-Relationship Diagram (ERD)
attribute
Entity Name
attribute
Date of birth
Author
Entity Relation: Writes
attribute
Title
attribute
Publication year
Book
Entity-Relationship Diagram (ERD)
An ERD contains three components:
Entity-Relationship Diagram (ERD)
Four goals of ER diagrams:
• Capture all required data
• Ensure data appears only once in the database design
• Does not include any data that is derived from other data
already in the data model
• Arrange data in the data model in a logical manner
Steps to Create ER Diagram
Step Task
1 Identify entities
2 Identify attributes
3 Assign unique identifiers (UID) / primary key (PK)
4 Identify unique keys
5 Identify relationships
6 Identify relationship cardinality (and optionality)
7 Assign foreign keys
8 Name relationships and ER language
9 Eliminate many-to-many relationships
10 Define data types
11 Normalize the database (Chapter 5)
What is an Entity?
"Something" of significance to the business about which data must be captured
Entity Characteristics:
Usually a noun
Singular form
Upper case
Examples: CUSTOMER, STUDENT, EMPLOYEE
What is an Entity?
An Entity can be a(n):
Person – example EMPLOYEE
Place – example BUILDING
Object – example PRODUCT
Event – example CONCERT
Example:
An EMPLOYEE entity must require a value for the employee id, first name and last
name attributes
Mandatory (Required) Attributes
Appears as bold in the entity rectangle
Optional Attributes
Attribute that may not have a value or is NULL (unknown)
Example:
An EMPLOYEE entity might contain an optional mobile number
Appears in normal font in the entity
Multivalued Attributes
An attribute that can have more than one value
A customer may have more than 1 contact number (home, mobile, office)
An EMPLOYEE may have more than one dependent
Consider later
Volatile and Derived Attributes
A volatile attribute contains a value that constantly changes over time:
length_of_membership
years_of_service
age
Would have to be updated constantly
Volatile and Derived Attributes
A derived attribute
Contains a value that can be calculated from existing attributes
Not stored in the database, but computed when required
length_of_membership can be calculated using the current date and
membership_date attribute
years_of_service can be calculated using current date and hire_date attribute
Age can be calculated using the current date and birth date
Nonvolatile Attributes
Attributes that rarely change
Examples:
Order date
Birth date
Hire date
Select nonvolatile attributes. For example, use birth date instead of age
Step 2: Define Attributes for
Blackwater Consulting
Attributes are listed under the entity name in lower case
Mandatory attributes in bold
Optional attributes in normal font
Words are joined with an underline
Unique Identifier (UID) /
Primary Key (PK)
Every entity must have a unique identifier or primary key
Referred to as primary key in relational (physical) model and database
Unique Identifier (UID) /
Primary Key (PK)
A single attribute or a combination of attributes
Must be unique – Uniquely identifies one and only one instance of an entity
A unique identifier value should never change
Must contain a value – cannot be NULL
There should be no practical limit to the number of unique identifier values available
Only one unique identifier should be specified for each entity. Remember, an UID can
be one or more attributes
Candidate Keys
Attributes that qualify as UIDs/Primary Keys (PK)
Each candidate key must meet the following criteria:
Same rules as unique identifier
Values must be unique for each instance within the entity
Cannot contain a NULL value
Determine Candidate Keys
Which attributes qualify as candidate keys?
Candidate Keys
Candidate keys identified by red rectangle
Step 3: Assign Unique Identifiers (UIDs)/
Primary Key (PK)
Listed as the first attribute in the box as PK (primary key)
Must contain a unique value
Is required and thus specified in bold
Underline
Step 4: Assign Unique Keys (UK)
All candidate keys not selected as UID/PK
Identified by UK
Can contain a NULL value; thus can be an optional attribute
diagrams.net for Data Modeling
Library Example
•Identify entities Let's expand our library system to include more
relevant entities:
•Book
•Author
•Borrower (library member)
•Library Branch
•Loan
Library Example
•Identify attributes For each entity, we'll list relevant attributes:
•Book: •Borrower:
•ISBN •Member ID
•Title •First Name
•Publication Year •Last Name
•Number of Pages •Email •Loan:
•Genre •Phone Number •Loan ID
•Current Status (available, borrowed, etc.) •Address •Borrow Date
•Membership Start Date •Due Date
•Author: •Return Date
•Author ID •Library Branch:
•First Name •Branch ID
•Last Name •Branch Name
•Birth Date •Address
•Nationality •Phone Number
Library Example
•Identify attributes For each entity, we'll list relevant attributes:
•Book: •Borrower:
•ISBN •Member ID
•Title •First Name
•Publication Year •Last Name
•Number of Pages •Email •Loan:
•Genre •Phone Number •Loan ID
•Current Status (available, borrowed, etc.) •Address •Borrow Date
•Membership Start Date •Due Date
•Author: •Return Date
•Author ID •Library Branch:
•First Name •Branch ID
•Last Name •Branch Name
•Birth Date •Address
•Nationality •Phone Number
Library Example
•Assign unique identifiers (UID) / primary key (PK) For each entity,
we'll choose a primary key: