CHAPTER 2: MODELING DATA IN THE ORGANIZATION Essentials of Database Management Jeffrey A. Hoffer, Heikki Topi, V. Ramesh Copyright © 2014 Pearson Education, Inc. 1 OBJECTIVES Define terms Understand importance of data modeling Write good names and definitions for entities, relationships, and attributes Distinguish unary, binary, and ternary relationships Model different types of attributes, entities, relationships, and cardinalities Draw E-R diagrams for common business situations Convert many-to-many relationships to associative entities Model time-dependent data using time stamps Chapter 2 Copyright © 2014 Pearson Education, Inc. 2 E-R MODEL CONSTRUCTS Entities: Relationships: 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) 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 Copyright © 2014 Pearson Education, Inc. 3 Sample E-R Diagram (Figure 2-1) Chapter 2 Copyright © 2014 Pearson Education, Inc. 4 Basic E-R notation (Figure 2-2) Entity symbols Attribute symbols A special entity that is also a relationship Relationship symbols Relationship degrees specify number of entity types involved Chapter 2 Relationship cardinalities specify how many of each entity type is allowed Copyright © 2014 Pearson Education, Inc. 5 BUSINESS RULES Are statements that define or constrain some aspect of the business Are derived from policies, procedures, events, functions Assert business structure Control/influence business behavior Are expressed in terms familiar to end users Are automated through DBMS software Chapter 2 Copyright © 2014 Pearson Education, Inc. 6 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 people Chapter 2 Copyright © 2014 Pearson Education, Inc. 7 DATA DEFINITIONS Explanation of a term or fact Term–word or phrase with specific meaning Fact–association between two or more terms Guidelines for good data definition A concise description of essential data meaning Gathered in conjunction with systems requirements Accompanied by diagrams Achieved by consensus, and iteratively refined Chapter 2 Copyright © 2014 Pearson Education, Inc. 8 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 Copyright © 2014 Pearson Education, Inc. 9 ENTITIES 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 Copyright © 2014 Pearson Education, Inc. 10 ENTITY TYPE AND ENTITY INSTANCES Chapter 2 Copyright © 2014 Pearson Education, Inc. 11 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 Copyright © 2014 Pearson Education, Inc. 12 Figure 2-4 Example of inappropriate entities System user Inappropriate entities System output Appropriate entities Chapter 2 Copyright © 2014 Pearson Education, Inc. 13 STRONG VS. WEAK ENTITIES, AND IDENTIFYING RELATIONSHIPS Strong entity exists independently of other types of entities has its own unique identifier Weak entity identifier underlined with single line 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 Copyright © 2014 Pearson Education, Inc. 14 Figure 2-5 Example of a weak identity and its identifying relationship Strong entity Chapter 2 Weak entity Copyright © 2014 Pearson Education, Inc. 15 ATTRIBUTES Attribute–property or characteristic of an entity or relationship type Classifications of attributes: Required versus Optional Attributes Simple versus Composite Attribute Single-Valued versus Multivalued Attribute Stored versus Derived Attributes Identifier Attributes Chapter 2 Copyright © 2014 Pearson Education, Inc. 16 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 Copyright © 2014 Pearson Education, Inc. 17 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 Copyright © 2014 Pearson Education, Inc. 18 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 Copyright © 2014 Pearson Education, Inc. Derived Calculated from date employed and current date 19 IDENTIFIERS (KEYS) Identifier (Key)–an attribute (or combination of attributes) that uniquely identifies individual instances of an entity type Simple versus Composite Identifier Candidate Identifier–an attribute that could be a key…satisfies the requirements for being an identifier Chapter 2 Copyright © 2014 Pearson Education, Inc. 20 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) Substitute new, simple keys for long, composite keys Chapter 2 Copyright © 2014 Pearson Education, Inc. 21 Figure 2-9 Simple and composite identifier attributes The identifier is boldfaced and underlined Chapter 2 Copyright © 2014 Pearson Education, Inc. 22 NAMING ATTRIBUTES Name should be a singular noun or noun phrase Name should be unique Name should follow a standard format e.g. [Entity type name { [ Qualifier ] } ] Class Similar attributes of different entity types should use the same qualifiers and classes Chapter 2 Copyright © 2014 Pearson Education, Inc. 23 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 Specify required vs. optional State min and max number of occurrences allowed Indicate relationships with other attributes Chapter 2 Copyright © 2014 Pearson Education, Inc. 24 MODELING RELATIONSHIPS Relationship Types vs. Relationship Instances Relationships can have attributes The relationship type is modeled as lines between entity types…the instance is between specific entity instances 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 Copyright © 2014 Pearson Education, Inc. 25 Figure 2-10 Relationship types and instances a) Relationship type (Completes) b) Relationship instances Chapter 2 Copyright © 2014 Pearson Education, Inc. 26 DEGREE OF RELATIONSHIPS Degree of a relationship is the number of entity types that participate in it Unary Relationship Binary Relationship Ternary Relationship Chapter 2 Copyright © 2014 Pearson Education, Inc. 27 Degree of relationships – from Figure 2-2 One entity related to another of the same entity type Chapter 2 Entities of two different types related to each other Entities of three different types related to each other Copyright © 2014 Pearson Education, Inc. 28 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 Copyright © 2014 Pearson Education, Inc. 29 Figure 2-12 Examples of relationships of different degrees a) Unary relationships Chapter 2 Copyright © 2014 Pearson Education, Inc. 30 Figure 2-12 Examples of relationships of different degrees (cont.) b) Binary relationships Chapter 2 Copyright © 2014 Pearson Education, Inc. 31 Figure 2-12 Examples of relationships of different degrees (cont.) c) Ternary relationship Note: a relationship can have attributes of its own Chapter 2 Copyright © 2014 Pearson Education, Inc. 32 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 The Chapter 2 Cardinality maximum number Copyright © 2014 Pearson Education, Inc. 33 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 Copyright © 2014 Pearson Education, Inc. 34 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 Copyright © 2014 Pearson Education, Inc. 35 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 Copyright © 2014 Pearson Education, Inc. 36 Figure 2-20 Examples of multiple relationships a) Employees and departments Entities can be related to one another in more than one way Chapter 2 Copyright © 2014 Pearson Education, Inc. 37 Figure 2-20 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. Chapter 2 Copyright © 2014 Pearson Education, Inc. 38 Figure 2-15a and 2-15b Multivalued attributes can be represented as relationships simple composite Chapter 2 Copyright © 2014 Pearson Education, Inc. 39 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 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 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 Copyright © 2014 Pearson Education, Inc. 40 Figure 2-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. Chapter 2 Copyright © 2014 Pearson Education, Inc. 41 Figure 2-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 2-11a has been replaced by two one-to-many relationships with the associative entity. Chapter 2 Copyright © 2014 Pearson Education, Inc. 42 Figure 2-13c An associative entity – bill of materials structure This could just be a relationship with attributes…it’s a judgment call. Chapter 2 Copyright © 2014 Pearson Education, Inc. 43 Figure 2-18 Cardinality constraints in a ternary relationship Chapter 2 Copyright © 2014 Pearson Education, Inc. 44 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 Copyright © 2014 Pearson Education, Inc. 45 Figure 2-21 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 Copyright © 2014 Pearson Education, Inc. 46 All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. Printed in the United States of America. Copyright © 2014 Pearson Education, Inc. 47