Sunday, August 26, 2007

ADO.net > DataTable > Table Constraints

TABLE CONSTRAINTS

• A constraint is a logical rule set on a table to preserve the integrity of the data
• The .net framework supports two types of constraints ForeignKeyConstraint and UniqueConstraint
• ForeignKeyConstraint class sets the rules that govern how the table propagates, updates and deletes child tables
• UniqueConstraint class ensures that a single column or array of columns have unique non-duplicated values

===============================================================

FOREIGNKEY CONSTRAINT
• ForeignKeyConstraint constructor takes the name of the object plus two DataColumn objects
• The first column is from parent table, second is from child table
• Sample code listing is as under:
DataColumn dCol1 = tableEmp.Columns(“empID”);
DataColumn dCol2 = tableOrd.Columns(“empID”);
ForeignKeyConstraint fk = new ForeignKeyConstraint(“EmpOrders”, c1, c2);
• The constraint is added to the child table and is configured using the UpdateRule, DeleteRule and AcceptRejectRule properties

====================================================================

UNIQUEKEY CONSTRAINT
• A unique key constraint can be created explicitly using its constructor as shown under:

UniqueConstraint uc;
uc = new UniqueConstraint(dcol);
tableEmp.Constraints.Add(uc);

No comments:

Post a Comment