23
Chapter 2 Chapter 2 Entity relationship Entity relationship model model

Chapter 2

  • Upload
    falala

  • View
    42

  • Download
    0

Embed Size (px)

DESCRIPTION

Chapter 2. Entity relationship model. Concept. Entity set Attribute Relationship Set Entity-Relationship Diagram. Entity Sets. A database can be modeled as: a collection of entities, relationship among entities. - PowerPoint PPT Presentation

Citation preview

Chapter 2Chapter 2

Entity relationship Entity relationship modelmodel

Concept Concept

Entity setEntity set Attribute Attribute Relationship SetRelationship Set Entity-Relationship DiagramEntity-Relationship Diagram

Entity SetsEntity Sets A A databasedatabase can be modeled as: can be modeled as:

a collection of entities,a collection of entities, relationship among entities.relationship among entities.

An An entityentity is an object that exists and is is an object that exists and is distinguishable from other objects.distinguishable from other objects. Example: specific person, company, event, Example: specific person, company, event,

plantplant Entities have Entities have attributesattributes

Example: people have Example: people have names names and and addressesaddresses An An entity setentity set is a set of entities of the same type is a set of entities of the same type

that share the same properties.that share the same properties. Example: set of all persons, companies, trees, holidaysExample: set of all persons, companies, trees, holidays

Entity SetsEntity Setscustomer-id customer- customer- customer- loan- amount name street city number

Weak Entity SetsWeak Entity Sets

An entity set that does not have a primary key is An entity set that does not have a primary key is referred to as a referred to as a weak entity setweak entity set..

The existence of a weak entity set depends on The existence of a weak entity set depends on the existence of a the existence of a identifying entityidentifying entity setset

The The discriminatordiscriminator (or partial key) (or partial key) of a weak entity of a weak entity set is the set of attributes that distinguishes set is the set of attributes that distinguishes among all the entities of a weak entity set.among all the entities of a weak entity set.

The primary key of a weak entity set is formed by The primary key of a weak entity set is formed by the primary key of the strong entity set on which the primary key of the strong entity set on which the weak entity set is existence dependent, plus the weak entity set is existence dependent, plus the weak entity set’s discriminator.the weak entity set’s discriminator.

ExamplesExamples

In a university, a In a university, a coursecourse is a strong entity and is a strong entity and a a course-offering course-offering can be modeled as a weak can be modeled as a weak entityentity

The discriminator of The discriminator of course-offeringcourse-offering would be would be semestersemester (including year) and (including year) and section-number section-number (if there is more than one section)(if there is more than one section)

If we model If we model course-offeringcourse-offering as a strong entity as a strong entity we would model we would model course-numbercourse-number as an as an attribute. attribute. Then the relationship with Then the relationship with coursecourse would be would be implicit in the implicit in the course-numbercourse-number attribute attribute

AttributesAttributes The information about one entity is expressed by The information about one entity is expressed by

a set of (attribute,value) pairsa set of (attribute,value) pairs:: customer = (customer-id, customer-name,

customer-street, customer-city)loan = (loan-number, amount)

Values of attributes belong to different value-sets Values of attributes belong to different value-sets or domains, for example, or domains, for example,

AmoutAmout is an integer between 1 and 12 is an integer between 1 and 12000000 Each entity has a value for each of its Each entity has a value for each of its

attributes.For each attribute ,there is a set of attributes.For each attribute ,there is a set of permitted values called permitted values called domain domain or or value set.value set.

Relationship Set Relationship Set

A relationship is an association among several entitiesA relationship is an association among several entities A relationship set is a subset of the cartesian product of A relationship set is a subset of the cartesian product of

entity sets. entity sets. Example:R-set:” Example:R-set:” Person HAS_EATEN Dish IN Place Person HAS_EATEN Dish IN Place ““

|---------------------------------|-----------------------------------------------------------------------------| ------| | RSet 'Person HAS_EATEN Dish IN Place‘| RSet 'Person HAS_EATEN Dish IN Place‘ | | |---------------------------------------|-----------------------------------------------------------------------------| | | Person | Person | Dish| Dish | Place | Place || |---------|---------------------------|--|----------------------------|--------------------| ------|--------------------| | Steve | Steve | Duck | Duck | Kuala Lumpur | Kuala Lumpur | | | Weiren | Weiren | Duck | Duck | Beijing| Beijing | | | Paolo| Paolo | Noodles | Noodles | Naples | Naples | | | Mike | Mike | Fondue| Fondue | Geneva | Geneva | | | Paolo| Paolo | Duck | Duck | Beijing | Beijing | | |------------------------------------|--------------------------------------------------------------------------------| |

Relationship SetRelationship Set(cont)(cont) An An attributeattribute can also be property of a relationship set. can also be property of a relationship set. For instance, the For instance, the depositor depositor relationship set between relationship set between

entity sets entity sets customer customer and and account account may have the may have the attribute attribute access-dateaccess-date

Mapping CardinalitiesMapping Cardinalities

Express the number of entities to which Express the number of entities to which another entity can be associated via a another entity can be associated via a relationship set.relationship set.

Most useful in describing binary relationship Most useful in describing binary relationship sets.sets.

For a binary relationship set the mapping For a binary relationship set the mapping cardinality must be one of the following cardinality must be one of the following types:types: One to oneOne to one One to manyOne to many Many to oneMany to one Many to many Many to many

