Previous
Left Arrow
Using SQL Server Management Studio to get all tables with the same foreign key
Current Code Entry
Go Back To Code List
[SQL Server]
  • Using SQL Server Management Studio to get a list of all tables and their Primary Keys

    Article Entry Date: February 27,2026 @ 06:20:43 / Last Updated On: February 27,2026 @ 06:22:10
In [SQL Server Management Studio] ([SSMS]), we can use a script to obtain all tables' primary keys. This will come in handy if you have a lot of tables and need to find the relationship between the tables using their [Primary Key].

In SSMS, open a New Query editor.
Copy and paste this code into the editor.
Next, press [Execute] or [F5] to run the script.



The outcome will give results similar to this, using our Virtual Class Database.



Once you have this information, you might want to check each Primary Key to find what tables it is used in as a Foreign Key to that table. Use the article below to learn more.▼

Other Articles Related to this Entry.



Using SQL Server Management Studio to get all tables with the same foreign key«