Top Banner
อ. ออ. ออออ ออออ ออออ ออออออออออออออออออ ออออออออออ อออออ 5 CS (204)321 รรรรรรรรรรรรร 1 (Database System I)
70

ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Feb 26, 2016

Download

Documents

Ervin

บทที่ 5. ตัวแบบความสัมพันธ์ของ เอนทิ ตี. อ. ดร. ชุรี เตชะวุฒิ. CS (204)321 ระบบฐานข้อมูล 1 (Database System I). Outlines. 1) Database design process 2) ER Model concepts 3) ER diagrams notation 4) Relationships and relationship types 5) Degree of relationship types - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

อ. ดร. ชร เตชะวฒ

ตวแบบความสมพนธของเอนทต

บทท 5

CS (204)321 ระบบฐานขอมล 1 (Database System I)

Page 2: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Outlines

1) Database design process2) ER Model concepts3) ER diagrams notation4) Relationships and relationship types5) Degree of relationship types6) Cardinality of relationships7) Cardinality constraints and cardinality ratio8) Weak entity types9) Transforming ER diagrams into relations

Page 3: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Database Design Process

Requirements collection & analysis

Conceptual design

Logical design (Data model mapping)

Physical design

Database requirements

Conceptual schema

Logical schema

Internal schema

Functional requirements

Functional analysis

High-level transaction specification

Application program design

Transaction implementation

Application programs

Miniworld

DBM

S-ind

epen

dent

DBM

S-sp

ecific

Page 4: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

ER Model Concepts

ER model describes data as entities, relationships, and attributes.

Entities and attributes Entity types and key attributes Relationships and relationships types

Three important concepts are:

Page 5: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Entities and attributes

EntitiesEntities are specific objects or things in the mini-world that are represented in the database. An entity may be an object with

AttributesAttributes are properties used to describe an entity, for example an EMPLOYEE entity may have a Name, SSN, Address, Sex, and BirthDate.

a physical existence (for example, PERSON, CAR, HOUSE, or EMPLOYEE) or with

a conceptual existence (for example, DEPARTMENT, PROJECT, JOB, or UNIVERSITY COURSE.)

Page 6: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Entities and attributes

A specific entity will have a value for each of its attributes, for example a specific employee entity may have Name=‘John Smith’, SSN=‘123456789’, Address=‘731 Fondren, Houston, TX’, Sex=‘M’, BirthDate=’09-JAN-55’.

Value

Each attribute has a value set (or data type) associated with it – e.g., integer, string, subrange, enumerated type, .....

Page 7: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Entities and attributes

Types of attributes

Simple (or atomic) attributes: Attributes that are not divisible such as SSN and sex.

Composite attributes: Attributes that can be divided into smaller subparts. For example, Address(Apt#, House#, Street, City, State, Zipcode, Country) or Name(FirstName, MiddleName, LastName).

Simple VS. Composite

Page 8: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Entities and attributes

Types of attributes (Continued)

Multivalued attributes: Attributes that have numbers of values. For example, Color of a car or PreviousDegrees of a STUDENT. Denoted as {Color} and {PreviousDegrees}.

Single-valued VS. Multi-valuedSingle-valued attributes: An attribute that has a single value, such as Age of person.

Derived attributes: It is derivable from stored attribute.; such as Age

Stored VS. DerivedStored attributes: e.g., BirthDate

Null-valued attribute: An attribute that it’s attribute value exists but is missing, or is unknown whether the attribute value exists.

Page 9: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Entity types and key attributes

Entity typesA set of entities that have the same attributes.Each entity type in the database is described by a name and a list of attributes.

Example of entity type

Entity type name:

Attributes:

Entity set:

(Extension)

EMPLOYEESsn, Name, Age, Salary

• e1 (152056009, John Smith, 55, 80k)

• e2

• e3

(123098987, Fred Brown, 40, 30k)

(143024992, Judy Clark, 25, 20k)

Page 10: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Entity types and key attributes

ER diagrammatic notation

Multivalued attributes are displayed in double ovals.

Composite attributes are attached to their component attributes by straight lines.

Use a rectangular box enclosing the entity type name.

Attribute names are enclosed in ovals and are attached to their entity type by straight lines.

Entity types (Continued)

Page 11: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Entity types and key attributes

Example of ER diagram

EMPLOYEE

Ssn

Age

Salary

Name

Entity types (Continued)

Page 12: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Entity types and key attributes

Key attributesAttributes of whose values are distinct for each individual entity and its values can be used to identify each entity uniquely.

Example key attributes

Entity type

PERSON

Key attribute

Social Security Number

CAR Registration (Number + State)

A key attributes may be composite.

ER diagrammatic notation

Each key attribute has its name underlined inside the oval.

Page 13: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Entity types and key attributes

Example of ER diagram

EMPLOYEE

Ssn

Age

Salary

Name

Key attributes (Continued)

Page 14: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Exercise

1) CAR is an entity type.2) Registration is a key attribute consisting of RegistrationNumber

