简体   繁体   English

如何构建关系数据库? 数据

[英]How to structure database for relational? data

I need to keep track of points scored and who they are scored against. 我需要跟踪得分和得分对象。 Previously, I did this using a flat file database (that was a mess) that looked like this: 以前,我是使用一个看起来像这样的平面文件数据库(很乱)完成此操作的:

03611100025
00001000000
21011000000
00003000000
00021000000
10001050000
00001402000
00001000100
00001013050
00001000100
00001000000

Each team got a row and a column, and I guess the whole thing turned into sort of a graph. 每个团队都有一行一行,我想整个事情都变成了一张图表。 Now I can do this is MySQL as well, but it'd be bloated and would force each row to have an absurd number of columns. 现在我也可以使用MySQL,但是它会显得过分膨胀,并会迫使每一行的列数都非常荒谬。

I feel like I must be missing some other drastically easier method with which to do this. 我觉得我一定会错过其他一些更简单的方法来执行此操作。 Can anybody lend a hand? 有人可以伸出援手吗?

Have 2 tables, Team and Game, where Team has a team name, and a team ID, and game has 2 team IDs (team 1 and team 2) and the score of the game. 有2个表,“团队”和“游戏”,“团队”有一个团队名称和一个团队ID,游戏有2个团队ID(团队1和团队2)和比赛得分。 Then, you have foreign keys (for integrity) between the Game and Team tables. 然后,您在游戏和团队表之间具有外键(出于完整性)。 This will reflect who played who and what the score was with a minimal schema and a very simple structure. 这将以最小的架构和非常简单的结构反映出谁扮演了谁以及得分是多少。

Team
|-------------------------|
| Primary (int)| id       |
|         (chr)| name     |
|-------------------------|

Game
|-------------------------|
| Primary (int)| team1    |
| Primary (int)| team2    |
|         (int)| score1   |
|         (int)| score2   |
|-------------------------|

So, some sample data would look like: 因此,一些示例数据如下所示:

Team
|------------------|
| id | name        |
|------------------|
|  1 | Blue Devils |
|  2 | Cardinals   |
|  3 | Fish        |
|  4 | Lemmings    |
|------------------|

Game
|---------------------------------|
| team1 | team2 | score1 | score2 |
|---------------------------------|
|     1 |     2 |      7 |      8 |
|     1 |     4 |      2 |     25 |
|     2 |     3 |      8 |      2 |
|     3 |     4 |     17 |     18 |
|---------------------------------|

This data indicates that team 1 (Blue Devils) played team 2 (Cardinals) with a score of 7 to 8. The rest of the data is similar. 此数据表明,第1队(蓝魔)和第2队(红雀队)的得分为7-8。其余数据相似。

If you do not need to track the team names, you can leave that field out, but this is often useful information. 如果您不需要跟踪团队名称,则可以不填写该字段,但这通常是有用的信息。

So, with this schema, you would get the scores for a particular team with a query like 因此,使用这种架构,您可以通过查询获得特定团队的分数

SELECT * FROM Game g 
 INNER JOIN Team t on t.team1 = g.id

You could then also add additional information if you need to, like when the game took place (date), and any other information, such as other statistics about the game or team. 然后,您还可以根据需要添加其他信息,例如游戏进行的时间(日期),以及其他任何信息,例如有关游戏或团队的其他统计信息。

The first response is correct. 第一个答案是正确的。 If you organize your data into relations, and create one table for each relation, the data will be a lot easier to work with. 如果将数据组织到关系中,并为每个关系创建一个表,则数据将更容易使用。 The background learning you need to do in relational database design will teach you more about this. 在关系数据库设计中需要进行的背景学习将教给您更多有关此的知识。

If you use the Team and Game structure from the response, you can still convert that structure into the kind of structure you imagined in your question. 如果您使用响应中的“团队和游戏”结构,您仍然可以将该结构转换为您在问题中想象的结构。 This process is called "crosstabulating" or "pivoting" and it's well documented in the tutorials. 此过程称为“交叉制表”或“透视”,并且在教程中有很好的记录。

While it's easy to move from a relational structure to a crosstabulated one, it's monstruously difficult to go the other way. 从关系结构转移到交叉表结构很容易,但从另一方向走是非常困难的。 It's also difficult to compose other queries against crosstabulated data. 对于交叉表数据很难组合其他查询。 An example might be finding the highest score scored against each team. 一个示例可能是找到对每个团队得分最高的分数。 If you put your data in a database, sooner or later you are ging to want to do more than one thing with it. 如果将数据存储在数据库中,则迟早会想对它做更多的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM