Constraints in ER Models. Types of Constraints. Keys. CS 317, Fall 2007



Similar documents
Entity-Relationship Model. Purpose of E/R Model. Entity Sets

BİL 354 Veritabanı Sistemleri. Entity-Relationship Model

Data Analysis 1. SET08104 Database Systems. Napier University

Unit 2.1. Data Analysis 1 - V Data Analysis 1. Dr Gordon Russell, Napier University

Database Design Overview. Conceptual Design ER Model. Entities and Entity Sets. Entity Set Representation. Keys

Conceptual Design Using the Entity-Relationship (ER) Model

COMP 378 Database Systems Notes for Chapter 7 of Database System Concepts Database Design and the Entity-Relationship Model

Data Modeling. Database Systems: The Complete Book Ch ,

CSC 742 Database Management Systems

2. Supports eæcient access to very large amounts

The Relational Model. Ramakrishnan&Gehrke, Chapter 3 CS4320 1

Review: Participation Constraints

The Relational Model. Why Study the Relational Model? Relational Database: Definitions. Chapter 3

The Entity-Relationship Model

Foundations of Information Management

Lecture 6. SQL, Logical DB Design

Chapter 3. Data Modeling Using the Entity-Relationship (ER) Model

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #5: En-ty/Rela-onal Models- - - Part 1

Chapter 2: Entity-Relationship Model. Entity Sets. " Example: specific person, company, event, plant

2. Conceptual Modeling using the Entity-Relationship Model

IV. The (Extended) Entity-Relationship Model

Databases Model the Real World. The Entity- Relationship Model. Conceptual Design. Steps in Database Design. ER Model Basics. ER Model Basics (Contd.

Chapter 2: Entity-Relationship Model

We know how to query a database using SQL. A set of tables and their schemas are given Data are properly loaded

Lecture 12: Entity Relationship Modelling

Data Modeling: Part 1. Entity Relationship (ER) Model

CS 377 Database Systems. Database Design Theory and Normalization. Li Xiong Department of Mathematics and Computer Science Emory University

Databases and BigData

not necessarily strictly sequential feedback loops exist, i.e. may need to revisit earlier stages during a later stage

THE ENTITY- RELATIONSHIP (ER) MODEL CHAPTER 7 (6/E) CHAPTER 3 (5/E)

Fundamentals of Database System

The Entity-Relationship Model

Chapter 2: Entity-Relationship Model. E-R R Diagrams

Lesson 8: Introduction to Databases E-R Data Modeling

Outline. Data Modeling. Conceptual Design. ER Model Basics: Entities. ER Model Basics: Relationships. Ternary Relationships. Yanlei Diao UMass Amherst

Entity-Relationship Model

Chapter 7 Data Modeling Using the Entity- Relationship (ER) Model

A brief overview of developing a conceptual data model as the first step in creating a relational database.

Foundations of Information Management

The Entity-Relationship Model

14 Databases. Source: Foundations of Computer Science Cengage Learning. Objectives After studying this chapter, the student should be able to:

XV. The Entity-Relationship Model

Database Design Process

Entity Relationship Diagram

ER modelling, Weak Entities, Class Hierarchies, Aggregation

Database Design. Marta Jakubowska-Sobczak IT/ADC based on slides prepared by Paula Figueiredo, IT/DB

OVERVIEW 1.1 DATABASE MANAGEMENT SYSTEM (DBMS) DEFINITION:-

CSCI-GA Database Systems Lecture 7: Schema Refinement and Normalization

DATABASE DESIGN. - Developing database and information systems is performed using a development lifecycle, which consists of a series of steps.

three Entity-Relationship Modeling chapter OVERVIEW CHAPTER

A. TRUE-FALSE: GROUP 2 PRACTICE EXAMPLES FOR THE REVIEW QUIZ:

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

Converting E-R Diagrams to Relational Model. Winter Lecture 17

Concepts of Database Management Seventh Edition. Chapter 6 Database Design 2: Design Method

Databasesystemer, forår 2005 IT Universitetet i København. Forelæsning 3: Business rules, constraints & triggers. 3. marts 2005

Database Design Process

Exercise 1: Relational Model

Modern Systems Analysis and Design

LOGICAL DATABASE DESIGN


7.1 The Information system

Databases -Normalization III. (N Spadaccini 2010 and W Liu 2012) Databases - Normalization III 1 / 31

Database Design. Database Design I: The Entity-Relationship Model. Entity Type (con t) Chapter 4. Entity: an object that is involved in the enterprise

Limitations of E-R Designs. Relational Normalization Theory. Redundancy and Other Problems. Redundancy. Anomalies. Example

Data Modeling Basics

Chapter 5: Logical Database Design and the Relational Model Part 2: Normalization. Introduction to Normalization. Normal Forms.

Information Systems Modelling Information Systems I: Databases

How To Find Out What A Key Is In A Database Engine

Introduction to Database Systems CS4320/CS5320. CS4320/4321: Introduction to Database Systems. CS4320/4321: Introduction to Database Systems

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 7

Why & How: Business Data Modelling. It should be a requirement of the job that business analysts document process AND data requirements

Winter Winter

Database Constraints and Design

Bridge from Entity Relationship modeling to creating SQL databases, tables, & relations

Database Design. Goal: specification of database schema Methodology: E-R Model is viewed as a set of

CS411 Database Systems

Why Is This Important? Schema Refinement and Normal Forms. The Evils of Redundancy. Functional Dependencies (FDs) Example (Contd.)

Database Design and the E-R Model

There are five fields or columns, with names and types as shown above.

Normalization. Reduces the liklihood of anomolies

Relational model. Relational model - practice. Relational Database Definitions 9/27/11. Relational model. Relational Database: Terminology

Once the schema has been designed, it can be implemented in the RDBMS.

Carnegie Mellon Univ. Dept. of Computer Science Database Applications. Overview - detailed. Goal. Faloutsos CMU SCS

Normalization in OODB Design

Database Design. Adrienne Watt. Port Moody

DATABASE INTRODUCTION

Introduction to Database Systems. Normalization

CS143 Notes: Normalization Theory

BCA. Database Management System

The E-R èentity-relationshipè data model views the real world as a set of basic objects èentitiesè and

Lecture Notes INFORMATION RESOURCES

AS LEVEL Computer Application Databases

Course Title: Microsoft Access Basic Duration: 12 hours

Review Entity-Relationship Diagrams and the Relational Model. Data Models. Review. Why Study the Relational Model? Steps in Database Design

Database Design Process. Databases - Entity-Relationship Modelling. Requirements Analysis. Database Design

Introduction to Triggers using SQL

Fundamentals of Database Design

Intermediate SQL C H A P T E R4. Practice Exercises. 4.1 Write the following queries in SQL:

SQL DDL. DBS Database Systems Designing Relational Databases. Inclusion Constraints. Key Constraints


Transcription:

Constraints in ER Models CS 317, Fall 2007 Types of Constraints Keys are attributes or sets of attributes that uniquely identify an entity within its entity set. Single-value constraints require that a value be unique in certain contexts. Referential integrity constrains require that a value referred to actually exists in the database. Domain constraints specify what set of values an attribute can take. General constraints are arbitrary constraints that should hold in the database. Constraints are part of the schema of a database. Keys A key is a set of attributes for one entity set such that no two entities in this set agree on all the attributes of the key. In the case of multi attribute keys, it is allowed for two entities to agree on some, but not all, of the key attributes. We must designate a key for every entity set. 1

Keys in E/R Diagrams Underline the key attribute(s). An Entity set E can have multiple keys. We usually designate one as the primary key. In an subclass hierarchy, only the root entity set has a key, and it must serve as the key for all entities in the hierarchy. Example: is Key for Beers Beers manf isa color Ales Example: a Multi-attribute Key dept number hours room Courses Note that hours and room could also serve as a key, but we must select only one key. 2

Our Academic Example Weak Entity Sets Occasionally, entities of an entity set need help to identify them uniquely. Entity set E is said to be weak if in order to identify entities of E uniquely, we need to follow one or more many-one relationships from E and include the key of the related entities from the connected entity sets. Example is almost a key for football players, but there might be two with the same. number is certainly not a key, since players on two teams could have the same number. But number, together with the team related to the player by Plays-on should be unique. 3

In E/R Diagrams number Players Playson Teams Double diamond for supporting many-one relationship. Double rectangle for the weak entity set. Another Example Weak Entity-Set Rules A weak entity set has one or more many-one relationships to other (supporting) entity sets. Not every many-one relationship from a weak entity set need be supporting. The key for a weak entity set is its own underlined attributes and the keys for the supporting entity sets. E.g., (player) number and (team) is a key for Players in the previous example. 4

Summarizing Weak Entity Sets If E is a weak entity set, its key consists of 1. Zero or more of its own attributes and 2. Key attributes from supporting relationships for E. A relationship R from a weak entity set E to F is supporting if 1. R is a binary, many-one relationship from E to F, 2. R has referential integrity from E to F. How does F help E? F supplies its key attributes to define E s key. If F is itself a weak entity set, some of its key attributes come to from entity sets to which F is connected by supporting relationships. Design Tips for E/R Modeling 1. Avoid redundancy. 2. Limit the use of weak entity sets. 3. Don t use an entity set when an attribute will do. 4. Confirm the correct cardinality and optionality of a relationship 5.. 5

Avoiding Redundancy Redundancy occurs when we say the same thing in two or more different ways. Redundancy wastes space and (more importantly) encourages inconsistency. The two instances of the same fact may become inconsistent if we change one and forget to change the other. Example addr Beers ManfBy Manfs Good or bad? GOOD! This design gives the address of each manufacturer exactly once. How about now? addr Beers ManfBy Manfs manf Bad! This design states the manufacturer of a beer twice: as an attribute and as a related entity. 6

And now? manf manfaddr Beers TERRIBLE DESIGN! This design repeats the manufacturer s address once for each beer and loses the address if there are temporarily no beers for a manufacturer => TERRIBLE Entity Sets Versus Attributes An entity set should satisfy at least one of the following conditions: It is more than the of something; it has at least one nonkey attribute. or It is the many in a many-one or many-many relationship. Example addr Beers ManfBy Manfs GOOD! Manfs deserves to be an entity set because of the non-key attribute addr. Beers deserves to be an entity set because it is the many of the many-one relationship ManfBy. 7

Example manf Beers GOOD Design. There is no need to make the manufacturer an entity set, because we record nothing about manufacturers besides their. Example Beers ManfBy Manfs BAD! Since the manufacturer is nothing but a, and is not at the many end of any relationship, it should not be an entity set. Entity vs. Attribute Should address be an attribute of Employees or an entity (connected to Employees by a relationship)? If we have several addresses per employee, address must be an entity (since attributes cannot be set-valued). If the structure (city, street, etc.) is important, e.g., we want to retrieve employees in a given city, address must be modeled as an entity (since attribute values are atomic). 8

Entity vs. Attribute - Warning Do not introduce un-necessary entities (and complexity) if not needed for your application! Entity vs. Attribute from to d ssn lot did budget Employees Works_In4 Departments Entity vs. Attribute Does Works_In4 allow an employee to work in a department for two or more periods??? from to d ssn lot did budget Employees Works_In4 Departments 9

Entity vs. Attribute (Contd.) Works_In4 does not allow an employee to work in a department for two or more periods. If we are not allowed to use multi-valued attributes? ssn lot from to did d budget Employees Works_In4 Departments Entity vs. Attribute (Contd.) What do we do? Similar to the problem of wanting to record several addresses for an employee: We want to record several values of the descriptive attributes for each instance of this relationship. Accomplished by introducing new entity set, Duration. Entity vs. Attribute ssn lot from to did d budget Employees Works_In4 Departments ssn lot did d budget Employees Works_In4 Departments from Duration to 10

Don t Overuse Weak Entity Sets Beginning database designers often doubt that anything could be a key by itself. They make all entity sets weak, supported by all other entity sets to which they are linked. In reality, we usually create unique ID s for entity sets. Examples include social-security numbers, automobile VIN s etc. When Do We Need Weak Entity Sets? The usual reason is that there is no global authority capable of creating unique ID s. Football Example: it is unlikely that there could be an agreement to assign unique player numbers across all football teams in the world. Converting Multi-way to Binary 11

Details Create an entity in the new Evaluation entity set for each instance (row) in the ternary Evaluation relationship. In the Student of relationship, relate each entity in the Evaluation entity set with the corresponding student entity. The multiplicity of Student of is many-to-one from Evaluation to Student. Without new attribute Can we perform the conversion without introducing a new, artificial attribute? Evaluation must be a weak entity set. What is/are the supporting relationships? All the relationships connected to Evaluation. The key for evaluation is composed of the key attributes of Students, Courses, and Professors. 12

Single Value Constraints There is at most one value in a given role. 1. Each attribute of an entity set has a single value. If the value is missing, we can invent a null value. E/R models cannot represent the requirement that an attribute cannot have a null value. 2. A many-one relationship implies a single value constraint. Referential Integrity Constraint Asserts that a value must exist in a given context. Usually used in the context of relationships. Example: Many-one Advises relationship between Students and Professors. Many-one requirement says that no student may have more than advising professor. Referential integrity constraint says that each student must have exactly one advising professor and that professor must be present in the database. More examples Each department has at most one chairperson who is its head (there are times when a department may not have a chairperson). Each chairperson can be the head of at most one department and this department must exist in the database. 13

Enforcing Referential Integrity We forbid the deletion of a referenced entity (e.g., a professor) until the professor advises no students. Conversely, we require that if we delete a referenced entity, we delete all entities that reference it. When we insert a student entity, we must specify an existing professor entity connected to the student by the Advises relationship. Deriving Relationship Parameters One course is studied by how many students? Answer = `zero or more'. This gives us the degree at the `student' end. The answer `zero or more' needs to be split into two parts. The `more' part means that the cardinality is `many'. The `zero' part means that the relationship is `optional'. If the answer was `one or more', then the relationship would be `mandatory'. 14

Contd. One student studies how many courses? Answer = `One' This gives us the degree at the `course' end of the relationship. The answer `one' means that the cardinality of this relationship is 1, and is `mandatory' If the answer had been `zero or one', then the cardinality of the relationship would have been 1, and be `optional'. Redundant relationships Some ER diagrams end up with a relationship loop -check to see if it is possible to break the loop without losing info Given three entities A, B, C, where there are relations A-B, B-C, and C-A, check if it is possible to navigate between A and C via B. If it is possible, then A-C was a redundant relationship. Splitting n:m Relationships A many to many relationship in an ER model is not necessarily incorrect. But they can be replaced using an intermediate entity. This should only be done where: the m:n relationship hides an entity the resulting ER diagram is easier to understand. 15

16