and State.3) Other important attributes for a car are VehicleID, Make, Model,

Year.4) A car can have more than one Color.

car1 ABC123

TEXAS TK629 Ford Mustang

Convertible 1989 red, black

car2 ABC123

NEW YORK

WP9872 Nissan Sentra

2-door 1992 blue

car3 VSY720

TEXAS TD729 Chrysler LeBaron

4-door 1993 white, blue

… … … … … … … … Requirements for CAR are as followings:

Possible attribute values for CAR entity type:

Page 15: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Exercise

Answer the following questions:

1) What is the name of your entity type?2) What are the attributes for the entity type? 3) What are the types of each attributes?4) Write ER diagram from the requirements above.

Page 16: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Exercise

Registration(RegistrationNumber, State), VehicleID, Make, Model, Year, {Color}

CAR

• car1((ABC123, TEXAS), TK629, Ford Mustang, Convertible, 1989, {red, black})

• car2

• car3

((ABC123, NEW YORK), WP9872, Nissan Sentra, 2-door, 1992, {blue})

((VSY720, TEXAS), TD729, Chrysler LeBaron, 4-door, 1993, {white, blue})…

CAREntity type name:

Attributes:

Entity set:

(Extension)

Page 17: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Exercise

ER diagram

CAR

RegistrationNumber

Year

Color

Make

State

VehicleID Model

Page 18: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

ER diagram notation

ENTITY TYPE

WEAK ENTITY TYPE

RELATIONSHIP TYPE

IDENTIFYING RELATIONSHIP TYPE

ATTRIBUTE

KEY ATTRIBUTE

MULTIVALUED ATTRIBUTE

COMPOSITE ATTRIBUTEDERIVED ATTRIBUTE

Page 19: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Example of COMPANY Database

Requirements for the COMPANY database The company is organized into DEPARTMENTs. Each department

has a name, number, and a employee who manages the department. We keep track of the start date of the department manager. A department may have several locations.

Each department controls a number of PROJECTs. Each project has a name, number, and is located at a single location.

We store each EMPLOYEE’s social security number, address, salary, sex and birth date. Each employee works for one department but may work on several projects. We keep track of the number of hours per week that an employee currently works on each project. We also keep track of the direct supervisor of each employee.

Each employee may have a number of DEPENDENTs. For each dependent, we keep their name, sex, birth date, and relationship to the employee.

ER diagram of COMPANY database

Page 20: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

กฎธรกจ (Business rule)

การพฒนาตวแบบขอมลใหมความถกตองนน นอกจากผออกแบบฐานขอมลจะตองเขาใจขอมลขององคกรอยางดแลว ยงตองเขาใจกฎเกณฑการทำางานหรอระเบยบปฏบตทกำาหนดขนขององคกรนนๆ กฎเกณฑเหลานเรยกวา “กฎธรกจ”

กฎธรกจไมเปนเอกภาพ (Universal) แตละองคกรมกฎเกณพหรอนโยบายแตกตางกนและสามารถเปลยนแปลงได

ตวแบบขอมลจะตองมความสมพนธกนกบกฎธรกจ ถงแมวาตวแบบขอมลจะไมสามารถแสดงกฎธรกจไดทงทงหมดกตาม

Page 21: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

กฎธรกจ (Business rule)

ตวอยางของกฎธรกจ เชน นกศกษาจะลงทะเบยนวชาหนงไดกตอเมอไดสอบผานวชาทตองผานกอนของวชานนๆแลว ลกคาของสหกรณจะเปนเจาของบญชเงนฝากไดเพยงบญชเดยวและแตละบญชตองมเจาของเพยงคนเดยว ใหพนกงานทกคนแตงกายในชดประจำาชาตทกวนศกร

