30
CMPT 258 Database Systems The Entity-Relationship Model (Chapter 2)

CMPT 258 Database Systems

Embed Size (px)

DESCRIPTION

CMPT 258 Database Systems. The Entity-Relationship Model ( Chapter 2). Overview of Database Design. Conceptual design : ( ER Model is used at this stage.) What are the entities and relationships in the enterprise? - PowerPoint PPT Presentation

Citation preview

Page 1: CMPT 258 Database Systems

CMPT 258 Database SystemsThe Entity-Relationship Model(Chapter 2)

Page 2: CMPT 258 Database Systems

Overview of Database Design• Conceptual design: (ER Model is used at this

stage.) – What are the entities and relationships in

the enterprise?– What information about these entities and

relationships should we store in the database?

– What are the integrity constraints that hold?

– A database ‘schema’ in the ER Model can be represented pictorially (ER diagrams).

– Can map an ER diagram into a relational schema.

Page 3: CMPT 258 Database Systems

ER Model Basics• Entity-Relationship (ER) data model – A database can be modeled as a collection of

entities and relationship among entities– Widely used to develop an initial database

design

• Entity: • Real-world object distinguishable from other

objects. – An entity is described (in DB) using a set of

attributes.

– Degree of an entity: number of attributes

Employees

ssnname

lot

Page 4: CMPT 258 Database Systems

ER Model Basics (cont.)• 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.

• Key: a minimal set of attributes whose values uniquely identify an entity in the set

Employees

ssnname

lot

Page 5: CMPT 258 Database Systems

ER Model Basics (cont.)– Each entity set has a key.

• Key (Candidate Key): a minimal set of attributes whose values uniquely identify an entity in the set

• There could be more than one candidate keys; if so, we designate one of them as the primary key.

Employees

ssnname

lot

Page 6: CMPT 258 Database Systems

ER Model Basics (cont.)– Each attribute has a domain.

• A domain defines the possible values of each attributes• E.g., attribute name might be the set of 20-character string• E.g., attribute rating may have its domain of integers 1

through 10.

•Specifying a domain ▫Data type specified for each domain

Page 7: CMPT 258 Database Systems

Domain Constraints•Typically include:

▫Numeric data types for integers and real numbers

▫Characters▫Booleans▫Fixed-length strings▫Variable-length strings▫Date, time, timestamp▫Money▫Other special data types

Page 8: CMPT 258 Database Systems
Page 9: CMPT 258 Database Systems

ER Model Basics (cont.)• Relationship: Association among two or more

entities. – E.g., James works in Pharmacy department.– Descriptive attribute, since–Used to record information about the

relationship, rather than about any one of the participating entities.

did

dname

budget

Departments

lot

sincename

Works_InEmployees

ssn

Page 10: CMPT 258 Database Systems

ER Model Basics (cont.)• Relationship: Association among two or more

entities. – A relationship must be uniquely identified

by the participating entities, without reference to the descriptive attributes.

did

dname

budget

Departments

lot

sincename

Works_InEmployees

ssn

Page 11: CMPT 258 Database Systems

ER Model Basics (cont.)

•Relationship Set: Collection of similar relationships.

•Same entity set could participate in different relationship sets

lot

name dnamebudgetdid

sincename dname

budgetdid

since

Manages

since

DepartmentsEmployees

ssn

Works_In

Page 12: CMPT 258 Database Systems

ER Model Basics (cont.)

•Instance of a relationship set is a set of relationships.

Page 13: CMPT 258 Database Systems

Ternary Relationship

•Suppose that each department has offices in several locations and we want to record the locations at which each employee works.

Page 14: CMPT 258 Database Systems

ER Model Basics (cont.)•A relationship might involve two entities

in the same entity set▫Reports_To (emp1, emp2)▫Two entities with different “roles” in same

set.▫role indicators▫Reports_To (subordinate_ssn, ▫supervisor_ssn)

subor-dinate

super-visor

Reports_To

lot

name

Employees

ssn

Page 15: CMPT 258 Database Systems

Key Constraints• Consider Works_In: An

employee can work in many departments; a dept can have many employees.▫1:1▫1:N▫N:1▫M:N

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

