开发者

How to store data with N columns

I need a way to store an int for N columns. Basically what I have is this:

Armies:
ArmyID  - UINT
UnitCount1 - UINT
UnitCount2 - UINT
UnitCount3 - UINT
UnitCount4 - UINT
...

I can't possible add a column fo开发者_C百科r each and every unit, so I need a fast way to store the number of each units in an army (you might have guesses it's for a game by now). Using XML is not an option as it will be dead slow.


Armies
------
ArmyID


Units
-----
UnitID
Description
Strength
Hitpoints


ArmyUnits
---------
ArmyID
UnitID
Count


You could use table like this:

Armies
(
  ArmyID
  UnitID
  UnitCount
)


Define an 1:n relationship (3rd normal form)

Table Armies
  ArmyID

Table Units
  ArmyId
  UnitId
  UnitCount

Or do it in one table

Table ArmyUnits
  ArmyId 
  UnitId
  UnitCount

This obviously means that you have more than one entry per ArmyId.


You can use two tables:

Armies:
ArmyID - UINT

Units:
ArmyID - UINT
UnitIndex - UINT
UnitCount - UINT


This will allow you to store your counts in a one to many relationship. With the UnitID in UnitCount table being a foreign key to the Armies table.

Armies
(
  ArmyID
  UnitID
)

UnitCount
(
  UnitID
  UnitCount
)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