This documentation is archived and is not being maintained.

ForeignKeyConstraint Class

Represents an action restriction enforced on a set of columns in a primary key/foreign key relationship when a value or row is either deleted or updated.

For a list of all members of this type, see ForeignKeyConstraint Members.


[Visual Basic]
Public Class ForeignKeyConstraint
   Inherits Constraint
public class ForeignKeyConstraint : Constraint
public __gc class ForeignKeyConstraint : public Constraint
class ForeignKeyConstraint extends Constraint

Thread Safety

This type is safe for multithreaded read operations. You must synchronize any write operations.


A ForeignKeyConstraint restricts the action performed when a value in a column (or columns) is either deleted or updated. Such a constraint is intended to be used with primary key columns. In a parent/child relationship between two tables, deleting a value from the parent table can affect the child rows in one of the following ways.

  • The child rows can also be deleted (a cascading action).
  • The values in the child column (or columns) can be set to null values.
  • The values in the child column (or columns) can be set to default values.
  • An exception can be generated.

ForeignKeyConstraint objects are contained in the ConstraintCollection of a DataTable, which is accessed through the Constraints property.

Constraints are not enforced unless the EnforceConstraints property is set to true.

The AcceptRejectRule is enforced whenever a DataTable object's AcceptChanges method is invoked.


[Visual Basic] The following example creates a ForeignKeyConstraint, sets some of its properties, and adds it to a DataTable object's ConstraintCollection.

[Visual Basic] 
' The next line goes into the Declarations section of the module:
' SuppliersProducts is a class derived from DataSet.
Private myDataSet As SuppliersProducts  

Private Sub CreateConstraint()
   ' Declare parent column and child column variables.
   Dim pCol As DataColumn
   Dim cCol As DataColumn
   Dim myFKC As ForeignKeyConstraint
   ' Set parent and child column variables.
   pCol = MyDataSet.Tables("Suppliers").Columns("SupplierID")
   cCol = MyDataSet.Tables("Products").Columns("SupplierID")
   myFKC = New ForeignKeyConstraint("SupplierFKConstraint", pCol, cCol)
   ' Set null values when a value is deleted.
   myFKC.DeleteRule = Rule.SetNull
   myFKC.UpdateRule = Rule.Cascade
   myFKC.AcceptRejectRule = AcceptRejectRule.Cascade
   ' Add the constraint, and set EnforceConstraints to true.
   myDataSet.EnforceConstraints = True
End Sub

[C#, C++, JScript] No example is available for C#, C++, or JScript. To view a Visual Basic example, click the Language Filter button Language Filter in the upper-left corner of the page.


Namespace: System.Data

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System.Data (in System.Data.dll)

See Also

ForeignKeyConstraint Members | System.Data Namespace | AcceptRejectRule | DataTable | ConstraintCollection | Rule