---
title: "Get-DbaDbForeignKey"
description: "Retrieves foreign key constraints from SQL Server database tables"
url: "https://dbatools.io/Get-DbaDbForeignKey/"
availability: "Windows, Linux, macOS"
tags: ["Database", "ForeignKey", "Table"]
author: "Claudio Silva (@ClaudioESSilva), claudioessilva.eu"
source: "https://github.com/dataplat/dbatools/blob/master/public/Get-DbaDbForeignKey.ps1"
last_updated: "2024-01-01"
---

# Get-DbaDbForeignKey

## Synopsis

Retrieves foreign key constraints from SQL Server database tables

## Description

Retrieves all foreign key constraint definitions from tables across one or more SQL Server databases.  
Essential for documenting referential integrity relationships, analyzing table dependencies before migrations, and troubleshooting cascade operations.  
Returns detailed foreign key properties including referenced tables, schema information, and constraint status (enabled/disabled, checked/unchecked).  
Supports filtering by database and excluding system tables to focus on user-defined constraints.

## Syntax

```powershell
Get-DbaDbForeignKey
    [-SqlInstance] <DbaInstanceParameter[]>
    [[-SqlCredential] <PSCredential>]
    [[-Database] <Object[]>]
    [[-ExcludeDatabase] <Object[]>]
    [-ExcludeSystemTable]
    [-EnableException]
    [<CommonParameters>]

```

## Examples

### Example 1: Gets all database Foreign Keys

```powershell
PS C:\> Get-DbaDbForeignKey -SqlInstance sql2016
```

### Example 2: Gets the Foreign Keys for the db1 database

```powershell
PS C:\> Get-DbaDbForeignKey -SqlInstance Server1 -Database db1
```

### Example 3: Gets the Foreign Keys for all databases except db1

```powershell
PS C:\> Get-DbaDbForeignKey -SqlInstance Server1 -ExcludeDatabase db1
```

### Example 4: Gets the Foreign Keys from all tables that are not system objects from all databases

```powershell
PS C:\> Get-DbaDbForeignKey -SqlInstance Server1 -ExcludeSystemTable
```

### Example 5: Gets the Foreign Keys for the databases on Sql1 and Sql2/sqlexpress

```powershell
PS C:\> 'Sql1','Sql2/sqlexpress' | Get-DbaDbForeignKey
```

### Required Parameters

##### -SqlInstance

The target SQL Server instance or instances

| Property | Value |
| --- | --- |
| Alias |  |
| Required | True |
| Pipeline | true (ByValue) |
| Default Value |  |

### Optional Parameters

##### -SqlCredential

Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).  
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.  
For MFA support, please use Connect-DbaInstance.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value |  |

##### -Database

Specifies which databases to scan for foreign key constraints. Accepts database names, wildcards, or arrays.  
Use this when you need to focus on specific databases rather than scanning all accessible databases on the instance.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value |  |

##### -ExcludeDatabase

Excludes specific databases from the foreign key scan. Useful for skipping large databases, test environments, or databases known to have no relevant constraints.  
Commonly used to exclude system databases like master, model, msdb, and tempdb when focusing on user databases.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value |  |

##### -ExcludeSystemTable

Excludes system tables from the foreign key analysis, focusing only on user-created tables.  
Use this switch when documenting application schemas or analyzing business logic relationships, as system table foreign keys are typically not relevant for most DBA tasks.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value | False |

##### -EnableException

By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.  
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.  
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.

| Property | Value |
| --- | --- |
| Alias |  |
| Required | False |
| Pipeline | false |
| Default Value | False |

## Outputs

**Microsoft.SqlServer.Management.Smo.ForeignKey**

Returns one ForeignKey object per foreign key constraint found in the specified databases. Each object represents a single foreign key relationship between tables.

**Default display properties (via Select-DefaultView):**

- ComputerName: The computer name of the SQL Server instance
- InstanceName: The SQL Server instance name
- SqlInstance: The full SQL Server instance name (computer\instance)
- Database: The database name containing the foreign key
- Schema: The schema name containing the table with the foreign key
- Table: The table name that contains the foreign key (referencing table)
- ID: Unique identifier of the foreign key constraint
- CreateDate: DateTime when the foreign key constraint was created
- DateLastModified: DateTime when the foreign key constraint was last modified
- Name: The name of the foreign key constraint
- IsEnabled: Boolean indicating if the foreign key constraint is currently enabled
- IsChecked: Boolean indicating if the constraint is enforced during INSERT/UPDATE operations
- NotForReplication: Boolean indicating if the constraint applies to replication operations
- ReferencedKey: The primary key or unique key being referenced by this foreign key
- ReferencedTable: The name of the table being referenced (referenced table)
- ReferencedTableSchema: The schema name of the referenced table

**Additional properties available (from SMO ForeignKey object):**

- Columns: Collection of columns that make up the foreign key
- DeleteAction: Action to take when the referenced row is deleted (NoAction, Cascade, SetNull, SetDefault)
- UpdateAction: Action to take when the referenced key is updated (NoAction, Cascade, SetNull, SetDefault)
- DatabaseEngineEdition: The SQL Server edition where the foreign key exists
- DatabaseEngineType: The type of database engine
- IsMemoryOptimized: Boolean indicating if the parent table is memory-optimized
- IsSystemNamed: Boolean indicating if the constraint was system-generated (auto-named)
- State: SMO object state (Existing, Creating, Dropping, etc.)
- Urn: Unique Resource Name for the constraint
- ExtendedProperties: Extended properties attached to the constraint
All properties from the base SMO ForeignKey object are accessible even though only default properties are displayed without using Select-Object *.

---

Part of [dbatools](https://dbatools.io/), a free and open source PowerShell module for SQL Server administration. Full command index: https://dbatools.io/commands/
