Many-to-many (M:M) relationship M:M Concept: A Shipment can contain many types of Materials and a...

7
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

Transcript of Many-to-many (M:M) relationship M:M Concept: A Shipment can contain many types of Materials and a...

Page 1: 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.

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

Page 2: 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.

3 Tables

2

ShipmentShipment

IDShipment

SentShipmentReceived

4001 9/1/2011 9/9/2011

4002 9/3/2011 9/23/2011

4003 9/22/2011 9/29/2011

4004 10/2/2011 10/15/2011

4005 10/21/2011

MaterialMaterial

IDCategory Hazardous

1 Electronics False

2 Composites False

3 Machinery False

4 Solvents True

5 Petrochemicals True

ShipmentItemsShipment

IDLineno

MaterialID

4001 1 3

4001 2 4

4001 3 5

4002 1 1

4002 2 2

4003 1 2

4003 2 4

4003 3 3

4004 1 3

4005 1 5

4005 2 2

4005 3 1

4005 4 1

4005 5 2

Page 3: 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.

Model Extended to include Suppliers

3

Page 4: 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.

4 Tables (to be used for queries)

4

MaterialMaterial

IDCategory Hazardous

1 Electronics False

2 Composites False

3 Machinery False

4 Solvents True

5 Petrochemicals True

ShipmentItemsShipment

IDLineno

MaterialID

4001 1 3

4001 2 4

4001 3 5

4002 1 1

4002 2 2

4003 1 2

4003 2 4

4003 3 3

4004 1 3

4005 1 5

4005 2 2

4005 3 1

4005 4 1

4005 5 2

ShipmentShipment

IDShipment

SentShipmentReceived

SupplierID

4001 9/1/2011 9/9/2011 3

4002 9/3/2011 9/23/2011 5

4003 9/22/2011 9/29/2011 2

4004 10/2/2011 10/15/2011 2

4005 10/21/2011 4

SupplierSupplier

IDSupplier

NameCity State

DeliveryEffRank

CostEffRank

1 Vanguard New Orleans Louisiana 2 3

2 Smithfield Houston Texas 1 4

3 Blackstone Jacksonville Florida 5 1

4 Commodore Norfolk Virginia 4 5

5 Lakeshores Long Beach California 3 2

Page 5: 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.

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

CategoryShipment

IDLineno

ShipmentReceived

Machinery 4001 1 9/9/2011

Solvents 4001 2 9/9/2011

Petrochemicals 4001 3 9/9/2011

Page 6: 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.

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

SUPPLIER

*SupplierIDSupplierName

CityState

DeliveryEffRankCostEffRank

SHIPMENT

*ShipmentIDShipmentSent

ShipmentReceivedSupplierID (FK)

ClientID (FK)

MATERIAL

*MaterialIDCategory

Hazardous

SHIPMENT ITEMS

*ShipmentID (FK)*Lineno

MaterialID (FK)

EMPLOYEE

*EmpIDEmpFirstnameEmpLastname

EmpStatusEmpRate

CLIENT

*ClientIDClientName

ClientServicesClientCity

ClientStateClientRevenue

EmpID (FK)

Page 7: 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.

All Tables

7

MaterialMaterial

IDCategory Hazardous

1 Electronics False

2 Composites False

3 Machinery False

4 Solvents True

5 Petrochemicals True

ShipmentItemsShipment

IDLineno

MaterialID

4001 1 3

4001 2 4

4001 3 5

4002 1 1

4002 2 2

4003 1 2

4003 2 4

4003 3 3

4004 1 3

4005 1 5

4005 2 2

4005 3 1

4005 4 1

4005 5 2

ShipmentShipment

IDShipment

SentShipmentReceived

SupplierID

ClientID

4001 9/1/2011 9/9/2011 3 6

4002 9/3/2011 9/23/2011 5 12

4003 9/22/2011 9/29/2011 2 9

4004 10/2/2011 10/15/2011 2 3

4005 10/21/2011 4 7

SupplierSupplier

IDSupplier

NameCity State

DeliveryEffRank

CostEffRank

1 Vanguard New Orleans Louisiana 2 3

2 Smithfield Houston Texas 1 4

3 Blackstone Jacksonville Florida 5 1

4 Commodore Norfolk Virginia 4 5

5 Lakeshores Long Beach California 3 2

ClientClient

IDClientName

ClientServices

ClientCity

ClientState

ClientRevenue

EmpID

1 BK Associates Commercial Portland Oregon $210,000.00 2

2 Blaloc Industrial Kansas City Missouri $330,000.00 2

3 Bankton Electric Government New York New York $210,000.00 3

4 Bick Industrial Raleigh North Carolina $550,000.00 1

5 TX Electric Government Houston Texas $160,000.00 3

6 Crow Commercial Dallas Texas $270,000.00 2

7 GRB Industrial Atlanta Georgia $180,000.00 1

8 H&P Industrial Denver Colorado $90,000.00 2

9 LB&B Industrial Boston Massachusetts $211,000.00 3

10 Congro Industrial Atlanta Georgia $122,000.00 3

11 Moss Enterprises Commercial Phoenix Arizona $33,000.00 1

12 Ruby Industrial San Antonio Texas $344,000.00 3

13 Silver Industries Industrial Omaha Nebraska $218,000.00 2

14 TPH Commercial Annaheim California $166,000.00 1

Employee

EmpID

Empfirst

Name

Emplast

name

Empstatus

EmpRate

1 Dan Pierce full time $120.00

2 Sean Smith full time $85.00

3 Laura Johnson full time $85.00

4 Mike Lucas full time $55.00

5 Larry Wilson full time $55.00

6 Keith Ethridge part time $25.00