Apophenia Overload
Apophenia Overload

Reputation: 2504

MySQL: Need assistance for schema- how to deal with lists?

I have a form for inputting information about a football player, with a field to enter in all of the teams that the player has been on. The idea is that records of these players will be used to generate an index of teams, and individual team pages. The team pages would then display records of all players who have once been on the team at some point.

That's how it works in abstract. I'm not exactly sure how to handle the field; I'm thinking of having the teams be comma separated when the user inputs them. However, I'm more concerned about how to handle the backend, in the database itself.

If each player has multiple teams associated with him, how should I store the teams attribute in the db table for players? At the moment, since the form is about a single player, the main table I'm working with is the players table. Each row of this table is about a single player and his stats, including an attribute that is a list of teams he has been on. I think I may need an additional table for teams. Each row on this secondary table wouldn't consist of much, just an id, and then a list of players. But then, how would I then handle that list of players?

Any suggestions as to how to deal with this situation? Is there a better solution?

Upvotes: 0

Views: 278

Answers (1)

keithjgrant
keithjgrant

Reputation: 12769

You are describing a many-to-many relationship. You need a "player" table, a "team" table, and a "player_team" assoc table. The assoc table would have both a player_id and a team_id. You can then load players associated with a given team by doing a join on the assoc table.

Upvotes: 2

Related Questions