Many-to-Many Relationships with a Link Table

A link table stores each relationship as a separate row, allowing each side to occur in several pairs.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace an article tag
  2. Keep duplicate links meaningful
  3. Give relationship rows their own uniqueness and facts
  4. Choose what counts as a distinct association

Trace an article tag

Article 7 can have tags 2 and 9. Tag 2 can also be attached to article 10. The resulting rows are (7,2), (7,9), and (10,2). See relations as ordered pairs.

Keep duplicate links meaningful

If membership is unique, constrain the column pair. If repeated events matter, each event can have its own identity and time. Composite keys express the first policy.[1][2]

Give relationship rows their own uniqueness and facts

A student can attend several workshops, and each workshop has several students. The attendance table records one pair per enrollment. A registration date or role can describe the pair itself. See composite keys.

Student 1, workshop X, registered Monday
Student 1, workshop Y, registered Tuesday
Student 2, workshop X, registered Monday
Unique pair can prevent duplicate enrollments

Choose what counts as a distinct association

If the same pair can participate more than once, include a session or attempt identity. Test both several links from one student and several from one workshop. Query the association without losing unmatched entities when relevant.

References

  1. PostgreSQL Global Development Group, Data Definition ↩
  2. PostgreSQL Global Development Group, SQL Language Tutorial ↩