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

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

You have been given a mission and a code. Use the code to complete the mission and you will save the world from obliteration…
Advanced SQL Topics Edward Wu.
Working with MS-ACCESS IS 240 – Database Management Lecture #2 – Assoc. Prof. M. E. Kabay, PhD, CISSP Norwich University
Chapter 1 The Study of Body Function Image PowerPoint
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 5 Author: Julia Richards and R. Scott Hawley.
Author: Graeme C. Simsion and Graham C. Witt Chapter 4 Subtypes & Supertypes.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
Factors, Primes & Composite Numbers
Business Transaction Management Software for Application Coordination 1 Business Processes and Coordination.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
0 - 0.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
Year 6 mental test 5 second questions
Introduction to Relational Database Systems 1 Lecture 4.
Introduction to SQL 1 Lecture 5. Introduction to SQL 2 Note in different implementations the syntax might slightly differ different features might be.
Relational data integrity
The MDGs and School Enrolment: An example of administrative data
ZMQS ZMQS
SQL: The Query Language Part 2
BT Wholesale October Creating your own telephone network WHOLESALE CALLS LINE ASSOCIATED.
Report Card P Only 4 files are exported in SAMS, but there are at least 7 tables could be exported in WebSAMS. Report Card P contains 4 functions: Extract,
Information Systems Today: Managing in the Digital World
Data Structures: A Pseudocode Approach with C
ABC Technology Project
CS 3630 Database Design and Implementation. Where Clause and Aggregate Functions -- List all rooms whose price is greater than the -- average room price.
Database Design Process
Chapter 12 Joining Tables Part C. SQL Copyright 2005 Radian Publishing Co.
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
Microsoft Access.
VOORBLAD.
15. Oktober Oktober Oktober 2012.
Solving Equations How to Solve Them
Squares and Square Root WALK. Solve each problem REVIEW:
We are learning how to read the 24 hour clock
David Walker Ottawa TMG Users Group 15 March 2014.
Past Tense Probe. Past Tense Probe Past Tense Probe – Practice 1.
Chapter 5 Test Review Sections 5-1 through 5-4.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
1 First EMRAS II Technical Meeting IAEA Headquarters, Vienna, 19–23 January 2009.
Addition 1’s to 20.
Model and Relationships 6 M 1 M M M M M M M M M M M M M M M M
25 seconds left…...
Test B, 100 Subtraction Facts
Week 1.
We will resume in: 25 Minutes.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Relational Algebra and Relational Calculus
Chapter 15 A Table with a View: Database Queries.
1 Unit 1 Kinematics Chapter 1 Day
PSSA Preparation.
TASK: Skill Development A proportional relationship is a set of equivalent ratios. Equivalent ratios have equal values using different numbers. Creating.
How Cells Obtain Energy from Food
Chapter 30 Induction and Inductance In this chapter we will study the following topics: -Faraday’s law of induction -Lenz’s rule -Electric field induced.
CpSc 3220 Designing a Database
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.
Database Management Fall 2003 Midterm Review Chapters 1 & 2.
This presentation prepared for MIS 421 / MBA 575 at Western Washington University. Material in this presentation drawn from Richard T. Watson, Data Management:
The Many-to-Many Relationship Fearful concatenation of circumstances Daniel Webster.
The Many-to-Many Relationship
The Many-to-Many Relationship
Presentation transcript:

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

2

3 A sales form

4 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

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

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

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

8 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 linenolineqtylinepricesalenoitemno

9 Creating a relational database CREATE TABLE sale ( salenoINTEGER, saledateDATE NOT NULL, saletextVARCHAR(50), PRIMARY KEY(saleno)); CREATE TABLE item ( itemnoINTEGER, itemnameVARCHAR(30) NOT NULL, itemtypeCHAR(1) NOT NULL, itemcolorVARCHAR(10), PRIMARY KEY(itemno)); CREATE TABLE lineitem ( linenoINTEGER, lineqtyINTEGER NOT NULL, linepriceDECIMAL(7,2) NOT NULL, salenoINTEGER, itemnoINTEGER, 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 _the_Summer_Olympicshttp://en.wikipedia.org/wiki/Field_hockey_at _the_Summer_Olympics 10

11 A three table join List the names of the three tables after FROM Specify two matching conditions with the associative table in both join conditions SELECT * FROM sale, lineitem, item WHERE sale.saleno = lineitem.saleno AND item.itemno = lineitem.itemno;

12 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, lineitem, item WHERE lineitem.saleno = sale.saleno AND item.itemno = lineitem.itemno AND saledate = ' '; itemnamelineqtylinepricetotal Pocket knife—Avon10.00 Safari chair Hammock Tent—8 person Tent—2 person160.00

13 EXISTS Existential qualifier 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); itemnameitemcolor Hat—Polar ExplorerRed Boots—snake proofBlack Pith helmetWhite StetsonBlack Work through tables on p. 118

14 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); itemnameitemcolor Hat—Polar ExplorerWhite Boots—snake proofGreen Pith helmetKhaki StetsonBrown

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

16 Divide The universal qualifier 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

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

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

19 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, lineitem WHERE 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

20 Set operations UNION Equivalent to OR INTERSECT Equivalent to AND

21 UNION List all items that were sold on January 16, 2011, or are brown. SELECT itemname FROM item, lineitem, sale WHERE item.itemno = lineitem.itemno AND lineitem.saleno = sale.saleno AND saledate = ' ' 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

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

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