CPSC 421 Database Management Systems. Lecture 7: More on Database Design

Similar documents
Conceptual Design Using the Entity-Relationship (ER) Model

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

Review: Participation Constraints

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

Lecture 6. SQL, Logical DB Design

Introduction to normalization. Introduction to normalization

Normalization in OODB Design

IT2305 Database Systems I (Compulsory)

Data Modeling. Database Systems: The Complete Book Ch ,

The 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.

Fundamentals of Database System

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

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

IT2304: Database Systems 1 (DBS 1)

Designing a Database Schema

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

2. Conceptual Modeling using the Entity-Relationship Model

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

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

SQL NULL s, Constraints, Triggers

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

Databases What the Specification Says

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

CIS 631 Database Management Systems Sample Final Exam

Lecture 12: Entity Relationship Modelling

Normal Form vs. Non-First Normal Form

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

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

Databases and BigData

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

Database Design Process

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

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

CSC 742 Database Management Systems

LiTH, Tekniska högskolan vid Linköpings universitet 1(7) IDA, Institutionen för datavetenskap Juha Takkinen

The Entity-Relationship Model

Lecture 4: More SQL and Relational Algebra

Relational Database Concepts

Fundamentals of Database Design

Chapter 2: Entity-Relationship Model

Foundations of Information Management

IV. The (Extended) Entity-Relationship Model

DATABASE MANAGEMENT SYSTEMS. Question Bank:

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

ER modelling, Weak Entities, Class Hierarchies, Aggregation

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

Using Temporary Tables to Improve Performance for SQL Data Services

Physical Database Design Process. Physical Database Design Process. Major Inputs to Physical Database. Components of Physical Database Design

SQL Data Definition. Database Systems Lecture 5 Natasha Alechina

Entity Relationship Diagram

Database Design and Normalization

Foundations of Information Management

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

XV. The Entity-Relationship Model

Introduction to Computing. Lectured by: Dr. Pham Tran Vu

Database Design Process

Graham Kemp (telephone , room 6475 EDIT) The examiner will visit the exam room at 15:00 and 17:00.

AVOIDANCE OF CYCLICAL REFERENCE OF FOREIGN KEYS IN DATA MODELING USING THE ENTITY-RELATIONSHIP MODEL

CPS352 Database Systems: Design Project

A Tool for Generating Relational Database Schema from EER Diagram

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

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

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

Chapter 10. Functional Dependencies and Normalization for Relational Databases. Copyright 2007 Ramez Elmasri and Shamkant B.

Theory of Relational Database Design and Normalization

TIM 50 - Business Information Systems

Information Systems Analysis and Design CSC John Mylopoulos Database Design Information Systems Analysis and Design CSC340

Exercise 1: Relational Model

Chapter 10 Functional Dependencies and Normalization for Relational Databases

ComponentNo. C_Description UnitOfMeasure. C_Quantity

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

Introduction This document s purpose is to define Microsoft SQL server database design standards.

Chapter 10. Functional Dependencies and Normalization for Relational Databases

Doing database design with MySQL

EECS 647: Introduction to Database Systems

Schema Design and Normal Forms Sid Name Level Rating Wage Hours

Temporal Features in SQL standard

The Entity-Relationship Model

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

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

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

- Eliminating redundant data - Ensuring data dependencies makes sense. ie:- data is stored logically

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

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

Designing Databases. Introduction

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

Below is a table called raw_search_log containing details of search queries. user_id INTEGER ID of the user that made the search.

City University of Hong Kong. Information on a Course offered by Department of Computer Science with effect from Semester A in 2014 / 2015

Database Management Systems

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

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

Ken Goldberg Database Lab Notes. There are three types of relationships: One-to-One (1:1) One-to-Many (1:N) Many-to-Many (M:N).

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

A Comparative Analysis of Entity-Relationship Diagrams 1

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

Chapter 6: Physical Database Design and Performance. Database Development Process. Physical Design Process. Physical Database Design

Theory of Relational Database Design and Normalization

Announcements. SQL is hot! Facebook. Goal. Database Design Process. IT420: Database Management and Organization. Normalization (Chapter 3)

Transcription:

CPSC 421 Database Management Systems Lecture 7: More on Database Design * Some material adapted from R. Ramakrishnan, L. Delcambre, and B. Ludaescher Today s Agenda More on Database Design Ternary relationships Entities versus Attributes Converting ER diagrams to relational schemas Quiz Start on normalization (introduction) CPSC 421, 2009 2 1

