42
楊楊楊楊楊 楊楊楊楊楊楊楊 2013 Fall 2013 Fall 1 Chapter 3: Chapter 3: Modeling Data in the Modeling Data in the Organization Organization : : 11 11 註註 註註 Chapter 2 Chapter 2

楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Embed Size (px)

Citation preview

Page 1: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

楊立偉教授台灣大學工管系

2013 Fall2013 Fall 11

Chapter 3:Chapter 3:Modeling Data in the Modeling Data in the

OrganizationOrganization

註 註 :: 於於 1111 版為版為 Chapter 2Chapter 2

Page 2: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 22

Business RulesBusiness Rules Statements that define or constrain some Statements that define or constrain some

aspect of the business aspect of the business 用來定義或限制 : Ex. 每人每學期修課總學分上限為 30 學分

Control/influence business behavior Control/influence business behavior 控制或影響行為 Ex. 避免學生超修行為

Expressed in terms familiar to end users Expressed in terms familiar to end users 以使用者熟悉的語言來表達 Automated through DBMS software Automated through DBMS software Ex. 修

課系統應自動擋掉超修行為

Page 3: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 33

A Good Business Rule Is:A Good Business Rule Is: Declarative–what, not how Declarative–what, not how 陳述清楚陳述清楚 Precise–clear, agreed-upon meaning Precise–clear, agreed-upon meaning 精確精確 Atomic–one statement Atomic–one statement 不可分割不可分割 Consistent–internally and externally Consistent–internally and externally 一致一致 Expressible–structured, natural language Expressible–structured, natural language 可可

被結構化的口語表達被結構化的口語表達 Distinct–non-redundant Distinct–non-redundant 非贅述的非贅述的 Business-oriented–understood by business Business-oriented–understood by business

people people 可被該領域的人了解可被該領域的人了解

Page 4: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 44

A Good Data Name Is:A Good Data Name Is: Related to business, not technical Related to business, not technical 非技術詞非技術詞 Meaningful and self-documenting Meaningful and self-documenting 看了就懂看了就懂 UniqueUnique 獨一獨一 Readable Readable 可讀性高可讀性高 Composed of words from an approved list Composed of words from an approved list

是可用列表中的語詞是可用列表中的語詞 Repeatable Repeatable 適用多種狀況適用多種狀況 Follows standard syntax Follows standard syntax 有固定命名方式有固定命名方式

Page 5: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 55

E-R Model ConstructsE-R Model Constructs Entities: Entities: 個體個體

Entity instanceEntity instance –– person, place, object, event, concept person, place, object, event, concept (often corresponds to (often corresponds to a row in a tablea row in a table))

Entity TypeEntity Type –– collection of entities (often corresponds collection of entities (often corresponds to to a tablea table))

Relationships: Relationships: 關係關係 Relationship instanceRelationship instance –– link between entities link between entities

(corresponds to (corresponds to primary key-foreign key equivalencies primary key-foreign key equivalencies in related tablesin related tables))

Relationship typeRelationship type –– category of category of relationship…link relationship…link between entity typesbetween entity types

Attribute: Attribute: 屬性屬性 –– property or characteristic of an entity property or characteristic of an entity or relationship type (often corresponds to or relationship type (often corresponds to a field in a a field in a tabletable))

Page 6: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 66

Sample E-R Diagram (Figure 3-1)

0 or 1

1…N 0…N

1

Page 7: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 77

Relationship degrees specify number of entity types involved

Entity symbols

A special entity that is also a relationship

Relationship symbols

Relationship cardinalities specify how many of each entity type is allowed

Attribute symbols

Basic E-R notation (Figure 3-2)

Page 8: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 88

What Should an Entity Be?What Should an Entity Be? SHOULD BE:SHOULD BE:

An object that will have many instances in An object that will have many instances in the databasethe database

An object that will be composed of An object that will be composed of multiple attributesmultiple attributes

An object that we are trying to modelAn object that we are trying to model SHOULD NOT BE:SHOULD NOT BE:

A A useruser of the database system of the database system An An outputoutput of the database system of the database system (e.g., a (e.g., a

report)report)

先找出 Entity

Page 9: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 99

Inappropriate entities

