I have a database schema for a game to hold save files where some tables have a one-to-one relationship to classes. I was wondering if I should split up some tables.
Example: troop_class table
. It could have a PK of
{ GameID,PostionX,PostionY,PostionZ,Team }
but then team don't really rely on the positions and could be split to two new tables
game_units Keys{GameID, troop_id} PK{GameID}
and
troop_team{troop_id, Team} PK{troop_id}.
I am wondering if I should break up the one-to-one mapping for table-class and start to normalize and create proper PK instead of using id
.
CREATE TABLE accounts(
AccountName VARCHAR(50) UNIQUE NOT NULL,
Password BLOB NOT NULL,
PRIMARY KEY(AccountName)
) Engine=InnoDB;
CREATE TABLE maps(
maps_id INT NOT NULL UNIQUE,
MapName VARCHAR(30) NOT NULL,
PRIMARY KEY (maps_id),
INDEX(maps_id)
) ENGINE=InnoDB;
CREATE TABLE game_searching_player(
AccountName VARCHAR(50) NOT NULL,
MapID INT NOT NULL,
PRIMARY KEY (AccountName, MapID),
FOREIGN KEY (AccountName) REFERENCES accounts(AccountName)
ON DELETE CASCADE ,
FOREIGN KEY (MapID) REFERENCES maps(maps_id)
ON DELETE CASCADE ,
INDEX(AccountName)
) ENGINE=InnoDB;
CREATE TABLE games(
games_id INT NOT NULL AUTO_INCREMENT UNIQUE,
Player1 VARCHAR(50) NOT NULL,
Player2 VARCHAR(50) NOT NULL,
WhosTurn INT NOT NULL,
TurnHasEnded BOOL NOT NULL,
HaveWon BOOL NOT NULL,
TurnNumber INT NOT NULL,
MapID INT NOT NULL,
Type VARCHAR(30) NOT NULL,
PRIMARY KEY (games_id),
FOREIGN KEY (Player1) REFERENCES accounts(AccountName)
ON DELETE CASCADE ,
FOREIGN KEY (Player2) REFERENCES accounts(AccountName)
ON DELETE CASCADE ,
FOREIGN KEY (MapID) REFERENCES maps(maps_id)
ON DELETE CASCADE ,
INDEX(games_id,Player1, Player2 )
) ENGINE=InnoDB;
CREATE TABLE player(
GameID INT NOT NULL,
Player VARCHAR(30) NOT NULL,
VictoryResources INT NOT NULL,
UniversalResources INT NOT NULL,
Type VARCHAR(30) NOT NULL,
PRIMARY KEY (GameID, Player),
FOREIGN KEY (GameID) REFERENCES games(games_id)
ON DELETE CASCADE ,
INDEX(GameID)
) ENGINE=InnoDB;
CREATE TABLE troop_class (
troop_class_id INT NOT NULL AUTO_INCREMENT UNIQUE,
GameID INT NOT NULL,
Health INT NOT NULL,
CombatInitiative INT NOT NULL,
Spotted BOOL NOT NULL,
Entrenched INT NOT NULL,
Initiative INT NOT NULL,
Ammo INT NOT NULL,
Petrol INT NOT NULL,
ActionPoints INT NOT NULL,
IsMoving BOOL NOT NULL,
PositionX INT NOT NULL,
PositionY INT NOT NULL,
PositionZ INT NOT NULL,
Team VARCHAR(30) NOT NULL,
Name VARCHAR(30) NOT NULL,
Type VARCHAR(30) NOT NULL,
PRIMARY KEY (troop_class_id),
FOREIGN KEY (GameID) REFERENCES games(games_id)
ON DELETE CASCADE ,
INDEX(GameID)
) ENGINE=InnoDB;
CREATE TABLE supply_unit(
supply_unit_id INT NOT NULL UNIQUE,
SupplyPetrol INT NOT NULL,
SupplyAmmo INT NOT NULL,
VictoryResources INT NOT NULL,
LoadWeight INT NOT NULL,
MaxWeight INT NOT NULL,
PRIMARY KEY (supply_unit_id),
FOREIGN KEY (supply_unit_id) REFERENCES troop_class(troop_class_id)
ON DELETE CASCADE
) ENGINE=InnoDB;
CREATE TABLE central_warehouse (
central_warehouse_id INT NOT NULL AUTO_INCREMENT UNIQUE,
GameID INT NOT NULL,
SupplyPetrol INT NOT NULL,
SupplyAmmo INT NOT NULL,
VictoryResources INT NOT NULL,
LoadWeight INT NOT NULL,
MaxWeight INT NOT NULL,
PositionX INT NOT NULL,
PositionY INT NOT NULL,
PositionZ INT NOT NULL,
Team VARCHAR(30) NOT NULL,
Name VARCHAR(30) NOT NULL,
Type VARCHAR(30) NOT NULL,
PRIMARY KEY (central_warehouse_id),
FOREIGN KEY (GameID) REFERENCES games(games_id)
ON DELETE CASCADE ,
INDEX(GameID)
) ENGINE=InnoDB;