Ternary versus Binary Relationships Product Supplier Product Provides Supplier Supplies Requires Authorizes These two schemas are not equivalent! When would we use a ternary relationship set? When would we use three binary relationship sets? CPSC 421, 2009 3 Ternary versus Binary Relationships Product Supplier Product Provides Supplier Supplies Requires Authorizes This ternary relationship set means that a supplier must be authorized to supply a particular part to a particular project For example office depot can supply printer paper to project 1 office max can supply paper clips to project 115 but this does not imply office max can supply paper clips to 1 CPSC 421, 2009 4 2

Ternary versus Binary Relationships Product Supplier pencils office depot paper office max Supplies supplies project 1 supplies project 115 This ternary relationship set means that a supplier must be authorized to supply a particular part to a particular project For example office depot can supply printer paper to project 1 office max can supply paper clips to project 115 but this does not imply office max can supply paper clips to 1 CPSC 421, 2009 5 Ternary versus Binary Relationships Product Supplier Product Provides Supplier Supplies Requires Authorizes The three binary relationship sets each represent something distinct a supplier can be provide certain products (office max can provide pencils) a project can require certain products (1 requires pencils) a supplier can be authorized to use a certain supplier (1 is authorized to use office max) therefore, we might assume that office-max supplies pencils to 1 CPSC 421, 2009 6 3

Ternary versus Binary Relationships pencils provides Product Provides Supplier pencils provides office max office depot Requires Authorizes Providing, Authorizing, and Requiring do not imply supplying!!! paper provides pencils requires office depot authorizes office max authorizes office depot project 1 project 1 project 1 The three binary relationship sets each represent something distinct a supplier can be provide certain products (office max can provide pencils) a project can require certain products (1 requires pencils) a supplier can be authorized to use a certain supplier (1 is authorized to use office max) therefore, we might assume that office-max supplies pencils to 1 CPSC 421, 2009 7 Duality: Entity vs Value / Attribute vs Relationship num name due ssn name office Assignment Should office be an attribute of? Should office be a separate Entity Set? Most attributes can be promoted to an Entity Set and some Entities can be demoted to an attribute value This is one reason why there are so many different ways to design a schema CPSC 421, 2009 8 4

Duality: Entity vs Value / Attribute vs Relationship num name due ssn name num Assignment Assigned Office Office What are some reasons to model Office as an Entity Set? An employee can have more than one office There are other attributes of Office Office needs to participate in other relationship sets E.g., a relationship set connecting to furniture or telephones or network drops (located in the office) CPSC 421, 2009 9 Duality: Entity vs Value / Attribute vs Relationship num name due ssn name num size Assignment Assigned Office Office Located In type id Furniture Example when Office should be an entity set Office needs to participate in other relationship sets CPSC 421, 2009 5

Key Constraints as Described in the Textbook code name Manager Department code name 0..1 Manager Department Limiting participation in a relationship set to at most 1 entity Same as maximum multiplicity of 1 (x..1 in our notation) Each Department has at most one manager, according to the key constraint on Manages CPSC 421, 2009 11 Participation Constraints as in the Textbook Manager Manager 1..1 Every entity must participate in a relationship set Same as maximum multiplicity of 1 (1..x in our notation) Each must have at least one manager, according to the participation constraint on CPSC 421, 2009 6

Translating ERDs to Relational Schemas (ssn, name, lot, home-dept) Member(ssn, number) Department(code, name, manager) (number, name, startdate, enddate, budget, sponsor) This mapping assumes: s have one home department Departments have one manager s can parvcipate in many projects CPSC 421, 2009 13 Translating Relationship Sets For relationship sets we must indicate which entities we want to have connected e.g., we need the key values for employees and teams stored together to represent the relationship these could be in an existing table that represents one of the involved entities or in a new table introduced explicitly to represent the relationship num name start Team Member Team CPSC 421, 2009 14 7

Translating Relationship Sets For many-to-many relationship sets create a new table to represent the relationship For example: TeamMember(ssn, num) with two foreign keys (ssn, name, lot) Team(num, name, start) num name start 0..* Team 0..* Member Team CPSC 421, 2009 15 Translating Relationship Sets For one-to-many relationship sets introduce a foreign key to the many side of the relationship For example: Department(code, name) (ssn, name, lot, homedept) where homedept is a foreign key (referencing Department code) code name 0..* 0..1 Home Department CPSC 421, 2009 16 8

Translating Relationship Sets Alternatively, for one-to-many relationship sets Create a new table (like in many-to-many relationships) For example: HomeDepartment(ssn, code) note that ssn is the key Department(code, name) (ssn, name, lot) What are the tradeoffs between these approaches? code name 0..* 0..1 Home Department CPSC 421, 2009 17 Translating Relationship Sets Creating a new table from one-to-many relationships requires a Join to obtain an employees home department Each ssn value associated with a home department is stored twice in the and HomeDepartment table code name 0..* 0..1 Home Department CPSC 421, 2009 18 9

