How can I list all foreign keys referencing a table in SQL Server?
How can I list all foreign keys referencing a table in SQL Server?
338
11-Jul-2023
Updated on 12-Jul-2023
Aryan Kumar
12-Jul-2023There are a few ways to list all foreign keys referencing a table in SQL Server. Here are two of the most common methods:
sys.foreign_keystableThe
sys.foreign_keystable contains a list of all foreign keys in a database. You can use this table to list all foreign keys referencing a table by using the following query:SQL
For example, the following code will list all foreign keys referencing the
Customerstable:SQL
sp_fkeysstored procedureThe
sp_fkeysstored procedure is a system stored procedure that can be used to list all foreign keys referencing a table. To use thesp_fkeysstored procedure, you need to specify the name of the table that you want to list the foreign keys for. For example, the following code will list all foreign keys referencing theCustomerstable:SQL
The
sp_fkeysstored procedure will return a list of all foreign keys referencing theCustomerstable. The list will include the name of the foreign key, the name of the referenced table, and the name of the column in the referenced table that is referenced by the foreign key.