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.
Here's another way from the question get table primary key using sql query:
<!-- language: lang-sql --> <pre><code>SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE OBJECTPROPERTY(OBJECT_ID(CONSTRAINT_SCHEMA+'.'+CONSTRAINT_NAME), 'IsPrimaryKey') = 1 AND TABLE_NAME = '<i><your table name></i>' </code></pre>It uses KEY_COLUMN_USAGE
to determine the constraints for a given table
Then uses <code>OBJECTPROPERTY(<i>id</i>, 'IsPrimaryKey')</code> to determine if each is a primary key