did

dname

budget

Departments

lot

sincename

Works_InEmployees

ssn

M N

Page 16: CMPT 258 Database Systems

Key Constraints• In contrast, each dept has

at most one manager, according to the key constraint on Manages.

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

dname

budgetdid

since

lot

name

ssn

ManagesEmployees Departments1 N

Page 17: CMPT 258 Database Systems

Key Constraints

• The constraint that each department has at most one manager is an example of a key constraint, and it implies that each Departments entity appear in at most one Manages relationship.

• Arrow states that given a Department entity, we can uniquely determine the manages relationship in which it appears.

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

dname

budgetdid

since

lot

name

ssn

ManagesEmployees Departments

Page 18: CMPT 258 Database Systems

Key Constraints• Each department

has only one manager

• And each employee can manage only one department.

• Cardinality ratio

18

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

dname

budgetdid

since

lot

name

ssn

ManagesEmployees Departments1 1

Page 19: CMPT 258 Database Systems

ExercisesEntity 1 Cardinality Ratio Entity 2

Student   SocialSecurityCard

Student   Teacher

ClassRoom   Wall

Country   CurrentPresident

Course   TextBook

Item (that can be found in an order)

  Order

Student   Class

Class   Instructor

Instructor   Office

E-bay Auction item

  E-bay bid

Page 20: CMPT 258 Database Systems

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 Departments entity must appear in an instance of the Manages relationship.

lot

name dnamebudgetdid

sincename dname

budgetdid

since

Manages

since

DepartmentsEmployees

ssn

Works_In

Page 21: CMPT 258 Database Systems

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 Departments entity must appear in an instance of the Manages relationship.

lot

name dnamebudgetdid

sincename dname

budgetdid

since

Manages

since

DepartmentsEmployees

ssn

Works_In

1

M N

N

Page 22: CMPT 258 Database Systems

• If the participation of an entity set in a relationship set is total, the two are connected by a thick line

lot

name dnamebudgetdid

sincename dname

budgetdid

since

Manages

since

DepartmentsEmployees

ssn

Works_In

Page 23: CMPT 258 Database Systems

Exercises

23

Page 24: CMPT 258 Database Systems

24

Page 25: CMPT 258 Database Systems

Weak Entities• Employees can purchase insurance to cover their

dependents.

lot

name

agepname

DependentsEmployees

ssn

Policy

cost

ssn

Page 26: CMPT 258 Database Systems

Weak Entities• Employees can purchase insurance to cover their

dependents.• Only dependents’ SSN is not needed• If an employee quits, any policy owned by the

employee is terminated and all relevant policy and dependent info will be deleted from the database.

lot

name

agepname

DependentsEmployees

ssn

Policy

cost

ssn

1 N

Page 27: CMPT 258 Database Systems

Weak Entities• Employees can purchase insurance to cover their

dependents.• Only dependents’ SSN is not needed• If an employee quits, any policy owned by the

employee is terminated and all relevant policy and dependent info will be deleted from the database.

lot

name

agepname

DependentsEmployees

ssn

Policy

cost

N1

Page 28: CMPT 258 Database Systems

Weak Entities• A weak entity can be identified uniquely only by

considering the primary key of another (owner) entity.▫Partial key: the set of attributes of a weak

entity set that uniquely identify a weak entity for a given owner entity

lot

name

agepname

DependentsEmployees

ssn

Policy

cost

1 N

Page 29: CMPT 258 Database Systems

Weak Entities• Owner entity set and weak entity set must

participate in a one-to-one or one-to-many relationship set (one owner, one or more weak entities).

• Weak entity set must have total participation in this identifying relationship set. ▫ identifying owner, strong entity

lot

name

agepname

DependentsEmployees

ssn

Policy

cost

Page 30: CMPT 258 Database Systems

Summary so far

•Entities and Entity Set (boxes)•Relationships and Relationship sets (diamonds)

▫binary▫ternary▫n-ary

•Key constraints (1:1,1:N, N:1, M:N, arrows on 1 side)

•Participation constraints (total and partial)•Weak entities - require strong entity for key•Next, a couple more advanced concepts…