I have a class, let's call it TableOwner
, and one of its fields is a DataTable
with a column for name (which is also the primary key) and a column for amount. So there is one primary key column and a second non-primary column.
In my program, I create a collection of TableOwner
instances. The collection is essentially a set: Logically there is no order of TableOwner
's, it only matters how many of each kind exist. Each one's DataTable
is populated with different values for amount, but they all have the same primary key.
For example, imagine TableOwner
is used to store data on bank accounts. Each instance represents one account, then the DataTable
might record how much of each currency there is in the account. For every account, the number of dollars, euros or pounds in that account may differ, but for every account, there is data on these same three currencies, even if the amount is 0 (no money in account) or null
(perhaps data is missing). So basically I want a 2-D table or matrix, but my class hierarchy impedes me from implementing it as such.
What is a sensible way to deal with all of this? I can think of the following approaches:
1) Make a DataColumn
first and then pass it to all new instances of TableOwner
to be used for their own DataTable
's. Downside: If I later decide I want to remove a row from the primary key (eg the user decides "You know what, I don't care about euros anymore, dump all the data on them and remove the column."), I must iterate through the whole collection and remove the row from each TableOwner
's table one by one.
2) Make a single DataTable
used by all of the TableOwner
instances (perhaps make it a static field). Instead of one amount column, the table will have many columns, such as Amount1, Amount2 and so on. Every TableOwner
instance creates its own amount column at initialization and remembers its name for later - now removing a row is an elementary DB operation. Downside: Seems inelegant and "wrong".
As you can see, both have a downside. Is there a better way?
Apologies if the question is very basic or worded strangely, as I don't know much about databases.
You should have a table with Columns for Owner, Currency and Amount
So instead of
Owner1 20$ £30 €40
You have three records
Owner | Currency | Amount
Owner1 $ 20
Owner1 £ 30
Owner1 € 40
(Where the currency symbols represent Currency IDs from a separate Currency table)
If a person doesn't have an amount in a particular currency, then there is no record for the combination of person and currency.
This is part of the process of normalisation