Find Primary Keys or Constraints in a database, SQL Server 2008

I was asked to list the Primary keys in our databases then I listed using the below script. This is very useful when you have large number of tables.

SELECT 
A.CONSTRAINT_CATALOG,
A.CONSTRAINT_SCHEMA,
B.CONSTRAINT_NAME,
A.CONSTRAINT_TYPE, 
B.COLUMN_NAME,
B.ORDINAL_POSITION
 from INFORMATION_SCHEMA.TABLE_CONSTRAINTS A Inner join 
      INFORMATION_SCHEMA.KEY_COLUMN_USAGE B  
on A.CONSTRAINT_NAME=B.CONSTRAINT_NAME
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s