LearnItFirst User Forum

New SQL Server 2008 DBA Course
Welcome Guest Search | New Posts | Members | Log In | Register

List all primary keys Options
Scott Whigham
Posted: Monday, August 28, 2006 11:26:22 AM


Rank: Super Mod

Joined: 3/20/2006
Posts: 460
Points: 1,002
Where do you live?: Dallas, TX
Code:
/*
    Author: Scott Whigham from http://www.LearnSqlServer.com/

    Description: This script lists all primary keys for all tables
   
    Versions: SQL Server 2005, 2000, 7.0
   
    Creation Date: August 28, 2006

    For more scripts like this one, visit http://forums.learnsqlserver.com/codesamples.aspx
*/
SELECT
    TC.TABLE_NAME AS "Table",
    TC.CONSTRAINT_NAME AS "Constraint Name"
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC JOIN INFORMATION_SCHEMA.TABLES T
    ON T.TABLE_NAME = TC.TABLE_NAME
WHERE T.TABLE_TYPE = 'BASE TABLE' -- only check "real" tables; no functions
    AND OBJECTPROPERTY ( OBJECT_ID ( T.TABLE_NAME ), 'IsMsShipped' ) = 0 -- Exclude system objects
    AND TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
ORDER BY TC.TABLE_NAME ASC
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.