Handling Many to Many Relationships. 2 Handling Many:Many Relationships Aims: To explain why M:M relationships cannot be implemented in relational database.

Slides:



Advertisements
Similar presentations
Chapter 5 Normalization of Database Tables
Advertisements

Data Modeling. What are you keeping track of? You begin to develop a database by deciding what you are going to keep track of. Each thing that you are.
HNDComputing – DeMontfort University  DeMontfort University 2011 Handling Many to Many relationships wk5 Database Design ConceptsDatabase Design Concepts.
BUSINESS DRIVEN TECHNOLOGY Plug-In T4 Designing Database Applications.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 4 Entity Relationship (ER) Modeling.
Database Systems: Design, Implementation, and Management Tenth Edition
Normalisation Ensuring data integrity in database design 1.
Logical Data Modeling Review Lecture for University of Agder, Grimstad DAT202 Databaser (5.5.11) Judith Molka-Danielsen
Normalization of Database Tables
The Relational Database Model:
Database Design Concepts Lecture 19 Term 2 week 8 Worked example of normalisation.
Normalization of Database Tables
Database Design Concepts INFO1408 Term 2 week 1 Data validation and Referential integrity.
The Relational Database Model. 2 Objectives How relational database model takes a logical view of data Understand how the relational model’s basic components.
Chapter 5 Normalization of Database Tables
Concepts of Database Management Seventh Edition
Chapter 4 Entity Relationship (ER) Modeling
Database Systems: Design, Implementation, and Management Tenth Edition
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 5 Normalization of Database Tables.
CS424 PK, FK, FD Normalization Primary and Foreign Keys Primary and foreign keys are the most basic components on which relational theory is based. Primary.
Lecture 9 Handling Many to Many Relationships INFO 1408 Database Design Concepts.
3 1 Chapter 3 The Relational Database Model Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Database Design Concepts Info1408
Database – Part 2a Dr. V.T. Raja Oregon State University.
Lecture 9 Handling Many to Many Relationships INFO 1408 Database Design Concepts.
3 The Relational Model MIS 304 Winter Class Objectives That the relational database model takes a logical view of data That the relational model’s.
Modelling Techniques - Normalisation Description and exemplification of normalisation.Description and exemplification of normalisation. Creation of un-normalised.
CREATE THE DIFFERENCE Normalisation (special thanks to Janet Francis for this presentation)
Chapter 1 Overview of Database Concepts Oracle 10g: SQL
1 Chapter 1 Overview of Database Concepts. 2 Chapter Objectives Identify the purpose of a database management system (DBMS) Distinguish a field from a.
Chapter 5 Entity Relationship (ER) Modelling
Data Modelling – ERD Entity Relationship Diagram’s Entity Relationship Diagrams and how to create them. 1.
Mapping from Data Model (ERD) to Relational Model
Database Systems: Design, Implementation, and Management Tenth Edition
5 1 Chapter 5 Normalization of Database Tables Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Database Systems: Design, Implementation, and Management Ninth Edition Chapter 6 Normalization of Database Tables.
1 DATABASE SYSTEMS DESIGN IMPLEMENTATION AND MANAGEMENT INTERNATIONAL EDITION ROB CORONEL CROCKETT Chapter 7 Normalisation.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 5 Normalization of Database.
Copyright 2008 McGraw-Hill Ryerson 1 TECHNOLOGY PLUG-IN T5 DESIGNING DATABASE APPLICATIONS.
1.  An introduction to data modelling  The purpose of data modelling  Modelling data relationships 2.
1 Relational Databases and SQL. Learning Objectives Understand techniques to model complex accounting phenomena in an E-R diagram Develop E-R diagrams.
Concepts of Database Management Sixth Edition Chapter 6 Database Design 2: Design Method.
Chapter 4 Entity Relationship (ER) Modeling.  ER model forms the basis of an ER diagram  ERD represents conceptual database as viewed by end user 
3 & 4 1 Chapters 3 and 4 Drawing ERDs October 16, 2006 Week 3.
Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.
M1G Introduction to Database Development 4. Improving the database design.
Btec National - Advanced Databases 1 Advanced Databases Entity Relationship Diagrams.
Database Systems: Design, Implementation, and Management Ninth Edition Chapter 4 Entity Relationship (ER) Modeling.
Normalization of Database Tables
The relational model A data model (in general) : Integrated collection of concepts for describing data (data requirements). Relational model was introduced.
Mr C Johnston ICT Teacher
Understand Primary, Foreign, and Composite Keys Database Administration Fundamentals LESSON 4.2.
* Database is a group of related objects * Objects can be Tables, Forms, Queries or Reports * All data reside in Tables * A Row in a Table is a record.
Logical Database Design and the Relational Model.
Btec National - IT SYSTEMS ANALYSIS AND DESIGN 1 IT Systems Analysis and Design Entity Relationship Diagrams.
Chapter 8: Object-Relational Modeling Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra, Joseph S. Valacich, Jeffrey A. Hoffer.
1 10 Systems Analysis and Design in a Changing World, 2 nd Edition, Satzinger, Jackson, & Burd Chapter 10 Designing Databases.
5 1 Chapter 5 Normalization of Database Tables Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Department of Mathematics Computer and Information Science1 CS 351: Database Management Systems Christopher I. G. Lanclos Chapter 4.
Normalisation Unit 6: Databases. Just to recap  What is an Entity  What is an Attribute?
Lecture # 14 Chapter # 5 The Relational Data Model and Relational Database Constraints Database Systems.
IT 5433 LM3 Relational Data Model. Learning Objectives: List the 5 properties of relations List the properties of a candidate key, primary key and foreign.
Tables and Their Characteristics
Data Modelling Introduction
Entity relationship diagrams
MIS2502: Data Analytics Relational Data Modeling
Chapter 4.1 V3.0 Napier University Dr Gordon Russell
Mapping an ERD to a Relational Database
INSTRUCTOR: MRS T.G. ZHOU
Normalisation 1 Unit 3.1 Dr Gordon Russell, Napier University
Presentation transcript:

