CS 2451 Database Systems: Entity-Relationship (ER) Model: Course Summary
CS 2451 Database Systems: Entity-Relationship (ER) Model: Course Summary
1 2
3 4
1
How Does One Build a Database? Building Database Applications: Steps
§ Requirements Analysis: what data, apps, critical 1. Start with a conceptual model
operations • “On paper” using certain techniques
E-R Model
§ Get from “client”
• ignore low-level details – focus on logical representation
• Typically expressed in some natural language
• “step-wise refinement” of design with client input
§ May require going back to the client for resolving 2. Design & implement schema
questions • Design and codify (in SQL) the relations/tables
• Refine the schema – normalization
§ Query and app development depends on client • Do physical layout – indexes, etc.
3. Import the data
specifications
4. Write applications using DBMS and other tools
• Many of the hard problems are taken care of by other people
(DBMS, API writers, library authors, web server, etc.)
• DBMS takes care of Query Optimization, Efficiency, etc.
5. Test!!
5 6
7 8
2
An Example: “mini” banner Example: ER Design for mini-banner:
§ Database containing information about
• Students
• Faculty
fid PROFESSOR name
• Courses
§ Students take courses “Who’s taking what, and what grade do they
§ Faculty teach courses expect?”
Teaches
§ How to ‘define’ student/faculty/course ?
• What data is needed ?
STUDENT Takes COURSE
One picture provides info on what your system stores and models
9 10
§ Since the ER model is data model independent, it can later § Can map an ER diagram into a relational schema.
be converted into the desired logical model (e.g. relational
model).
11 12
3
ER Model Definitions ER Model Basics (Contd.)
§ Entity: Real-world object distinguishable from other
objects. § Relationship: Association among two or more entities.
• An entity is described (in DB) using a set of attributes. E.g., Dan takes Database Course; Attishoo works in
§ Entity Set: A collection of similar entities. E.g., all Pharmacy department.
employees.
• Relationship can also have attributes (that appear only for this
• All entities in an entity set have the same set of attributes. (Until
we consider ISA hierarchies, anyway!) relationship set)
• Each entity set has a key. § Representation/Syntax: a Diamond symbol
• Each attribute has a domain.
• Attributes represented by Oval (same as before)
§ An entity instance is a particular example or occurrence
of an entity type…eg: Employee John Doe § Relationship Set: Collection of similar relationships.
§ Representation/Syntax: • An n-ary relationship set R relates n entity sets E1 ... En; each
relationship in R involves entities e1 ∈ E1, ..., en ∈ En
• Entity set represented by rectangle
• Attribute represented by Oval Same entity set could participate in different relationship sets,
Key attribute underlined or in different “roles” in same set.
Composite Attribute: when it has multiple fields (ex: address)
13 14
15 16
4
Example of a composite attribute Connectivity in the E-R Diagram?
17 18
id
STUDENTS Takes COURSES Student
name
(Nadala, Roxana)
sid name serno subj cid
exp-grade
This often indicates the need for recursive queries
attributes (recall these have domains)
19 20
5
Roles vs. Separate Entities Weak Entity Sets
§ A weak entity can be identified uniquely only by
considering the primary key of another (owner) entity.
id TA IsTAfor Student id • Owner entity set and weak entity set must participate in a one-
to-many relationship set (one owner, many weak entities).
• Weak entity set must have total participation in this identifying
name name relationship set.
• If Student is deleted, then we MUST delete the Parent
What is the difference TA • Syntax: Bold face rectangles, Double lined rectangles,…
between these two Student TA
representations?
id
Person
name
21 22
23 24
6
UML Diagrams – Alternate Syntax for ER
Defining Constraints in ER Model
Diagrams
§ Unified Modeling Language (UML) § Contraints capture properties of the relationship and entities
§ Read on your own • Convey the business rules of the application
§ You’ve seen an example on the lab slides! § Every entity set has a key attribute..similar to Rel. Model
• No two elements can have the same value on this attribute
Example: Student ID
§ How many elements in entity set are associated with
another entity in the relationship ?
• Can a student take more than one course ?
§ Does every element in the entity set appear/participate in
the relationship ?
• Must every student take a course ?
§ Define constraints based on properties of the
mapping/relation between entity sets
25 26
27 28
7
Example: the Takes Course Relationship
Mapping Cardinality, Participation
Constraints, Structural constraints
§ Student can be enrolled in many courses and each Course
can have many students § Type of mapping (cardinality)
• Type of mapping: • 1-1, 1-many, many-many, many-1
Many to Many • Provides some information on relationship sets
§ Want to model the condition that every student must take at § Participation constraints
least one course • Total vs Partial
• Each student must appear in Takes relationship Total: Every student sid must appear in Takes relationship
Partial: All faculty need not appear in Teaches relationship
§ How many courses can a student take ?
• Do we want to specify a limit ?
§ Structural constraints:
• Minimum and maximum times they can appear in relationship
§ How many students must be enrolled in a course ? • Syntax ??
• Is there a minimum size for a class ?
29 30
31 32
8
Conceptual Design Using the ER Summary of Conceptual Design
Model
§ Design choices: § Conceptual design follows requirements analysis,
• Should a concept be modeled as an entity or an attribute? • Yields a high-level description of data to be stored
• Should a concept be modeled as an entity or a relationship? • Visual language – the diagram is the syntax!
• Identifying relationships: constraints, type, participation § ER model popular for conceptual design
§ Constraints in the ER Model: • Constructs are expressive, close to the way people think about their
• A lot of data semantics can (and should) be captured. applications.
• But some constraints cannot be captured in ER diagrams. • There are additional constructs in a “real” ER model based tools.
§ Can automate mapping of ER model to relational tables!
33 34
35 36
9
Initial Design of Entity Types: Refining the initial design by introducing
EMPLOYEE, DEPARTMENT, PROJECT, DEPENDENT relationships
§ The initial design is typically not complete
§ Some aspects in the requirements will be represented as
relationships
§ ER model has three main concepts:
• Entities (and their entity types and entity sets)
• Attributes (simple, composite, multivalued)
• Relationships (and their relationship types and relationship sets)
§ We introduce relationship concepts next
37 38
Relationships and Relationship Types (1) Relationship type vs. relationship set
§ A relationship relates two or more distinct entities with a § Relationship Type:
specific meaning. • Is the schema description of a relationship
• For example, EMPLOYEE John Smith works on the ProductX
PROJECT, or EMPLOYEE Franklin Wong manages the Research • Identifies the relationship name and the participating entity types
DEPARTMENT. • Also identifies certain relationship constraints
§ Relationships of the same type are grouped or typed into a § Relationship Set:
relationship type.
• For example, the WORKS_ON relationship type in which • The current set of relationship instances represented in the database
EMPLOYEEs and PROJECTs participate, or the MANAGES • The current state of a relationship type
relationship type in which EMPLOYEEs and DEPARTMENTs
participate.
§ The degree of a relationship type is the number of
participating entity types.
• Both MANAGES and WORKS_ON are binary relationships.
39 40
10
Relationship instances of the WORKS_FOR N:1
Relationship instances of the M:N WORKS_ON
relationship between EMPLOYEE and
relationship between EMPLOYEE and PROJECT
DEPARTMENT
41 42
43 44
11
ER DIAGRAM – Relationship Types are:
WORKS_FOR, MANAGES, WORKS_ON, CONTROLS, SUPERVISION, DEPENDENTS_OF
Discussion on Relationship Types
§ In the refined design, some attributes from the initial entity
types are refined into relationships:
• Manager of DEPARTMENT -> MANAGES
• Works_on of EMPLOYEE -> WORKS_ON
• Department of EMPLOYEE -> WORKS_FOR
• etc
§ In general, more than one relationship type can exist
between the same participating entity types
• MANAGES and WORKS_FOR are distinct relationship types
between EMPLOYEE and DEPARTMENT
• Different meanings and different relationship instances.
45 46
47 48
12
Many-to-one (N:1) Relationship
One-to-One Relationship Example
E3 D2
E4
D3
E5 r1
E6 D4
r2
E7
r3
E8
P2
§ Example: A department may have multiple projects, but a r2 D2
project may have only one department.
P3
r3
D3
Project Department
Has 4 P4 r4
0..* 0..1 D4
Each department has Each project has P5 r5
zero or more projects. zero or one departments.
13
Many-to-Many Relationships Many-to-many (M:N) Relationship
§ In a many-to-many relationship, each instance of an entity
class E1 can be associated with more than one instance of
another entity class E2 and vice versa.
Employee Project
WorksOn 4
0..* 0..*
Each project has Each employee works on
zero or more employees. zero or more projects.
53 54
55 56
14
A Recursive Relationship Supervision` Recursive Relationship Type is: SUPERVISION
(participation role names are shown)
57 58
59 60
15
Example Attribute of a Relationship Type:
Hours of WORKS_ON Participation Constraints
§ Cardinality is the maximum number of relationship
instances for an entity participating in a relationship type.
61 62
One-to-Many Participation
Participation Constraints Example
Relationship Example
§ Example: A project is associated with one department, and
a department may have zero or more projects. Project Has Department
P1
r1 D1
Project P2
Department r2 D2
Has 4
0..* 1..1
P3
A department may not
r3
A project may have only one D3
have any projects. department.
P4 r4
Each project has
A department may have a department. D4
multiple projects. r5
P5
16
Many-to-Many Relationship
Participation Constraints Example 2
Participation Example
§ Example: A project must have one or more employees, and
an employee must work on one or more projects. Employee WorksOn Project
E1 r1 P1
E2 r2
r3 P2
Employee Project E3
WorksOn 4 r4
1..* 1..* E4
r5 P3
A project must have An employee may work on E5
at least one employee. multiple projects. r6
Each employee works on E6 P4
A project may have
r7
at least one project.
multiple employees. E7 r8
E8 P5
r9
r10
65 66
67 68
17
The (min,max) notation for relationship
COMPANY ER Schema Diagram using
constraints (min, max) notation
69 70
71 72
18
UML class diagram for COMPANY database
ER Model Example in UML notation
schema
3 Supervises
0..1
Supervisor Manages 4
0..*
Employee 0..1 0..* Department
Supervisee 3Has
number {PK} number {PK}
name 0..* 0..1 name
address
Composite state 0..1
Has
attribute city 6
street 0..*
title WorksOn 4
Project
salary 0..* 0..*
number {PK}
name
budget
location [1..3]
Relationship responsibility
/totalEmp
attributes hours
73 74
19