58
atabase Management Systems, 1 Conceptual Design Using the Entity-Relationship (ER) Model

Conceptual Design Using the Entity-Relationship (ER) Model

  • Upload
    mort

  • View
    407

  • Download
    3

Embed Size (px)

DESCRIPTION

Conceptual Design Using the Entity-Relationship (ER) Model. Database Design Process. Requirements analysis What data, what applications, what most frequent operations,… Conceptual database design High level description of the data and the constraint - PowerPoint PPT Presentation

Citation preview

Page 1: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 1

Conceptual Design Using the Entity-Relationship (ER)

Model

Page 2: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 2

Database Design Process Requirements analysis

– What data, what applications, what most frequent operations,…

Conceptual database design– High level description of the data and the

constraint– This step can use ER or similar high level

models Logical database design

– Convert database design into a database schema, e.g. relational db schema

Page 3: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 3

Database Design Process (cont.) Schema refinement

– Analyze the the collection of the data for potential problems and refine it

Physical database design– Ensure that the design meets the

performance requirements, based on used indexation, etc.

Security design– Identify different user groups with different

roles, so that data protection is enforced accordingly.

Page 4: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 4

ER Model Basics Entity: Real-world object

distinguishable from other objects. – An entity is described (in DB)

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. – Each entity set has a key, uniquely

identifies it.– Each attribute has a domain.– Can map entity set to a relation

easily.

CREATE TABLE Employees (ssn CHAR(11), name CHAR(20), lot INTEGER, PRIMARY KEY (ssn))

Employees

ssnname

lot

ssn name lot

123-22-3666 Attishoo 48

231-31-5368 Smiley 22

131-24-3650 Smethurst 35

Page 5: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 5

ER Model Basics (Contd.)

Relationship: Association among 2 or more entities. E.g., Zeynep works in Sales department.

Relationship Set: Collection of similar relationships. Same entity set could participate in different

relationship sets, or in different “roles” in the same set.

lot

dname

budgetdid

sincename

Works_In DepartmentsEmployees

ssn

Reports_To

lot

name

Employees

subor-dinate

super-visor

ssn

Page 6: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 6

ER Model Basics (Contd.) Relationship sets can also

have descriptive attributes (e.g., the since attribute of Works_In).

In translating a relationship set to a relation, attributes of the relation must include:– Keys for each participating

entity set (as foreign keys).

This set of attributes forms superkey for the relation.

– All descriptive attributes.

CREATE TABLE Works_In( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (ssn, did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments)

ssn did since

123-22-3666 51 1/1/91123-22-3666 56 3/3/93231-31-5368 51 2/2/92

Page 7: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 7

Key Constraints

Consider Works_In: An employee can work in many departments; a dept can have many employees, many-to-many, as in the previous figure

In contrast, each dept has at most one manager, according to the key constraint on Manages, one-to-many…

Page 8: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 8

Key Constraints

dname

budgetdid

since

lot

name

ssn

ManagesEmployees Departments

Page 9: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 9

Types of relationship sets

Many-to-Many1-to-1 1-to Many Many-to-1

Page 10: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 10

Translating ER Diagrams with Key Constraints-1

Map relationship to a table: Example: manages relationship– Note that did is the key now!– Separate tables for Employees and Departments.

CREATE TABLE Manages( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments)

THIS MAPPING CAN NOT INCLUDE TOTAL PARTICIPATION!

Page 11: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 11

Translating ER Diagrams with Key Constraints-2

Since each department has a unique manager, we could instead combine Manages and Departments, create relationship Dept_Mgr.

Every dept. may not have a manager, null values allowed for ssn.

CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11), since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees)

This shows key constraint but not total participation because of absence of NOT NULL on ssn

Page 12: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 12

Participation Constraints:Total vs. Partial

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).

For total participation, every did value in Departments table must appear in a row of the Manages table, with a non-null ssn value, indicating that every department has a manager.

Page 13: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 13

Participation constraints (cont.)

lot

name dnamebudgetdid

sincename dname

budgetdid

since

Manages DepartmentsEmployees

ssn

Page 14: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 14

Participation Constraints(Cont.)

In the corresponding SQL mapping, the Null value for the ssn in Dept_Mgr is not allowed…

ON DELETE NO ACTION specification is actually the default case:– It ensures that an Employee tuple cannot be deleted

while it is pointed to by a Dept_Mgr

Page 15: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 15

Participation Constraints in SQL

