下图是我目前所拥有的。
设计
发射器表
发射器有自己的属性,例如位置(根据 x 和 y)、id、级别和上次更新此信息的时间。更重要的是,任何氏族成员都可以保护发射器。发射器只能由该部族的一名成员保护。一个发射器可以被许多玩家防御。
玩家表
或者更确切地说,氏族成员。该表专门包含有关玩家的信息。这也有关于 x 和 y 的位置以及关于该玩家的其他信息。 (我可能也会在这里添加 lastupdated)一个玩家可以防御多个发射器。
Emitter_Player 连接表
此表包含单个 emitter_player 组合。 (假设存在这样的关系)首先请问发射器和播放器之间的这种关系是否正确?我认为这将是多对多的。现在,我可以让这件事变得简单,只需将防御点添加到这个连接表(我真正关心的是玩家在特定发射器中有多少“防御”)这也是正确的做法吗?但是,我想“第一次就做对”并添加有关玩家保卫该发射器的每个特定单位的信息。
单位表
此表包含有关游戏中所有单位的信息。该表具有单位 ID 及其关联的防御和进攻值。这个表很少被插入,当它被插入时(新单位被添加到游戏中)它会被手动更新。
问题
修复设计
发射器不包含玩家之外的单位。玩家必须是发射器中每个单元的所有者。所以,发射器和单位之间没有关系。此外,虽然玩家可能在发射器之外有单位,但我不关心这个例子。我只关心发射器中的单位。因此,我认为 Emitter_Player 连接表和 Units 表之间存在多对多关系。我的理由是,一个 Emitter_Player 组合可以很容易地拥有许多不同类型的单元,而一种类型的单元可以在许多不同的 Emitter_Player 组合中。
插入信息
有了两个连接表,我现在对如何向这个数据库中插入信息感到非常困惑。
查询信息
同样,我完全不知道如何从该数据库访问信息。
目标
图表
我想最终创建图表(来自玩家和发射器)显示他们随时间的进展。我不知道如何查询。
每周变化
我希望能够告知玩家自上周以来他们取得了进步还是失去了进步。 (在这种情况下,标记它们以供审核)
结论
我尽量做到详细,如果您需要更多信息,请告诉我。我希望尽快完成这项工作,但我真的完全失去了进一步的想法。
最佳答案
你的开端相当不错,但我会提出一些建议:
在我看来,您可以将 emitters_has_players_has_units
表合并到 emitters_has_players
表中。只需将 unit_id
作为 emitters_has_players
主键中的第三个组件:
您还会注意到我在特定的 emitter-player-unit
关联中添加了一个 quantity
列。这对于跟踪特定玩家对特定发射器拥有多少特定单位是必要的。
在整个数据库中保持列名一致也是一种很好的做法。您最初命名 id 列的方式很长(因为它包括完整的表名作为前缀)。
下面是一些如何查询上述设计的示例:
-- Get all associated emitters of a particular player
SELECT a.*
FROM emitters a
JOIN
(
SELECT DISTINCT emitter_id
FROM emitters_has_players
WHERE player_id = 1
) b ON a.emitter_id = b.emitter_id
-- Get all players associated with a particular emitter
SELECT a.*
FROM players a
JOIN
(
SELECT DISTINCT player_id
FROM emitters_has_players
WHERE emitter_id = 1
) b ON a.player_id = b.player_id
-- Get the count of players for a particular emitter
SELECT COUNT(DISTINCT player_id) AS player_count
FROM emitters_has_players
WHERE emitter_id = 1
-- Get all units associated with a particular player-emitter association
SELECT b.*
FROM emitters_has_players a
JOIN units b ON a.unit_id = b.unit_id
WHERE a.emitter_id = 1 AND a.player_id = 1
-- Get the defense points of a particular player-emitter association
SELECT SUM(b.averagedefense * a.quantity) AS total_def_pts
FROM emitters_has_players a
JOIN units b ON a.unit_id = b.unit_id
WHERE a.emitter_id = 1 AND a.player_id = 1
-- Create a new player-emitter-unit association
INSERT INTO emitters_has_players
VALUES (1,1,1,1) -- Where the fourth "1" is the quantity of units initially.
-- Player adds on one more of a particular unit for a particular emitter
UPDATE emitters_has_players
SET qty = qty + 1
WHERE emitter_id = 1 AND
player_id = 1 AND
unit_id = 1
关于mysql - 数据库设计 - 困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11706592/