0 votes
1 view
in SQL by (20.3k points)

Simple question, how do you list the primary key of a table with T-SQL? I know how to get indexes on a table but can't remember how to get the PK.

1 Answer

0 votes
by (36.7k points)

Try using the code given below:

SELECT Col.Column_Name from 

    INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab, 

    INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col 

WHERE 

    Col.Constraint_Name = Tab.Constraint_Name

    AND Col.Table_Name = Tab.Table_Name

    AND Constraint_Type = 'PRIMARY KEY'

    AND Col.Table_Name = '<your table name>'

...