ในระหวางการพฒนาตวแบบออาร เราอาจนำากฎเหลานประยกตใชได เนองจากกฎบางกฎบงบอกถงความสมพนธระหวางเอนทต หรอคาของคารดนลลตการมสวนรวมในความสมพนธ หรอกฎความคงสภาพอน แตกฎบางกฎอาจนำาไปแสดงในตวแบบขอมลไมได เชน ตวอยางของกฎธรกจ ขอสดทาย เปนตน

Page 22: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Relationships and relationship types

A relationship relates two or more distinct entities with a specific meaning.

A relationship type defines a set of associations among entities. e.g.WORKS_ON relationship type in which EMPLOYEEs and PROJECTs participate.MANAGEs relationship type in which EMPLOYEEs and DEPARMENTs participate.

Relationships

Relationship types

e.g.EMPLOYEE “John Smith” works on “the Product X” PROJECT.EMPLOYEE “Franklin Wong” manages “the Research” DEPARMENT.

Page 23: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Relationships and relationship types

Relationship instances

Each relationship type R consists of relationship instances ri .

EMPLOYEE WORKS_FOR DEPARTMENT

e1

e2

e3

e4

e5…

d1

d2

d3…

r1

r2

r3

r4

r5

Example of relationship type WORKS_FOR between two entity types

Page 24: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Relationships and relationship types

ER diagrammatic notation

Use a diamond-shaped box to display a relationship type enclosing the relationship name.

It must be connected by straight lines to the rectangular boxes representing the participating entity types.

Page 25: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Relationships and relationship types

Example of ER diagram

EMPLOYEE

Ssn

Age

Salary

Name

PROJECT

Name

Number

Location

WORKS_FOR

Page 26: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Degree of relationship types

Unary relationship Binary relationship Ternary relationship

More than one relationship type can exist with the same participating entity types.

e.g. MANAGES and WORKS_FOR are distinct relationships between EMPLOYEE and DEPARTMENT participate.

It is the number of participating entity types.

Three kinds of degree of a relationship type:

Page 27: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Degree of relationship types

Unary Binary Ternaryor n-ary

One entity related to another of the same entity type.

Entities of two different types related to each other.

Entities of three different types related to each other.

Page 28: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Cardinality of relationships

one - to - many

one - to - one

Each entity in the relationship will have exactly one related entity.

An entity on one side of the relationship can have many related entities, but an entity on the other side will have a maximum of one related entity.

many - to - many

Entities on both sides of the relationship can have many related entities on the other side.

Page 29: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Cardinality constraints and cardinality ratio

Minimum cardinality

Cardinality constraintsThe number of instances of one entity that can or must be associated with each instance of another entity.

- If zero, then optional.- If one or more, then mandatory.

Maximum cardinality

- The maximum number

Cardinality ratio (or Relationship participant)

คอ การบงบอกลกษณะของความสมพนธทละเอยดมากขนกวาการระบคารดนลลตทำาใหทราบจำานวนเอนทตตำาทสดและสงทสดทสมพนธกบเอนทตในอกเซตหนง

Page 30: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Cardinality constraints and cardinality ratio

ความสมพนธแบบบงคบ (Mandatory relationship)เปนความสมพนธทเกดขนเมอเอนทตทกตวของเซตตองมสวนรวมในความสมพนธของเอนทตเซตเสมอ

ความสมพนธแบบทางเลอก (Optional relationship)

เปนความสมพนธทเกดขนเมอมเอนทตบางตวไมมสวนรวมในความสมพนธของเอนทตเซต

Page 31: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Example

MOVIE make VDON1

MOVIE make VDO(1,1)(0,20)

MOVIE make VDON1

รานใหเชาวดโอเกบสตอกของวดโอ (VDO) ทผลตจากภาพยนตร (MOVIE) โดยภาพยนตรหนงเรองจะนำาไปจดทำาเปนมวนวดโอสงสด 20 มวนหรอไมจดทำาเลย และวดโอหนงมวนสรางจากภาพยนตรเพยงหนงเรองเทานน

Page 32: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Cardinality constraints and cardinality ratio

Cardinality ratio (Continued)

Mandatory one

Mandatory many

Optional one

Optional many

1

N

1

N

Page 33: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Cardinality constraints and cardinality ratio

Cardinality ratio (Continued)

Mandatory one

Mandatory many

Optional one

Optional many

Page 34: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Cardinality constraints and cardinality ratio

Cardinality ratio (Continued)