Mapping CardinalitiesMapping Cardinalities

One to one: One to one: An entity in A is associated with at most one entity An entity in A is associated with at most one entity in B, & an entity in B is associated with at most one entity in A. in B, & an entity in B is associated with at most one entity in A.

One to many: One to many: An entity in A is associated with any number(zero An entity in A is associated with any number(zero or more) of entities in B.An entity in B however can be associated or more) of entities in B.An entity in B however can be associated with at most one entity in A. with at most one entity in A.

Many to one: Many to one: An entity in A is associated with at most one An entity in A is associated with at most one entity in B.An entity in B however can be associated with any entity in B.An entity in B however can be associated with any number(zero or more) of entities in A. number(zero or more) of entities in A.

Many to many: Many to many: An entity in A is associated with any An entity in A is associated with any number(zero or more) of entities in B & an entity in B is associated number(zero or more) of entities in B & an entity in B is associated with any number(zero or more) of entities in A. with any number(zero or more) of entities in A.

ER-DiagramER-Diagram

Rectangles represent entity sets. Diamonds represent relationship sets. Lines link attributes to entity sets and entity sets to

relationship sets. Ellipses represent attributes

Double ellipses represent multivalued attributes. Dashed ellipses denote derived attributes.

Underline indicates primary key attributes (will study later)

ER-DiagramER-Diagram

Cardinality ConstraintsCardinality Constraints We express cardinality constraints byWe express cardinality constraints by drawing drawing

either a directed line (either a directed line (), signifying “one,” or an ), signifying “one,” or an undirected line (—), signifying “many,” between undirected line (—), signifying “many,” between the relationship set and the entity set.the relationship set and the entity set.

One to many relationship One to many relationship

Many to one relationship Many to one relationship

One to one relationship One to one relationship

RolesRoles Entity sets of a relationship need not be distinctEntity sets of a relationship need not be distinct The labels “manager” and “worker” are called The labels “manager” and “worker” are called rolesroles; they ; they

specify how employee entities interact via the works-for specify how employee entities interact via the works-for relationship set.relationship set.

Roles are indicated in E-R diagrams by labeling the lines that Roles are indicated in E-R diagrams by labeling the lines that connect diamonds to rectangles.connect diamonds to rectangles.

Role labels are optional, and are used to clarify semantics of Role labels are optional, and are used to clarify semantics of the relationshipthe relationship

KeysKeys

A A super keysuper key of an entity set is a set of one of an entity set is a set of one or more attributes whose values uniquely or more attributes whose values uniquely determine each entity.determine each entity.

A A candidate keycandidate key of an entity set is a of an entity set is a minimal super keyminimal super key Customer-idCustomer-id is candidate key of is candidate key of customercustomer account-numberaccount-number is candidate key of is candidate key of accountaccount

Although several candidate keys may Although several candidate keys may exist, one of the candidate keys is selected exist, one of the candidate keys is selected to be the to be the primary keyprimary key..

KeysKeys

AN ENTITY RELATIONSHIP AN ENTITY RELATIONSHIP

DIAGRAM METHODOLOGYDIAGRAM METHODOLOGY Identify Entities Find Relationships Draw Rough ERD Fill in Cardinality Define Primary Keys Draw Key-Based ERD Identify Attributes Map Attributes Draw fully attributed ERD Check Results

ExerciseExercise

CSDL đề án công ty theo dõi các thông tin liên CSDL đề án công ty theo dõi các thông tin liên quan đến nhân viên, phòng ban và đề ánquan đến nhân viên, phòng ban và đề án Cty có nhiều phòng ban, mỗi phòng ban có tên duy nhất, Cty có nhiều phòng ban, mỗi phòng ban có tên duy nhất,

mã phòng duy nhất, một trưởng phòng và ngày nhận mã phòng duy nhất, một trưởng phòng và ngày nhận chức. Mỗi phòng ban có thể ở nhiều địa điểm khác nhau.chức. Mỗi phòng ban có thể ở nhiều địa điểm khác nhau.

Đề án có tên duy nhất, mã duy nhất, do 1 một phòng Đề án có tên duy nhất, mã duy nhất, do 1 một phòng ban chủ trì và được triển khai ở 1 địa điểm.ban chủ trì và được triển khai ở 1 địa điểm.

Nhân viên có mã số, tên, địa chỉ, ngày sinh, phái và Nhân viên có mã số, tên, địa chỉ, ngày sinh, phái và lương. Mỗi nhân viên làm việc ở 1 phòng ban, tham gia lương. Mỗi nhân viên làm việc ở 1 phòng ban, tham gia vào các đề án với số giờ làm việc khác nhau. Mỗi nhân vào các đề án với số giờ làm việc khác nhau. Mỗi nhân viên đều có một người quản lý trực tiếp.viên đều có một người quản lý trực tiếp.

Một nhân viên có thể có nhiều thân nhân. Mỗi thân nhân Một nhân viên có thể có nhiều thân nhân. Mỗi thân nhân có tên, phái, ngày sinh và mối quan hệ với nhân viên đó.có tên, phái, ngày sinh và mối quan hệ với nhân viên đó.

ExerciseExercise

Lam_viec

La_truong_phong

Phan_cong

NHANVIENTENNV

NGSINH DCHI

PHAI

LUONG

HONV

PHONGBAN

TENPB

Phu_trach

DEANTENDA

DDIEM_DA

MANVMAPHG

MADA