|
1 | 1 | --- |
2 | 2 | title: "View Foreign Key Properties" |
3 | | -description: "You can view the foreign key attributes of a relationship with SQL Server Management Studio or T-SQL queries." |
| 3 | +description: View the foreign key attributes of a relationship with SQL Server Management Studio or T-SQL queries. |
4 | 4 | author: WilliamDAssafMSFT |
5 | 5 | ms.author: wiassaf |
6 | | -ms.date: "04/13/2022" |
| 6 | +ms.reviewer: randolphwest |
| 7 | +ms.date: 03/19/2024 |
7 | 8 | ms.service: sql |
8 | 9 | ms.subservice: table-view-index |
9 | 10 | ms.topic: conceptual |
10 | 11 | helpviewer_keywords: |
11 | 12 | - "foreign keys [SQL Server], attributes" |
12 | 13 | - "displaying foreign keys attributes" |
13 | 14 | - "viewing foreign keys attributes" |
14 | | -monikerRange: ">=aps-pdw-2016||=azuresqldb-current||=azure-sqldw-latest||>=sql-server-2016||>=sql-server-linux-2017||=azuresqldb-mi-current" |
| 15 | +monikerRange: ">=aps-pdw-2016 || =azuresqldb-current || =azure-sqldw-latest || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current" |
15 | 16 | --- |
16 | 17 | # View foreign key properties |
| 18 | + |
17 | 19 | [!INCLUDE [sqlserver2016-asdb-asdbmi-asa-pdw](../../includes/applies-to-version/sqlserver2016-asdb-asdbmi-asa-pdw.md)] |
18 | 20 |
|
19 | | - You can view the foreign key attributes of a relationship in [!INCLUDE[ssnoversion](../../includes/ssnoversion-md.md)] by using [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)] or [!INCLUDE[tsql](../../includes/tsql-md.md)]. |
20 | | - |
21 | | -### <a name="Permissions"></a> Permissions |
22 | | - [!INCLUDE[ssCatViewPerm](../../includes/sscatviewperm-md.md)] For more information, see [Metadata Visibility Configuration](../../relational-databases/security/metadata-visibility-configuration.md). |
23 | | - |
24 | | -## <a name="SSMSProcedure"></a> Using SQL Server Management Studio |
25 | | - |
26 | | -#### To view the foreign key attributes of a relationship in a specific table |
27 | | - |
28 | | -1. Open the Table Designer for the table containing the foreign key you want to view, right-click in the Table Designer, and choose **Relationships** from the shortcut menu. |
29 | | - |
30 | | -2. In the **Foreign Key Relationships** dialog box, select the relationship with properties you want to view. |
31 | | - |
32 | | - If the foreign key columns are related to a primary key, the primary key columns are identified in **Table Designer** by a primary key symbol in the row selector. |
33 | | - |
34 | | -<a name="TsqlExample"></a> |
35 | | -## <a name="TsqlProcedure"></a> Using Transact-SQL |
36 | | - |
37 | | -#### To view the foreign key attributes of a relationship in a specific table |
38 | | - |
39 | | -1. In **Object Explorer**, connect to an instance of [!INCLUDE[ssDE](../../includes/ssde-md.md)]. |
40 | | - |
41 | | -2. On the Standard bar, select **New Query**. |
42 | | - |
43 | | -3. Copy and paste the following example into the query window and select **Execute**. The example returns all foreign keys and their properties for the table `HumanResources.Employee` in the sample database. |
44 | | - |
45 | | - ```sql |
46 | | - USE AdventureWorks2022; |
47 | | - GO |
48 | | - SELECT |
49 | | - f.name AS foreign_key_name |
50 | | - ,OBJECT_NAME(f.parent_object_id) AS table_name |
51 | | - ,COL_NAME(fc.parent_object_id, fc.parent_column_id) AS constraint_column_name |
52 | | - ,OBJECT_NAME (f.referenced_object_id) AS referenced_object |
53 | | - ,COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS referenced_column_name |
54 | | - ,f.is_disabled, f.is_not_trusted |
55 | | - ,f.delete_referential_action_desc |
56 | | - ,f.update_referential_action_desc |
57 | | - FROM sys.foreign_keys AS f |
58 | | - INNER JOIN sys.foreign_key_columns AS fc |
59 | | - ON f.object_id = fc.constraint_object_id |
60 | | - WHERE f.parent_object_id = OBJECT_ID('HumanResources.Employee'); |
61 | | - ``` |
62 | | - |
63 | | - For more information, see [sys.foreign_keys (Transact-SQL)](../../relational-databases/system-catalog-views/sys-foreign-keys-transact-sql.md) and [sys.foreign_key_columns (Transact-SQL)](../../relational-databases/system-catalog-views/sys-foreign-key-columns-transact-sql.md). |
64 | | - |
65 | | -## Next steps |
| 21 | +You can view the foreign key attributes of a relationship in [!INCLUDE [ssnoversion](../../includes/ssnoversion-md.md)] by using [!INCLUDE [ssManStudioFull](../../includes/ssmanstudiofull-md.md)] or [!INCLUDE [tsql](../../includes/tsql-md.md)]. |
| 22 | + |
| 23 | +### Permissions |
| 24 | + |
| 25 | +[!INCLUDE [ssCatViewPerm](../../includes/sscatviewperm-md.md)] For more information, see [Metadata Visibility Configuration](../security/metadata-visibility-configuration.md). |
| 26 | + |
| 27 | +## <a id="SSMSProcedure"></a> Use SQL Server Management Studio |
| 28 | + |
| 29 | +1. Open the Table Designer for the table containing the foreign key you want to view, right-click in the Table Designer, and choose **Relationships** from the shortcut menu. |
| 30 | + |
| 31 | +1. In the **Foreign Key Relationships** dialog box, select the relationship with properties you want to view. |
| 32 | + |
| 33 | +If the foreign key columns are related to a primary key, the primary key columns are identified in **Table Designer** by a primary key symbol in the row selector. |
| 34 | + |
| 35 | +## <a id="TsqlExample"></a> Use Transact-SQL |
| 36 | + |
| 37 | +1. In **Object Explorer**, connect to an instance of [!INCLUDE [ssDE](../../includes/ssde-md.md)]. |
| 38 | + |
| 39 | +1. On the Standard bar, select **New Query**. |
| 40 | + |
| 41 | +1. Copy and paste the following example into the query window and select **Execute**. The example returns all foreign keys and their properties for the table `HumanResources.Employee` in the sample database. |
| 42 | + |
| 43 | + ```sql |
| 44 | + USE AdventureWorks2022; |
| 45 | + GO |
| 46 | + |
| 47 | + SELECT f.name AS foreign_key_name, |
| 48 | + OBJECT_NAME(f.parent_object_id) AS table_name, |
| 49 | + COL_NAME(fc.parent_object_id, fc.parent_column_id) AS constraint_column_name, |
| 50 | + OBJECT_NAME(f.referenced_object_id) AS referenced_object, |
| 51 | + COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS referenced_column_name, |
| 52 | + f.is_disabled, |
| 53 | + f.is_not_trusted, |
| 54 | + f.delete_referential_action_desc, |
| 55 | + f.update_referential_action_desc |
| 56 | + FROM sys.foreign_keys AS f |
| 57 | + INNER JOIN sys.foreign_key_columns AS fc |
| 58 | + ON f.object_id = fc.constraint_object_id |
| 59 | + WHERE f.parent_object_id = OBJECT_ID('HumanResources.Employee'); |
| 60 | + ``` |
| 61 | + |
| 62 | +For more information, see [sys.foreign_keys](../system-catalog-views/sys-foreign-keys-transact-sql.md) and [sys.foreign_key_columns](../system-catalog-views/sys-foreign-key-columns-transact-sql.md). |
| 63 | + |
| 64 | +## Related content |
66 | 65 |
|
67 | 66 | - [ALTER TABLE Statement](../../odbc/microsoft/alter-table-statement.md) |
68 | 67 | - [Disable foreign key constraints for replication](disable-foreign-key-constraints-for-replication.md) |
69 | | -- [Disable Foreign Key Constraints with INSERT and UPDATE Statements](disable-foreign-key-constraints-with-insert-and-update-statements.md) |
| 68 | +- [Disable foreign key constraints with INSERT and UPDATE statements](disable-foreign-key-constraints-with-insert-and-update-statements.md) |
0 commit comments