Search code examples
c#database-designado.netdatatabledatabase-normalization

Normalize a table with a two-column primary key and one other column


My DataTable has 3 columns: A, B and C. None are guaranteed to have unique entries, in non-trivial cases every column will have non-unique entries.

The combination of {A, B} is guaranteed to be unique. Because ADO.NET DataTable allows using multiple columns as the primary key, I use A and B as my primary key.

A vs B represents a matrix with 0 as the default value, so it's possible for me to guarantee that entries of B will come from a finite pool and for each entry of A, every possible {A, B} will appear (ie such that there will be exactly number of unique A values * number of unique B values rows in the table).

My question has two parts:

1) How to normalize this so that I have one key and no update anomalies (I'm not sure but I think I want a 3NF)? (Unless I am mistaken, simply adding an extra "ID" column with non-redundant integers will allow update anomalies)

2) How to set it up (in C# using the ADO.NET library) so that whenever I want to add a row, I specify entries of B and C, and then it automatically generates a suitable value of A, so that I can "insert C at {A, B}"? A can be an integer or a string.

The two are related, since your answer for 2 may affect 1 and vice versa.


Solution

  • You can use DataColumn.Expression property to automatically generate value for a column based on the values of other columns.

    http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx