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 M:M is converted into two 1:M relationships - Intersection entity/table is created The intersection table must have 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 for the intersection table 1
3 Tables 2 Shipment ID Shipment Sent Shipment Received 40019/1/20119/9/ /3/20119/23/ /22/20119/29/ /2/201110/15/ /21/2011 Material ID CategoryHazardous 1ElectronicsFalse 2CompositesFalse 3MachineryFalse 4SolventsTrue 5PetrochemicalsTrue ShipmentItems Shipment ID Lineno Material ID
Model Extended to include Suppliers 3
4 Tables (to be used for queries) 4 Material ID CategoryHazardous 1ElectronicsFalse 2CompositesFalse 3MachineryFalse 4SolventsTrue 5PetrochemicalsTrue ShipmentItems Shipment ID Lineno Material ID Shipment ID Shipment Sent Shipment Received Supplier ID 40019/1/20119/9/ /3/20119/23/ /22/20119/29/ /2/201110/15/ /21/20114 Supplier ID Supplier Name CityState Delivery EffRank Cost EffRank 1VanguardNew OrleansLouisiana23 2SmithfieldHoustonTexas14 3BlackstoneJacksonvilleFlorida51 4CommodoreNorfolkVirginia45 5LakeshoresLong BeachCalifornia32
Querying across 3 tables List the material categories, the shipment ID, Line #, and date received for all items received on 9/9/2011 SELECT Category, Shipment.ShipmentID, Lineno, ShipmentReceived FROM Shipment, ShipmentItems, Material Where Shipment.ShipmentID = ShipmentItems.ShipmentID AND ShipmentItems.MaterialID = Material.MaterialID AND ShipmentReceived = #9/9/2011#; 5 Category Shipment ID Lineno Shipment Received Machinery400119/9/2011 Solvents400129/9/2011 Petrochemicals400139/9/2011
Model Further Extended to include Employees & Clients 6 If you can SQL using 2 or 3 tables – you have the skills to run SQL using 1,000 tables
All Tables 7 Material ID CategoryHazardous 1ElectronicsFalse 2CompositesFalse 3MachineryFalse 4SolventsTrue 5PetrochemicalsTrue ShipmentItems Shipment ID Lineno Material ID Shipment ID Shipment Sent Shipment Received Supplier ID Client ID 40019/1/20119/9/ /3/20119/23/ /22/20119/29/ /2/201110/15/ /21/ Supplier ID Supplier Name CityState Delivery EffRank Cost EffRank 1VanguardNew OrleansLouisiana23 2SmithfieldHoustonTexas14 3BlackstoneJacksonvilleFlorida51 4CommodoreNorfolkVirginia45 5LakeshoresLong BeachCalifornia32 Client ID Client Name Client Services Client City Client State Client Revenue Emp ID 1BK AssociatesCommercialPortlandOregon$210, BlalocIndustrialKansas CityMissouri$330, Bankton ElectricGovernmentNew York $210, BickIndustrialRaleighNorth Carolina$550, TX ElectricGovernmentHoustonTexas$160, CrowCommercialDallasTexas$270, GRBIndustrialAtlantaGeorgia$180, H&PIndustrialDenverColorado$90, LB&BIndustrialBostonMassachusetts$211, CongroIndustrialAtlantaGeorgia$122, Moss EnterprisesCommercialPhoenixArizona$33, RubyIndustrialSan AntonioTexas$344, Silver IndustriesIndustrialOmahaNebraska$218, TPHCommercialAnnaheimCalifornia$166, Employee Emp ID Emp first Name Emp last name Emp status Emp Rate 1DanPiercefull time$ SeanSmithfull time$ LauraJohnsonfull time$ MikeLucasfull time$ LarryWilsonfull time$ KeithEthridgepart time$25.00