Capture of participation constraints using one entity set, in a binary relationship.

CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11) NOT NULL, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, ON DELETE NO ACTION)

Page 16: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 16

Participation Constraints (cont.) Many participations cannot be captured by SQL

easily For example, take the two relationship set as in

the following Manages and Works_In ER diagram.

lot

name dnamebudgetdid

sincename dname

budgetdid

since

Manages

since

DepartmentsEmployees

ssn

Works_In

Page 17: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 17

Participation Constraints (cont.) How to ensure total participation in SQL

relation corresponding to Works_In relationship? Note that ssn and did alone cannot be keys for ths relationship….

We have to guarantee that every did value in Departments appears in a tuple of Works_In– This tuple must also have NOT NULL values in the

foreign key fields– This cannot be achieved similar to Manages

relationship, because did cannot be taken as the key for Works_In relationship

– This situation needs assertions in SQL, which exist in the current SQL releases…

Page 18: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 18

Participation Constraints (cont.) Another constraint that cannot be

express in SQL easily is the requirement that each employee must manage at least one department.– Again, in SQL this is achieved using

Assertions, which are constraints that associated to multiple relations or tables.

– Assertions are imposed by the CHECK clause in SQL, although its implementation is cumbersome… this is left out of scope of this lecture

Page 19: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 19

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 one-to-many relationship set: one owner many weak entities

Weak entity set must have total participation in this identifying relationship set.

A weak entity always has a partial key, it can only be uniquely defined if we take its key together with the key of the owner entity.

Page 20: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 20

Weak Entities (cont.) Employee is owner entity, Dependent is

weak entity, Policy is the relationship set:

lot

name

agepname

DependentsEmployees

ssn

Policy

cost

Page 21: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 21

Translating Weak Entity Sets

Weak entity set and identifying relationship set are translated into a single table. ssn cannot be null.– When the owner entity is deleted, all owned

weak entities must also be deleted.

CREATE TABLE Dep_Policy ( pname CHAR(20), age INTEGER, cost REAL, ssn CHAR(11) NOT NULL, PRIMARY KEY (pname, ssn), FOREIGN KEY (ssn) REFERENCES Employees, ON DELETE CASCADE)

Page 22: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 22

ISA (`is a’) HierarchiesAs in C++, or other PLs, attributes are inherited.If we declare A ISA B, every A entity is also considered to be a B entity.

Page 23: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 23

ISA (`is a’) Hierarchies-2

Contract_Emps

namessn

Employees

lot

hourly_wages

ISA

Hourly_Emps

contractid

hours_worked

Overlap constraints: Can Zeynep be an Hourly_Emps as well as a Contract_Emps entity? (posibility:Allowed using OVERLAP)

Covering constraints: Does every Employees entity also have to be an Hourly_Emps or a Contract_Emps entity? (allowed using COVER)

Subclasses are defined first!

Page 24: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 24

