Java 如何为这种情况设计数据库?
我正在用Java和mysql做一个小游戏。我在如何正确设计表格方面遇到了一些困难。我需要一些建议:让我具体一点,我有3门课: 节点 珍宝Java 如何为这种情况设计数据库?,java,sql,mysql,database-design,Java,Sql,Mysql,Database Design,我正在用Java和mysql做一个小游戏。我在如何正确设计表格方面遇到了一些困难。我需要一些建议:让我具体一点,我有3门课: 节点 珍宝 public class Treasure { Integer id; private Integer position; // nodeId private Integer goldValue; 玩家 public class Player { private Integer id; private Integer w
public class Treasure {
Integer id;
private Integer position; // nodeId
private Integer goldValue;
玩家
public class Player {
private Integer id;
private Integer wealth;
private Integer strength;
private Integer start;
private Integer goal;
private Integer currentPosition; // nodeId
private Integer currentGoal; // another nodeId
private Vector<Integer> path;
private Vector<Integer> treasureIds;
private int currentMoveIndex;
Graph<Integer> telescope;
公共类播放器{
私有整数id;
私人财富;
私有整数强度;
私有整数启动;
私有整数目标;
私有整数currentPosition;//节点ID
私有整数currentGoal;//另一个节点ID
专用向量路径;
专用向量机;
私有索引;
图形望远镜;
我是mysql和数据库的新手,一般来说,我觉得在这种情况下我必须使用外键。但是,我仍然不清楚如何实现它。此外,还有几个限制:
Chan Nguyen仍然不清楚您要存储什么,但您可以有一个用于节点的表:
Node
id
position
foodTax
hasTreasureMap
currentPlayerID /*Refers to Player table*/
Treasure
id
nodeID /* refers to Node table*/
value
Player
id
/*unrelated fields such as wealth, strength, start*/
position /*refers to ID in Node*/
currentGoal /*refers to ID in Node*/
PlayerTreasures
playerID /* refers to Player table*/
treasureID /* refers to Treasure table*/
Paths
nodeID /*refers to NodeID - I assume that a path is made of Nodes*/
sequenceInPath /*I assume that the ordering in a path matters*/
playerID /*refers to the player who owns this path*/
…我不知道望远镜是做什么用的。同样,这个建议可能不起作用,我不确定你在用这些数据做什么。仍然不清楚你想存储什么,但你可以有一个节点表:
Node
id
position
foodTax
hasTreasureMap
currentPlayerID /*Refers to Player table*/
Treasure
id
nodeID /* refers to Node table*/
value
Player
id
/*unrelated fields such as wealth, strength, start*/
position /*refers to ID in Node*/
currentGoal /*refers to ID in Node*/
PlayerTreasures
playerID /* refers to Player table*/
treasureID /* refers to Treasure table*/
Paths
nodeID /*refers to NodeID - I assume that a path is made of Nodes*/
sequenceInPath /*I assume that the ordering in a path matters*/
playerID /*refers to the player who owns this path*/
…我不知道望远镜是做什么用的。同样,这个建议可能不起作用,我不确定你在用这些数据做什么。外键只是另一个记录的id。如果一个宝藏应该与一个节点相关联,那么你可以将该节点的id放在宝藏记录中 只是一个命名建议:通过表名加上“id”来调用您的id,而不仅仅是“id”。例如,将节点表的id称为“nodeid”(或“Node_id”或类似的名称)。然后,当您将外键放入表中时,使用相同的名称,就像您的宝藏表可以包含字段一样(treasureid、nodeid、goldvalue)。随着数据库变得越来越复杂,这使得跟踪您的操作变得更加容易。否则,您必须以某种方式跟踪“position”是一个节点id的事实,因此必须跟踪“currentPosition”和可能六个其他名称。这使您的连接变得明显,如“从节点上的节点加入宝藏中选择任何内容。nodeid=treasure.nodeid”
(如果你可以在同一个表中有两个外键,比如你的玩家有一个当前节点id和一个目标节点id,那么你必须稍微复杂一点,称它们为“当前节点id”和“目标节点id”“或者类似的。但是很明显,这是一个nodeid,而不是其他的东西,就像其他阅读你代码的人可能会支持currentGoal是一个宝藏id或gold points的总数或其他几十种东西中的任何一种。)外键只是另一个记录的id。如果一个宝藏应该与一个节点关联,那么您可以将该节点的id放入宝藏记录中 只是一个命名建议:通过表名加上“id”来调用您的id,而不仅仅是“id”。例如,将节点表的id称为“nodeid”(或“Node_id”或类似的名称)。然后,当您将外键放入表中时,使用相同的名称,就像您的宝藏表可以包含字段一样(treasureid、nodeid、goldvalue)。随着数据库变得越来越复杂,这使得跟踪您的操作变得更加容易。否则,您必须以某种方式跟踪“position”是一个节点id的事实,因此必须跟踪“currentPosition”和可能六个其他名称。这使您的连接变得明显,如“从节点上的节点加入宝藏中选择任何内容。nodeid=treasure.nodeid”
(如果你可以在同一个表中有两个外键,比如你的玩家有一个当前节点id和一个目标节点id,那么你必须稍微复杂一点,称它们为“当前节点id”和“目标节点id”“或者类似的。但是很明显,这是一个nodeid,而不是其他的东西,就像其他阅读你代码的人可能会支持currentGoal是一个宝藏id或gold points的总数或其他几十种东西中的任何一种。)你想存储什么样的数据?你打算用它做什么?当不清楚它们的用途时,很难帮助设计表格。我的地图是一个二维整数数组。所以所有位置都是整数。我将把这个整数映射到一个坐标(x,y)在绘图时。但这与数据库无关。谢谢你的回答。你想存储什么样的数据?你打算用它做什么?当不清楚它们的用途时,很难帮助设计表。我的映射是一个二维整数数组。所以所有位置都是整数。我将把这个整数映射到坐标在绘图时吃(x,y)。但这与数据库完全无关。谢谢你的回复。谢谢,我想我知道了主意;)谢谢,我想我知道了主意;)谢谢杰伊。我会记住你为这些列命名的话;)谢谢杰伊。我会记住你为这些列命名的话;)