Find All Primary Keys and The Tables They Are Associated With
I previously posted about finding user table basic information. In this post, let us use a handy TSQL to find all the Primary Key Constraints and the tables they are associated with.
SELECT O.NAME AS [PK_NAME], T.NAME AS [TABLE_NAME]
FROM ADVENTUREWORKS.SYS.OBJECTS O
JOIN ADVENTUREWORKS.SYS.TABLES T
ON O.PARENT_OBJECT_ID = T.OBJECT_ID
WHERE O.TYPE_DESC = 'PRIMARY_KEY_CONSTRAINT'
Here’s the code in action: