How to design Database for this situation?
I'm working a minimal game with Java and mysql. I encountered some difficulties with how to design my tables correctly. I need some advices: Let me be specific, I have 3 classes:
Node
public class Node {
private Integer id;
private Integer position;
private Integer foodTax;
private Boolean hasTreasureMap;
private Integer currentPlayer; // playerId
Treasure
public class Treasure {
Integer id;
private Integer position; // nodeId
private Integer goldValue;
Player
public class Player {
private Integer id;
private Integer wealth;
private Integer strength开发者_Go百科;
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;
I'm a newbie to mysql, and database in general. I think I have to use foreign key in this case. However, I'm still vague how to implement it. Besides, there are several constraints:
- Treasure's position is fixed.
- Node position is fix.
- Only player position can be changed.
- A node can only have a player at a time.( I try to make it as simple as I can, cause if there are two players in the same node, I don't know how to handle it )
So the only foreign key here in my opinion is 'currentPlayer' id of class Node? Please correct me if I was wrong. Any idea?
Best regards, Chan Nguyen
A foreign key is just the id of another record. If a Treasure is supposed to be associated with a Node, then you can put the id of the Node in the Treasure record.
Just a suggestion on naming: Call your IDs by the table name plus "id", rather than just "id". For example, call the ID of the Node table "nodeid" (or "node_id" or something similar). Then when you put a foreign key in a table, use the same name, like your Treasure table could then have the fields (treasureid, nodeid, goldvalue). This makes it a lot easier to keep track of what your doing as the database gets more complicated. Otherwise you have to somehow keep track of the fact that "position" is a node id and so ic "currentPosition" and maybe half a dozen other names. It makes your joins obvious, like "select whatever from node join treasure on node.nodeid=treasure.nodeid".
(If you could have two foreign keys to the same table, like where your Player has a current node id and a goal node id, then you have to get a little more complicated, call them "current_nodeid" and "goal_nodeid" or some such. But its still then clear that is a nodeid and not something else, like someone else reading your code might supppose that currentGoal is a Treasure id or a total number of gold points or any of dozens of other things.)
IT's still not clear what you're trying to store, but you could have a table for Nodes:
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*/
...I have no idea what the Telescope
is for. Again, this suggestion might not work, I'm not exactly sure what you're doing with this data.
精彩评论