สญลกษณทใชกำาหนดคารดนลลตในตวแบบออาร มดงน1. Chen’s model

1 1 1 N M N

2. Crow’s foot model

3. IDEF1X (Integrating Definition for Information modeling)

ใชจดแทนความสมพนธดาน many

4. Rein85 แทนความสมพนธดาน many

แทนความสมพนธดาน one

Page 35: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Examples of unary relationships

PERSON is_married_to

one-to-one

EMPLOYEE manages

one-to-many

Page 36: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Examples of binary relationships

EMPLOYEE is_assigned PARKING PLACE

PRODUCTLINE contains PRODUCT

STUDENT registers_for COURSE

one-to-one

one-to-many

many-to-many

Page 37: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Examples of ternary/n-ary relationships

COMPANY sell PRODUCT

COUNTRY

NM

P

• ใชเมอความสมพนธแบบไบนารไมสามารถอธบายความสมพนธระหวาง 3 เอนทตได และสอดคลองกบความเปนจรงมากกวา• บรษทสงสนคาไปจำาหนายยงประเทศตางๆทวโลก และสนคาชนดเดยวกนอาจมราคาตางกนเมอสงไปขายตางประเทศกน บรษทจะสงสนคาไปบางประเทศเทานนขนอยกบประเภทสนคา

Page 38: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Examples of ternary/n-ary relationships

Occurrence diagram

COMPANY

PRODUCT

c1

c2

c3

p1

p2

p3…r1

r2

r3

s1

s2

s3…

COUNTRY

Page 39: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

ความสมพนธทพฒนามาจากแอททรบวต

STUDENT

บางครงเอนทตหรอความสมพนธของเอนทตอาจมจดเรมตนมาจากแอททรบวตของเอนทต ตวอยางเชน หนวยอาคารสถานทตองการเกบขอมลเกยวกบการใชจกรยานยนตของนกศกษา โดยจะบนทกขอมลของนกศกษาไดแก รหสประจำาตว ชอ ทอยและขอมลของรถจกรยานยนตทนกศกษาเปนเจาของ std_name

std_code

address

motorcycle

Page 40: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

ความสมพนธทพฒนามาจากแอททรบวต

STUDENT

จากการสำารวจพบวา นกศกษาสวนใหญไมไดเปนเจาของรถจกรยานยนต ดงนนคาของแอททรบวต motorcycle สวนใหญจะเปนคาวาง (Null) นอกจากนยงตองการเกบขอมลเฉพาะของแอททรบวตนเพมอก เชน ทะเบยนรถ ยหอ และรนเปนตน

ในกรณเชนน motorcycle ควรเปนเอนทตมากกวาแอททรบวตstd_name

std_code address

license

make

model

MOTORCYCLEown 11

Page 41: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Multiple relationship

เอนทต 2 เอนทตอาจมความสมพนธมากกวา 1 แบบ

PERSON

work_in

DIVISION

1N

manage1 1

เอนทต PERSON ทำางานในแผนกหนง และในขณะเดยวกนมบางคนทเปนผจดการของแผนกดวย

Page 42: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Weak entity types

โดยทวไปเอนทตเซตแบงเปน 2 ประเภท1. Strong/Independent entity คอ เอนทตเซตทสามารถเกดขนไดโดยอสระ และมตวช

เฉพาะเพอบงชเอนทตเสมอ2. Weak/Dependent entity คอ เอนทตเซตแบบออนทไมสามารถเกดขนไดดวยตวเอง

และตองพงพาเอนทตเซตอนเสมอ ไมมตวชเฉพาะของตวเอง

Page 43: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Weak entity types

An entity that does not have a key attribute.

Entities are identified by the combination of:

A weak entity must participate in an identifying relationship type with an owner or identifying entity type.

Example:

Suppose that a DEPENDENT entity is identified by the dependent’s first name and birhtdate, and the specific EMPLOYEE that the dependent is related to.

DEPENDENT is a weak entity type with EMPLOYEE as its identifying entity type via the identifying relationship type DEPENDENT_OF.

A partial key of the weak entity type

The particular entity they are related to in the identifying entity type

Page 44: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Weak entity types

EMPLOYEE DEPENDENTS_OF DEPENDENT

N1

EMPLOYEE(fname, lname, ssn, bdate, address, sex, salary, superssn, dno)

DEPENDENT(essn, dependent_name, sex, bdate, relationship)

สญลกษณแทน Identifying relationship type

