Quote:
Originally Posted by Annalyzer
[...] then it would make more sense to me to write code that used BG1/AC to refer to the aircraft carrier in battlegroup 1 and BG1/D2 to refer to the second destroyer in battlegroup 1 than to use some arbitrary primary key that didn't describe which ship/battlegroup I was really talking about.
This is all dynamic, so there should never be any need for you to actually refer to the data by ID (or a name) in the code.
All you would be doing, by using "BG1/D2" to refer to a battle-group<->ship relationship, is replace two 8byte integers with varying amounts of text, which will increase it's size and query times considerably without any gain at all.
The first design posted is pretty close to what I would do.
Except, I would not include the UserID in the BattleGroup table primary key.
Ideally, the PK would be a Auto_Increment'ed integer, and the UserID a foreign key integer.
I would also remove it from the link table (Battle_Ship).
As the UserID is already in the BattleGroup table there is no need to include it here. The tables can easily be joined if there is need for that.
So, something more like:
- Ships
-
ShipID PK
-
Armor
-
Shields
-
etc...
-
etc...
-
- BattleGroup
-
BattleGroupID PK
-
UserID FK
-
Location
-
Destination
-
Strategy
-
- Battle_Ship
-
BattleGroupID PK/FK
-
ShipID PK/FK
-
Quantity
-
This would obviously allow for more than four groups per user, but that sort of limitation should be enforced by the application code anyways, not the database.
Storing the battle-group number would be redundant, as you could just count the groups owned by a user ID to get the total count, and select them ordered by the group ID to get them in order. (Which your application could then assign indexes to, based on the positions in the result table... or simply use the given ID returned by the database.)