Handling Many to Many Relationships

2 Handling Many:Many Relationships Aims: To explain why M:M relationships cannot be implemented in relational database systems To demonstrate how to decompose many to many (M:M) relationships Introduce other types of relationships

Entities and Tables Each entity will become a table in the database Each table will have several attributes i.e. A Customer would have, as a minimum ‘forename’, ‘surname’, ‘address’ attributes Each row in every table will be unique 3

Row Uniqueness To ensure that each row is unique we add a primary key to each table A primary key may be a single attribute in each table i.e. customer ID or it could be composed of several attributes in a table – this is know as a composite primary key 4

Primary Keys and Foreign keys Relationships between entities identified in an ERD are implemented in relational databases through the primary keys To implement the relationships we ‘post’ the primary key from one table into the other tables – these are known as foreign keys The only data that is ever repeated in the tables is the primary key as a foreign key in another table 5

Normalisation To maintain the integrity (the correctness) of the data we apply normalisation techniques to the database There are several levels of normalisation but is sufficient most database applications are normalised to 3 rd Normal Form For the purposes of this module we will cover 1 st, 2 nd and 3 rd normal forms 6

1 st Normal Form (1NF) To be in 1NF each attribute value will contain only atomic values The attribute could be composed of several component parts but the value is seen by the DBMS as a single value For example, a customer’s address – 22, High Road 7

1NF and M:M Relationships To create a relationship between two tables we ‘post’ the primary key from one table into the other table as a foreign key The data types in each table in the relationship must be the same i.e. customerID = Integer Also the value of the foreign key of the ‘posted’ table must exist as a primary key value in the ‘posting’ table 8

1NF and M:M Relationships The problem with M:M relationships is deciding which table is the ‘provider’ and which is the ‘recipient’ For example, the ERD below has been drawn for an ordering system 9

1NF and M:M Relationships The relationship reads: An Order must be for at least 1 but could be for many Parts A Part may be used on many orders 10

1NF and M:M Relationships If we decided that the Parts table will be the provider of the primary key and the Orders table will contain the foreign key then the relationship would be implemented using the same data type i.e. PK = PartID (integer) in the Part table FK =PartID (integer) in the Orders table 11

1NF and M:M Relationships OrderNoOrderDatePartID 1012/10/ /10/20072, 3, /10/20071, 4 12 PartIDDescription 1Wobbly Widget 2Dog-eared Brob 310 mm Dowel 4Bevel Gear PartID exists for OrderNo 10 but not for orders 11 & 12 as they are ‘sets’ of integers

1NF and M:M Relationships Multiple values (or sets) cannot be entered as foreign key values as they do not exist in the same format in the Part table It would violate the referential integrity of the data The same problem would exist if we tried to post the orderNo from the Orders table to the Parts table as a foreign key 13

1NF and M:M Relationships The same problem would also exist if the data types were text i.e. 14 OrderNoOrderDatePartID 1012/10/2007PT /10/2007PT123, PT234, PTC /10/2007PT234, PTC245 PartIDDescription PT123Wobbly Widget PT234Dog-eared Brob PTB12310 mm Dowel PTC245Bevel Gear

Decomposition of M:M Relationships The solution to the problem is to decompose the entities by introducing an intermediary table – see below The new tables multiplicity is now the Many end of the relationship and the original entities multiplicity becomes ‘1’ The optionality of the new entity is mandatory but the optionality of the original entities remains as before 15

Decomposition of M:M Relationships The new entity, which will eventually become a table in the database would not have been identified in the original systems investigation but it is required to fulfil the business needs and to maintain the referential integrity of the data We always ‘post’ the primary keys from the ‘1’ end of the relationship to the ‘many’ end of the relationship 16

