You are on page 1of 12

IOE 

373 – Data Processing

Two Most-Common Types

Introduction to Relationships in  One-to-Many


Databases  Many-to-Many

IOE 373 Lecture 02

Luis Guzman, University of Michigan 1
IOE 373 – Data Processing

One-to-Many Example 1
 A one-to-many relationship between two  Cars and Manufacturers
tables A and B means that  Every car model is made by one manufacturer:
 one record in table A can be related to many  Civic—Honda
records in table B, but  Focus—Ford
 Each record in table B can only be related to one  Sonata—Hyundai
record in table A.  However, most manufacturers make more than
one car model:
 Honda: Civic, Accord, Fit, etc.
 Ford: Focus, Fiesta, Mustang, Fusion, etc.

Luis Guzman, University of Michigan 2
IOE 373 – Data Processing

Partial Tables
Linking the tables; one-to-many
Design Views We add a ManufacturerID field to the Models table:

Datasheet Views We then populate this field with the appropriate


ManufacturerID for each model:

So how do we link these tables to indicate which


Manufacturer makes each model? The ManufacturerID in the Models table is a foreign key.

Luis Guzman, University of Michigan 3
IOE 373 – Data Processing

Definition Creating the Relationship


 Foreign Key: A field in one table which is not  In Access, open the Relationship
the primary key in that table but is the designer (Database
primary key of another table. Tools/Relationships)
 Entering a value in the foreign key field  Select the tables that are
indicates that the record in the first table is involved in the relationship:
related to the record in the second table  Drag the foreign key field from
having that primary key value. one table to the other.
 Check the Referential Integrity
box and click “Create”.

Luis Guzman, University of Michigan 4
IOE 373 – Data Processing

Representing One-to-Many
One-to-Many Relationships - Recap

 The relationship is now formalized in Access.  A one-to-many relationship is expressed in


 Access will enforce the relationship—you will Access and other DBMS’s by using a foreign
not be allowed to enter a ManufacturerID in key.
the Models table which doesn’t have a  A foreign key is a field in a table which is not
matching entry in the Manufacturers table. the primary key of that table, but is the
 Therefore, you will probably want to populate primary key of another table.
the Manufacturers table before the Models
table.

Luis Guzman, University of Michigan 5
IOE 373 – Data Processing

There isn’t just one country per


Consider… company
In the table below, what seems to be too simplistic?  Most large car companies manufacture vehicles
in multiple countries.
 In general, it isn’t accurate to say that “Hondas
are made in Japan” or “Fords are made in the
US.”
 How will we represent that many companies
• Ignore that there aren’t enough manufacturers build cars in multiple countries, and that many
listed. countries host factories from many companies?
• Ignore that there are a lot of other fields that we (The US, for example, has factories for all four of
could add to this table. the manufacturers listed.)
• What is wrong with the ManufacturerCountry field?

Luis Guzman, University of Michigan 6
IOE 373 – Data Processing

How are we going to link


manufacturers to countries, then??? Many-to-Many Relationship

 So: A company can build cars in many countries,


and a country can have factories from many
companies.
 This is what is called a many-to-many
relationship.
 You can’t express a many-to-many relationship
simply by drawing a line between two tables.
(The first step is hinted at here: Create a Countries table!)  Many-to-many relationships require a third table,
called an intersection table.

Luis Guzman, University of Michigan 7
IOE 373 – Data Processing

Creating the Many-to-Many


The solution Relationship

1. Remove the ManufacturerCountry field from


the Manufacturers table.
2. Create a table for countries.
3. Create a new table which matches
ManufacturerID’s with CountryID’s.
4. This table has a primary key which combines
the primary keys of the other two tables.
• Each row in the ManufacturerCountries table indicates that THAT manufacturer
makes cars in THAT country. 5. Create the relationships—two one-to-many
• For example: The first row says that Honda makes cars in the USA. The next relationships.
row says that Honda makes cars in Japan.
• Note that this intersection table uses BOTH fields as its primary key. In each
column, the values are not unique. However, the combination of the two
values is unique—we don’t enter that Honda makes cars in the USA twice.

Luis Guzman, University of Michigan 8
IOE 373 – Data Processing

Relationship Type as Business


To create the one-to-many relationship, we add a
Decision 
foreign key (DeptID) to the Employees table, and
link it to the DeptID in the Departments table:
 A business uses a database to track employees and
departments.

• To create the many-to-many relationship, we create


• If the business determines that an employee can an intersection table.
only work for one department at a time, the • The intersection table’s primary key is compound, and
includes the primary key of the two related tables:
relationship is one-to-many;
• If an employee can work for more than one
department, the relationship is many-to-many.

Luis Guzman, University of Michigan 9
IOE 373 – Data Processing

Summary: One-to-Many Relationships Summary: Many-to-Many Relationships

 Things to know:
 Things to know:
 Occurs when you can make a statement like this: “Each  Occurs when you can make a statement like this: “Each Product
Department can have more than one employee, but each may be available from more than one supplier, and a supplier
employee can be in at most one department.” may provide more than one product.”
 Requires intersection table (like ProductSuppliers above)
 Requires a foreign key field to be added to the table on  Primary key of intersection table is always compound and always
the “many” side. includes the full primary keys of the two entity tables (Products,
 The foreign key field links to the primary key of the table Suppliers)
The intersection table may also contain data that applies
on the “one” side.

specifically to the intersection, such as Price above (different
suppliers may have different prices for products).

Luis Guzman, University of Michigan 10
IOE 373 – Data Processing

Appendix

21 22

Luis Guzman, University of Michigan 11
IOE 373 – Data Processing

23

Luis Guzman, University of Michigan 12

You might also like