I recently had the following question posted to this site:
Hello,
I’d think this is a simple question/problem. I want to get the primary key name for each table in my schema. By doing so, it will make my search easier when looking at the database. I have schema X with many tables. I want to know the primary key to each table and return it in a report form. So, basically my question is, “Give me the column name of each primary key of each table in schema X.” Is there a simple SQL statement to do this?
Thank you.
Solution
The following query allows you to view all primary keys for all user tables in the current database. You can tweak it to return results for a given schema or any other set of parameters.
Image may be NSFW.
Clik here to view.
There it is – quick and easy. Please let me know if anyone else has any other questions they’d like me to answer on this blog.
Filed under: SQL Server, SQL Server 2005, SQL Server 2008, SQL Server 2012, T-SQL Tagged: Indexes, Primary Key, SQL Server Image may be NSFW.
Clik here to view.

Clik here to view.
