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.

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.
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.
Relational Algebra Relational Calculus. Relational Algebra Operators Relational algebra defines the theoretical way of manipulating table contents using.
Chapter 14 Getting to First Base: Introduction to Database Concepts.
LIS 557 Database Design and Management William Voon Michael Cole Spring '04.
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.
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:
ระบบฐานข้อมูลขั้นสูง (Advanced Database Systems) Lecturer AJ. Suwan Janin Phone:
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.
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
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
PLUG IT IN 3 Fundamentals of Relational Database Operations.
1 CS 3630 Database Design and Implementation. 2 Sets Foundation of relational database. Basic Operations Power set Mapping.
Concepts of Database Management Seventh Edition
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.
Relational Theory and Design
SQL LANGUAGE and Relational Data Model TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 3 The Relational Database Model.
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.
3 1 Database Systems The Relational Database Model.
Alighieri: Introduction to MS Access 1 What is a Database? RELATIONAL DATABASE A database is an organized collection of information. A database is designed.
SQL constrains and keys. SORTED RESULTS Sort the results by a specified criterion SELECT columns FROM tables WHERE predicates ORDER BY column ASC/DESC;
Order Database – ER Diagram Prepared by Bryan Borcher Spring 2014.
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.
VOCAB REVIEW. A field that can be computed from other fields Calculated field Click for the answer Next Question.
Lecture 5 Data Model Design Jeffery S. Horsburgh Hydroinformatics Fall 2012 This work was funded by National Science Foundation Grant EPS
Database Systems: Design, Implementation, and Management Tenth Edition
The Many-to-Many Relationship
CIS 207 The Relational Database Model
Lecture 2 The Relational Model
Theory behind the relational engine
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

A sales form

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 SALE *saleno saledate saletext LINEITEM *lineno lineqty lineprice ITEM *itemno itemname itemtype itemcolor

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 linenolineqtylinepricesalenoitemno

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_lineitem_sale FOREIGN KEY(saleno) REFERENCES sale, CONSTRAINT fk_lineitem_item FOREIGN KEY(itemno) REFERENCES item);

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;

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

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

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

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

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—Avon

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#)); TARGET *target# target1 target2 … TARGET- SOURCE t-s1 t-s2 … SOURCE *source# source1 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, lineitem WHERE item.itemno = lineitem.itemno GROUP BY item. itemno, item.itemname HAVING COUNT(DISTINCT saleno) = (SELECT COUNT(DISTINCT saleno) FROM sale);

Set operations UNION –Equivalent to OR INTERSECT –Equivalent to AND

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

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