views:

250

answers:

9

I have 2 database tables: Teams and Games.

For the purpose of this question, we are dealing with football (soccer) teams and games.

Each Game has exactly 2 teams, generally a home team and an away team although occasionally both teams can be neutral.

My question is whether I should represent this data relationship using 2 foreign keys in the Games table (home_team_id, away_team_id) or whether I should use a many-to-many relationship with a games_teams table to link the two, in which case I would need to also store whether the team was the home or away team and seems a little overkill.

To add to the confusion, I am using the ORM libs in KohanaPHP and these would expect an fk to be called team_id or a link table to contain only 2 columns. If you have experience with this problem in KohanaPHP then please leave a reply, else any general advice is also much appreciated.

+2  A: 

If you want to be able to pin a "Xth Normal Form" badge on your database server, then it should probably be treated as many-to-many, otherwise, I should think you'll reduce your query overheads with 1 fewer table that you're just going to join through every time you want some useful data out.

MalphasWats
I disagree. The two table design is at least BCNF, going further would be useless in this case.
Ben S
12 seconds ahead of Will and also correct. De-normalization can be your friend.
Jonathan Fingland
+7  A: 

Just use the two columns, otherwise you'd just need to qualify it in the joiner table. It's not as if this is a sleeping time bomb and suddenly one day you'll discover you need to have a real many to many.

Will Hartung
+1 for the sleeping time bomb
Jonathan Fingland
Maybe the rules will change to so that 6 teams play in a hexagon...
Ben S
seriously though. Will is right. it's faster, and that particular requirement isn't going to change any time soon.
Jonathan Fingland
+4  A: 

2 columns is perfectly appropriate here in my opinion. The fact that there can only possibly be two teams for any game is reflected in your database schema by having two columns. By introducing a linking table, you introduce the possibility that a single game could have 2 home teams, 2 away teams, and you would need additional validation to ensure that this scenario never occurs. By keeping things isolated to the two columns, your schema inherently enforces data integrity.

Ryan Brunner
That's incorrect. If you specify that the two columns are unique.
Elizabeth Buckwalter
Sorry, I may have been a little unclear. What I was saying was that the linked table approach allows for the possibility of 2 home teams (or any number really), and so requires validation be built around it. You're right that the columns can be made unique with the two column approach.
Ryan Brunner
+1  A: 

In terms of normalization: Yes. There is only a one-to-many or a many-to-many that gets broken down into one or more one-to-many relationships.

But realistically speaking, if I were to save something like GENDER. Would I really need a multi-state and a datetime stamp attached to this?

The surprising answer is YES - but only if I need to track gender changes for business reasons - for most practical purposes the answer is NO.

I would keep one table with two keys - unless there is a business reason to track it as such.

Normalize until it hurts, denormalize until it works. :)

Raj More
A: 

Depending on how you have abstracted it, I would say that soccer game MUST have two teams, in which case having the columns in the Game table is not only more convenient, it's more correct.

I could even imagine the team id's being natural parts of the primary key of Game.

Console
A: 

Totally think you should NOT have a separate table for this. Easier on the programmer, easier on the DB. Good to think about the what ifs, but sounds like you already have. Don't get caught thinking that normalization is always the way to go for everything.

bstiles
A: 

Two columns would be best.

James Bailey
A: 

In my opinion, these are two one-to-many relationships, so you should be fine.

data
A: 

I respect the answers of the other in saying that using the two columns is the best choice, however, you mention that you are using the ORM library in Kohana. By using two columns in the games table you lose the ORM features for many-to-many relationships. If you set up a games_teams pivot table, you can do the following:

$game = ORM::factory('game', 1); // 1 is the game id

Then you can loop through the teams in that game:

foreach ($game->teams as $team) {
// do stuff with $team
}
wmid