Relation Ship
In a one-to-one relationship, each record in one table relates directly
to a single record in another table.
An example is an Employees table and a company Soccer Players table. An
employee will be listed in the Soccer Players table only once, and vice
versa. Each soccer player has one matching record in the Employees
table. This set of data is a subset of the Employee ID records in the
Employees table.
- Each soccer player has one matching record in the Employees table.
- This set of data is a subset of the Employee ID records in the
Employees table.
|
One to Many In a one-to-many relationship,
a record in one table relates to multiple records in a second table, but
the records in the second table relate to only one record in the first
table.
An example is a Suppliers table and a Products table. One supplier might
be the only source of multiple products, all of which relate only to that
supplier.
- Figure 1 One supplier ...
- Figure 2 can supply more than one product ...
- Figure 3 ... but each product has only one supplier
|
Many to Many In a many-to-many relationship, a record
in one table relates to multiple records in a second table, and a record
in the second table relates to multiple records in the first table.
This type of relationship requires a third table, called a junction
table. The junction table refers the primary key in the master
table, with (one or more) foreign keys.
For example, the Orders table and the Products table have a
many-to-many relationship. One order can contain many products, and
each product can appear in many orders.
The third table — in this example, the Order Details table — contains
primary keys from the Orders and Products tables. The Order Details
table is the junction table for the other two.
All junction tables connect tables that have one-to-many
relationships with the junction tables, and the
primary keys of those connected tables are the foreign keys of the
junction table.
When one table can relate to many others simply by referencing information
in them, the parts of your database are like the cells of your body.
Each is active, each is the right size for its business, and together
they can achieve effects vastly beyond their individual abilities.
- Primary key from the Orders table.
- Primary key from the Products table.
- One order can have many products ...
- and each product can appear on many orders
|