Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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 (40.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>'

You can master these queries and become proficient in SQL queries by enrolling in an industry-recognized SQL certification.

Browse Categories

...