Translating Relationship Sets When a many-many relationship set has attributes Put them in the table that represents the relationship Team(num, ssn, role, startdate, enddate) (num, name, startdate, enddate, budget) (ssn, name, office) role startdate enddate 0..* 0..* Team num name startdate enddate budget CPSC 421, 2009 19 Translating Relationship Sets When a one-to-many relationship set has attributes Add them to the table where the relationship is represented Department(code, name, manager, startdate, enddate) (ssn, name, office, dept) or else Manager(code, ssn, startdate, enddate) startdate enddate code name 0..1 0..1 Manager Department CPSC 421, 2009 20

Participation Constraints in SQL We can require any table to be in a binary relationship using a foreign key by constraining the attribute to be NOT NULL CREATE TABLE Department ( code INTEGER, name VARCHAR(20), manager VARCHAR(9) NOT NULL, startdate DATE, enddate DATE, PRIMARY KEY (code), FOREIGN KEY (manager) REFERENCES, ON DELETE NO ACTION ); startdate enddate code name 1..1 0..1 Manager Department CPSC 421, 2009 21 Translating Weak Entity Sets Weak entity sets and identifying relationship sets are translated into a single table We must include the key of the strong entity as a foreign key The key for the table is the key of the strong entity plus the partial key When the owner entity is deleted, all owned weak entities must also be deleted CREATE TABLE Policy ( name VARCHAR(20), age INTEGER, ssn VARCHAR(11) NOT NULL, PRIMARY KEY (name, ssn) REFERENCES, ON DELETE CASCADE ); CPSC 421, 2009 22 11

Summary of the Translation [Elmasri & Navathe] 1. Create table and choose key for each entity set, include (single-valued) attributes 2. Create table for each weak entity set, include (single-valued) attributes and the key of the owner as a foreign key. Set the key as foreign key plus partial key. 3. For each one-to-one relationship set, add a foreign key to one of the entity sets involved in the relationship (a foreign key to the other entity in the relationship)* 4. For each one-to-many relationship set, add a foreign key to the entity set on the many side of the relationship (to reference the entity set on the one side of the relationship)* 5. For each many-to-many relationship set, create a new table. Include a foreign key for each participant entity set in the relationship set. The key for the new table is the set of all such foreign keys. 6. For each multi-valued attribute, construct a separate table. Repeat the key for the entity in this new table. It serves as both a key for this table and a foreign key to the original table for the entity. * Unless the relationship set has attributes, in which case create a new table for the relationship set CPSC 421, 2009 23 Normalization Normalization is the process of replacing a table with two or more tables For example, consider this schema: Versus these: Emp EID A01 A A13 A03 Name Ali Eric Eric Anne DeptID EmpDept EID Name A01 Ali A Eric A13 Eric A03 Anne Dept DeptID Dept Name HR DeptName HR Which schema do you think is better? Why? CPSC 421, 2009 24

Normalization Issues The EmpDept schema combines two different concepts information, together with Department information What is wrong with this? If we separate the two concepts we could save space but some queries would run slower (Joins) If we combine the two ideas we have redundancy but some queries would run faster (no Joins) So we have a tradeoff Redundancy has a side effect: anomalies CPSC 421, 2009 25 Types of Anomalies EmpDept EID Name A01 Ali A Eric A13 Eric A03 Anne Dept DeptName HR Update Anomaly : If the department changes its name, we must change multiple rows in EmpDept Insertion Anomaly : If a department has no employees, where do we store its id and name? Deletion Anomaly : If A quits, the information about the HR department will be lost These are in addition to redundancy in general For example, the department name is stored multiple times CPSC 421, 2009 26 13

Using NULL values EmpDept EID Name A01 Ali NULL NULL A13 Eric A03 Anne Dept DeptName HR Using NULL values can help insertion and deletion anomalies But NULL values have their own issues They make aggregate operators harder to use Not always clear what NULL means (does not apply, unknown, know but missing, see Melton podcast!) May need outer joins instead of ordinary joins In this case, EID is a primary care, and so it cannot contain a NULL value! They don t address update anomalies or redundancy issues CPSC 421, 2009 27 Decomposition Emp EID A01 A A13 A03 Name Ali Eric Eric Anne DeptID Dept DeptID Name HR The normalized version of EmpDept Normalization involves decomposing (partitioning) the table into separate tables Check to see if redundancy still exists ( repeat) The key to understanding when and how to decompose schemas is through functional dependencies which generalizes the notion of keys CPSC 421, 2009 28 14

For Thursday Reading Ch 3: 3.5 Be sure to understand: Binary versus ternary relationships How to convert from ER models to relational schemas Next time we will talk about normalization Homework proposal due on Thursday Homework 3 due next Thursday CPSC 421, 2009 29 15