Should a two-to-many data relationship be treated as many-to-many?

 ̄綄美尐妖づ 提交于 2019-12-04 12:36:51

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.

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.

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.

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.

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.

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.

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!