Skip to content

Instantly share code, notes, and snippets.

@jeremykdev
Created July 16, 2016 17:05
Show Gist options
  • Save jeremykdev/aac1c2940c437f8c03adeed43576bca1 to your computer and use it in GitHub Desktop.
Save jeremykdev/aac1c2940c437f8c03adeed43576bca1 to your computer and use it in GitHub Desktop.
Find tables in a Microsoft SQL Server database without a primary key defined
SELECT
S.name AS SchemaName
,T.name AS TableName
FROM sys.tables AS T
INNER JOIN sys.schemas AS S ON ( T.schema_id = S.schema_id )
WHERE NOT EXISTS (
SELECT *
FROM sys.indexes AS I
WHERE I.object_id = T.object_id
AND I.is_primary_key = 1
)
ORDER BY S.name, T.name;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment