CHAPTER 2 MODELING DATA IN THE ORGANIZATION Modern

  • Slides: 55
Download presentation
CHAPTER 2: MODELING DATA IN THE ORGANIZATION Modern Database Management 12 th Edition Global

CHAPTER 2: MODELING DATA IN THE ORGANIZATION Modern Database Management 12 th Edition Global Edition Jeff Hoffer, Ramesh Venkataraman, Heikki Topi 授課老師:楊立偉教授,台灣大學 管系

E-R MODEL CONSTRUCTS � Entities: 個體 Entity instance–person, place, object, event, concept (often corresponds

E-R MODEL CONSTRUCTS � Entities: 個體 Entity instance–person, place, object, event, concept (often corresponds to a row in a table) � Entity Type–collection of entities (often corresponds to a table) � � Relationships: 關係 Relationship instance–link between entities (corresponds to primary key-foreign key equivalencies in related tables) � Relationship type–category of relationship…link between entity types � � Attributes: 屬性 � Properties or characteristics of an entity or relationship type (often corresponds to a field in a table) Chapter 2 2 -2

Sample E-R Diagram (Figure 2 -1) Chapter 2 1 0 or 1 1…N 0…N

Sample E-R Diagram (Figure 2 -1) Chapter 2 1 0 or 1 1…N 0…N 2 -3

Basic E-R notation (Figure 2 -2) Entity symbols A special entity that is also

Basic E-R notation (Figure 2 -2) Entity symbols A special entity that is also a relationship Relationship degrees specify number of entity types involved Chapter 2 Attribute symbols Relationship cardinalities specify how many of each entity type is allowed 2 -4

BUSINESS RULES Are statements that define or constrain some aspect of the business 用來定義或限制

BUSINESS RULES Are statements that define or constrain some aspect of the business 用來定義或限制 : Ex. 每人 每學期修課總學分上限為 30學分 � Are derived from policies, procedures, events, functions; Assert business structure 定出商業結構 � Control/influence business behavior 控制或影響行 為 Ex. 避免學生超修行為 � Are expressed in terms familiar to end users 以使 用者熟悉的語言來表達 � Are automated through DBMS software 例如修課 系統應自動擋掉超修行為 � Chapter 2 2 -5

A GOOD BUSINESS RULE IS: � Declarative–what, not how 做什麼(而非如何 做) � Precise–clear, agreed-upon

A GOOD BUSINESS RULE IS: � Declarative–what, not how 做什麼(而非如何 做) � Precise–clear, agreed-upon meaning 精確 � Atomic–one statement 不可分割 � Consistent–internally and externally 一致 � Expressible–structured, natural language 可 被結構化的口語表達 � Distinct–non-redundant 非贅述的 � Business-oriented–understood by business Chapter 2 2 -6 people 可被該領域的人了解

A GOOD DATA NAME IS: Related to business, not technical, characteristics 非技術詞 � Meaningful

A GOOD DATA NAME IS: Related to business, not technical, characteristics 非技術詞 � Meaningful and self-documenting 看了就懂 � Unique 獨一 � Readable 可讀性高(非只是一堆代號) � Composed of words from an approved list 是可用 列表中的語詞 � Repeatable 適用多種狀況 � Written in standard syntax 有固定命名方式 (例如 英文動名詞加名詞,全小寫,不用複數等) � Chapter 2 2 -8

ENTITIES 先找出 Entity � Entity – a person, a place, an object, an event,

ENTITIES 先找出 Entity � Entity – a person, a place, an object, an event, or a concept in the user environment about which the organization wishes to maintain data � Entity type – a collection of entities that share common properties or characteristics � Entity instance – A single occurrence of an entity type Chapter 2 2 -9

ENTITY TYPE AND ENTITY INSTANCES Chapter 2 2 -10

ENTITY TYPE AND ENTITY INSTANCES Chapter 2 2 -10

AN ENTITY… � SHOULD BE: � An object that will have many instances in

AN ENTITY… � SHOULD BE: � An object that will have many instances in the database � An object that will be composed of multiple attributes � An object that we are trying to model � SHOULD NOT BE: � A user of the database system � An output of the database system (e. g. , a report) Chapter 2 2 -11

Figure 2 -4 Example of inappropriate entities System user X 會計出納員 這是操作的人 不在系統範圍內 (除非含用戶管理)

Figure 2 -4 Example of inappropriate entities System user X 會計出納員 這是操作的人 不在系統範圍內 (除非含用戶管理) Inappropriate entities System output X 費用報告 這是運算後的結果 不在系統範圍內 (除非含報表管理) Appropriate entities Chapter 2 2 -12

GUIDELINES FOR NAMING AND DEFINING ENTITIES � Definitions: 舉例說明 � Names: 命名 � “An

GUIDELINES FOR NAMING AND DEFINING ENTITIES � Definitions: 舉例說明 � Names: 命名 � “An X is…” � Singular noun 取名用單數 � Describe unique � Specific to organization characteristics of each � Concise, or abbreviation instance � For event entities, the � Explicit about what is and is result not the process not the entity (例如訂購單, 而非訂購) � When an instance is created � Name consistent for all or destroyed diagrams � Changes to other entity types � History that should be kept Chapter 2 2 -13

ATTRIBUTES � Attribute–Property or Characteristic of an entity or relationship type. i. e. Field

ATTRIBUTES � Attribute–Property or Characteristic of an entity or relationship type. i. e. Field � Classifications of attributes: � Required versus Optional Attributes 是否必填 � Simple versus Composite Attribute 複合屬性 Ex. 姓名 vs 姓+ 名 � Single-Valued versus Multivalued Attribute � Stored versus Derived Attributes 是否有多值 是否為衍生屬性 Ex. 出生年 vs 年齡 Chapter 2 � Identifier Attributes 是否可供識別用 Ex. 學號 2 -14

REQUIRED VS. OPTIONAL ATTRIBUTES Required – must have a value for every entity (or

REQUIRED VS. OPTIONAL ATTRIBUTES Required – must have a value for every entity (or relationship) instance with which it is associated Chapter 2 Optional – may not have a value for every entity (or relationship) instance with which it is associated 2 -15

CHOOSING DATA TYPES from Ch. 5 Chapter 2 2 -16

CHOOSING DATA TYPES from Ch. 5 Chapter 2 2 -16

FIELD DATA INTEGRITY from Ch. 5 � � Default value–assumed value if no explicit

FIELD DATA INTEGRITY from Ch. 5 � � Default value–assumed value if no explicit value 給定預設值 Range control–allowable value limitations (constraints or validation rules) 允許值之限制 Null value control–allowing or prohibiting empty fields 是否允許無值(未曾填過)或空值(填了留白) Referential integrity–range control (and null value allowances) foreign-key to primary-key matchups 外鍵查表的對應檢查 Note: Sarbanes-Oxley Act (SOX) legislates importance of financial data integrity 沙賓法案其中規範了財務性資料的完整性檢查 Chapter 2 2 -17

SIMPLE VS. COMPOSITE ATTRIBUTES � Composite attribute – An attribute that has meaningful component

SIMPLE VS. COMPOSITE ATTRIBUTES � Composite attribute – An attribute that has meaningful component parts (attributes) The address is broken into component parts 地址 Figure 2 -7 A composite attribute Chapter 2 2 -18

MULTI-VALUED AND DERIVED ATTRIBUTES Multivalued – may take on more than one value for

MULTI-VALUED AND DERIVED ATTRIBUTES Multivalued – may take on more than one value for a given entity (or relationship) instance Derived – values can be calculated from related attribute values (not physically stored in the database) Figure 2 -8 Entity with multivalued attribute (Skill) and derived attribute (Years Employed) 技能 Multivalued an employee can have more than one skill Chapter 2 年資 Derived Calculated from date employed and current date 2 -19

IDENTIFIERS (KEYS) � Identifier (Key)–an attribute (or combination of attributes) that uniquely identifies individual

IDENTIFIERS (KEYS) � Identifier (Key)–an attribute (or combination of attributes) that uniquely identifies individual instances of an entity type Ex. 學號 or 系所+姓名 � Simple versus Composite Identifier � Candidate Identifier–an attribute that could be an identifier…satisfies the requirements for being an identifier Chapter 2 2 -20

CRITERIA FOR IDENTIFIERS � Choose Identifiers that � Will not change in value 不會更改

CRITERIA FOR IDENTIFIERS � Choose Identifiers that � Will not change in value 不會更改 � Will not be null 不會無值 � Avoid intelligent identifiers (e. g. , containing locations or people that might change) 不要包含 可能更改的欄位 ex. 戶口所在縣市之代碼 (遷戶口怎麼 辦) � Substitute new, simple keys for long, composite keys 簡短單一最好 Ex. 流水號是不錯的選擇 Chapter 2 2 -21

Figure 2 -9 Simple and composite identifier attributes ID欄位 (或稱 Key鍵值) The identifier is

Figure 2 -9 Simple and composite identifier attributes ID欄位 (或稱 Key鍵值) The identifier is boldfaced and underlined Chapter 2 2 -22

NAMING ATTRIBUTES � Name should be a singular noun or noun phrase 用單數名詞或名詞片語 �

NAMING ATTRIBUTES � Name should be a singular noun or noun phrase 用單數名詞或名詞片語 � Name should be unique 獨一 � Name should follow a standard format type name { [ Qualifier ] } ] Class 例如 Student_ID、Student_Name、Student_Address � e. g. [Entity � Similar attributes of different entity types should use the same qualifiers and classes � 例如Student_ID、Professor_ID Chapter 2 2 -23

DEFINING ATTRIBUTES � � � � State what the attribute is and possibly why

DEFINING ATTRIBUTES � � � � State what the attribute is and possibly why it is important 試 著說明這屬性是什麼,為什麼需要 Make it clear what is and is not included in the attribute’s value 合法值的範圍,並給一些範例 Include aliases in documentation 若有別稱要一併說明 State source of values 說明值的來源 (有哪些值) State whether attribute value can change once set 是否可修 改 Specify required vs. optional 是否必填 State min and max number of occurrences allowed 是否有出 現次數的下限或上限 (例如全班成績只可以有10個A) Indicate relationships with other attributes 試著說明與其他屬 性的關係 Chapter 2 2 -24

MODELING RELATIONSHIPS � Relationship Types vs. Relationship Instances � The relationship type is modeled

MODELING RELATIONSHIPS � Relationship Types vs. Relationship Instances � The relationship type is modeled as lines between entity types…the instance is between specific entity instances � Relationships can have attributes � These describe features pertaining to the association between the entities in the relationship Two entities can have more than one type of relationship between them (multiple relationships) � Associative Entity–combination of relationship and entity � Chapter 2 2 -25

Figure 2 -10 Relationship types and instances a) Relationship type (Completes) b) Relationship instances

Figure 2 -10 Relationship types and instances a) Relationship type (Completes) b) Relationship instances Chapter 2 2 -26

DEGREE OF RELATIONSHIPS � Degree of a relationship is the number of entity types

DEGREE OF RELATIONSHIPS � Degree of a relationship is the number of entity types that participate in it (1) Unary Relationship �A relationship between different instances of the same entity is called a recursive relationship (2) Binary Relationship (3) Ternary Relationship 建議少用更多n-ary的關係;可轉換成多個binary關係;或用associative entity表達 Dey, Debabrata, Veda C. Storey, and Terence M. Barron. "Improving database design through the analysis of relationships. " ACM Transactions on Database Systems (TODS)24. 4 (1999): 453 -486. Chapter 2 2 -27

Degree of relationships – from Figure 2 -2 One entity related to another of

Degree of relationships – from Figure 2 -2 One entity related to another of the same entity type Ex. 朋友關係 Chapter 2 Entities of two different types related to each other Ex. 修課關係 Entities of three different types related to each other Ex. 作分派關係 2 -28

CARDINALITY OF RELATIONSHIPS � One-to-One � Each entity in the relationship will have exactly

CARDINALITY OF RELATIONSHIPS � One-to-One � Each entity in the relationship will have exactly one related entity � One-to-Many � 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 Chapter 2 2 -29

Figure 2 -12 Examples of relationships of different degrees a) Unary relationships Chapter 2

Figure 2 -12 Examples of relationships of different degrees a) Unary relationships Chapter 2 2 -30

Figure 2 -12 Examples of relationships of different degrees (cont. ) b) Binary relationships

Figure 2 -12 Examples of relationships of different degrees (cont. ) b) Binary relationships Chapter 2 2 -31

Figure 2 -12 Examples of relationships of different degrees (cont. ) c) Ternary relationship

Figure 2 -12 Examples of relationships of different degrees (cont. ) c) Ternary relationship 零件 廠商 倉庫 供應出貨關係 Note: a relationship can have attributes of its own Chapter 2 2 -32

CARDINALITY CONSTRAINTS � Cardinality Constraints—the number of instances of one entity that can or

CARDINALITY CONSTRAINTS � Cardinality Constraints—the number of instances of one entity that can or must be associated with each instance of another entity � Minimum Cardinality 最小值 �If zero, then optional �If one or more, then mandatory � Maximum Cardinality 最大值 �The maximum number Chapter 2 2 -33

Figure 2 -17 Examples of cardinality constraints a) Mandatory cardinalities A patient history is

Figure 2 -17 Examples of cardinality constraints a) Mandatory cardinalities A patient history is recorded for one and only one patient Chapter 2 A patient must have recorded at least one history, and can have many 2 -34

Figure 2 -17 Examples of cardinality constraints (cont. ) b) One optional, one mandatory

Figure 2 -17 Examples of cardinality constraints (cont. ) b) One optional, one mandatory A project must be assigned to at least one employee, and may be assigned to many Chapter 2 An employee can be assigned to any number of projects, or may not be assigned to any at all 2 -35

Figure 2 -17 Examples of cardinality constraints (cont. ) c) Optional cardinalities A person

Figure 2 -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 Chapter 2 2 -36

