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)

Syntax

[SfcObjectAttribute(SfcContainerRelationship.ChildContainer, 
    SfcContainerCardinality.ZeroToAny, typeof(ForeignKey), SfcObjectFlags.Design)]
public ForeignKeyCollection ForeignKeys { get; }
public:
[SfcObjectAttribute(SfcContainerRelationship::ChildContainer, 
    SfcContainerCardinality::ZeroToAny, (ForeignKey^::typeid), SfcObjectFlags::Design)]
property ForeignKeyCollection^ ForeignKeys {
    ForeignKeyCollection^ get();
}
[<SfcObjectAttribute(SfcContainerRelationship.ChildContainer,
    SfcContainerCardinality.ZeroToAny, typeof(ForeignKey), SfcObjectFlags.Design)>]
member ForeignKeys : ForeignKeyCollection with get
<SfcObjectAttribute(SfcContainerRelationship.ChildContainer, SfcContainerCardinality.ZeroToAny,
    GetType(ForeignKey), SfcObjectFlags.Design)>
Public ReadOnly Property ForeignKeys As ForeignKeyCollection

Property Value

Type: Microsoft.SqlServer.Management.Smo.ForeignKeyCollection

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

Examples

Legacy Code Example

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

C#

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.");
   }
}

Powershell

$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."
   }
}

See Also

Table Class
Microsoft.SqlServer.Management.Smo Namespace

CREATE TABLE (Transact-SQL)
Unable to find linked topic '811e00f9-303f-42b5-8bd4-2cdb829c84e9'.

Return to top