สญลกษณแทน Weak entity type

Partial key/identifier

Page 45: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Attribute variation

หรอ การสรางตวแบบสำาหรบขอมลทแปรตามกาลเวลา เอนทตแบบออนสามารถนำามาประยกตใช เมอแอททรบวต

บางตวมคาแปรเปลยนตามเวลา (Time-dependent attribute) บางครงเรยก Historical attribute

PERSON develop WEIGH_HEIGH

N1

PERSON(person_id, name, date_of_birth)

DEVELOP(person_id, date_measured, weight, height)

ตวอยาง ตองการตดตามการพฒนาสขภาพของสถานเลยงเดกกำาพรา โดยรวบรวมขอมลความสง และนำาหนกของเดกทเปลยนแปลงในแตละเดอน

Page 46: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Composite/Associative entity

หรอ เอนทตประกอบ เปนเอนทตทนำาตวชเฉพาะของเอนทตอน 2 ตวหรอมากกวา

มาประกอบกนเปนตวชเฉพาะของตวเอง ซงเอนทตประเภทนมกจะมาจากความสมพนธแบบ M:N

STUDENT REGISTRY COURSE

STUDENT REGISTRY COURSE1 N

N N1 1

Page 47: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Composite/Associative entity

กำาหนดตวชเฉพาะของเอนทตประกอบได 2 วธ

1. Default identifier นำาตวชเฉพาะของเอนทตประกอบเดมมาผสมกน

เชน {std_code, crs_code}

2. Surrogate identifier สรางตวชเฉพาะขนมาใหม ในกรณทพบวาคา

default identifier มโอกาสซำากนหรอมตวชเฉพาะทผใชคนเคยอยแลว

เชน {reg_no}

Page 48: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Transforming ER Diagrams into Relations

Step 1: Mapping Regular Entities to Relations

Step 2: Mapping Weak Entities

Step 3: Mapping Unary/Binary Relationships

Step 4: Mapping Multivalued attributes

Step 5: Mapping N-ary Relationship Types.

Page 49: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Transforming ER Diagrams into Relations

1. Mapping Regular Entities to Relations

Simple attributes: ER attributes map directly onto the relation

Composite attributes: Use only their simple, component attributes

Multi-valued attributes: Becomes a separate relation with a foreign key taken from the superior entity

Example:

We create the relations EMPLOYEE, DEPARTMENT, and PROJECT in the relational schema corresponding to the regular entities in the ER diagram.

SSN is a primary key for the relation EMPLOYEE

DNUMBER is a primary key for the relation DEPARTMENT

and PNUMBER is a primary key for the relation PROJECT.

Page 50: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Transforming ER Diagrams into Relations

Figure 3.2:The ER conceptual schema diagram for the COMPANY database.

Page 51: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Transforming ER Diagrams into Relations

2. Mapping Weak Entities

Becomes a separate relation with a foreign key taken from the parent entity.

Primary key composed of:Partial identifier of weak entity type Primary key of identifying entity type (strong entity type)

Example:

Create the relation DEPENDENT in this step to correspond to the weak entity type DEPENDENT.

Include the primary key SSN of the EMPLOYEE relation as a foreign key attribute of DEPENDENT (renamed to ESSN).

The primary key of the DEPENDENT relation is the combination {ESSN, DEPENDENT_NAME} because DEPENDENT_NAME is the partial key of DEPENDENT.

Page 52: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Transforming ER Diagrams into Relations

3. Mapping Binary Relationships

One-to-One: Primary key on the optional side becomes a foreign key on the mandatory side.

Example:

The 1:1 relation MANAGES is mapped by choosing the entity type EMPLOYEEE as the optional side and DEPARTMENT as the mandatory side.

We include the primary key of the EMPLOYEE relation as foreign key in the DEPARTMENT relation and rename it MGRSSN.

We also include the simple attribute StartDate of the MANAGES relationship type in the DEPARMENT relation and rename it MGRSTARTDATE.

Page 53: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Transforming ER Diagrams into Relations

3. Mapping Binary Relationships

One-to-Many: Primary key on the one side becomes a foreign key on the many side.

Example:

The 1:N relationship types WORKS_FOR, CONTROLS, and SUPERVISION in the figure.

For WORKS_FOR we include the primary key DNUMBER of the DEPARTMENT relation as foreign key in the EMPLOYEE relation and call it DNO.

Page 54: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Transforming ER Diagrams into Relations