Figure 2 -21 Examples of multiple relationships a) Employees and departments Entities can be

Figure 2 -21 Examples of multiple relationships a) Employees and departments Entities can be related to one another in more than one way Chapter 2 2 -37

Figure 2 -21 Examples of multiple relationships (cont. ) b) Professors and courses (fixed

Figure 2 -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 course. Each professor must be qualified to teach at least one course. Chapter 2 2 -38

Figure 2 -15 a and 2 -15 b Multivalued attributes can be represented as

Figure 2 -15 a and 2 -15 b Multivalued attributes can be represented as relationships simple Ch 4. First Normal Form時會教 composite Chapter 2 2 -39

ASSOCIATIVE ENTITIES � An entity–has attributes � A relationship–links entities together � When should

ASSOCIATIVE ENTITIES � An entity–has attributes � A relationship–links entities together � When should a relationship with attributes instead be an associative entity? � All relationships for the associative entity should be many (tips: 多對多 關係) � The associative entity could have meaning independent of the other entities 有獨立意義時 � The associative entity preferably has a unique identifier, and should also have other attributes 自己有獨立id時 � The associative entity may participate in other relationships other than the entities of the associated relationship (理由類同下條) � Ternary relationships should be converted to associative entities Chapter 2 2 -40

Figure 2 -11 a A binary relationship with an attribute Here, the date completed

Figure 2 -11 a 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. Chapter 2 2 -41

Figure 2 -11 b An associative entity (CERTIFICATE) 轉成一張表 Associative entity is like a

Figure 2 -11 b 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 2 -11 a has been replaced by two one-to-many relationships with the associative entity. Chapter 2 2 -42

原圖及改以Associative Entity表示後之比較 為何需要 E_ID E_NAM E CNO 1 John 001 1 2 Mary 002

原圖及改以Associative Entity表示後之比較 為何需要 E_ID E_NAM E CNO 1 John 001 1 2 Mary 002 3 Bob 每 1列代表 1位員 E_ID CDat e C_ID C_Title A 會計 A 10/1 B 行銷 1 B 10/5 C 管理 003 2 A 10/1 004 2 C 10/9 每 1列代表 1門課 005 3 B 10/5 每 1列代表 1張證書 Chapter 2 2 -45

Figure 2 -13 c An associative entity – bill of materials structure This could

Figure 2 -13 c An associative entity – bill of materials structure This could just be a relationship with attributes…it’s a judgment call. Chapter 2 2 -46

對比原圖 用Associative Entity表示 Chapter 2 2 -47

對比原圖 用Associative Entity表示 Chapter 2 2 -47

Figure 2 -18 Cardinality constraints in a ternary relationship Chapter 2 2 -48

Figure 2 -18 Cardinality constraints in a ternary relationship Chapter 2 2 -48

Figure 2 -19 Simple example of time-stamping 歷史價格 注意大括號與小括號 Time stamp – a time

Figure 2 -19 Simple example of time-stamping 歷史價格 注意大括號與小括號 Time stamp – a time value that is associated with a data value, often indicating when some event occurred that affected the data value Chapter 2 The Price History attribute is both multivalued and composite. 2 -50

Figure 2 -20 c E-R diagram with associative entity for product assignment to product

Figure 2 -20 c E-R diagram with associative entity for product assignment to product line over time Modeling time-dependent data has become more important due to regulations such as HIPAA 健康保險可攜性及責任法案 and Sarbanes-Oxley 沙賓法案. Chapter 2 The Assignment associative entity shows the date range of a product’s assignment to a particular product line. 2 -51

STRONG VS. WEAK ENTITIES, AND IDENTIFYING RELATIONSHIPS � Strong entity � exists independently of

STRONG VS. WEAK ENTITIES, AND IDENTIFYING RELATIONSHIPS � Strong entity � exists independently of other types of entities � has its own unique identifier � identifier underlined with single line � Weak entity � dependent on a strong entity (identifying owner)…cannot exist on its own � does not have a unique identifier (only a partial identifier) � entity box and partial identifier have double lines � Identifying relationship � links strong entities to weak entities Chapter 2 2 -52

Figure 2 -5 Example of a weak identity and its identifying relationship 扶養親屬 Strong

Figure 2 -5 Example of a weak identity and its identifying relationship 扶養親屬 Strong entity Chapter 2 Weak entity 2 -53

Figure 2 -22 Data model for Pine Valley Furniture Company in Microsoft Visio notation

Figure 2 -22 Data model for Pine Valley Furniture Company in Microsoft Visio notation Different modeling software tools may have different notation for the same constructs. 不同 具之圖例表達 可能略有不同 Chapter 2 2 -54

http: //draw. io Chapter 2 2 -55

http: //draw. io Chapter 2 2 -55