The Many-to-Many Relationship

Slides:



Advertisements
Similar presentations
More Diagramming & Practice with Relationship Modeling
Advertisements

Relational Database Example in Access - Order System Please use speaker notes for additional information!
The Many-to-Many Relationship Fearful concatenation of circumstances Daniel Webster.
What is a Database By: Cristian Dubon.
The Many-to-Many Relationship. A sales form The many-to-many relationship Create a third entity to map an m:m relationship –An associative entity The.
The Many-to-Many Relationship Fearful concatenation of circumstances Daniel Webster.
The Relational Database Model
CSE 190: Internet E-Commerce Lecture 10: Data Tier.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 6-1 COS 346 Day 8.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 6-1 COS 346 Day 7.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 6-1 David M. Kroenke’s Chapter Six: Transforming Data Models into Database.
Database Management Fall 2003 Midterm Review Chapters 1 & 2.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 6-1 David M. Kroenke’s Chapter Six: Transforming ER Models into Database.
The Relational Model Codd (1970): based on set theory Relational model: represents the database as a collection of relations (a table of values --> file)
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 6-1 David M. Kroenke’s Chapter Six: Transforming ER Models into Database.
Database – Part 2a Dr. V.T. Raja Oregon State University.
10/3/2000SIMS 257: Database Management -- Ray Larson Relational Algebra and Calculus University of California, Berkeley School of Information Management.
This presentation prepared for MIS 421 / MBA 575 at Western Washington University. Material in this presentation drawn from Richard T. Watson, Data Management:
Introduction –All information systems create, read, update and delete data. This data is stored in files and databases. Files are collections of similar.
Irwin/McGraw-Hill Copyright © 2000 The McGraw-Hill Companies. All Rights reserved Whitten Bentley DittmanSYSTEMS ANALYSIS AND DESIGN METHODS5th Edition.
Relational Theory and Design
The Many-to-Many Relationship Fearful concatenation of circumstances Daniel Webster.
Database Management Supplement 1. 2 I. The Hierarchy of Data Database File (Entity, Table) Record (info for a specific entity, Row) Field (Attribute,
* 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.
SQL constrains and keys. SORTED RESULTS Sort the results by a specified criterion SELECT columns FROM tables WHERE predicates ORDER BY column ASC/DESC;
Week 2 Lecture The Relational Database Model Samuel ConnSamuel Conn, Faculty Suggestions for using the Lecture Slides.
Quiz Which of the following is not a mandatory characteristic of a relation? Rows are not ordered (Not required) Each row is a unique There is a.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
MS SQL Create Table Cust USE jxc00 GO CREATE TABLE cust ( cust_id smallint IDENTITY(1,1) NOT NULL, cust_name char(10)
Database Design Chapters 17 and 18.
Creating a database table
View Integration and Implementation Compromises
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Basic Database Design COSC 2328 – Web Programming.
© 2014 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any manner.
© The McGraw-Hill Companies, All Rights Reserved APPENDIX C DESIGNING DATABASES APPENDIX C DESIGNING DATABASES.
DESIGNING DATABASE APPLICATIONS
Modeling a 1:1 recursive relationship
Creating Databases for Web Applications
Relational Databases.
Database Systems: Design, Implementation, and Management Tenth Edition
The Many-to-Many Relationship
Chapter 4 Relational Model Characteristics
CIS 207 The Relational Database Model
Lecture 2 The Relational Model
Database Management  .
David M. Kroenke and David J
Chapter 3 The Relational Database Model
From ER to Relational Model
Accounting System Design
ERD’s REVIEW DBS201.
The 1:M Relationship (continued)
Database Processing: David M. Kroenke’s Chapter Six:
CHAPTER 4: LOGICAL DATABASE DESIGN AND THE RELATIONAL MODEL
CIS16 Application Programming with Visual Basic
Accounting System Design
DATABASE SYSTEM.
Chapter 2: Intro to Relational Model
DCT 2053 DATABASE CONCEPT Chapter 2.2 CONTINUE
Database Design: Relational Model
Object Relational Mapping Tools
Week 2 The Relational Data Model
ICT Database Lesson 2 Designing a Database.
INSTRUCTOR: MRS T.G. ZHOU
Database Processing: David M. Kroenke’s Chapter Six:
The University of Akron College of Applied Science & Technology Dept
Database Processing: David M. Kroenke’s Chapter Six:
Database Systems: Design, Implementation, and Management
Relational Database Operators
Presentation transcript:

The Many-to-Many Relationship Fearful concatenation of circumstances Daniel Webster

http://www.mdmproofing.com/iym/products/receipt-splitter/

A sales form

The many-to-many relationship Create a third entity to map an m:m relationship An associative entity The + on the crow's foot indicates that LINEITEM is identified by concatenating saleno and lineno LINEITEM is known as a weak entity, and it has an identifying relationship with SALE

Preference settings Foreign key same name as primary key Associative table name of form tableA_tableB

The many-to-many relationship MySQL Workbench m:m symbol

The many-to-many relationship MySQL Workbench Identifying relationship Non-identifying relationship

Why a third entity? Store data about the relationship Think of an m:m as two 1:m relationships

Creating a relational database Same rules apply The associative table has two foreign keys One for each of the entities in the m:m relationship A foreign key can also be part of the primary key of an associative entity lineitem lineno lineqty lineprice saleno itemno 1 4.50 2 25.00 6 20.00 16 3 19

Creating a relational database CREATE TABLE sale ( saleno INTEGER, saledate DATE NOT NULL, saletext VARCHAR(50), PRIMARY KEY(saleno)); CREATE TABLE item ( itemno INTEGER, itemname VARCHAR(30) NOT NULL, itemtype CHAR(1) NOT NULL, itemcolor VARCHAR(10), PRIMARY KEY(itemno)); CREATE TABLE lineitem ( lineno INTEGER, lineqty INTEGER NOT NULL, lineprice DECIMAL(7,2) NOT NULL, PRIMARY KEY(lineno,saleno), CONSTRAINT fk_has_sale FOREIGN KEY(saleno) REFERENCES sale(saleno), CONSTRAINT fk_has_item FOREIGN KEY(itemno) REFERENCES item(itemno));

Exercise A keen field hockey fan wants to keep track of which countries won which medals in the various summer Olympics for both the men’s and women’s events Design a data model Create the database Populate with data for the last two Olympics http://en.wikipedia.org/wiki/Field_hockey_at_the_Summer_Olympics

A three table join Specify two matching conditions with the associative table in both join conditions SELECT * FROM sale JOIN lineitem ON sale.saleno = lineitem.saleno JOIN item ON item.itemno = lineitem.itemno;

A three table join List the names of items, quantity, and value of items sold on January 16, 2011 SELECT itemname, lineqty, lineprice, lineqty*lineprice AS total FROM sale JOIN lineitem ON lineitem.saleno = sale.saleno JOIN item ON item.itemno = lineitem.itemno WHERE saledate = '2011-01-16'; itemname lineqty lineprice total Pocket knife—Avon 1 0.00 Safari chair 50 36.00 1800.00 Hammock 40.50 2025.00 Tent—8 person 8 153.00 1224.00 Tent—2 person 60.00

EXISTS Existential quantifier Returns true or false Returns true if the table contains at least one row satisfying the specified condition Report all clothing items (type “C”) for which a sale is recorded SELECT itemname, itemcolor FROM item WHERE itemtype = 'C' AND EXISTS (SELECT * FROM lineitem WHERE lineitem.itemno = item.itemno); itemname itemcolor Hat—Polar Explorer Red Boots—snake proof Black Pith helmet White Stetson

SELECT itemname, itemcolor FROM item WHERE itemtype = 'C’ itemno itemname itemtype itemcolor 1 Pocket knife—Nile E Brown 2 Pocket knife—Avon 3 Compass N — 4 Geopositioning system 5 Map measure 6 Hat—Polar Explorer C Red 7 White 8 Boots—snake proof Green 9 Black 10 Safari chair F Khaki 11 Hammock 12 Tent—8 person 13 Tent—2 person 14 Safari cooking kit 15 Pith helmet 16 17 Map case 18 Sextant 19 Stetson 20 SELECT itemname, itemcolor FROM item WHERE itemtype = 'C’ AND EXISTS (SELECT * FROM lineitem WHERE lineitem.itemno = item.itemno); lineno lineqty lineprice saleno itemno 1 4.5 2 25 6 20 16 3 19 4 2.25 500 65 9 60 13 75 14 5 10 50 36 40.5 11 8 153 12 itemname itemcolor Hat—Polar Explorer Red Boots—snake proof Black Pith helmet White Stetson

NOT EXISTS Returns true if the table contains no rows satisfying the specified condition Report all clothing items (type “C”) that have not been sold SELECT itemname, itemcolor FROM item WHERE itemtype = 'C' AND NOT EXISTS (SELECT * FROM lineitem WHERE item.itemno = lineitem.itemno); itemname itemcolor Hat—Polar Explorer White Boots—snake proof Green Pith helmet Khaki Stetson Brown

SELECT itemname, itemcolor FROM item WHERE itemtype = 'C' itemno itemname itemtype itemcolor 1 Pocket knife—Nile E Brown 2 Pocket knife—Avon 3 Compass N — 4 Geopositioning system 5 Map measure 6 Hat—Polar Explorer C Red 7 White 8 Boots—snake proof Green 9 Black 10 Safari chair F Khaki 11 Hammock 12 Tent—8 person 13 Tent—2 person 14 Safari cooking kit 15 Pith helmet 16 17 Map case 18 Sextant 19 Stetson 20 SELECT itemname, itemcolor FROM item WHERE itemtype = 'C' AND NOT EXISTS (SELECT * FROM lineitem WHERE item.itemno = lineitem.itemno); lineno lineqty lineprice saleno itemno 1 4.5 2 25 6 20 16 3 19 4 2.25 500 65 9 60 13 75 14 5 10 50 36 40.5 11 8 153 12 itemname itemcolor Hat—Polar Explorer White Boots—snake proof Green Pith helmet Khaki Stetson Brown

Exercise Report all brown items that have been sold Report all brown items that have not been sold

Divide The universal quantifier Not directly mapped into SQL forall Not directly mapped into SQL Implement using NOT EXISTS Find all items that have appeared in all sales becomes Find items such that there does not exist a sale in which this item does not appear

Divide Find the items that have appeared in all sales SELECT itemname FROM item WHERE NOT EXISTS (SELECT * FROM sale (SELECT * FROM lineitem WHERE lineitem.itemno = item.itemno AND lineitem.saleno = sale.saleno)); itemname Pocket knife—Thames See the book’s web site for a detailed explanation of how divide works (Support/SQL Divide)

A template for divide Find the target1 that have appeared in all sources SELECT target1 FROM target WHERE NOT EXISTS (SELECT * FROM source (SELECT * FROM target-source WHERE target-source.target# = target.target# AND target-source.source# = source.source#));

Beyond the great divide Find the items that have appeared in all sales can be rephrased as Find all the items for which the number of sales that include this item is equal to the total number of sales. SELECT item.itemno, item.itemname FROM item JOIN lineitem ON item.itemno = lineitem.itemno GROUP BY item. itemno, item.itemname HAVING COUNT(DISTINCT saleno) = (SELECT COUNT(DISTINCT saleno) FROM sale); First determine the number of sales in which an item has appeared Second compare the number of sales to the total number of sales

Set operations UNION Equivalent to OR INTERSECT Equivalent to AND

UNION List all items that were sold on January 16, 2011, or are brown. SELECT itemname FROM item JOIN lineitem ON item.itemno = lineitem.itemno JOIN sale ON lineitem.saleno = sale.saleno WHERE saledate = '2011-01-16' UNION SELECT itemname FROM item WHERE itemcolor = 'Brown'; itemname Hammock Map case Pocket knife—Avon Pocket knife—Nile Safari chair Stetson Tent—2 person Tent—8 person

INTERSECT List all items that were sold on January 16, 2011, and are brown. SELECT itemname FROM item JOIN lineitem ON item.itemno = lineitem.itemno JOIN sale ON lineitem.saleno = sale.saleno WHERE saledate = '2011-01-16' INTERSECT SELECT itemname FROM item WHERE itemcolor = 'Brown'; itemname Pocket knife—Avon INTERSECT not supported by MySQL

Conclusion Introduced m:m relationship Associative entity Weak entity EXISTS Divide Set operations