LearnItFirst User Forum

SQL Server 2008 SSAS Training Videos
Welcome Guest Search | New Posts | Members | Log In | Register

List all tables without a primary key Options
Scott Whigham
Posted: Monday, August 28, 2006 11:25:40 AM


Rank: Super Mod

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

    Description: This script lists all tables without a primary key
   
    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
   T.TABLE_NAME AS "Tables without PKs"
FROM INFORMATION_SCHEMA.TABLES AS T
WHERE NOT EXISTS
   (SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC
      WHERE CONSTRAINT_TYPE = 'PRIMARY KEY'
         AND T.TABLE_NAME = TC.TABLE_NAME)
         AND T.TABLE_TYPE = 'BASE TABLE'
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.