ISA (`is a’) Hierarchies-3

Reasons for using ISA: – To add descriptive attributes specific to a

subclass.

– To identify the set of entities that participate in a particular relationship, e.g. only Senior Employees can be allowed to be Managers of Departments.

Page 25: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 25

Translating ISA Hierarchies to Relations

General approach:– 3 relations: Employees, Hourly_Emps and Contract_Emps.

Hourly_Emps: Every employee is recorded in Employees. For hourly emps, extra info recorded in Hourly_Emps (hourly_wages, hours_worked, ssn); must delete Hourly_Emps tuple if referenced Employees tuple is deleted).

Queries involving all employees are easy, those involving just Hourly_Emps require a join to get some attributes.

Alternative: two subentities exist: Hourly_Emps and Contract_Emps:– Each employee must be in one of these two subclasses.

Page 26: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 26

Translating ISA Hierarchies to Relations(cont.)

The second alternative is not applicable if there are employees who are neither of the subclasses.

Also, with the second method there may be more redundant fields, repeated in the subs.

Overlap and covering constraints can only be expressed using assertions.

Page 27: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 27

Aggregation-1Suppose:entity set Projectseach Projects is sponsored by

at least one Departmentseach Departments that

sponsors a Projects might assign employees to monitor sponsorship budgetdidpid

started_on

pbudgetdname

DepartmentsProjects Sponsors

Employees

lotname

ssn

since

Intuitively…Monitors should be a relationship set that associates a Sponsors relation (versus a Projects or Departments) with an Employees entity.

Page 28: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 28

Aggregation-2 Used when we have to

model a relationship involving (entity sets and) a relationship set.

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

budgetdidpid

started_on

pbudgetdname

until

DepartmentsProjects Sponsors

Employees

Monitors

lotname

ssn

since

Page 29: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 29

Aggregation vs.Ternary relation

Each relationship in an aggregation may have a different descriptive attribute like since and until.

Suppose: No need to record until

attribute of Monitors

budgetdidpid

started_on

pbudgetdname

DepartmentsProjects Sponsors2

Employees

lotname

ssn

since

But suppose we have constraint that: Each sponsorship (of a project by a department) be monitoredby at most one employee?

Then… one can’t do it with Sponsors2 Need aggregated relationship Sponsors

Then…one can use ternary relationship Sponsors2

Page 30: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 30

Aggregation vs.Ternary relation (Contd.)

Monitors is a distinct relationship, with a descriptive attribute (until). Also, can say that each sponsorship is monitored by at most one employee.

budgetdidpid

started_on

pbudgetdname

until

DepartmentsProjects Sponsors

Employees

Monitors

lotname

ssn

since

Page 31: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 31

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.

Need for further refinement of the schema:– Relational schema obtained from ER diagram is a good first

step. But ER design is subjective & can’t express certain constraints; so this relational schema may need refinement.

Page 32: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 32

Entity vs. Attribute

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

Depending upon the purpose 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).

Page 33: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 33

Entity vs. Attribute (Contd.)

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

name

Employees

ssn lot

Works_In2

from todname

budgetdid

Departments

Similar to the problem of wanting to record several addresses for an employee: we want to record several values of the descriptive attributes for each instance of this relationship.

Page 34: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 34

Entity vs. Attribute (Contd.)

Use Duration entity to allow more than one duration per relationship…

dnamebudgetdid

name

Departments

ssn lot

Employees Works_In3

Durationfrom to

Page 35: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 35

Entity vs. Relationship Following ER diagram OK if a manager gets a

separate discretionary budget for each dept.

Manages2

name dname

budgetdid

Employees Departments

ssn lot

dbudgetsince

Page 36: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 36

Entity vs. Relationship What if a manager gets a discretionary budget is sum that covers all

depts managed by that employee? Then the previous ER diagram will cause– Redundancy of dbudget, where the same value is stored for each dept managed

by the same manager.– Misleading as it suggests dbudget tied to managed dept rather than emloyee.

Instead a separate entity Mgr_Appts can be used with apptnum as key. This way the redundancy is eliminated!

Employees

since

name dnamebudgetdid

Departments

ssn lot

Mgr_Appts

Manages3

dbudgetapptnum

Page 37: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 37

Binary vs. Ternary Relationships

If each policy is owned by just 1 employee: – Key constraint on Policies would mean policy can only cover 1 dependent!

Every policy must be owned by some employee Dependents is a weak entity set

agepname

DependentsCovers

name

Employees

ssn lot

Policies

policyid cost

Bad design: with or without dark arrow

Page 38: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 38

Binary vs. Ternary Relationships

What are the additional constraints in the 2nd diagram? A policy cannot be owned by more than one employee, every policy must be owned by some employee an cover at least one dependent, dependent is a weak entity set.

Beneficiary is an identifying relationship for the weak entity Dependents, ie. Each dependent will have one policy only…

Beneficiary

agepname

Dependents

policyid cost

Policies

Purchaser

name

Employees

ssn lot

Better design With two binary relationships

Page 39: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 39

Binary vs. Ternary Relationships (Contd.)

The key constraints allow us to combine Purchaser with Policies and Beneficiary with Dependents.

Participation constraints lead to NOT NULL constraints.

CREATE TABLE Policies ( policyid INTEGER, cost REAL, ssn CHAR(11) NOT NULL, PRIMARY KEY (policyid). FOREIGN KEY (ssn) REFERENCES Employees, ON DELETE CASCADE)CREATE TABLE Dependents ( pname CHAR(20), age INTEGER, policyid INTEGER, PRIMARY KEY (pname, policyid). FOREIGN KEY (policyid) REFERENCES Policies, ON DELETE CASCADE)

Page 40: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 40

Binary vs. Ternary Relationships (Contd.)

Previous example illustrated a case when 2 binary relationships were better than a ternary relationship.

An example in the other direction: a ternary relation Contracts with descriptive attribute qty relates entity sets Parts, Departments and Suppliers.

No combination of binary relationships is an adequate substitute for ternary relationship:– S ``can-supply’’ P, D ``needs’’ P, and D ``deals-with’’ S

does not imply that D has agreed to buy P from S.– How do we record qty?

Page 41: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 41

Summary of Conceptual Design

Conceptual design follows requirements analysis, – Yields a high-level description of data to be stored

ER model popular for conceptual design– Constructs are expressive, close to the way people

think about their applications. 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.

Page 42: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 42

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.– Some of these constraints can be expressed in SQL only

if we use general CHECK constraints or assertions.– 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.

Page 43: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 43

Summary of ER (Contd.)

ER design is subjective. There are often many ways to model a given scenario! Analyzing alternatives can be tricky, especially for a large enterprise. 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. FD information and normalization techniques are especially useful.

Page 44: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 44

Views Views are computed as needed using view

definition Example

CREATE VIEW B-Students(name, sid, course)AS SELECT S.name, S.sid, E.cid

FROM Students S, Enrolled EWHERE S.sid=E.sid AND E.grade=‘B’

Whenever B-Students is used in a query, the view definition is first evaluated, before using B-Students in any other query operation.

View concept provides logical data independence, as it can be used to mask the changes in the conceptual schema.

Views can be defined taken security aspect into consideration, e.g., dbadmin, user, group, etc..

Page 45: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 45

Updates on Views The updateable views are the one defined on

single base tables, without aggregate operations. Update on an updateable table implies update of

the corresponding base table as well. A view can be dropped by DROP VIEW command. Deleting base tables is more restrictive, as they

have to have RESTRICT or CASCADE options, concerning integrity…

With RESTRICT, drop of a base is possible if no reference to it exist; with CASCADE, drop of base causes drop of all references recursively…

Page 46: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 46

Example

Reader addrname

Books

manfname

Bookshops

name

license

addr

Note:license =Books, yes,no

Sells Bookshops sell someBooks.

Likes

readers like to readsome Books.Attend

Readers attendsome Bookshops.

Page 47: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 47

Relationship Set The current “value” of an entity set is

the set of entities that belong to it.Example: the set of all Bakkals in our

database. The “value” of a relationship is a set of

lists of currently related entities, one from each of the related entity sets.

Page 48: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 48

Examples A University database contains information about professors (identified by social security number, or SSN) and courses (identified by courseid). Professors teach courses.

Each of the following situations concerns the Teaches relationship set. For each situation, draw an ER diagram that describes it (assuming that no further constraints hold).

Page 49: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 49

Examples (cont’)1) Professors can teach the same course in several semesters,

and each offering must be recorded.2) Professors can teach the same course in several semesters,

and only the most recent such offering needs to be recorded. (Assume this condition applies to all subsequent questions.)

3) Every professor must teach some course4) Every professor teaches exactly one course (no more, no

less).5) Every professor teaches exactly one course (no more no

less), and every course must be taught by some professor.6) Now assume that certain courses can be taught by a team

of professors jointly, but it is possible that no one professor in a team can teach the course. Model this situation introducing additional entity sets and relationship sets if necessary.

Page 50: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 50

Example (cont’)1) Professors can teach the same course in several

semesters, and each offering must be recorded.

Page 51: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 51

Example (cont’)2) Professors can teach the same course in several

semesters, and only the most recent such offering needs to be recorded. (Assume this condition applies to all subsequent questions.)

Page 52: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 52

Example (cont’)3) Every professor must teach some course

Page 53: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 53

Example (cont’)4) Every professor teaches exactly one course (no

more, no less).

Page 54: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 54

Example (cont’)5) Every professor teaches exactly one course (no

more no less), and every course must be taught by some professor.

Page 55: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 55

Example (cont’)6) Now assume that certain courses can be taught

by a team of professors jointly, but it is possible that no one professor in a team can teach the course. Model this situation introducing additional entity sets and relationship sets if necessary.

Page 56: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 56

Page 57: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 57

Example

A company database needs to store information about employees (identified by ssn, with salary and phone as attributes); departments (identified by dno, with dname and budget as attributes); and children of employees (with name and age as attributes). Employees work in departments; each department is managed by an employee; a child must be identified uniquely by name when the parent (who is an employee; assume that only one parent works for the company) is known. We are not interested in information about a child once the parent leaves the company.

Page 58: Conceptual Design Using the Entity-Relationship (ER) Model

Database Management Systems, 58