Skip to content

Find All Primary Keys and The Tables They Are Associated With

October 23, 2008

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:

 

kick it on DotNetKicks.com

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

  • Archives


  • RSS SQLServerPedia

  • RSS SQL Crunch – Hot SQL Links

  • My Bookmarks

  • Business Career DBA Tool Events free ebook Humor IT News Members Only Microsoft MySQL Operating System Oracle Personal Reporting Services SQL Server SQLServerPedia Syndication SSC SSIS TSQL Tutorial Uncategorized Video

  • Software Blogs - BlogCatalog Blog Directory
  • %d bloggers like this: