问题
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.
回答1:
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
回答2:
A key doesn't have to be a single column to satisfy 3NF. It can be multiple columns - in this example (A,B) - or as in the example in the wikipedia link you've given.
However if you want to have a single column key, then you can create a new column with an autogenerated ID (eg: IDENTITY(1,1)
in SQL server), and create a unique index on columns (A,B)
来源:https://stackoverflow.com/questions/13233088/normalize-a-table-with-a-two-column-primary-key-and-one-other-column