3. Mapping Binary Relationships

Many-to-Many: Create a new relation with the primary keys of the two entities as its primary key

Example:

The M:N relationship type WORKS_ON from the ER diagram is mapped by creating a relation WORKS_ON in the relational database schema.

The primary keys of the PROJECT and EMPLOYEE relations are included as foreign keys in WORKS_ON and renamed PNO and ESSN, respectively.

Attribute HOURS in WORKS_ON represents the HOURS attribute of the relation type. The primary key of the WORKS_ON relation is the combination of the foreign key attributes {ESSN, PNO}.

Page 55: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Transforming ER Diagrams into Relations

3. Mapping Unary Relationships

One-to-One และ One-to-Many: เอนทตเซตในความสมพนธจะแปลงเปนหนงรเลชน จากนนจงเพมคยนอก (ซงอางถงคยหลกของรเลชน) เขาไปในรเลชนเดยวกนน

Example:

EMPLOYEE(emp_code,emp_name)

EMPLOYEE(emp_code, emp_name, supervisor_code)

EMPLOYEE supervise

N

1supervisor

supervisee

Page 56: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Transforming ER Diagrams into Relations

3. Mapping Unary Relationships

Many-to-Many: แปลงเอนทตเซตในความสมพนธเปนรเลชน จากนนสรางรเลชนใหมสำาหรบความสมพนธและบรรจคยหลกของเอนทตทเกยวของทงสองบทบาท โดยตงชอใหแตกตางกน Example:

COURSE(crs_code, crs_name, crs_credit)

COURSE(crs_code, crs_name, crs_credit)

PREREQUISITE(course_code, pre_crsCode)

COURSE pre_requisite

N

Mpre_course

offered_course

Page 57: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Transforming ER Diagrams into Relations

4. Mapping Multivalued attributes

Create a new relation that includes a multivalued attribute plus the primary key attribute of the entity type - as a foreign key in R

If the multivalued attribute is composite, we include its simple components.

Example:

The relation DEPT_LOCATIONS is created. The attribute DLOCATION represents the multivalued attribute LOCATIONS of DEPARTMENT.

DNUMBER-as foreign key-represents the primary key of the DEPARTMENT relation.

The primary key is the combination of {DNUMBER, DLOCATION}.

Page 58: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Transforming ER Diagrams into Relations

5. Mapping N-ary Relationship Types.

For each n-ary relationship type R, where n>2, create a new relationship S to represent R.

Include as foreign key attributes in S the primary keys of the relations that represent the participating entity types.

Also include any simple attributes of the n-ary relationship type (or simple components of composite attributes) as attributes of S.

Example:

The relationship type SUPPLY in the ER below. This can be mapped to the relation SUPPLY shown in the relational schema, whose primary key is the combination of the three foreign keys {SNAME, PARTNO, PROJNAME}

Page 59: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Transforming ER Diagrams into Relations

Example of ternary relationship types: The SUPPLY relationship.

Page 60: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Transforming ER Diagrams into Relations

Mapping the n-ary relationship type SUPPLY

Page 61: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

Summary of Mapping Constructs and Constraints

ER Model Relational ModelEntity type “Entity” relation1:1 or 1:N relationship type Foreign key (or “relationship” relation)M:N relationship type “Relationship” relation and two foreign keysn-ary relationship type “Relationship” relation and n foreign keysSimple attribute AttributeComposite attribute Set of simple component attributesMultivalued attribute Relation and foreign keyValue set DomainKey attribute Primary (or secondary) key

Correspondence between ER and Relational Models

Page 62: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

กฎความคงสภาพทบงคบใชกบความสมพนธระหวางตาราง

การกำาหนดเงอนไขบางอยางสำาหรบคยนอก เพอใหขอมลมความคงสภาพอยเสมอ

มวธการกำาหนดตามการมสวนรวม (Relationship participation) วาเปนแบบบงคบ (Mandatory) หรอแบบเลอก (Optional) ดงน1. คยหลกของตารางตองไมเปนคาวาง (NOT NULL)

2. สำาหรบเอนทตแบบออน ซงมคารดนลลตแบบ 1:N โดยปรยาย และความสมพนธดานหนงเปนแบบบงคบ ดงนนเงอนไขของคยนอกตองไมเปนคาวาง (NOT NULL) ลบแบบตอเรยง (ON DELETE CASCADE) และแกไขแบบตอเรยง (ON UPDATE CASCADE)

