Sunteți pe pagina 1din 40

The Entity-Relationship Model

Chapter 2

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

Overview of Database Design


Conceptual design: (ER Model is used at this stage.)

What are the entities and relationships in the enterprise?

Students sign up for classes

What information about these entities and relationships should we store in the database?

Student number and phone number of each student

What are the integrity constraints or business rules that hold?

No student takes more than seven courses per semester

A database `schema in the ER Model can be represented pictorially (ER diagrams). Can map an ER diagram into a relational schema.
2

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

Entity & Entity Set

Entity: Real-world object distinguishable from other objects. An entity is described (in database) using a set of attributes.

Entity Set: A collection of similar entities, e.g., all employees.

All entities in an entity set have the same set of attributes (until we consider ISA hierarchies, anyway!)

Each attribute has a Key domain. Each entity set has a key ssn uniquely identifies an entity in the set Entity set

Attribute
name

lot Employees
3

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

Relationship
since name dname lot Employees
Works_In

ssn

did

budget Departments

Relationship: Association among two or more entities, e.g., Jessica works in Pharmacy department.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

Relationship Set
since name ssn Employees lot
Works_In

dname did budget Departments

Relationship Set: Collection of similar relationships.

Relationship set

EMPLOYEES

WORKS_IN

DEPARTMENTS

An n-ary relationship set R relates n entity sets E1 ... En; each relationship in R involves entities e1 in E1, ..., en in En

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

A Ternary Relationship Example


since name

ssn
Employees

lot
Works_In

dname did

budget

Departments

Locations address Employees capacity Works_Ins

Each department has offices in several locations We want to record the locations at which each employee works

Works at two locations

Departments

Locations

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

Relationship Participation
Same entity set could participate in different relationship sets, or in different roles in the same set.
name
ssn lot

Employees participates in two different roles

Employees

supervisor

subordinate

Reports_To

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

Mapping to Tables
since name dname lot
Works_In

ssn

did

budget Departments

Employees

Employees
ssn name lot

Works_In
ssn 1234 did 8

Departments
did 8 dname DB budget 1,000,000

1234

Jenny

A456

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

Key Constraints: 1-to-Many

Consider Works_In: An employee can work in many departments; a dept can have many employees (many-tomamy relationship). In contrast, each dept has at most one manager, according to the key constraint on Manages (1-tomany relationship).

from name ssn

to dname did budget

lot

Employees

Works_In
manyto-many

Departments

since name ssn lot did dname budget

Employees

Manages
1-tomany

Departments

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

Key Constraints
since name ssn lot did dname budget

Employees

Manages

Departments

EMPLOYEES

MANAGES

DEPARTMENTS


1-to Many

Given a Departments entity, we can uniquely determine the Manages relationship in which it appears

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

10

Different Kinds of Relationships

Many-to-Many

1-to-1

1-to Many

Many-to-1

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

11

Participation Constraints
Does every department have a manager?

If so, this is a participation constraint

the participation of Departments in Manages is said to be total (vs. partial). Every did value in Departments table must appear in a row of the Manages table (with a non-null ssn value! - more details later in this course)
name ssn Employees since dname

lot Manages

did

budget Departments

Works_In

Partial participation
since
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

Total participation
(a participation constraint)
12

Total vs. Partial Participation


Does every department have a manager ?
Employees Manages Departments

Employees

Manages

Departments

Employees Employees

Manages Manages

Departments Departments


Partial Participation


Has no manager
Total Participation

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

Everyone has a manager

13

Weak Entities
A weak entity can be identified uniquely only by considering the primary key of another (owner) entity.

Owner entity set and weak entity set must participate in a oneto-many relationship set (one owner, many weak entities). Weak entity set must have total participation in this identifying relationship set.
Partial key name ssn lot cost dpname age

Employees

Policy

Dependents

The owner entity set

A weak entity set


Draw both with dark lines to indicate that Policy is the identifying relationship
14

Need both ssn and dpname to identify a Dependents entity

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

ISA (`is a) Hierarchies


name ssn lot

Employees

As in C++, or other PLs, hourly_wages attributes are inherited. If we declare A ISA B, every A entity is also considered to be a B entity.

hours_worked ISA contractid

Hourly_Emps

Contract_Emps

Overlap constraints: Can Joe be an Hourly_Emps as well as a Contract_Emps entity? (Allowed/disallowed)

Covering constraints: Does every Employees entity also have to be an Hourly_Emps or a Contract_Emps entity? (Yes/no)
15

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

