Technical Article

SQL 2005 - List Primary Key Columns

,

This script will list all the Primary key columns and Constraint name from current database using information schema views.

SELECT p.TABLE_NAME,c.CONSTRAINT_NAME,c.COLUMN_NAME 
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS p ,
     INFORMATION_SCHEMA.KEY_COLUMN_USAGE c
WHERE CONSTRAINT_TYPE = 'PRIMARY KEY'
AND c.TABLE_NAME = p.TABLE_NAME
AND c.CONSTRAINT_NAME = p.CONSTRAINT_NAME
ORDER by c.TABLE_NAME

Rate

4.2 (5)

You rated this post out of 5. Change rating

Share

Share

Rate

4.2 (5)

You rated this post out of 5. Change rating