Tables that don’t have Primary Key

This query returns table list which do not have primary key.

SELECT  s.[name] + N'.' + t.[name] [Table]
  FROM sys.tables t
  INNER JOIN sys.schemas s ON t.[schema_id] = s.[schema_id]
  WHERE NOT EXISTS
  ( SELECT * FROM sys.key_constraints  kc
    WHERE kc.[type] = N'PK' AND kc.parent_object_id = t.[object_id]
  );
You can easily find the tables with primary keys if you just use EXISTS in the WHERE clause.
Spread the love

Leave a Comment