How to use sp_fkeys Stored Procedure In SQL
The sp_fkeys is a stored procedure present in each database of the SQL Server that stores information about the foreign key relationships for a specified table in the current database. We can query this stored procedure to list all the foreign keys referencing a given table in SQL Server. Following is the syntax to use sp_fkeys stored procedure:
Syntax
EXEC sp_fkeys 'Table_name'
where:
- EXEC: is a statement used to execute the stored procedure sp_keys.
- Table_Name: is the name of the table for which we want to list all of the referencing foreign keys.
Example
To list all the foreign keys referencing the above table Cities we will have to run the following query:
Query:
EXEC sp_fkeys 'Cities'
Output:
Note:The above query returns a single table which has been broken into two parts inorder to demonstrate the output in a better way.
Explanation:The above query returns a table maintained by the stored procedure sp_fkeys. The table consists of many columns out of which the columns FKTABLE_NAME and FKCOLUMN_NAME represents the name of the foreign keys and their respective tables that refers to the table Cities.
How to List all Foreign Keys Referencing a Given Table in SQL Server?
SQL Server is a Relational Database Management System(RDBMS) that allows users to create and manage databases efficiently. In SQL Server, understanding the table’s relationship is very important for database design and maintenance.
Foreign keys play an important role in establishing relations between different tables. In this article, we will learn how to list all the foreign keys referencing a given table in SQL Server.
Contact Us