Decomposition of M:M Relationships – a New Entity 17 OrderNoOrderDate 1012/10/ /10/ /10/2007 PartIDDescription PT123Wobbly Widget PT234Dog-eared Brob PTB12310 mm Dowel PTC245Bevel Gear PartIDOrderNoQty PT PT PTB12311 PTC PT PTC Posting from Part to Order Line Posting from Order to Order Line Part Orderline Order

Other Solutions? Adding the intermediary table is the only correct solution to the problem of M:M relationships However, some database designers think that by adding extra columns is the answer 18

Adding Extra Columns? OrderNoOrderDatePartID-1PartID-2PartID /10/2007PT /10/2007PT123PT234PTC /10/2007PT234PTC The problem here is that the database designer does not know the maximum parts required for future orders and extra columns cannot be added by the user as and when needed It also introduces redundant data in the form of NULL values

Adding Extra Rows? Adding extra rows is not an option as we would be repeating primary key values which would violate the entity integrity rule whereby all rows are uniquely identified by the primary key It would also introduce redundant data i.e. dates 20 OrderNoOrderDatePartID 1012/10/2007PT /10/2007PT /10/2007PT /10/2007PTC /10/2007PT /10/2007PTC245

21 M:M Relationships A M:M relationship between 2 entity types must be decomposed into two 1:M relationships.

22 M:M Relationships StudentModule chooses MM Becomes ModuleStudent Module Choice makes is for M M 1 1

23 The Decomposition Rules AB r MM Becomes AB M M 1 1

24 Or - AB r MM Becomes AB M M 1 1

25 Naming Naming the new entity type and the new relationships is sometimes not easy Consider what it is representing If all else fails, concatenate/ join the names of the 2 original entity types (e.g. Student Module).

26 Exercise Decompose this M:M relationship to form two 1:M relationships: Assign the new entity and relationship types suitable names. Doctor Patient examines M M

27 Solution

Table Types When we have modelled our entities we could then design the tables by adding the attributes of the proposed table We describe the tables using table types whereby the table name is appended with an attribute list in parentheses The primary key is shown emboldened and underlined Foreign keys are shown in italics 28

Table Types cont. The table types for the following ERD could be: Customer (customerNo, surname, address…) Orders (orderNo, orderDate, customerNo…) The ellipses (…) denote other possible attributes 29

30 Identifiers We have seen that an entity must have an Identifier – Primary Key The new entity type created by decomposition needs an identifier Start with a composite of the Identifiers of the 2 original entity types Need to consider carefully whether this will uniquely identify every occurrence of the new entity type.

31 Identifiers cont. For the second example: Doctor(doctor#,.... ) Patient(patient#,... ) Appointment (Doctor#patient#,..) Is this a suitable identifier?.

32 Identifiers cont. To decide if an identifier is suitable: Think of some other attributes for the entity: Is one pair of doctor#, patient# values associated with just one value of each of these attributes?.

33 To decide if an identifier is suitable: Think of some other attributes for the entity: Is one pair of doctor#, patient# values associated with just one value of each of these attributes?. No

34 Could a patient see the same doctor more than once?

35 Could a patient see the same doctor more than once? Yes – So add date Appointment (doctor#,patient#date, … )

36 Could a patient see the doctor more than once in a day?

37 Could a patient see the doctor more than once in a day? Yes ( not common) so add time Appointment (doctor#,patient#date,time..)

38 This is getting a little complicated maybe we should add a new key field appointment number Appointment (AppointmentNo doctorNo, patientNo, date, time,..) Note patientNo and doctorNo are now foreign keys

39 Why Decompose? Student(studentNo, name,...) Module(moduleNo, description,...) How do we know which students are taking which modules?. We don ’ t StudentModule chooses MM Back to the first example Look at the original M:M relationship:

40 Why Decompose? cont. Decomposing gives us a new table: Student Module (studentNo, moduleNo, ) Is this a suitable identifier ? Now we can list which student has chosen which module.

41 Exercise Actor (actorNo, name,...) Play (playNo, title,...) Decompose this M:M relationship Assign the new entity type an appropriate name and think of some additional attributes for it Assign the new entity type a suitable identifier. Actor Play appears _in MM

42 Solution Actor (actorNo, name … ) Play ( playNo, name, writer, length … ) Production (actorNo, playNo, first_performance_date, director, venue/theatre_name... etc!)

43 Common Decomposition problem Many decomposition entities represent business transactions ( or pieces of paper) For example, booking, order etc They may be very difficult to name

44 Common decomposition problem- example Orderline (product#,order#, …) The orderline represents each line of the order

45 Other types of relationships Recursive relationships An individual entity can have a relationship with an entity of the same type

46 Another example- Estate agents It is possible to have more than one relationship between two entities

Exercise Write the table types for the following ERD 47

48 Summary We have looked at decomposition of m:m relationships. Discussed how to identify a unique identifier Introduced recursive relationships Introduced multiple relationships between entities

49 References Data Analysis for database Design By D R Howe