Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a many-to-many join table have more than two columns?

I have some tables that benefit from many-to-many tables. For example the team table.

Team member can hold more than one 'position' in the team, all the positions are listed in the position db table. The previous positions held are also stored for this I have a separate table, so I have

  • member table (containing team details)
  • positions table (containing positions)
  • member_to_positions table (id of member and id of position)
  • member_to_previous_positions (id of member and id of position)

Simple, however the crux comes now that a team member can belong to many teams aghhh. I already have a team_to_member look-up table. Now the problem comes how do I tie a position to a team? A member may have been team leader on one team, and is currently team radio man and press officer on a different team. How do I just pull the info per member to show his current position, but also his past history including past teams. Do I need to add a position_to team table and somehow cross reference that, or can I add the team to the member to positions table?

It's all very confusing, this normalization.

like image 776
Paul M Avatar asked Jun 23 '09 22:06

Paul M


People also ask

How do I join 3 columns in SQL?

If you'd like to get data stored in tables joined by a compound key that's a primary key in one table and a foreign key in another table, simply use a join condition on multiple columns. In one joined table (in our example, enrollment ), we have a primary key built from two columns ( student_id and course_code ).

How do you join two tables with many-to-many relationships?

When you need to establish a many-to-many relationship between two or more tables, the simplest way is to use a Junction Table. A Junction table in a database, also referred to as a Bridge table or Associative Table, bridges the tables together by referencing the primary keys of each data table.

What is the maximum number of tables that can be joined?

Theoretically, there is no upper limit on the number of tables that can be joined using a SELECT statement. (One join condition always combines two tables!)

What kind of join is many-to-many?

A many-to-many relationship occurs when multiple records in a table are associated with multiple records in another table. For example, a many-to-many relationship exists between customers and products: customers can purchase various products, and products can be purchased by many customers.


1 Answers

Yes, a many-to-many junction table can have additional attributes (columns).

For example, if there's a table called PassengerFlight table that's keyed by PassengerID and FlightID, there could be a third column showing the status of the given passenger on the given flight. Two different statuses might be "confirmed" and "wait listed", each of them coded somehow.

In addition, there can be ternary relationships, relationships that involve three entities and not just two. These tables are going to have three foreign keys that taken together are the primary key for the relationship table.

like image 174
Walter Mitty Avatar answered Nov 03 '22 00:11

Walter Mitty