Home All Groups Group Topic Archive Search About

Questions about DataTable constraints

Author
12 Mar 2006 7:51 PM
deko
Some general questions about constraints in ADO.NET DataTables...

Here is an example from MSDN:
http://msdn2.microsoft.com/en-us/library/system.data.datatable.constraints.aspx

private void CreateConstraint(DataSet dataSet,
    string table1, string table2, string column1, string column2)
{
    ForeignKeyConstraint idKeyRestraint = new
        ForeignKeyConstraint(dataSet.Tables[table1].Columns[column1],
        dataSet.Tables[table2].Columns[column2]);

    // Set null values when a value is deleted.
    idKeyRestraint.DeleteRule = Rule.SetNull; //??????????????
    idKeyRestraint.UpdateRule = Rule.Cascade;

    // Set AcceptRejectRule to cascade changes.
    idKeyRestraint.AcceptRejectRule = AcceptRejectRule.Cascade;

    dataSet.Tables[table1].Constraints.Add(idKeyRestraint);
    dataSet.EnforceConstraints = true;
}

all well and good, but...

Why would I want to set a null value when something is deleted???????????

How do I implement cascading deletes that will just delete the row?  That
is, I want the related row that contains the foreign key to be deleted when
I delete the parent row in the parent table.

Also, when I create a table that will contain a foreign key, do I need to
take any extra steps after creating the FK column?

For example,

tblChildTable.Columns.Add("parentTablePK", typeof(int));  //foreign key

Do I need to disallow nulls or add any additional code after adding this FK
column to my DataTable?

AddThis Social Bookmark Button