ISA (is a) Hierarchies


hourly_wages

ssn

name lot

Employees
hours_worked ISA

Reasons for using ISA

Manages

To add descriptive attributes specific to a subclass


Example: We only need to track hours worked for hourly employees

Hourly_Emps Contract_Emps

contractid

To identify entities that participate in a relationship


Example: Only contract employees can be managers (i.e., participating in the Manages relationship)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

16

Aggregation

name ssn Employees lot

Used when we have to model a relationship involving (entity sets and) a relationship set.
started_on pid Projects

Monitors

until

Aggregation allows us to treat a relationship set as an entity set for purpose of participation in (other) relationships

since did

dname budget

pbudget

Sponsors

Departments

Aggregation vs. ternary relationship: Monitors is a distinct relationship, with a descriptive attribute. Also, can say that each sponsorship is monitored by at most one employee.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 17

Conceptual Design Using the ER Model

Design choices:

Should a concept be modeled as an entity or an attribute? Should a concept be modeled as an entity or a relationship? Identifying relationships: Binary or ternary? Aggregation?

Constraints in the ER Model:


A lot of data semantics can (and should) be captured.

But some constraints cannot be captured in ER diagrams, e.g., functional dependencies (discussed later in this course)
18

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

Entity vs. Attribute (1)


Should address be an attribute of Employees or an entity (connected to Employees by a relationship)?
Option 1
name ssn address

lot
name ssn Employees lot Street City State Address

Employees

live_at

Option 2
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 19

Entity vs. Attribute (2)

Depends upon the use we want to make of address information, and the semantics of the data:
If we have several addresses per employee, address must be an entity (since attributes cannot be set-valued).
If the structure (city, street, etc.) is important, e.g., we want to retrieve employees in a given city, address must be modeled as an entity (since attribute values are atomic).
name address lot name
Employees can have multiple addresses Can retrieve employees in a given city

ssn

City lot live_at Street State Address


20

ssn
Employees Employees

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

Entity vs. Attribute (Contd.)


name ssn lot Works_In4 from to

dname
did budget Departments

Employees

Works_In4 does

ssn Same key value 1234 1234

did 9 9

from 1998 2006

to 2002 2009

not allow an employee to work in a department for two or more periods

same instance
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 21

Entity vs. Attribute (Contd.)

Works_In4 does not allow an employee to work in a department for two or more periods. Similar to the problem of wanting to record several addresses for an employee:

name

dname

ssn
Employees

lot
Works_In5

did

budget Departments

from

Duration

to

Works_In5
ssn 1234 1234 did 9 9 from 1998 2006 to 2002 2009

We want to record several values of the descriptive attributes for each instance of this relationship. Accomplished by introducing new entity set, Duration.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

22

Entity vs. Relationship

First ER diagram OK if a manager gets a separate discretionary budget for each dept. What if a manager gets a discretionary budget that covers all managed depts?

since name ssn Employees name lot

dbudget dname did

budget
Departments

Manages2

ssn Employees

lot since did dname budget Departments

Redundancy: dbudget stored for each dept managed by manager. Misleading: Suggests dbudget associated with department-mgr combination.

ISA

Manages2

Managers

dbudget

This fixes the problem !


23

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

Binary vs. Ternary Relationships (1)


name 1. An employee can own several policies 2. Each policy can be owned by several employee 3. Each dependent can be covered by several policies ssn Employees lot Covers pname age Dependents

Policies policyid cost Joint policy

If each policy is owned by just one employee, and each dependent is tied to the covering policy, we need some constraints Owns 2
policies

Employees

Covers

Policy

Dependents Covered by 2 policies


24

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

Binary vs. Ternary Relationships (2)


Constraints: Each policy is owned by just one employee, and each dependent is tied to the covering policy.
name pname lot Dependents Employees Purchaser Beneficiary age

ssn

Key constraint A policy cannot be owned jointly by two or more employees Total participation Every policy must be owned by some employee

Policies policyid cost

Weak entity set Each dependent is uniquely identified by a policy (i.e., policyid and pname)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

25

When to use N-ary Relationships ?


qty
Suppliers Contracts Departments

Parts

No combination of binary relationships is an adequate substitute for the above ternary relationship:

The binary relationships S can-supply P, D needs P, and D deals-with S do not imply that D has agreed to buy P from S. How do we record qty in the binary relationships ?
26

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

Example: University Database


We have four entity sets: Professor, Project, Graduate, Department

rank age ssn Specialty

pid

sponsor start-date end_date

Professor

Project
budget

Dept
dno dname Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke office

Graduate
ssn age name deg_prog 27

Example: University Database

Each project is managed by one professor (principal investigator) Professors can manage multiple projects
rank age ssn Specialty

pid

sponsor start-date end_date

Professor

Manages

Project
budget

Dept
dno dname Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke office

Graduate
grad

ssn
age

name
deg_prog 28

Example: University Database

Each project is worked on by one or more professors (co-investigators) Professors can work on multiple projects
rank age ssn Specialty Work_in

pid

sponsor start-date end_date

Professor

Manages

Project
budget

Dept
dno dname Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke office

Graduate
ssn
age

name
deg_prog 29

Example: University Database

Each project is worked on by one or more graduate students (research assistants) Graduate students can work on multiple projects
rank age ssn Specialty Work_in

pid

sponsor start-date end_date

Professor

Manages

Project
budget

Work_proj

hours

Dept
dno dname Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke office

Graduate
ssn
age

name
deg_prog 30

Example: University Database

When graduate students work on multiple projects, they must have a supervisor (professor) for each one.
rank age ssn Specialty Work_in

pid

sponsor start-date end_date

Professor

Manages

Project
budget

Supervises

Work_proj

hours

Dept
dno dname Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke office

Graduate
ssn
age

name
deg_prog 31

Example: University Database


Departments have a professor who runs the department

rank age ssn Specialty Work_in

pid

sponsor start-date end_date

Professor

Manages

Project
budget

Runs

Supervises

Work_proj

hours

Dept
dno dname Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke office

Graduate
ssn
age

name
deg_prog 32

Example: University Database

Professors work in one or more departments For each department a professor works in, a time percentage is associated with their job
rank age ssn Specialty Work_in

pid

sponsor start-date end_date

Professor

Manages

Project
budget

Work_dept

Runs

Supervises

Work_proj

hours

pc_time

Dept
dno dname office

Graduate
ssn
age

name
deg_prog 33

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

Example: University Database


Graduate students have one major department in which they are working on their degree
rank age ssn Specialty Work_in

pid

sponsor start-date end_date

Professor

Manages

Project
budget

Work_dept

Runs

Supervises

Work_proj

hours

pc_time

Dept
dno dname office

Major

Graduate
ssn
age

name
deg_prog 34

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

Example: University Database


Each graduate student has another, more senior graduate student who advises him or her on what courses to take
rank age ssn Specialty Work_in

pid

sponsor start-date end_date

Professor

Manages

Project
budget

Work_dept

Runs

Supervises

Work_proj

hours

pc_time

senior

Dept
dno dname office

Major

Graduate
grad

Advisor

ssn
age

name
deg_prog 35

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

Example: University Database


rank age ssn Specialty Work_in

pid

sponsor start-date end_date

Professor

Manages

Project
budget

Work_dept

Runs

Supervises

Work_proj

hours

pc_time

senior

Dept
dno dname office

Major

Graduate
grad

Advisor

ssn
age

name
deg_prog 36

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

Summary of Conceptual Design

Conceptual design follows requirements analysis

Yields a high-level description of data to be stored Constructs are expressive, close to the way people think about their applications.

ER model popular for conceptual design

Basic constructs: entities, relationships, and attributes (of entities and relationships).
Some additional constructs: weak entities, ISA hierarchies, and aggregation.

Note: There are many variations on ER model.


37

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

Summary of ER (Contd.)

Several kinds of integrity constraints can be expressed in the ER model:


key constraints, participation constraints, and overlap/covering constraints for ISA hierarchies. Some foreign key constraints are also implicit in the definition of a relationship set (more later in the course). Some constraints (notably, functional dependencies) cannot be expressed in the ER model. Constraints play an important role in determining the best database design for an enterprise.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

38

Summary of ER (Contd.)

There are often many ways to model a given scenario! Analyzing alternative ER designs can be tricky. Common choices include:
Entity vs. attribute, entity vs. relationship, binary or n-ary relationship, whether or not to use ISA hierarchies, and whether or not to use aggregation.

Ensuring good database design: resulting relational schema should be analyzed and refined further.
Functional dependency information and normalization techniques are especially useful (discussed later).

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

39

Homework #1
Do Exercises 2.4 and 2.8 in the textbook
Due: 01/26/2012

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

40

S-ar putea să vă placă și