System System useruser

System System outputoutput

Figure 3-4 Example of inappropriate entities

Appropriate entities

X 會計出納員( 這是操作的人 )

X 費用報告( 這是運算後的結果 )

Page 10: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 1010

AttributesAttributes Attribute–property or characteristic of an Attribute–property or characteristic of an

entity or relationship typeentity or relationship type Classifications of attributes:Classifications of attributes:

Required versus Optional Attributes Required versus Optional Attributes 是否必填是否必填 Simple versus Composite Attribute Simple versus Composite Attribute

複合屬性 複合屬性 Ex. Ex. 姓名 姓名 vs vs 姓姓 ++ 名名 Single-Valued versus Multivalued Attribute Single-Valued versus Multivalued Attribute 是否有是否有

多值多值 Stored versus Derived Attributes Stored versus Derived Attributes

是否為衍生屬性 是否為衍生屬性 Ex. Ex. 出生年 出生年 vs vs 年齡年齡 Identifier Attributes Identifier Attributes 是否可供識別用 是否可供識別用 Ex. Ex. 學號學號

Page 11: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3

Data Modeling Concepts: Attributes

Attribute – a descriptive property or characteristic of an entity. Synonyms include element, property, and field.

Just as a physical student can have attributes, such as hair color, height, etc., data entity has data attributes

Compound attribute – an attribute that consists of other attributes. Synonyms in different data modeling languages are numerous: concatenated attribute, composite attribute, and data structure.例如 : 姓名屬性可由姓 + 名所組成右方紅框內即為複合屬性

Page 12: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3

Data Modeling Concepts: Data Type

資料型別 – 這個屬性可以裝哪種資料Data type – a property of an attribute that identifies what type of data can be stored in that attribute.

Representative Logical Data Types for AttributesData Type Logical Business Meaning

NUMBER Any number, real or integer. 實數或整數TEXT A string of characters, inclusive of numbers. When numbers are included in a

TEXT attribute, it means that we do not expect to perform arithmetic or comparisons with those numbers. 文字

MEMO Same as TEXT but of an indeterminate size. Some business systems require the ability to attach potentially lengthy notes to a give database record. 不定長度的文字

DATE Any date in any format.TIME Any time in any format.YES/NO An attribute that can assume only one of these two values. → 布林 (boolean)VALUE SET A finite set of values. In most cases, a coding scheme would be established (e.g.,

FR=Freshman, SO=Sophomore, JR=Junior, SR=Senior). 有限的值集合IMAGE Any picture or image.

Page 13: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3

Data Modeling Concepts: Domains資料範圍 – 這個屬性裝哪些值才是合法Domain – a property of an attribute that defines what values an attribute can legitimately take on.

Representative Logical Domains for Logical Data TypesData Type Domain Examples

NUMBER For integers, specify the range. 值域與小數位數For real numbers, specify the range and precision.

{10-99}{1.000-799.999}

TEXT Maximum size of attribute. Actual values usually infinite; however, users may specify certain narrative restrictions. 最大文字長度

Text(30)

DATE Variation on the MMDDYYYY format. MMDDYYYYMMYYYY

TIME For AM/PM times: HHMMTFor military (24-hour times): HHMM

HHMMTHHMM

YES/NO {YES, NO} {YES, NO} {ON, OFF}

VALUE SET {value#1, value#2,…value#n}{table of codes and meanings}

{M=MaleF=Female}

Page 14: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3

Data Modeling Concepts: Default Value 預設值

Default value – the value that will be recorded if a value is not specified by the user.

Permissible Default Values for AttributesDefault Value Interpretation Examples

A legal value from the domain

For an instance of the attribute, if the user does not specify a value, then use this value.

01.00

NONE or NULL For an instance of the attribute, if the user does not specify a value, then leave it blank. 空值或無值

NONENULL

Required or NOT NULL

For an instance of the attribute, require that the user enter a legal value from the domain. (This is used when no value in the domain is common enough to be a default but some value must be entered.) 必填 或 不可為無值

REQUIREDNOT NULL

Page 15: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 1515

Identifiers (Keys)Identifiers (Keys)

Identifier (Key)–Identifier (Key)–an attribute (or an attribute (or combination of attributes)combination of attributes) that that uniquely identifies individual instances uniquely identifies individual instances of an entity type of an entity type Ex. 學號 or 系所 + 姓名

Simple versus Composite IdentifierSimple versus Composite Identifier Candidate Identifier – an attribute that Candidate Identifier – an attribute that

could be a key…satisfies the could be a key…satisfies the requirements for being an identifierrequirements for being an identifier

Page 16: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 1616

Characteristics of IdentifiersCharacteristics of Identifiers

Will not change in value Will not change in value 不會更改 Will not be null Will not be null 不會無值 No intelligent identifiers (e.g., No intelligent identifiers (e.g.,

containing locations or people that containing locations or people that might change) might change) 不要包含可能更改的欄位 Ex. 系所 + 姓名

Substitute new, simple keys for long, Substitute new, simple keys for long, composite keys composite keys 簡短單一最好

Page 17: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 1717

Figure 3-7 A composite attribute

An attribute broken into component parts

Figure 3-8 Entity with multivalued attribute (Skill) and derived attribute (Years_Employed)

Multivaluedan employee can have more than one skill

Derivedfrom date employed and current date

年資技能

地址

Page 18: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 1818

Figure 3-9 Simple and composite identifier attributes

The identifier is boldfaced and underlined

ID 欄位 ( 或稱 Key 鍵值 )

Page 19: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 1919

Figure 3-19 Simple example of time-stamping

This attribute is both multivalued and composite

歷史價格

注意大括號與小括號

Page 20: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 2020

More on RelationshipsMore on Relationships Relationship Types vs. Relationship InstancesRelationship Types vs. Relationship Instances

The relationship The relationship typetype is modeled as lines between is modeled as lines between entity types…the entity types…the instanceinstance is between specific is between specific entity instancesentity instances

Relationships can have attributesRelationships can have attributes These describe features pertaining to the These describe features pertaining to the

association between the entities in the relationshipassociation between the entities in the relationship Two entities can have more than one type of Two entities can have more than one type of

relationship between them (relationship between them (multiple multiple relationshipsrelationships))

Associative Entity Associative Entity – combination of – combination of relationship and entity relationship and entity 特殊關係所轉換成的特殊關係所轉換成的entityentity

Page 21: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 2121

Figure 3-10 Relationship types and instances

a) Relationship type

b) Relationship instances

Page 22: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 2222

DegreeDegree of Relationships of Relationships

Degree of a relationship is the Degree of a relationship is the number of entity types that number of entity types that participate in itparticipate in it Unary RelationshipUnary Relationship

A relationship between different instances of A relationship between different instances of the same entity is called a the same entity is called a recursive recursive relationshiprelationship

Binary RelationshipBinary Relationship Ternary RelationshipTernary Relationship

Page 23: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 2323

Degree of relationships – from Figure 3-2

Entities of two different types related to each other

Entities of three different types related to each other

One entity related to another of the same entity type

Ex. 朋友關係Ex. 修課關係

Ex. 工作分派關係

Page 24: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 2424

CardinalityCardinality of of RelationshipsRelationships

One-to-OneOne-to-One Each entity in the relationship will have exactly Each entity in the relationship will have exactly

one related entityone related entity One-to-ManyOne-to-Many

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

Many-to-ManyMany-to-Many Entities on both sides of the relationship can have Entities on both sides of the relationship can have

many related entities on the other sidemany related entities on the other side

Page 25: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 2525

Cardinality ConstraintsCardinality Constraints Cardinality Constraints—the number of Cardinality Constraints—the number of

instances of one entity that can or must instances of one entity that can or must be associated with each instance of be associated with each instance of another entityanother entity

Minimum Cardinality Minimum Cardinality 最小值最小值 If zero, then optionalIf zero, then optional If one or more, then mandatoryIf one or more, then mandatory

Maximum Cardinality Maximum Cardinality 最大值最大值 The maximum numberThe maximum number

Page 26: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 2626

Figure 3-12 Examples of relationships of different degrees

a) Unary relationships

Page 27: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 2727

Figure 3-12 Examples of relationships of different degrees (cont.)

b) Binary relationships

