Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Table.ForeignKeys Property


Applies To: SQL Server 2016 Preview

Represents a collection of ForeignKey objects. Each ForeignKey object represents a foreign key defined on the table.

Namespace:   Microsoft.SqlServer.Management.Smo
Assembly:  Microsoft.SqlServer.Smo (in Microsoft.SqlServer.Smo.dll)

	SfcContainerCardinality.ZeroToAny, typeof(ForeignKey), SfcObjectFlags.Design)]
public ForeignKeyCollection ForeignKeys { get; }

Property Value

Type: Microsoft.SqlServer.Management.Smo.ForeignKeyCollection

A ForeignKey object that represents all the foreign keys defined on the table.

Legacy Code Example

The following code example shows how to display each foreign key in the AdventureWorks2012 tables.


Server srv = new Server("(local)");
Database db = srv.Databases["AdventureWorks2012"];

Foreach (Table tb in db.Tables) 
   foreach (ForeignKey f in tb.ForeignKeys)
      Console.WriteLine("The " + tb.Name + " table contains the " + f.ToString() + " foreign key.");


$srv = new-Object Microsoft.SqlServer.Management.Smo.Server("(local)")
$db = New-Object Microsoft.SqlServer.Management.Smo.Database
$db = $srv.Databases.Item("AdventureWorks2012")

Foreach ($tb in $db.Tables) 
   foreach ($f in $tb.ForeignKeys)
      Write-Host "The" $tb.Name "table contains the" $f "foreign key."

Return to top
© 2015 Microsoft