Pages

Sep 15, 2008

How to find the the Primary Key columns in a DB in SQL SERVER 2005

How to List Primary Key columns in a DB:
This the Query that ll list all the Primary Key columns with its table name and column name in a DataBase,

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_NAMEAND
AND c.CONSTRAINT_NAME = p.CONSTRAINT_NAME
ORDER by c.TABLE_NAME

No comments: