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

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!
Sometimes you need to use data from more than one table. In example1, the report displays data from two separate tables. Employee IDs exist in the EMPLOYEES.
A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
The Many-to-Many Relationship Fearful concatenation of circumstances Daniel Webster.
What is a Database By: Cristian Dubon.
The Relational Model and Relational Algebra Nothing is so practical as a good theory Kurt Lewin, 1945.
BUSINESS DRIVEN TECHNOLOGY Plug-In T4 Designing Database Applications.
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.
Concepts of Database Management Sixth Edition
Concepts of Database Management Seventh Edition
The Relational Database Model – some relations you might want to avoid!!!
The Relational Database Model
A Practical Introduction to Transactional Database Modeling and Design Mike Burr.
Maintenance Modifying the data –Add records –Delete records –Update records Modifying the design –Add fields into tables –Remove fields from a table –Change.
Chapter 14 Getting to First Base: Introduction to Database Concepts.
Database Management Fall 2003 Midterm Review Chapters 1 & 2.
1 Chapter 2 Reviewing Tables and Queries. 2 Chapter Objectives Identify the steps required to develop an Access application Specify the characteristics.
The Relational Model Codd (1970): based on set theory Relational model: represents the database as a collection of relations (a table of values --> file)
10/3/2000SIMS 257: Database Management -- Ray Larson Relational Algebra and Calculus University of California, Berkeley School of Information Management.
Michael F. Price College of Business Chapter 6: Logical database design and the relational model.
Database Relationships Objective 5.01 Understand database tables used in business.
This presentation prepared for MIS 421 / MBA 575 at Western Washington University. Material in this presentation drawn from Richard T. Watson, Data Management:
Database Design Concepts
Concepts of Database Management, Fifth Edition
Relational Model & Relational Algebra. 2 Relational Model u Terminology of relational model. u How tables are used to represent data. u Connection between.
Introduction –All information systems create, read, update and delete data. This data is stored in files and databases. Files are collections of similar.
Many-to-many (M:M) relationship M:M Concept: A Shipment can contain many types of Materials and a type of Material can be sent in many different shipments.
RELATIONSHIPS Generally there are two main database types: flat-file and relational.
Chapter 9 Joining Data from Multiple Tables
A Guide to MySQL 5. 2 Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables Use a subquery.
PLUG IT IN 3 Fundamentals of Relational Database Operations.
Module 11: Programming Across Multiple Servers. Overview Introducing Distributed Queries Setting Up a Linked Server Environment Working with Linked Servers.
1 CS 3630 Database Design and Implementation. 2 Sets Foundation of relational database. Basic Operations Power set Mapping.
Concepts of Database Management Seventh Edition
DATABASE TRANSACTION. Transaction It is a logical unit of work that must succeed or fail in its entirety. A transaction is an atomic operation which may.
IS 475/675 - Introduction to Database Design
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.
3 1 Chapter 3 The Relational Database Model Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
C-1 Management Information Systems for the Information Age Copyright 2004 The McGraw-Hill Companies, Inc. All rights reserved Extended Learning Module.
11/07/2003Akbar Mokhtarani (LBNL)1 Normalization of Relational Tables Akbar Mokhtarani LBNL (HENPC group) November 7, 2003.
Relational Algebra – Part 2
Description and exemplification of entity-relationship modelling.
Relational Theory and Design
SQL LANGUAGE and Relational Data Model TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
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.
Alighieri: Introduction to MS Access 1 What is a Database? RELATIONAL DATABASE A database is an organized collection of information. A database is designed.
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.
Database Planning Database Design Normalization.
Lecture 5 Data Model Design Jeffery S. Horsburgh Hydroinformatics Fall 2012 This work was funded by National Science Foundation Grant EPS
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
Rationale Databases are an integral part of an organization. Aspiring Database Developers should be able to efficiently design and implement databases.
Database Systems: Design, Implementation, and Management Tenth Edition
The Many-to-Many Relationship
CIS 207 The Relational Database Model
Lecture 2 The Relational Model
David M. Kroenke and David J
Chapter 3 The Relational Database Model
The 1:M Relationship (continued)
The Many-to-Many Relationship
DATABASE SYSTEM.
Database Systems: Design, Implementation, and Management
Relational Database Operators
Presentation transcript:

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

2 A sales form

3 The many-to-many relationship Create a third entity to map an m:m relationship An associative entity The vertical bar on the crow's foot indicates that LINEITEM is identified by concatenating saleno and lineno

4 The many-to-many relationship Create a third entity to map an m:m relationship An associative entity The vertical bar on the crow's foot indicates that LINEITEM is identified by concatenating saleno and lineno

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

6 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

7 Creating a relational database CREATE TABLE sale ( salenoINTEGER, saledateDATE, saletextVARCHAR(50), PRIMARY KEY(saleno)); CREATE TABLE item ( itemnoINTEGER, itemnameVARCHAR(30), itemtypeCHAR(1), itemcolorVARCHAR(10), PRIMARY KEY(itemno)); CREATE TABLE lineitem ( linenoINTEGER, lineqtyINTEGER, linepriceDECIMAL(7,2), 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));

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

9 A three table join List the names of items, quantity, and value of items sold on January 16, 2003 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

10 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

11 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

12 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

13 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

14 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#));

15 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

16 Set operations UNION Equivalent to OR INTERSECT Equivalent to AND

17 UNION List all items that were sold on January 16, 2003, 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

18 INTERSECT List all items that were sold on January 16, 2003, 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

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