CCNC/CCNC Module 5/Designing and creating tables/Table Relationships/Many To Many

From WikiEducator
Jump to: navigation, search
Tutorial.png Table Relationships 

Designing Relationships | Many-To-Many | Testing by Adding Data | Self Assessment | Summary & FAQs


The many-to-many relationship

Icon objectives.jpg

Outcomes

Upon completion of this tutorial the learner will be able to:
  • Design the many-to-many table relationship
  • Create many-to-many relationships between tables
  • Apply rules to relationships to increase data integrity


Designing the relationship

Rdbms-many-to-many-relationship.JPG

As was stated previously, the many-to-many relationship is implemented as two one-to-many relationships. The entity relationship diagram shows this very well. In the many-to-many relationship a cross reference (_xref_) table exists between the two tables with many records. For each Person joined to an Address, a record would exist in the _xref_ table with a reference to both the People and Addresses primary keys. The _xref_ table can also include a primary key for itself as shown here with the xref_id field. Keep in mind that the concatenation of the person_id and address_id would also be a candidate primary key. When designing the _xref_ table a brief relationship descriptor is useful. In this example there is an address_type.

Creating the relationships

Figure 1. A Many-to-Many relationship

To set the relationships between People and Addresses open the relationship designer and add the People (this table may already be referenced in the relationship designer), the Addresses and People_xref_Addresses tables. Drag and drop the person_id from the People_xref_Addresses onto the same field on the People table. This should create the relationship, confirm the creation by the new line drawn between the two tables. Drag and drop the address_id from the People_xref_Addresses onto the same field on the Addresses table. This should create the relationship, confirm the creation by the new line drawn between the two tables.

Right click on each of the new relationship lines joining the tables and set the relationship properties. Setting the update cascade is not as important as setting the delete cascade. Having the delete cascade set will ensure not data will be orphaned. Orphaning data happens when the 'parent' record is deleted leaving the 'child' record behind. In our example this would occur if you deleted an address record without deleting the corresponding _xref_ record(s).

Test your knowledge



Icon qmark.gif

many-to-many relationships

 

1. A many-to-many relationship is like two one-to-many relationships?

TRUE
Absolutely Correct!
FALSE
Incorrect, click here to review the topic.

2. In a many-to-many relationship the concatenation of the two primary keys could also be a candidate key for the _xref_ table?

TRUE
Absolutely Correct!
FALSE
Incorrect, click here to review the topic.

Your score is 0 / 0