Page 63: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

กฎความคงสภาพทบงคบใชกบความสมพนธระหวางตาราง

3. สำาหรบ Binary relationship ทมความสมพนธแบบ 1:N

Participation คาของคยนอก

Delete Update

1. แบบบงคบทงค NN R C

2. แบบเลอกทงค NA R หรอ SN

C

3. แบบบงคบดาน “1” และแบบเลอกดาน “N”

NN R C

4. แบบบงคบดาน “N” และแบบเลอกดาน “1”

NA R หรอ SN

C

NN (NOT NULL) NA (NOT ALLOW) R (Restricted delete)C (Cascade update) SN (Set to null)

Page 64: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

กฎความคงสภาพทบงคบใชกบความสมพนธระหวางตาราง

3. สำาหรบ Binary relationship ทมความสมพนธแบบ 1:N

ตวอยาง ADVISOR(adv_code, adv_name, office) STUDENT(std_code, std_name, major, adv_code)

โดย adv_code ทเปนคยนอกของตาราง STUDENT จะใชขอ 3 เปนเกณฑ

ADVISOR advise STUDENTN1

Page 65: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

กฎความคงสภาพทบงคบใชกบความสมพนธระหวางตาราง

4. สำาหรบ Binary relationship ทมความสมพนธแบบ M:N

Participation คาของคยนอก

Delete Update

1. แบบบงคบทงค NN R C

2. แบบเลอกทงค NN C C

3. ดานนงเปนแบบบงคบ

และอกดานนงเปนแบบเลอก3.1. คยหลกดานเลอก3.2. คยหลกดานบงคบ

NN

NN

C

R

C

C

Page 66: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

กฎความคงสภาพทบงคบใชกบความสมพนธระหวางตาราง

4. สำาหรบ Binary relationship ทมความสมพนธแบบ M:N

ตวอยาง STUDENT(std_code, std_name, major) SUBJECT(subj_code, subj_name) REGISTRATION(std_code, subj_code, mark)

จะใชขอ 3 เปนเกณฑ โดย std_code ของตาราง REGISTRATION ใชเกณฑ 3.1.และ subj_code ของตาราง REGISTRATION ใชเกณฑ 3.2.

STUDENT take SUBJECTNM

STUDENT REGISTRATION SUBJECTNN1 1

Page 67: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

กฎความคงสภาพทบงคบใชกบความสมพนธระหวางตาราง

5. สำาหรบ Binary relationship ทมความสมพนธแบบ 1:1

Participation คาของคยนอก

Delete Update

1. แบบบงคบทงค NN R C

2. แบบเลอกทงค NA SN C

3. ดานใดดานนงเปนแบบบงคบ

3.1. คยนอกดานเลอก

NN R C

Page 68: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

กฎความคงสภาพทบงคบใชกบความสมพนธระหวางตาราง

5. สำาหรบ Binary relationship ทมความสมพนธแบบ 1:1

ตวอยาง STUDENT(std_code, std_name, major) PROJECT(proj_id, proj_title, date_completed, std_code)

จะใชขอ 3 เปนเกณฑ โดย std_code ทเปนคยนอกของตาราง PROJECT ใชเกณฑ 3.1.

STUDENT complete PROJECT11

Page 69: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

การสรางพจนานกรมขอมล (Data dictionary)

กอนทจะสรางฐานขอมลจรงโดยใชระบบจดการฐานขอมล เราสามารถสรางพจนานกรมขอมล เพอกำาหนดรายละเอยดเกยวกบโครงสรางของตาราง

และความสมพนธระหวางตาราง

Page 70: ตัวแบบความสัมพันธ์ของ เอนทิ ตี

การสรางพจนานกรมขอมล (Data dictionary)

ชอตาราง

ชอแอททรบวต

ความหมาย ชนดขอมล

เปนคาวาง

คย

รเลชนทอางถง

STUDENT

std_code รหสประจำาตวนกศกษา

CHAR(10)

ไมได PK

std_name

ชอนกศกษา CHAR(30)

ไมได

PROJECT

proj_id รหสโปรเจค NUMBER(4)

ไมได FK

proj_title ชอโปรเจค CHAR(60)

ไมได PK

std_code รหสนกศกษาทเปนเจาของโปรเจค

CHAR(10)

ไมได FK

STUDENT

date_complete

วนททำาโปรเจค

DATE(4)

ได