Page 28: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 2828

Figure 3-12 Examples of relationships of different degrees (cont.)

c) Ternary relationship

Note: a relationship can have attributes of its own

廠商

零件

倉庫

供應出貨關係

Page 29: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 2929

Figure 3-17 Examples of cardinality constraints

a) Mandatory cardinalities 必填

A patient must have recorded at least one history, and can have many

A patient history is recorded for one and only one patient

Page 30: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 3030

Figure 3-17 Examples of cardinality constraints (cont.)

b) One optional, one mandatory

An employee can be assigned to any number of projects, or may not be assigned to any at all

A project must be assigned to at least one employee, and may be assigned to many

Page 31: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 3131

Figure 3-17 Examples of cardinality constraints (cont.)

c) Optional cardinalities

A person is married to at most one other person, or may not be married at all

Page 32: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 3232

Entities can be related to one another in more than one way

Figure 3-21 Examples of multiple relationships

a) Employees and departments

Page 33: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 3333

Figure 3-21 Examples of multiple relationships (cont.)

b) Professors and courses (fixed lower limit constraint)

Here, min cardinality constraint is 2.

At least two professors must be qualified to teach each course. Each professor must be qualified to teach at least one course.

Page 34: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 3434

Figure 3-15a and 3-15b Multivalued attributes can berepresented as relationships

simple

composite

Page 35: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 3535

Strong vs. Weak Entities, andStrong vs. Weak Entities, andIdentifying RelationshipsIdentifying Relationships

Strong entities Strong entities exist independently of other types of entitiesexist independently of other types of entities has its own unique identifierhas its own unique identifier identifier underlined with single lineidentifier underlined with single line

Weak entityWeak entity dependent on a strong entity (identifying owner)…dependent on a strong entity (identifying owner)…

cannot exist on its owncannot exist on its own does not have a unique identifier (only a partial does not have a unique identifier (only a partial

identifier)identifier) partial identifier underlined with double linepartial identifier underlined with double line entity box has double lineentity box has double line

Identifying relationshipIdentifying relationship links strong entities to weak entitieslinks strong entities to weak entities

Page 36: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 3636

Strong entity Weak entity

Identifying relationship (Figure 3-5)

扶養親屬

Page 37: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 3737

Associative EntitiesAssociative Entities An An entityentity–has attributes–has attributes

A A relationshiprelationship–links entities together–links entities together

When should a When should a relationship with attributesrelationship with attributes instead be an instead be an

associative entityassociative entity All relationships for the associative entity should be All relationships for the associative entity should be manymany The associative entity could have meaning independent of the The associative entity could have meaning independent of the

other entities other entities 有獨立意義時有獨立意義時 The associative entity preferably has a unique identifier, and The associative entity preferably has a unique identifier, and

should also have other attributes should also have other attributes 自己有獨立自己有獨立 idid 時時 The associative entity may participate in other relationships The associative entity may participate in other relationships

other than the entities of the associated relationship (other than the entities of the associated relationship ( 理由類同下理由類同下條條 ))

Ternary relationships should be converted to associative Ternary relationships should be converted to associative entitiesentities

Page 38: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 3838

Figure 3-11a A binary relationship with an attribute

Here, the date completed attribute pertains specifically to the employee’s completion of a course…it is an attribute of the relationship

Page 39: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 3939

Figure 3-11b An associative entity (CERTIFICATE)

Associative entity is like a relationship with an attribute, but it is also considered to be an entity in its own right

Note that the many-to-many cardinality between entities in Figure 3-11a has been replaced by two one-to-many relationships with the associative entity

轉成一張表

Page 40: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 4040

Figure 3-13c An associative entity – bill of materials structure

This could just be a relationship with attributes…it’s a judgment call

Page 41: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 4141

Figure 3-18 Ternary relationship as an associative entity

Page 42: 楊立偉教授 台灣大學工管系 2013 Fall 1 Chapter 3: Modeling Data in the Organization 註 : 於 11 版為 Chapter 2

Chapter 3 4242

Microsoft Visio Notation for Pine Valley Furniture

E-R diagram

(Figure 3-22)

Different modeling software tools may have different notation for the same constructs圖例可能略有不同